From 1734f1f7d43a52ca4f0155b631859c2fcdb29712 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=8E=B2=E5=AE=9D=E5=9D=8F=E5=9D=8F=E5=9D=8F?=
<2758988938@qq.com>
Date: Mon, 20 Mar 2023 12:23:52 +0800
Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=8D=A2=E7=99=BE=E5=BA=A6=E4=B8=80?=
=?UTF-8?q?=E4=B8=8B=E4=B8=BA=E7=99=BE=E5=BA=A6=E7=99=BE=E7=A7=91=20(#627)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 4 +--
plugin/baidu/search.go | 56 ++++++++++++++++++++++++++++++------------
2 files changed, 42 insertions(+), 18 deletions(-)
diff --git a/README.md b/README.md
index b1a594aeca..c7e7cbe5eb 100644
--- a/README.md
+++ b/README.md
@@ -436,11 +436,11 @@ print("run[CQ:image,file="+j["img"]+"]")
- 百度一下
+ 百度百科
`import _ "github.com/FloatTech/ZeroBot-Plugin/plugin/baidu"`
- - [x] 百度下[xxx]
+ - [x] 百度/百科[xxx]
diff --git a/plugin/baidu/search.go b/plugin/baidu/search.go
index 642804d458..853ab76725 100644
--- a/plugin/baidu/search.go
+++ b/plugin/baidu/search.go
@@ -1,27 +1,51 @@
-// Package baidu 百度一下
+// Package baidu 百度百科
package baidu
import (
- "net/url"
+ "encoding/json"
+ "fmt"
+ "github.com/FloatTech/floatbox/web"
+ ctrl "github.com/FloatTech/zbpctrl"
+ "github.com/FloatTech/zbputils/control"
zero "github.com/wdvxdr1123/ZeroBot"
"github.com/wdvxdr1123/ZeroBot/message"
+)
- ctrl "github.com/FloatTech/zbpctrl"
- "github.com/FloatTech/zbputils/control"
- "github.com/FloatTech/zbputils/ctxext"
+const (
+ api = "https://api.a20safe.com/api.php?api=21&key=7d06a110e9e20a684e02934549db1d3d&text=%s" // api地址
)
-func init() {
- control.Register("baidu", &ctrl.Options[*zero.Ctx]{
+type result struct {
+ Code int `json:"code"`
+ Msg string `json:"msg"`
+ Data []struct {
+ Content string `json:"content"`
+ } `json:"data"`
+}
+
+func init() { // 主函数
+ en := control.Register("baidu", &ctrl.Options[*zero.Ctx]{
DisableOnDefault: false,
- Brief: "不会百度吗",
- Help: "- 百度下[xxx]",
- }).OnPrefix("百度下").SetBlock(true).Limit(ctxext.LimitByGroup).
- Handle(func(ctx *zero.Ctx) {
- txt := ctx.State["args"].(string)
- if txt != "" {
- ctx.SendChain(message.Text("https://buhuibaidu.me/?s=" + url.QueryEscape(txt)))
- }
- })
+ Help: "百度百科\n" +
+ "- 百度/百科[关键字]",
+ })
+ en.OnRegex(`^[百度|百科]\s*(.+)$`).SetBlock(true).Handle(func(ctx *zero.Ctx) {
+ es, err := web.GetData(fmt.Sprintf(api, ctx.State["regex_matched"].([]string)[1])) // 将网站返回结果赋值
+ if err != nil {
+ ctx.SendChain(message.Text("出现错误捏:", err))
+ return
+ }
+ var r result // r数组
+ err = json.Unmarshal(es, &r) // 填api返回结果,struct地址
+ if err != nil {
+ ctx.SendChain(message.Text("出现错误捏:", err))
+ return
+ }
+ if r.Code == 0 && len(r.Data) > 0 {
+ ctx.SendChain(message.Text(r.Data[0].Content)) // 输出提取后的结果
+ } else {
+ ctx.SendChain(message.Text("API访问错误"))
+ }
+ })
}