mirror of
https://github.com/FloatTech/ZeroBot-Plugin.git
synced 2025-12-21 23:50:09 +08:00
🎨 优化青云客
This commit is contained in:
parent
c83c4079cf
commit
3b1bef9c3d
@ -1,4 +1,5 @@
|
|||||||
package qingyunke
|
// TODO: 待优化
|
||||||
|
/*package qingyunke
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -21,9 +22,9 @@ func getPicture() string {
|
|||||||
log.Println("正在" + url + "寻找图片")
|
log.Println("正在" + url + "寻找图片")
|
||||||
urls := getImgs(url)
|
urls := getImgs(url)
|
||||||
fmt.Println(urls)
|
fmt.Println(urls)
|
||||||
imageUrl := urls[rand.Intn(len(urls))]
|
imageURL := urls[rand.Intn(len(urls))]
|
||||||
log.Println("取到" + imageUrl)
|
log.Println("取到" + imageURL)
|
||||||
return imageUrl
|
return imageURL
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleError(err error, why string) {
|
func HandleError(err error, why string) {
|
||||||
@ -55,3 +56,4 @@ func GetPageStr(url string) (pageStr string) {
|
|||||||
pageStr = string(pageBytes)
|
pageStr = string(pageBytes)
|
||||||
return pageStr
|
return pageStr
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
@ -1,3 +1,6 @@
|
|||||||
|
/*
|
||||||
|
基于青云客接口的聊天对话功能
|
||||||
|
*/
|
||||||
package qingyunke
|
package qingyunke
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@ -16,62 +19,14 @@ import (
|
|||||||
"github.com/wdvxdr1123/ZeroBot/message"
|
"github.com/wdvxdr1123/ZeroBot/message"
|
||||||
)
|
)
|
||||||
|
|
||||||
var prio = -1
|
var (
|
||||||
var poke = rate.NewManager(time.Minute, 20) // 青云客接口回复
|
prio = 3
|
||||||
var enable = true
|
bucket = rate.NewManager(time.Minute, 20) // 青云客接口回复
|
||||||
|
enable = true
|
||||||
|
)
|
||||||
|
|
||||||
func init() { // 插件主体
|
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).
|
zero.OnFullMatch("开启自动回复", zero.SuperUserPermission).SetBlock(true).SetPriority(prio).
|
||||||
Handle(func(ctx *zero.Ctx) {
|
Handle(func(ctx *zero.Ctx) {
|
||||||
enable = true
|
enable = true
|
||||||
@ -82,28 +37,81 @@ func init() { // 插件主体
|
|||||||
enable = false
|
enable = false
|
||||||
ctx.SendChain(message.Text("自动回复关闭"))
|
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"`
|
Result int `json:"result"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//青云客取消息
|
// 青云客取消息
|
||||||
func getMessage(msg string) string {
|
func getMessage(msg string) (string, error) {
|
||||||
url := "http://api.qingyunke.com/api.php"
|
url := "http://api.qingyunke.com/api.php"
|
||||||
key := "free"
|
key := "free"
|
||||||
appid := "0"
|
appid := "0"
|
||||||
//msg := "早上好"
|
|
||||||
url = fmt.Sprintf(url+"?key=%s&appid=%s&msg=%s", key, appid, msg)
|
url = fmt.Sprintf(url+"?key=%s&appid=%s&msg=%s", key, appid, msg)
|
||||||
fmt.Println(url)
|
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
req, err := http.NewRequest("GET", url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("new.request", err)
|
return "", err
|
||||||
}
|
}
|
||||||
// 自定义Header
|
// 自定义Header
|
||||||
req.Header.Set("User-Agent", getAgent())
|
req.Header.Set("User-Agent", getAgent())
|
||||||
@ -111,20 +119,20 @@ func getMessage(msg string) string {
|
|||||||
req.Header.Set("Host", "api.qingyunke.com")
|
req.Header.Set("Host", "api.qingyunke.com")
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("http.get.url", err)
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
bytes, err := ioutil.ReadAll(resp.Body)
|
bytes, err := ioutil.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("ioutil.ReadAll", err)
|
return "", err
|
||||||
}
|
}
|
||||||
fmt.Println(string(bytes))
|
fmt.Println(string(bytes))
|
||||||
var QYData QYData
|
var dataQYK dataQYK
|
||||||
if err := json.Unmarshal(bytes, &QYData); err != nil {
|
if err := json.Unmarshal(bytes, &dataQYK); err != nil {
|
||||||
fmt.Println("json transform", err)
|
return "", err
|
||||||
}
|
}
|
||||||
return QYData.Content
|
return dataQYK.Content, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAgent() string {
|
func getAgent() string {
|
||||||
@ -145,7 +153,7 @@ func getAgent() string {
|
|||||||
return agent[r.Intn(len1)]
|
return agent[r.Intn(len1)]
|
||||||
}
|
}
|
||||||
|
|
||||||
func atriSwitch() zero.Rule {
|
func switchQYK() zero.Rule {
|
||||||
return func(ctx *zero.Ctx) bool {
|
return func(ctx *zero.Ctx) bool {
|
||||||
return enable
|
return enable
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
package qingyunke
|
// TODO: 移动到 manager 搭配自动验证使用
|
||||||
|
/*package qingyunke
|
||||||
|
|
||||||
import (
|
import (
|
||||||
zero "github.com/wdvxdr1123/ZeroBot"
|
zero "github.com/wdvxdr1123/ZeroBot"
|
||||||
@ -15,3 +16,4 @@ func init() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
Loading…
Reference in New Issue
Block a user