WolframAlpha plugin (#18) Nothing major here. Unlike Taigabot, however, we're using the Short Answers API which returns a single line of text. Less verbose, but who cares.
Anirudh Oppiliappan x@icyphox.sh
Sat, 01 Jan 2022 17:41:31 +0530
1 files changed,
48 insertions(+),
0 deletions(-)
jump to
A
plugins/wolframalpha.go
@@ -0,0 +1,48 @@
+package plugins + +import ( + "fmt" + "io" + "net/http" + "net/url" + "strings" + + "git.icyphox.sh/paprika/config" + "gopkg.in/irc.v3" +) + +func init() { + Register(WolframAlpha{}) +} + +type WolframAlpha struct{} + +func (WolframAlpha) Triggers() []string { + return []string{".wa", ".calc"} +} + +func (WolframAlpha) Execute(m *irc.Message) (string, error) { + parts := strings.SplitN(m.Trailing(), " ", 2) + trigger := parts[0] + if len(parts) < 2 { + return fmt.Sprintf("Usage: %s <query>", trigger), nil + } + query := url.QueryEscape(parts[1]) + + appID := config.C.ApiKeys["wolframalpha"] + url := fmt.Sprintf( + "https://api.wolframalpha.com/v1/result?i=%s&appid=%s", + query, appID, + ) + + r, err := http.Get(url) + if err != nil || r.StatusCode != 200 { + return "Error getting result", err + } + + result, err := io.ReadAll(r.Body) + if err != nil { + return "Error getting result", err + } + return fmt.Sprintf("\x02Result:\x02 %s", string(result)), nil +}