2024-04-14 19:08:12 +08:00
|
|
|
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{})
|
2024-04-14 20:40:55 +08:00
|
|
|
// fmt.Println("resultSlipe:", resultSlipe)
|
2024-04-14 19:08:12 +08:00
|
|
|
if trans, ok := resultSlipe[0].(map[string]interface{})["trans"]; ok {
|
|
|
|
// println("trans:", trans.([]interface{}))
|
|
|
|
transSlice := trans.([]interface{})
|
2024-04-14 20:40:55 +08:00
|
|
|
msg := "[CQ:reply,id=" + a.MID + "]("
|
2024-04-14 19:08:12 +08:00
|
|
|
for _, item := range transSlice {
|
|
|
|
// fmt.Print(item, " ")
|
2024-04-14 20:40:55 +08:00
|
|
|
msg += item.(string) + " "
|
2024-04-14 19:08:12 +08:00
|
|
|
}
|
|
|
|
return msg + ")"
|
|
|
|
}
|
|
|
|
// return string(resultSlipe[0].([]byte))
|
|
|
|
return "未找到关于" + resultSlipe[0].(map[string]interface{})["trans"].([]interface{})[0].(string) + "的释义"
|
|
|
|
}
|