From 3b1bef9c3d4ddf8f8466aec91dc454fcabba832d Mon Sep 17 00:00:00 2001 From: Kanri Date: Mon, 6 Sep 2021 15:54:57 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=A8=20=E4=BC=98=E5=8C=96=E9=9D=92?= =?UTF-8?q?=E4=BA=91=E5=AE=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin_qingyunke/picture.go | 10 ++- plugin_qingyunke/qingyunke.go | 146 ++++++++++++++++++---------------- plugin_qingyunke/request.go | 4 +- 3 files changed, 86 insertions(+), 74 deletions(-) diff --git a/plugin_qingyunke/picture.go b/plugin_qingyunke/picture.go index 90f8446abe..cc507d3803 100644 --- a/plugin_qingyunke/picture.go +++ b/plugin_qingyunke/picture.go @@ -1,4 +1,5 @@ -package qingyunke +// TODO: 待优化 +/*package qingyunke import ( "fmt" @@ -21,9 +22,9 @@ func getPicture() string { log.Println("正在" + url + "寻找图片") urls := getImgs(url) fmt.Println(urls) - imageUrl := urls[rand.Intn(len(urls))] - log.Println("取到" + imageUrl) - return imageUrl + imageURL := urls[rand.Intn(len(urls))] + log.Println("取到" + imageURL) + return imageURL } func HandleError(err error, why string) { @@ -55,3 +56,4 @@ func GetPageStr(url string) (pageStr string) { pageStr = string(pageBytes) return pageStr } +*/ \ No newline at end of file diff --git a/plugin_qingyunke/qingyunke.go b/plugin_qingyunke/qingyunke.go index 2201f9de46..f9450deb31 100644 --- a/plugin_qingyunke/qingyunke.go +++ b/plugin_qingyunke/qingyunke.go @@ -1,3 +1,6 @@ +/* +基于青云客接口的聊天对话功能 +*/ package qingyunke import ( @@ -16,62 +19,14 @@ import ( "github.com/wdvxdr1123/ZeroBot/message" ) -var prio = -1 -var poke = rate.NewManager(time.Minute, 20) // 青云客接口回复 -var enable = true +var ( + prio = 3 + bucket = rate.NewManager(time.Minute, 20) // 青云客接口回复 + enable = true +) func init() { // 插件主体 - // 被喊名字 - zero.OnRegex("(^.{1,30}$)", zero.OnlyToMe, atriSwitch()).SetBlock(false).FirstPriority(). - Handle(func(ctx *zero.Ctx) { - switch { - case poke.Load(ctx.Event.UserID).Acquire(): - time.Sleep(time.Second * 1) - msg := ctx.State["regex_matched"].([]string)[1] - fmt.Println(msg) - reply := getMessage(msg) - reg := regexp.MustCompile(`\{face:(\d+)\}(.*)`) - faceReply := -1 - var textReply string - if reg.MatchString(reply) { - fmt.Println(reg.FindStringSubmatch(reply)) - faceReply, _ = strconv.Atoi(reg.FindStringSubmatch(reply)[1]) - textReply = reg.FindStringSubmatch(reply)[2] - } else { - textReply = reply - } - textReply = strings.Replace(textReply, "菲菲", "椛椛", -1) - if ctx.Event.DetailType == "group" { - if faceReply != -1 { - ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(textReply), message.Face(faceReply)) - } else { - ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(textReply)) - } - } - if ctx.Event.DetailType == "private" { - if faceReply != -1 { - ctx.SendChain(message.Text(textReply), message.Face(faceReply)) - } else { - ctx.SendChain(message.Text(textReply)) - } - } - - default: - //频繁触发,不回复 - } - }) - zero.OnRegex("CQ:image,file=|CQ:face,id=", zero.OnlyToMe, atriSwitch()).SetBlock(false).FirstPriority(). - Handle(func(ctx *zero.Ctx) { - imageUrl := getPicture() - time.Sleep(time.Second * 1) - if ctx.Event.DetailType == "group" { - ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Image(imageUrl)) - } - if ctx.Event.DetailType == "private" { - ctx.SendChain(message.Image(imageUrl)) - } - }) - + // 开关 zero.OnFullMatch("开启自动回复", zero.SuperUserPermission).SetBlock(true).SetPriority(prio). Handle(func(ctx *zero.Ctx) { enable = true @@ -82,28 +37,81 @@ func init() { // 插件主体 enable = false ctx.SendChain(message.Text("自动回复关闭")) }) - - // 群空调 + // 回复 + zero.OnRegex("(^.{1,30}$)", zero.OnlyToMe, switchQYK()).SetBlock(false).SetPriority(prio). + Handle(func(ctx *zero.Ctx) { + if !bucket.Load(ctx.Event.UserID).Acquire() { + // 频繁触发,不回复 + return + } + msg := ctx.State["regex_matched"].([]string)[1] + // 调用青云客接口 + reply, err := getMessage(msg) + if err != nil { + ctx.SendChain(message.Text("ERROR: ", err)) + return + } + // 挑出 face 表情 + reg := regexp.MustCompile(`\{face:(\d+)\}(.*)`) + faceReply := -1 + var textReply string + if reg.MatchString(reply) { + faceReply, _ = strconv.Atoi(reg.FindStringSubmatch(reply)[1]) + textReply = reg.FindStringSubmatch(reply)[2] + } else { + textReply = reply + } + textReply = strings.ReplaceAll(textReply, "菲菲", zero.BotConfig.NickName[0]) + // 回复 + time.Sleep(time.Second * 1) + if ctx.Event.MessageType == "group" { + if faceReply != -1 { + ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(textReply), message.Face(faceReply)) + } else { + ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Text(textReply)) + } + } + if ctx.Event.MessageType == "private" { + if faceReply != -1 { + ctx.SendChain(message.Text(textReply), message.Face(faceReply)) + } else { + ctx.SendChain(message.Text(textReply)) + } + } + }) + // TODO: 待优化 + /* + zero.OnRegex("CQ:image,file=|CQ:face,id=", zero.OnlyToMe, switchQYK()).SetBlock(false).SetPriority(prio). + Handle(func(ctx *zero.Ctx) { + imageURL := getPicture() + time.Sleep(time.Second * 1) + if ctx.Event.MessageType == "group" { + ctx.SendChain(message.Reply(ctx.Event.MessageID), message.Image(imageURL)) + } + if ctx.Event.MessageType == "private" { + ctx.SendChain(message.Image(imageURL)) + } + }) + */ } -type QYData struct { +// 青云客数据 +type dataQYK struct { Result int `json:"result"` Content string `json:"content"` } -//青云客取消息 -func getMessage(msg string) string { +// 青云客取消息 +func getMessage(msg string) (string, error) { url := "http://api.qingyunke.com/api.php" key := "free" appid := "0" - //msg := "早上好" url = fmt.Sprintf(url+"?key=%s&appid=%s&msg=%s", key, appid, msg) - fmt.Println(url) client := &http.Client{} req, err := http.NewRequest("GET", url, nil) if err != nil { - fmt.Println("new.request", err) + return "", err } // 自定义Header req.Header.Set("User-Agent", getAgent()) @@ -111,20 +119,20 @@ func getMessage(msg string) string { req.Header.Set("Host", "api.qingyunke.com") resp, err := client.Do(req) if err != nil { - fmt.Println("http.get.url", err) + return "", err } defer resp.Body.Close() bytes, err := ioutil.ReadAll(resp.Body) if err != nil { - fmt.Println("ioutil.ReadAll", err) + return "", err } fmt.Println(string(bytes)) - var QYData QYData - if err := json.Unmarshal(bytes, &QYData); err != nil { - fmt.Println("json transform", err) + var dataQYK dataQYK + if err := json.Unmarshal(bytes, &dataQYK); err != nil { + return "", err } - return QYData.Content + return dataQYK.Content, nil } func getAgent() string { @@ -145,7 +153,7 @@ func getAgent() string { return agent[r.Intn(len1)] } -func atriSwitch() zero.Rule { +func switchQYK() zero.Rule { return func(ctx *zero.Ctx) bool { return enable } diff --git a/plugin_qingyunke/request.go b/plugin_qingyunke/request.go index f0f01296bf..09e48fa086 100644 --- a/plugin_qingyunke/request.go +++ b/plugin_qingyunke/request.go @@ -1,4 +1,5 @@ -package qingyunke +// TODO: 移动到 manager 搭配自动验证使用 +/*package qingyunke import ( zero "github.com/wdvxdr1123/ZeroBot" @@ -15,3 +16,4 @@ func init() { } }) } +*/ \ No newline at end of file