Skip to content

Commit

Permalink
🎨 优化青云客
Browse files Browse the repository at this point in the history
  • Loading branch information
kanrichan committed Sep 6, 2021
1 parent c83c407 commit 3b1bef9
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 74 deletions.
10 changes: 6 additions & 4 deletions plugin_qingyunke/picture.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package qingyunke
// TODO: 待优化
/*package qingyunke
import (
"fmt"
Expand All @@ -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) {
Expand Down Expand Up @@ -55,3 +56,4 @@ func GetPageStr(url string) (pageStr string) {
pageStr = string(pageBytes)
return pageStr
}
*/
146 changes: 77 additions & 69 deletions plugin_qingyunke/qingyunke.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*
基于青云客接口的聊天对话功能
*/
package qingyunke

import (
Expand All @@ -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
Expand All @@ -82,49 +37,102 @@ 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())
req.Header.Set("Connection", "keep-alive")
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 {
Expand All @@ -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
}
Expand Down
4 changes: 3 additions & 1 deletion plugin_qingyunke/request.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package qingyunke
// TODO: 移动到 manager 搭配自动验证使用
/*package qingyunke
import (
zero "github.com/wdvxdr1123/ZeroBot"
Expand All @@ -15,3 +16,4 @@ func init() {
}
})
}
*/

0 comments on commit 3b1bef9

Please sign in to comment.