2024-01-01 16:52:18 +00:00
// Package api contains helpers to interact with remote APIs in templates
2022-09-26 21:32:48 +00:00
package api
2022-11-02 21:38:14 +00:00
import "github.com/Luzifer/twitch-bot/v3/plugins"
2022-09-26 21:32:48 +00:00
2024-01-01 16:52:18 +00:00
// Register provides the plugins.RegisterFunc
2022-09-26 21:32:48 +00:00
func Register ( args plugins . RegistrationArguments ) error {
2023-08-25 21:37:37 +00:00
args . RegisterTemplateFunction ( "jsonAPI" , plugins . GenericTemplateFunctionGetter ( jsonAPI ) , plugins . TemplateFuncDocumentation {
Description : "Fetches remote URL and applies jq-like query to it returning the result as string. (Remote API needs to return status 200 within 5 seconds.)" ,
Syntax : "jsonAPI <url> <jq-like path> [fallback]" ,
Example : & plugins . TemplateFuncDocumentationExample {
Template : ` {{ jsonAPI "https://api.github.com/repos/Luzifer/twitch-bot" ".owner.login" }} ` ,
FakedOutput : "Luzifer" ,
} ,
} )
args . RegisterTemplateFunction ( "textAPI" , plugins . GenericTemplateFunctionGetter ( textAPI ) , plugins . TemplateFuncDocumentation {
Description : "Fetches remote URL and returns the result as string. (Remote API needs to return status 200 within 5 seconds.)" ,
Syntax : "textAPI <url> [fallback]" ,
Example : & plugins . TemplateFuncDocumentationExample {
MatchMessage : "!weather (.*)" ,
MessageContent : "!weather Hamburg" ,
Template : ` {{ textAPI ( printf "https://api.scorpstuff.com/weather.php?units=metric&city=%s" ( urlquery ( group 1 ) ) ) }} ` ,
FakedOutput : "Weather for Hamburg, DE: Few clouds with a temperature of 22 C (71.6 F). [...]" ,
} ,
} )
2022-09-26 21:32:48 +00:00
return nil
}