-
Notifications
You must be signed in to change notification settings - Fork 2k
/
nbnhhsh.go
53 lines (48 loc) · 1.35 KB
/
nbnhhsh.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Package nbnhhsh 能不能好好说话
package nbnhhsh
import (
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/FloatTech/zbputils/control"
"github.com/tidwall/gjson"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/FloatTech/zbputils/control/order"
)
func init() {
control.Register("nbnhhsh", order.AcquirePrio(), &control.Options{
DisableOnDefault: false,
Help: "拼音首字母释义工具\n- ?? [缩写]",
}).OnRegex(`^[??]{1,2} ?([a-z0-9]+)$`).SetBlock(false).
Handle(func(ctx *zero.Ctx) {
keyword := ctx.State["regex_matched"].([]string)[1]
ctx.SendChain(message.Text(keyword + ": " + strings.Join(getValue(keyword), ", ")))
})
}
func getValue(text string) []string {
urlValues := url.Values{}
urlValues.Add("text", text)
resp, err := http.PostForm("https://lab.magiconch.com/api/nbnhhsh/guess", urlValues)
if err == nil {
body, err := ioutil.ReadAll(resp.Body)
if err == nil {
resp.Body.Close()
json := gjson.ParseBytes(body)
res := make([]string, 0)
var jsonPath string
if json.Get("0.trans").Exists() {
jsonPath = "0.trans"
} else {
jsonPath = "0.inputting"
}
for _, value := range json.Get(jsonPath).Array() {
res = append(res, value.String())
}
return res
}
return []string{err.Error()}
}
return []string{err.Error()}
}