Skip to content

Commit

Permalink
add 维基百科 (#673)
Browse files Browse the repository at this point in the history
* add 维基百科

* make happy
  • Loading branch information
lianhong2758 committed Apr 18, 2023
1 parent 33e5b02 commit 8eb058f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,11 @@ print("run[CQ:image,file="+j["img"]+"]")

</details>
<details>
<summary>百度百科</summary>
<summary>百科</summary>

`import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/baidu"`

- [x] 百度/百科[xxx]
- [x] 百度/百科/维基/wiki[xxx]

</details>
<details>
Expand Down
19 changes: 14 additions & 5 deletions plugin/baidu/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
)

const (
api = "https://api.a20safe.com/api.php?api=21&key=7d06a110e9e20a684e02934549db1d3d&text=%s" // api地址
duURL = "https://api.a20safe.com/api.php?api=21&key=%s&text=%s" // api地址
wikiURL = "https://api.a20safe.com/api.php?api=23&key=%s&text=%s"
key = "7d06a110e9e20a684e02934549db1d3d"
)

type result struct {
Expand All @@ -27,11 +29,18 @@ type result struct {
func init() { // 主函数
en := control.Register("baidu", &ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
Help: "百度百科\n" +
"- 百度/百科[关键字]",
Help: "百科\n" +
"- 百度/百科/维基/wiki[关键字]",
})
en.OnRegex(`^百[度科]\s*(.+)$`).SetBlock(true).Handle(func(ctx *zero.Ctx) {
es, err := web.GetData(fmt.Sprintf(api, ctx.State["regex_matched"].([]string)[1])) // 将网站返回结果赋值
en.OnRegex(`^(百度|维基|百科|wiki)\s*(.+)$`).SetBlock(true).Handle(func(ctx *zero.Ctx) {
var es []byte
var err error
switch ctx.State["regex_matched"].([]string)[1] {
case "百度", "百科":
es, err = web.GetData(fmt.Sprintf(duURL, key, ctx.State["regex_matched"].([]string)[2])) // 将网站返回结果赋值
case "wiki", "维基":
es, err = web.GetData(fmt.Sprintf(wikiURL, key, ctx.State["regex_matched"].([]string)[2])) // 将网站返回结果赋值
}
if err != nil {
ctx.SendChain(message.Text("出现错误捏:", err))
return
Expand Down

0 comments on commit 8eb058f

Please sign in to comment.