add wolframalpha command

This commit is contained in:
lyx0 2023-12-17 17:17:55 +01:00
parent 33f91a6333
commit 64fb0255f1
3 changed files with 35 additions and 0 deletions

View file

@ -200,6 +200,8 @@ func (app *application) handleCommand(message twitch.PrivateMessage) {
case "location":
app.SetUserLocation(message)
}
case "wa":
reply = app.WolframAlphaQuery(message.Message[5:len(message.Message)])
// --------------------------------
// 100 user level

View file

@ -24,6 +24,7 @@ type config struct {
twitchClientId string
twitchClientSecret string
twitchID string
wolframAlphaAppID string
commandPrefix string
env string
db struct {
@ -42,6 +43,7 @@ type application struct {
Models data.Models
Scheduler *cron.Cron
Environment string
Config config
// Rdb *redis.Client
}
@ -71,6 +73,7 @@ func main() {
cfg.twitchOauth = os.Getenv("TWITCH_OAUTH")
cfg.twitchClientId = os.Getenv("TWITCH_CLIENT_ID")
cfg.twitchClientSecret = os.Getenv("TWITCH_CLIENT_SECRET")
cfg.wolframAlphaAppID = os.Getenv("WOLFRAMALPHA_APP_ID")
cfg.commandPrefix = os.Getenv("TWITCH_COMMAND_PREFIX")
cfg.twitchID = os.Getenv("TWITCH_ID")
cfg.env = os.Getenv("ENV")
@ -125,6 +128,7 @@ func main() {
Db: db,
Models: data.NewModels(db),
Scheduler: cron.New(),
Config: cfg,
Environment: cfg.env,
}

View file

@ -0,0 +1,29 @@
package main
import (
"fmt"
"io"
"net/http"
"net/url"
)
func (app *application) WolframAlphaQuery(query string) string {
escaped := url.QueryEscape(query)
url := fmt.Sprintf("http://api.wolframalpha.com/v1/result?appid=%s&i=%s", app.Config.wolframAlphaAppID, 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
}