add 维基百科 (#673)

* add 维基百科

* make happy
This commit is contained in:
莲宝坏坏坏 2023-04-19 00:47:44 +08:00 committed by GitHub
parent 33e5b0286f
commit 8eb058fb26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 7 deletions

View File

@ -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>

View File

@ -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 {
@ -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