forked from yqchilde/wxbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
50 lines (43 loc) · 1.29 KB
/
main.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
package baidubaike
import (
"fmt"
"github.com/imroc/req/v3"
"github.com/yqchilde/wxbot/engine/control"
"github.com/yqchilde/wxbot/engine/robot"
)
func init() {
engine := control.Register("baidubaike", &control.Options{
Alias: "百度百科",
Help: "用法:百度百科 XX\n" +
"示例:百度百科 okr",
})
engine.OnRegex(`^百度百科 ?(.*?)$`).SetBlock(true).Handle(func(ctx *robot.Ctx) {
word := ctx.State["regex_matched"].([]string)[1]
if data, err := getBaiKe(word); err == nil {
if data == nil {
ctx.ReplyText("没查到该百科含义")
} else {
ctx.ReplyText("🏷" + word + ":\n" + fmt.Sprintf("%s\n🔎 摘要: %s\n©️ 版权: %s", data.Desc, data.Abstract, data.Copyrights))
}
} else {
ctx.ReplyText("查询失败,这一定不是bug🤔")
}
})
}
type apiResponse struct {
Key string `json:"key"`
Desc string `json:"desc"`
Abstract string `json:"abstract"`
Copyrights string `json:"copyrights"`
}
func getBaiKe(keyword string) (*apiResponse, error) {
var data apiResponse
api := "https://baike.baidu.com/api/openapi/BaikeLemmaCardApi?appid=379020&bk_length=1000&bk_key=" + keyword
if err := req.C().Get(api).Do().Into(&data); err != nil {
return nil, err
}
if len(data.Abstract) == 0 {
return nil, nil
}
return &data, nil
}