Merge pull request #60 from DiheChen/master

Introduce new feature: 拼音首字母缩写释义工具
This commit is contained in:
Kanri 2021-09-12 15:44:30 +08:00 committed by GitHub
commit 193d6efa67
3 changed files with 42 additions and 0 deletions

View File

@ -129,6 +129,8 @@
- [x] @Bot 任意文本(任意一句话回复)
- **关键字搜图** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_image_finder"`
- [x] 来张 [xxx]
- **拼音首字母释义工具** `import _ "github.com/FloatTech/ZeroBot-Plugin/plugin_nbnhhsh"`
- [x] ?? [缩写]
- **TODO...**
## 使用方法

View File

@ -15,6 +15,7 @@ import (
// 实用类
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_github" // 搜索GitHub仓库
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_manager" // 群管
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_nbnhhsh" // 拼音首字母缩写释义工具
_ "github.com/FloatTech/ZeroBot-Plugin/plugin_runcode" // 在线运行代码
// 娱乐类

39
plugin_nbnhhsh/nbnhhsh.go Normal file
View File

@ -0,0 +1,39 @@
package nbnhhsh
import (
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/tidwall/gjson"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
)
func init() {
zero.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, _ := http.PostForm("https://lab.magiconch.com/api/nbnhhsh/guess", urlValues)
body, _ := ioutil.ReadAll(resp.Body)
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
}