Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beta49 #157

Merged
merged 3 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Processor/ProcessGroupMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func (p *Processors) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {
mylog.Printf("信息被自定义黑白名单拦截")
return nil
}
//群没有at,但用户可以选择加一个
if config.GetAddAtGroup() {
messageText = "[CQ:at,qq=" + config.GetAppIDStr() + "] " + messageText
}
//框架内指令
p.HandleFrameworkCommand(messageText, data, "group")
// 转换appid
Expand Down
38 changes: 38 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ type Settings struct {
RecordBitRate int `yaml:"record_bitRate"`
NoWhiteResponse string `yaml:"No_White_Response"`
SendError bool `yaml:"send_error"`
AddAtGroup bool `yaml:"add_at_group"`
SendErrorPicAsUrl bool `yaml:"send_error_pic_as_url"`
UrlPicTransfer bool `yaml:"url_pic_transfer"`
}

// LoadConfig 从文件中加载配置并初始化单例配置
Expand Down Expand Up @@ -925,3 +928,38 @@ func GetSendError() bool {
return instance.Settings.SendError
}

// 获取GetAddAtGroup的值
func GetAddAtGroup() bool {
mu.Lock()
defer mu.Unlock()

if instance == nil {
mylog.Println("Warning: instance is nil when trying to GetAddGroupAt value.")
return true
}
return instance.Settings.AddAtGroup
}

// 获取GetSendErrorPicAsUrl的值
func GetSendErrorPicAsUrl() bool {
mu.Lock()
defer mu.Unlock()

if instance == nil {
mylog.Println("Warning: instance is nil when trying to GetErrorPicAsUrl value.")
return true
}
return instance.Settings.SendErrorPicAsUrl
}

// 获取GetUrlPicTransfer的值
func GetUrlPicTransfer() bool {
mu.Lock()
defer mu.Unlock()

if instance == nil {
mylog.Println("Warning: instance is nil when trying to GetUrlPicTransfer value.")
return true
}
return instance.Settings.UrlPicTransfer
}
36 changes: 36 additions & 0 deletions echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ type StringToInt64MappingSeq struct {
mapping map[string]int64
}

// Int64Stack 用于存储 int64 的栈
type Int64Stack struct {
mu sync.Mutex
stack []int64
}

// 定义一个全局的 Int64Stack 实例
var globalInt64Stack = &Int64Stack{
stack: make([]int64, 0),
}

var globalEchoMapping = &EchoMapping{
msgTypeMapping: make(map[string]string),
msgIDMapping: make(map[string]string),
Expand Down Expand Up @@ -120,3 +131,28 @@ func GetMappingFileTimeLimit(key string) int64 {
defer globalStringToInt64MappingSeq.mu.Unlock()
return globalStringToInt64MappingSeq.mapping[key]
}

// Add 添加一个新的 int64 到全局栈中
func AddFileTimeLimit(value int64) {
globalInt64Stack.mu.Lock()
defer globalInt64Stack.mu.Unlock()

// 添加新元素到栈顶
globalInt64Stack.stack = append(globalInt64Stack.stack, value)

// 如果栈的大小超过 10,移除最早添加的元素
if len(globalInt64Stack.stack) > 10 {
globalInt64Stack.stack = globalInt64Stack.stack[1:]
}
}

// Get 获取全局栈顶的元素
func GetFileTimeLimit() int64 {
globalInt64Stack.mu.Lock()
defer globalInt64Stack.mu.Unlock()

if len(globalInt64Stack.stack) == 0 {
return 0 // 当栈为空时返回 0
}
return globalInt64Stack.stack[len(globalInt64Stack.stack)-1]
}
121 changes: 118 additions & 3 deletions handlers/send_group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"context"
"encoding/base64"
"io"
"net/http"
"os"
"strconv"
"time"
Expand All @@ -15,6 +17,7 @@ import (
"github.com/hoshinonyaruko/gensokyo/images"
"github.com/hoshinonyaruko/gensokyo/mylog"
"github.com/hoshinonyaruko/gensokyo/silk"
"github.com/hoshinonyaruko/gensokyo/url"
"github.com/tencent-connect/botgo/dto"
"github.com/tencent-connect/botgo/openapi"
)
Expand Down Expand Up @@ -133,6 +136,9 @@ func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
echo.AddMappingSeq(messageID, msgseq+1)
//时间限制
lastSendTimestamp := echo.GetMappingFileTimeLimit(messageID)
if lastSendTimestamp == 0 {
lastSendTimestamp = echo.GetFileTimeLimit()
}
now := time.Now()
millis := now.UnixMilli()
diff := millis - lastSendTimestamp
Expand All @@ -153,6 +159,7 @@ func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
mylog.Println("延迟完成")
_, err := apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), richMediaMessageCopy)
echo.AddMappingFileTimeLimit(messageID, millis)
echo.AddFileTimeLimit(millis)
if err != nil {
mylog.Printf("发送 %s 信息失败_send_group_msg: %v", key, err)
if config.GetSendError() { //把报错当作文本发出去
Expand All @@ -172,13 +179,65 @@ func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
mylog.Printf("发送文本报错信息失败: %v", err)
}
}
if config.GetSendErrorPicAsUrl() {
msgseq := echo.GetMappingSeq(messageID)
echo.AddMappingSeq(messageID, msgseq+1)
groupReply := generateGroupMessage(messageID, nil, richMediaMessageCopy.URL, msgseq+1)
// 进行类型断言
groupMessage, ok := groupReply.(*dto.MessageToCreate)
if !ok {
mylog.Println("Error: Expected MessageToCreate type.")
return // 或其他错误处理
}
groupMessage.Timestamp = time.Now().Unix() // 设置时间戳
//重新为err赋值
_, err = apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), groupMessage)
if err != nil {
mylog.Printf("发送图片报错后转url发送失败: %v", err)
}
}
}
})
} else {
_, err := apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), richMediaMessage)
echo.AddMappingFileTimeLimit(messageID, millis)
echo.AddFileTimeLimit(millis)
if err != nil {
mylog.Printf("发送 %s 信息失败_send_group_msg: %v", key, err)
if config.GetSendError() { //把报错当作文本发出去
msgseq := echo.GetMappingSeq(messageID)
echo.AddMappingSeq(messageID, msgseq+1)
groupReply := generateGroupMessage(messageID, nil, err.Error(), msgseq+1)
// 进行类型断言
groupMessage, ok := groupReply.(*dto.MessageToCreate)
if !ok {
mylog.Println("Error: Expected MessageToCreate type.")
return // 或其他错误处理
}
groupMessage.Timestamp = time.Now().Unix() // 设置时间戳
//重新为err赋值
_, err = apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), groupMessage)
if err != nil {
mylog.Printf("发送文本报错信息失败: %v", err)
}
}
if config.GetSendErrorPicAsUrl() {
msgseq := echo.GetMappingSeq(messageID)
echo.AddMappingSeq(messageID, msgseq+1)
groupReply := generateGroupMessage(messageID, nil, richMediaMessageCopy.URL, msgseq+1)
// 进行类型断言
groupMessage, ok := groupReply.(*dto.MessageToCreate)
if !ok {
mylog.Println("Error: Expected MessageToCreate type.")
return // 或其他错误处理
}
groupMessage.Timestamp = time.Now().Unix() // 设置时间戳
//重新为err赋值
_, err = apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), groupMessage)
if err != nil {
mylog.Printf("发送图片报错后转url发送失败: %v", err)
}
}
}
}
//发送成功回执
Expand Down Expand Up @@ -329,12 +388,68 @@ func generateGroupMessage(id string, foundItems map[string][]string, messageText
SrvSendMsg: true,
}
} else if imageURLs, ok := foundItems["url_image"]; ok && len(imageURLs) > 0 {
var newpiclink string
if config.GetUrlPicTransfer() {
// 从URL下载图片
resp, err := http.Get("http://" + imageURLs[0])
if err != nil {
mylog.Printf("Error downloading the image: %v", err)
return &dto.MessageToCreate{
Content: "错误: 下载图片失败",
MsgID: id,
MsgSeq: msgseq,
MsgType: 0, // 默认文本类型
}
}
defer resp.Body.Close()

// 读取图片数据
imageData, err := io.ReadAll(resp.Body)
if err != nil {
mylog.Printf("Error reading the image data: %v", err)
return &dto.MessageToCreate{
Content: "错误: 读取图片数据失败",
MsgID: id,
MsgSeq: msgseq,
MsgType: 0,
}
}

// 转换为base64
base64Encoded := base64.StdEncoding.EncodeToString(imageData)

// 上传图片并获取新的URL
newURL, err := images.UploadBase64ImageToServer(base64Encoded)
if err != nil {
mylog.Printf("Error uploading base64 encoded image: %v", err)
return &dto.MessageToCreate{
Content: "错误: 上传图片失败",
MsgID: id,
MsgSeq: msgseq,
MsgType: 0,
}
}
// 将图片链接缩短 避免 url not allow
if config.GetLotusValue() {
// 连接到另一个gensokyo
newURL = url.GenerateShortURL(newURL)
} else {
// 自己是主节点
newURL = url.GenerateShortURL(newURL)
// 使用getBaseURL函数来获取baseUrl并与newURL组合
newURL = url.GetBaseURL() + "/url/" + newURL
}
newpiclink = newURL
} else {
newpiclink = "http://" + imageURLs[0]
}

// 发链接图片
return &dto.RichMediaMessage{
EventID: id,
FileType: 1, // 1代表图片
URL: "http://" + imageURLs[0], //url在base64时候被截断了,在这里补全
Content: "", // 这个字段文档没有了
FileType: 1, // 1代表图片
URL: newpiclink, // 新图片链接
Content: "", // 这个字段文档没有了
SrvSendMsg: true,
}
} else if voiceURLs, ok := foundItems["base64_record"]; ok && len(voiceURLs) > 0 {
Expand Down
59 changes: 58 additions & 1 deletion handlers/send_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope
echo.AddMappingSeq(messageID, msgseq+1)
//时间限制
lastSendTimestamp := echo.GetMappingFileTimeLimit(messageID)
if lastSendTimestamp == 0 {
lastSendTimestamp = echo.GetFileTimeLimit()
}
now := time.Now()
millis := now.UnixMilli()
diff := millis - lastSendTimestamp
Expand All @@ -153,6 +156,7 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope
mylog.Println("延迟完成")
_, err := apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), richMediaMessageCopy)
echo.AddMappingFileTimeLimit(messageID, millis)
echo.AddFileTimeLimit(millis)
if err != nil {
mylog.Printf("发送 %s 信息失败_send_msg: %v", key, err)
if config.GetSendError() { //把报错当作文本发出去
Expand All @@ -172,14 +176,67 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope
mylog.Printf("发送文本报错信息失败: %v", err)
}
}
if config.GetSendErrorPicAsUrl() {
msgseq := echo.GetMappingSeq(messageID)
echo.AddMappingSeq(messageID, msgseq+1)
groupReply := generateGroupMessage(messageID, nil, richMediaMessageCopy.URL, msgseq+1)
// 进行类型断言
groupMessage, ok := groupReply.(*dto.MessageToCreate)
if !ok {
mylog.Println("Error: Expected MessageToCreate type.")
return // 或其他错误处理
}
groupMessage.Timestamp = time.Now().Unix() // 设置时间戳
//重新为err赋值
_, err = apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), groupMessage)
if err != nil {
mylog.Printf("发送图片报错后转url发送失败: %v", err)
}
}
}
})
} else {
_, err := apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), richMediaMessage)
echo.AddMappingFileTimeLimit(messageID, millis)
echo.AddFileTimeLimit(millis)
if err != nil {
mylog.Printf("发送 %s 信息失败_send_group_msg: %v", key, err)
mylog.Printf("发送 %s 信息失败_send_msg: %v", key, err)
if config.GetSendError() { //把报错当作文本发出去
msgseq := echo.GetMappingSeq(messageID)
echo.AddMappingSeq(messageID, msgseq+1)
groupReply := generateGroupMessage(messageID, nil, err.Error(), msgseq+1)
// 进行类型断言
groupMessage, ok := groupReply.(*dto.MessageToCreate)
if !ok {
mylog.Println("Error: Expected MessageToCreate type.")
return // 或其他错误处理
}
groupMessage.Timestamp = time.Now().Unix() // 设置时间戳
//重新为err赋值
_, err = apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), groupMessage)
if err != nil {
mylog.Printf("发送文本报错信息失败: %v", err)
}
}
if config.GetSendErrorPicAsUrl() {
msgseq := echo.GetMappingSeq(messageID)
echo.AddMappingSeq(messageID, msgseq+1)
groupReply := generateGroupMessage(messageID, nil, richMediaMessageCopy.URL, msgseq+1)
// 进行类型断言
groupMessage, ok := groupReply.(*dto.MessageToCreate)
if !ok {
mylog.Println("Error: Expected MessageToCreate type.")
return // 或其他错误处理
}
groupMessage.Timestamp = time.Now().Unix() // 设置时间戳
//重新为err赋值
_, err = apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), groupMessage)
if err != nil {
mylog.Printf("发送图片报错后转url发送失败: %v", err)
}
}
}

}
//发送成功回执
SendResponse(client, err, &message)
Expand Down
5 changes: 5 additions & 0 deletions handlers/send_private_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ func handleSendPrivateMsg(client callapi.Client, api openapi.OpenAPI, apiv2 open
echo.AddMappingSeq(messageID, msgseq+1)
//时间限制
lastSendTimestamp := echo.GetMappingFileTimeLimit(messageID)
if lastSendTimestamp == 0 {
lastSendTimestamp = echo.GetFileTimeLimit()
}
now := time.Now()
millis := now.UnixMilli()
diff := millis - lastSendTimestamp
Expand All @@ -158,13 +161,15 @@ func handleSendPrivateMsg(client callapi.Client, api openapi.OpenAPI, apiv2 open
mylog.Println("延迟完成")
_, err := apiv2.PostC2CMessage(context.TODO(), UserID, richMediaMessageCopy)
echo.AddMappingFileTimeLimit(messageID, millis)
echo.AddFileTimeLimit(millis)
if err != nil {
mylog.Printf("发送 %s 私聊信息失败: %v", key, err)
}
})
} else { // 发送消息
_, err := apiv2.PostC2CMessage(context.TODO(), UserID, richMediaMessage)
echo.AddMappingFileTimeLimit(messageID, millis)
echo.AddFileTimeLimit(millis)
if err != nil {
mylog.Printf("发送 %s 私聊信息失败: %v", key, err)
}
Expand Down
Loading