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

Beta128 #298

Merged
merged 7 commits into from
Jan 12, 2024
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/ProcessC2CMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func (p *Processors) ProcessC2CMessage(data *dto.WSC2CMessageData) error {
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
privateMsg.Echo = echostr
//用向应用端(如果支持)发送echo,来确定客户端的send_msg对应的触发词原文
echo.AddMsgIDv3(AppIDString, echostr, messageText)
}
// 将当前s和appid和message进行映射
echo.AddMsgID(AppIDString, s, data.ID)
Expand Down Expand Up @@ -220,6 +222,8 @@ func (p *Processors) ProcessC2CMessage(data *dto.WSC2CMessageData) error {
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
groupMsg.Echo = echostr
//用向应用端(如果支持)发送echo,来确定客户端的send_msg对应的触发词原文
echo.AddMsgIDv3(AppIDString, echostr, messageText)
}
// 获取MasterID数组
masterIDs := config.GetMasterID()
Expand Down
6 changes: 6 additions & 0 deletions Processor/ProcessChannelDirectMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
privateMsg.Echo = echostr
//用向应用端(如果支持)发送echo,来确定客户端的send_msg对应的触发词原文
echo.AddMsgIDv3(AppIDString, echostr, messageText)
}
// 将当前s和appid和message进行映射
echo.AddMsgID(AppIDString, s, data.ID)
Expand Down Expand Up @@ -218,6 +220,8 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
onebotMsg.Echo = echostr
//用向应用端(如果支持)发送echo,来确定客户端的send_msg对应的触发词原文
echo.AddMsgIDv3(AppIDString, echostr, messageText)
}
// 获取MasterID数组
masterIDs := config.GetMasterID()
Expand Down Expand Up @@ -368,6 +372,8 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData)
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
groupMsg.Echo = echostr
//用向应用端(如果支持)发送echo,来确定客户端的send_msg对应的触发词原文
echo.AddMsgIDv3(AppIDString, echostr, messageText)
}
// 获取MasterID数组
masterIDs := config.GetMasterID()
Expand Down
2 changes: 2 additions & 0 deletions Processor/ProcessGroupMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ func (p *Processors) ProcessGroupMessage(data *dto.WSGroupATMessageData) error {
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
groupMsg.Echo = echostr
//用向应用端(如果支持)发送echo,来确定客户端的send_msg对应的触发词原文
echo.AddMsgIDv3(AppIDString, echostr, messageText)
}
// 获取MasterID数组
masterIDs := config.GetMasterID()
Expand Down
9 changes: 8 additions & 1 deletion Processor/ProcessGuildATMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ func (p *Processors) ProcessGuildATMessage(data *dto.WSATMessageData) error {
SubType: "channel",
Time: t.Unix(),
Avatar: data.Author.Avatar,
Echo: echostr,
}
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
onebotMsg.Echo = echostr
//用向应用端(如果支持)发送echo,来确定客户端的send_msg对应的触发词原文
echo.AddMsgIDv3(AppIDString, echostr, messageText)
}
// 获取MasterID数组
masterIDs := config.GetMasterID()
Expand Down Expand Up @@ -226,6 +231,8 @@ func (p *Processors) ProcessGuildATMessage(data *dto.WSATMessageData) error {
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
groupMsg.Echo = echostr
//用向应用端(如果支持)发送echo,来确定客户端的send_msg对应的触发词原文
echo.AddMsgIDv3(AppIDString, echostr, messageText)
}
// 获取MasterID数组
masterIDs := config.GetMasterID()
Expand Down
4 changes: 4 additions & 0 deletions Processor/ProcessGuildNormalMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func (p *Processors) ProcessGuildNormalMessage(data *dto.WSMessageData) error {
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
onebotMsg.Echo = echostr
//用向应用端(如果支持)发送echo,来确定客户端的send_msg对应的触发词原文
echo.AddMsgIDv3(AppIDString, echostr, messageText)
}
// 获取MasterID数组
masterIDs := config.GetMasterID()
Expand Down Expand Up @@ -225,6 +227,8 @@ func (p *Processors) ProcessGuildNormalMessage(data *dto.WSMessageData) error {
// 根据条件判断是否添加Echo字段
if config.GetTwoWayEcho() {
groupMsg.Echo = echostr
//用向应用端(如果支持)发送echo,来确定客户端的send_msg对应的触发词原文
echo.AddMsgIDv3(AppIDString, echostr, messageText)
}
// 获取MasterID数组
masterIDs := config.GetMasterID()
Expand Down
4 changes: 4 additions & 0 deletions botgo/dto/keyboard/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ type Action struct {
ClickLimit uint32 `json:"click_limit,omitempty"` // 可点击的次数, 默认不限
Data string `json:"data,omitempty"` // 操作相关数据
AtBotShowChannelList bool `json:"at_bot_show_channel_list,omitempty"` // false:当前 true:弹出展示子频道选择器
UnsupportTips string `json:"unsupport_tips"` //2024-1-12 新增字段
AnChor int `json:"anchor"` //本字段仅在指令按钮下有效,设置后后会忽略 action.enter 配置。设置为 1 时 ,点击按钮自动唤起启手Q选图器,其他值暂无效果。(仅支持手机端版本 8983+ 的单聊场景,桌面端不支持)
Enter bool `json:"enter"` //指令按钮可用,点击按钮后直接自动发送 data,默认 false。支持版本 8983
Reply bool `json:"reply"` //指令按钮可用,指令是否带引用回复本消息,默认 false。支持版本 8983
}

// Permission 按纽操作权限
Expand Down
26 changes: 26 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ type Settings struct {
WhiteEnable []bool `yaml:"white_enable"`
IdentifyAppids []int64 `yaml:"identify_appids"`
TransFormApiIds bool `yaml:"transform_api_ids"`
CustomTemplateID string `yaml:"custom_template_id"`
KeyBoardID string `yaml:"keyboard_id"`
}

// LoadConfig 从文件中加载配置并初始化单例配置
Expand Down Expand Up @@ -1650,3 +1652,27 @@ func GetTransFormApiIds() bool {
}
return instance.Settings.TransFormApiIds
}

// 获取 CustomTemplateID 的值
func GetCustomTemplateID() string {
mu.Lock()
defer mu.Unlock()

if instance == nil {
mylog.Println("Warning: instance is nil when trying to get CustomTemplateID.")
return ""
}
return instance.Settings.CustomTemplateID
}

// 获取 KeyBoardIDD 的值
func GetKeyBoardID() string {
mu.Lock()
defer mu.Unlock()

if instance == nil {
mylog.Println("Warning: instance is nil when trying to get KeyBoardID.")
return ""
}
return instance.Settings.KeyBoardID
}
9 changes: 9 additions & 0 deletions echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ func AddMsgIDv3(appid string, s string, msgID string) {
globalEchoMapping.msgIDMapping[key] = msgID
}

// GetMsgIDv3 返回给定appid和s的msgID
func GetMsgIDv3(appid string, s string) string {
key := globalEchoMapping.GenerateKeyv3(appid, s)
globalEchoMapping.mu.Lock()
defer globalEchoMapping.mu.Unlock()

return globalEchoMapping.msgIDMapping[key]
}

// 添加group和userid对应的messageid
func AddMsgIDv2(appid string, groupid int64, userid int64, msgID string) {
key := globalEchoMapping.GenerateKeyv2(appid, groupid, userid)
Expand Down
185 changes: 159 additions & 26 deletions handlers/send_group_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"

"github.com/hoshinonyaruko/gensokyo/callapi"
Expand All @@ -18,6 +20,7 @@ import (
"github.com/hoshinonyaruko/gensokyo/mylog"
"github.com/hoshinonyaruko/gensokyo/silk"
"github.com/tencent-connect/botgo/dto"
"github.com/tencent-connect/botgo/dto/keyboard"
"github.com/tencent-connect/botgo/openapi"
)

Expand All @@ -33,22 +36,18 @@ func HandleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
// 当 message.Echo 是字符串类型时执行此块
msgType = echo.GetMsgTypeByKey(echoStr)
}
//如果获取不到 就用user_id获取信息类型
if msgType == "" {
msgType = GetMessageTypeByUserid(config.GetAppIDStr(), message.Params.UserID)
}

//如果获取不到 就用group_id获取信息类型
if msgType == "" {
msgType = GetMessageTypeByGroupid(config.GetAppIDStr(), message.Params.GroupID)
}
//新增 内存获取不到从数据库获取
if msgType == "" {
msgType = GetMessageTypeByUseridV2(message.Params.UserID)
msgType = GetMessageTypeByUserid(config.GetAppIDStr(), message.Params.UserID)
}
if msgType == "" {
msgType = GetMessageTypeByGroupidV2(message.Params.GroupID)
}
if msgType == "" {
msgType = GetMessageTypeByUseridV2(message.Params.UserID)
}
mylog.Printf("send_group_msg获取到信息类型:%v", msgType)
var idInt64 int64
var err error
Expand Down Expand Up @@ -202,26 +201,52 @@ func HandleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap
mylog.Printf("Error: Expected RichMediaMessage type for key ")
return "", nil
}
// 上传图片并获取FileInfo
fileInfo, err := uploadMedia(context.TODO(), message.Params.GroupID.(string), richMediaMessage, apiv2)
if err != nil {
mylog.Printf("上传图片失败: %v", err)
return "", nil // 或其他错误处理
}
// 创建包含文本和图像信息的消息
msgseq = echo.GetMappingSeq(messageID)
echo.AddMappingSeq(messageID, msgseq+1)
groupMessage := &dto.MessageToCreate{
Content: messageText, // 添加文本内容
Media: dto.Media{
FileInfo: fileInfo, // 添加图像信息
},
MsgID: messageID,
MsgSeq: msgseq,
MsgType: 7, // 假设7是组合消息类型
var groupMessage *dto.MessageToCreate
var transmd bool
var md *dto.Markdown
var kb *keyboard.MessageKeyboard
//判断是否需要自动转换md
if config.GetTwoWayEcho() {
md, kb, transmd = auto_md(message, messageText, richMediaMessage)
}
groupMessage.Timestamp = time.Now().Unix() // 设置时间戳

//如果没有转换成md发送
if !transmd {
// 上传图片并获取FileInfo
fileInfo, err := uploadMedia(context.TODO(), message.Params.GroupID.(string), richMediaMessage, apiv2)
if err != nil {
mylog.Printf("上传图片失败: %v", err)
return "", nil // 或其他错误处理
}
// 创建包含文本和图像信息的消息
msgseq = echo.GetMappingSeq(messageID)
echo.AddMappingSeq(messageID, msgseq+1)
groupMessage = &dto.MessageToCreate{
Content: messageText, // 添加文本内容
Media: dto.Media{
FileInfo: fileInfo, // 添加图像信息
},
MsgID: messageID,
MsgSeq: msgseq,
MsgType: 7, // 假设7是组合消息类型
}
groupMessage.Timestamp = time.Now().Unix() // 设置时间戳
} else {
//将kb和md组合成groupMessage并用MsgType=2发送

msgseq = echo.GetMappingSeq(messageID)
echo.AddMappingSeq(messageID, msgseq+1)
groupMessage = &dto.MessageToCreate{
Content: "markdown", // 添加文本内容
MsgID: messageID,
MsgSeq: msgseq,
Markdown: md,
Keyboard: kb,
MsgType: 2, // 假设7是组合消息类型
}
groupMessage.Timestamp = time.Now().Unix() // 设置时间戳

}
// 发送组合消息
ret, err = apiv2.PostGroupMessage(context.TODO(), message.Params.GroupID.(string), groupMessage)
if err != nil {
Expand Down Expand Up @@ -1020,3 +1045,111 @@ func SendStackMessages(apiv2 openapi.OpenAPI, messageid string, originalGroupID

}
}

func auto_md(message callapi.ActionMessage, messageText string, richMediaMessage *dto.RichMediaMessage) (md *dto.Markdown, kb *keyboard.MessageKeyboard, transmd bool) {
if echoStr, ok := message.Echo.(string); ok {
// 当 message.Echo 是字符串类型时才执行此块
msg_on_touch := echo.GetMsgIDv3(config.GetAppIDStr(), echoStr)
mylog.Printf("msg_on_touch:%v", msg_on_touch)
// 判断是否是 GetVisualkPrefixs 数组开头的文本
visualkPrefixs := config.GetVisualkPrefixs()
var matchedPrefix *config.VisualPrefixConfig
// 去掉前缀开头的*
for i, vp := range visualkPrefixs {
if strings.HasPrefix(vp.Prefix, "*") {
visualkPrefixs[i].Prefix = strings.TrimPrefix(vp.Prefix, "*")
}
}

for _, vp := range visualkPrefixs {
if strings.HasPrefix(msg_on_touch, vp.Prefix) {
if len(msg_on_touch) >= len(vp.Prefix) {
if msg_on_touch != " " {
transmd = true
matchedPrefix = &vp
break // 匹配到了
}
}
}
}
if transmd {
//将messageText和groupReply组合成一个md
// 处理 Markdown
CustomTemplateID := config.GetCustomTemplateID()
imgURL := richMediaMessage.URL
height, width, err := images.GetImageDimensions(imgURL)
if err != nil {
mylog.Printf("获取图片宽高出错")
}
imgDesc := fmt.Sprintf("图片 #%dpx #%dpx", width, height)
// 检查messageText是否以\r开头
if !strings.HasPrefix(messageText, "\r") {
messageText = "\r" + messageText
}
// 创建 MarkdownParams 的实例
mdParams := []*dto.MarkdownParams{
{Key: "text_start", Values: []string{" "}}, //空着
{Key: "img_dec", Values: []string{imgDesc}},
{Key: "img_url", Values: []string{imgURL}},
{Key: "text_end", Values: []string{messageText}},
}
// 组合模板 Markdown
md = &dto.Markdown{
CustomTemplateID: CustomTemplateID,
Params: mdParams,
}
whiteList := matchedPrefix.WhiteList
// 创建 CustomKeyboard
customKeyboard := &keyboard.CustomKeyboard{
Rows: []*keyboard.Row{},
}

var currentRow *keyboard.Row
buttonCount := 0 // 当前行的按钮计数

for _, whiteLabel := range whiteList {
// 使用 strconv.Atoi 检查 whiteLabel 是否为纯数字
if _, err := strconv.Atoi(whiteLabel); err == nil {
// 如果没有错误,表示 whiteLabel 是一个数字,因此忽略这个元素并继续下一个迭代
continue
}

// 创建按钮
button := &keyboard.Button{
RenderData: &keyboard.RenderData{
Label: whiteLabel,
VisitedLabel: whiteLabel,
Style: 1,
},
Action: &keyboard.Action{
Type: 2, // 帮用户输入二级指令
Permission: &keyboard.Permission{
Type: 2, //所有人可操作
},
Data: msg_on_touch + " " + whiteLabel,
UnsupportTips: "请升级新版手机QQ",
},
}

// 如果当前行为空或已满(4个按钮),则创建一个新行
if currentRow == nil || buttonCount == 4 {
currentRow = &keyboard.Row{}
customKeyboard.Rows = append(customKeyboard.Rows, currentRow)
buttonCount = 0 // 重置按钮计数
}

// 将按钮添加到当前行
currentRow.Buttons = append(currentRow.Buttons, button)
buttonCount++
}

// 在循环结束后,最后一行可能不满4个按钮,但已经被正确处理

// 创建 MessageKeyboard 并设置其 Content
kb = &keyboard.MessageKeyboard{
Content: customKeyboard,
}
}
}
return md, kb, transmd
}
Loading
Loading