mirror of
https://github.com/lyx0/nourybot-matrix.git
synced 2024-11-13 19:49:54 +01:00
add wolframalpha command
This commit is contained in:
parent
9927aa2724
commit
2e6c2e3704
|
@ -67,6 +67,13 @@ func (app *application) ParseCommand(evt *event.Event) {
|
|||
return
|
||||
}
|
||||
|
||||
case "wa":
|
||||
if msgLen < 2 {
|
||||
reply = "Not enough arguments provided. Usage: !wa [query]"
|
||||
} else {
|
||||
reply = commands.WolframAlphaQuery(evt.Content.AsMessage().Body[4:len(evt.Content.AsMessage().Body)])
|
||||
}
|
||||
|
||||
}
|
||||
if reply != "" {
|
||||
app.SendText(evt, reply)
|
||||
|
|
36
pkg/commands/wolframalpha.go
Normal file
36
pkg/commands/wolframalpha.go
Normal file
|
@ -0,0 +1,36 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func WolframAlphaQuery(query string) string {
|
||||
err := godotenv.Load()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
appid := os.Getenv("WOLFRAMALPHA_APP_ID")
|
||||
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
|
||||
}
|
Loading…
Reference in a new issue