feat: add command wa

This commit is contained in:
2025-12-20 00:36:12 +08:00
parent 25f2a5b92c
commit 6b0f0c451c
4 changed files with 101 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ api_keys:
draw_api_key: SILICONFLOW_API_KEY
exchange_rate_api_key: EXCHANGE_RATE_API_KEY
okx_mirror_api_key: OKX_MIRROR_API_KEY
wolfram_alpha_app_id: WOLFRAM_ALPHA_APP_ID
sqlite:
path: ./db/bot.db

View File

@@ -40,6 +40,7 @@ func init() {
"specialtitle": specialtitleCommand,
"which": whichCommand,
"calc": calcCommand,
"wa": waCommand,
}
}

98
internal/cmds/wa.go Normal file
View File

@@ -0,0 +1,98 @@
package cmds
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
"github.com/awfufu/go-hurobot/internal/config"
"github.com/awfufu/qbot"
)
const waHelpMsg string = `Calculates the result of a mathematical expression using WolframAlpha.
Usage: /wa <expression>
Example: /wa integrate x^2`
var waCommand *Command = &Command{
Name: "wa",
HelpMsg: waHelpMsg,
Permission: getCmdPermLevel("wa"),
NeedRawMsg: false,
MinArgs: 2,
Exec: waExec,
}
func waExec(b *qbot.Sender, msg *qbot.Message) {
appID := config.Cfg.ApiKeys.WolframAlphaAppID
if appID == "" {
b.SendGroupMsg(msg.GroupID, "WolframAlpha AppID not configured")
return
}
exprString := ""
for _, item := range msg.Array[1:] {
if item.Type() == qbot.TextType {
exprString += item.Text() + " "
} else {
b.SendGroupMsg(msg.GroupID, "invalid expression")
return
}
}
// Use WolframAlpha Simple API
// https://products.wolframalpha.com/simple-api/documentation/
apiURL := "https://api.wolframalpha.com/v1/simple"
params := url.Values{}
params.Set("appid", appID)
params.Set("i", exprString)
// params.Set("background", "black") // Optional: optimize for dark mode?
// params.Set("foreground", "white")
resp, err := http.Get(apiURL + "?" + params.Encode())
if err != nil {
b.SendGroupMsg(msg.GroupID, fmt.Sprintf("Request failed: %v", err))
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotImplemented {
b.SendGroupMsg(msg.GroupID, "No short answer available")
return
} else if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
b.SendGroupMsg(msg.GroupID, fmt.Sprintf("Error: %s", string(body)))
return
}
contentType := resp.Header.Get("Content-Type")
if strings.HasPrefix(contentType, "image/") {
// Create temporary file
tmpFile, err := os.CreateTemp("", "wolfram_*.gif")
if err != nil {
b.SendGroupMsg(msg.GroupID, fmt.Sprintf("Failed to create temp file: %v", err))
return
}
defer os.Remove(tmpFile.Name())
defer tmpFile.Close()
_, err = io.Copy(tmpFile, resp.Body)
if err != nil {
b.SendGroupMsg(msg.GroupID, fmt.Sprintf("Failed to write image: %v", err))
return
}
// Send image
b.SendGroupMsg(msg.GroupID, qbot.Image("file://"+tmpFile.Name()))
} else {
// Fallback for text response (though Simple API usually returns image)
body, err := io.ReadAll(resp.Body)
if err != nil {
b.SendGroupMsg(msg.GroupID, fmt.Sprintf("Failed to read response: %v", err))
return
}
b.SendGroupMsg(msg.GroupID, string(body))
}
}

View File

@@ -22,6 +22,7 @@ type yamlConfig struct {
DrawApiKey string `yaml:"draw_api_key"`
ExchangeRateAPIKey string `yaml:"exchange_rate_api_key"`
OkxMirrorAPIKey string `yaml:"okx_mirror_api_key"`
WolframAlphaAppID string `yaml:"wolfram_alpha_app_id"`
} `yaml:"api_keys"`
// SQLite 配置