go-bot/workers/hhsh.go

68 lines
1.4 KiB
Go

package workers
import (
"fmt"
"strings"
"time"
"github.com/imroc/req/v3"
)
func init() {
RegisterWorkerFactory("hhsh", func(parms []string, uid, gid, role, mid, rawMsg string) Worker {
return &Hhsh{
StdAns: NewStdAns(parms, uid, gid, role, mid, rawMsg),
}
})
}
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)
var data map[string]interface{}
client := req.C().SetTimeout(time.Second * 10)
resp, err := client.R().
SetSuccessResult(&data).
Get(url)
if err != nil {
return "接口异常!"
}
defer resp.Body.Close()
fmt.Println("hhsh body:", resp.String())
// var pkg []Package
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 + "][CQ:at,qq=" + a.UID + "] "
for _, item := range transSlice {
// fmt.Print(item, " ")
msg += item.(string) + " "
}
return msg
}
// return string(resultSlipe[0].([]byte))
return "未找到关于 " + parms[1] + " 的释义"
}