move wolframalpha code to internal/commands

This commit is contained in:
lyx0 2023-12-17 17:23:42 +01:00
parent 64fb0255f1
commit 15956b235b
2 changed files with 33 additions and 2 deletions

View file

@ -200,14 +200,16 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
case "location": case "location":
app.SetUserLocation(message) app.SetUserLocation(message)
} }
case "wa":
reply = app.WolframAlphaQuery(message.Message[5:len(message.Message)])
// -------------------------------- // --------------------------------
// 100 user level // 100 user level
// trusted // trusted
// vip // vip
// -------------------------------- // --------------------------------
case "wa":
if userLevel >= 100 {
reply = commands.WolframAlphaQuery(message.Message[5:len(message.Message)], app.Config.wolframAlphaAppID)
}
case "debug": case "debug":
switch cmdParams[1] { switch cmdParams[1] {
case "user": case "user":

View file

@ -0,0 +1,29 @@
package commands
import (
"fmt"
"io"
"net/http"
"net/url"
)
func WolframAlphaQuery(query, appid string) string {
escaped := url.QueryEscape(query)
url := fmt.Sprintf("http://api.wolframalpha.com/v1/result?appid=%s&i=%s", appid, escaped)
resp, err := http.Get(url)
if err != nil {
return ""
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return ""
}
reply := string(body)
return reply
}