From 6b0f0c451cce017ad3a4abc58d78dc177a9b609f Mon Sep 17 00:00:00 2001 From: awfufu Date: Sat, 20 Dec 2025 00:36:12 +0800 Subject: [PATCH] feat: add command `wa` --- config.yaml.example | 1 + internal/cmds/cmds.go | 1 + internal/cmds/wa.go | 98 +++++++++++++++++++++++++++++++++++++++ internal/config/config.go | 1 + 4 files changed, 101 insertions(+) create mode 100644 internal/cmds/wa.go diff --git a/config.yaml.example b/config.yaml.example index 52a4cab..533ca8b 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -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 diff --git a/internal/cmds/cmds.go b/internal/cmds/cmds.go index 7e3a548..a8e7e56 100644 --- a/internal/cmds/cmds.go +++ b/internal/cmds/cmds.go @@ -40,6 +40,7 @@ func init() { "specialtitle": specialtitleCommand, "which": whichCommand, "calc": calcCommand, + "wa": waCommand, } } diff --git a/internal/cmds/wa.go b/internal/cmds/wa.go new file mode 100644 index 0000000..142b34e --- /dev/null +++ b/internal/cmds/wa.go @@ -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 +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)) + } +} diff --git a/internal/config/config.go b/internal/config/config.go index 5924396..4434e0e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 配置