all repos — paprika @ wolframalpha

go rewrite of taigabot

plugins/wolframalpha.go (view raw)

 1package plugins
 2
 3import (
 4	"fmt"
 5	"io"
 6	"net/http"
 7	"net/url"
 8	"strings"
 9
10	"git.icyphox.sh/paprika/config"
11	"gopkg.in/irc.v3"
12)
13
14func init() {
15	Register(WolframAlpha{})
16}
17
18type WolframAlpha struct{}
19
20func (WolframAlpha) Triggers() []string {
21	return []string{".wa", ".calc"}
22}
23
24func (WolframAlpha) Execute(m *irc.Message) (string, error) {
25	parts := strings.SplitN(m.Trailing(), " ", 2)
26	trigger := parts[0]
27	if len(parts) < 2 {
28		return fmt.Sprintf("Usage: %s <query>", trigger), nil
29	}
30	query := url.QueryEscape(parts[1])
31
32	appID := config.C.ApiKeys["wolframalpha"]
33	url := fmt.Sprintf(
34		"https://api.wolframalpha.com/v1/result?i=%s&appid=%s",
35		query, appID,
36	)
37
38	r, err := http.Get(url)
39	if err != nil || r.StatusCode != 200 {
40		return "Error getting result", err
41	}
42
43	result, err := io.ReadAll(r.Body)
44	if err != nil {
45		return "Error getting result", err
46	}
47	return fmt.Sprintf("\x02Result:\x02 %s", string(result)), nil
48}