From 5211d1d8c6bcfe34900030022ddf965c44d4d988 Mon Sep 17 00:00:00 2001 From: liyp Date: Sun, 14 Apr 2024 19:08:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8F=92=E4=BB=B6hhsh?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 ++ go.sum | 2 ++ workers/hhsh.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ workers/newworker.go | 4 +++ 4 files changed, 68 insertions(+) create mode 100644 workers/hhsh.go diff --git a/go.mod b/go.mod index 604a2d9..a7740e0 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,5 @@ module go-bot go 1.21.0 require github.com/BurntSushi/toml v1.3.2 + +require github.com/goccy/go-json v0.10.2 // indirect diff --git a/go.sum b/go.sum index ef0f966..ea2ce8a 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= diff --git a/workers/hhsh.go b/workers/hhsh.go new file mode 100644 index 0000000..c7a96b3 --- /dev/null +++ b/workers/hhsh.go @@ -0,0 +1,60 @@ +package workers + +import ( + "fmt" + "io" + "net/http" + "strings" + + "github.com/goccy/go-json" +) + +type Hhsh struct { + *StdAns +} + +func (a *Hhsh) GetMsg() string { + + if len(a.Parms) < 2 { + return "请输入参数 如:!hhsh awsl" + } + // 去除换行符 + raw_msg := strings.TrimRight(a.RawMsg, "\n") + fmt.Println("raw_msg:", raw_msg) + parms := strings.Split(raw_msg, " ") + + url := "https://api.beiyu.vip/api/hhsh?str=" + parms[1] + + // 输出请求地址 + // fmt.Println("url:", url) + req, err := http.Get(url) + if err != nil { + return "接口异常!" + } + defer req.Body.Close() + body, err := io.ReadAll(req.Body) + if err != nil { + return err.Error() + } + // fmt.Println("pkg body:", string(body)) + // var pkg []Package + var data map[string]interface{} + err = json.Unmarshal(body, &data) + if err != nil { + return err.Error() + } + resultSlipe := data["data"].([]interface{}) + fmt.Println("resultSlipe:", resultSlipe) + if trans, ok := resultSlipe[0].(map[string]interface{})["trans"]; ok { + // println("trans:", trans.([]interface{})) + transSlice := trans.([]interface{}) + msg := "[CQ:reply,id={" + a.MID + "}](" + for _, item := range transSlice { + // fmt.Print(item, " ") + msg += item.(string) + "" + } + return msg + ")" + } + // return string(resultSlipe[0].([]byte)) + return "未找到关于" + resultSlipe[0].(map[string]interface{})["trans"].([]interface{})[0].(string) + "的释义" +} diff --git a/workers/newworker.go b/workers/newworker.go index 5561673..ea9b0dd 100644 --- a/workers/newworker.go +++ b/workers/newworker.go @@ -14,6 +14,10 @@ func NewWorker(parms []string, uid, gid, role, mid, rawMsg string) Worker { return &Pkg{ StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), } + case "hhsh": + return &Hhsh{ + StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg), + } default: return &Emm{ StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg)}