diff --git a/.gitignore b/.gitignore index 6eb03ec0..0dbd90b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ # botgo specific config.yml -idmap.db -idmap.db.lock -idmap.ini +*.db +*.db.lock +*.ini # Go specific *.exe diff --git a/Processor/ProcessChannelDirectMessage.go b/Processor/ProcessChannelDirectMessage.go index 57f545ac..e4fa4c14 100644 --- a/Processor/ProcessChannelDirectMessage.go +++ b/Processor/ProcessChannelDirectMessage.go @@ -45,8 +45,15 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) //将真实id写入数据库,可取出ChannelID idmap.WriteConfigv2(data.Author.ID, "channel_id", data.ChannelID) //将channelid写入数据库,可取出guild_id + ChannelID64, err := idmap.StoreIDv2(data.ChannelID) + if err != nil { + log.Printf("Error storing ID: %v", err) + return nil + } + //转成int再互转 + idmap.WriteConfigv2(fmt.Sprint(ChannelID64), "guild_id", data.GuildID) + //直接储存 适用于私信场景私聊 idmap.WriteConfigv2(data.ChannelID, "guild_id", data.GuildID) - //收到私聊信息调用的具体还原步骤 //1,idmap还原真实userid, //2,通过idmap获取channelid, @@ -168,6 +175,7 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) echo.AddMsgType(AppIDString, userid64, "guild_private") //储存当前群或频道号的类型 idmap.WriteConfigv2(data.ChannelID, "type", "guild_private") + //todo 完善频道类型信息转换 //调试 PrintStructWithFieldNames(onebotMsg) @@ -184,7 +192,7 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) log.Printf("Error storing ID: %v", err) return nil } - //转成int再互转 + //转成int再互转 适用于群场景私聊 idmap.WriteConfigv2(fmt.Sprint(ChannelID64), "guild_id", data.GuildID) //转换at messageText := handlers.RevertTransformedText(data) @@ -254,7 +262,8 @@ func (p *Processors) ProcessChannelDirectMessage(data *dto.WSDirectMessageData) echo.AddMsgID(AppIDString, userid64, data.ID) echo.AddMsgType(AppIDString, userid64, "guild_private") //储存当前群或频道号的类型 - idmap.WriteConfigv2(data.ChannelID, "type", "guild_private") + idmap.WriteConfigv2(fmt.Sprint(ChannelID64), "type", "guild_private") + echo.AddMsgType(AppIDString, ChannelID64, "guild_private") //调试 PrintStructWithFieldNames(groupMsg) diff --git a/Processor/ProcessGroupMessage.go b/Processor/ProcessGroupMessage.go index b8a2a2ea..26f9c1d8 100644 --- a/Processor/ProcessGroupMessage.go +++ b/Processor/ProcessGroupMessage.go @@ -98,7 +98,8 @@ func (p *Processors) ProcessGroupMessage(data *dto.WSGroupATMessageData) error { echo.AddMsgID(AppIDString, GroupID64, data.ID) echo.AddMsgType(AppIDString, GroupID64, "group") //储存当前群或频道号的类型 - idmap.WriteConfigv2(data.GroupID, "type", "group") + idmap.WriteConfigv2(fmt.Sprint(GroupID64), "type", "group") + echo.AddMsgType(AppIDString, GroupID64, "group") // 调试 PrintStructWithFieldNames(groupMsg) diff --git a/Processor/ProcessGuildATMessage.go b/Processor/ProcessGuildATMessage.go index 5ff0a5bf..c6e54ca1 100644 --- a/Processor/ProcessGuildATMessage.go +++ b/Processor/ProcessGuildATMessage.go @@ -91,6 +91,7 @@ func (p *Processors) ProcessGuildATMessage(data *dto.WSATMessageData) error { echo.AddMsgType(AppIDString, userid64, "guild") //储存当前群或频道号的类型 idmap.WriteConfigv2(data.ChannelID, "type", "guild") + //todo 完善频道转换 //调试 PrintStructWithFieldNames(onebotMsg) @@ -181,7 +182,8 @@ func (p *Processors) ProcessGuildATMessage(data *dto.WSATMessageData) error { echo.AddMsgID(AppIDString, ChannelID64, data.ID) echo.AddMsgType(AppIDString, ChannelID64, "guild") //储存当前群或频道号的类型 - idmap.WriteConfigv2(data.ChannelID, "type", "guild") + idmap.WriteConfigv2(fmt.Sprint(ChannelID64), "type", "guild") + echo.AddMsgType(AppIDString, ChannelID64, "guild") //调试 PrintStructWithFieldNames(groupMsg) diff --git a/Processor/ProcessGuildNormalMessage.go b/Processor/ProcessGuildNormalMessage.go index a7ffe172..3f51f0b9 100644 --- a/Processor/ProcessGuildNormalMessage.go +++ b/Processor/ProcessGuildNormalMessage.go @@ -90,6 +90,7 @@ func (p *Processors) ProcessGuildNormalMessage(data *dto.WSMessageData) error { echo.AddMsgType(AppIDString, userid64, "guild") //储存当前群或频道号的类型 idmap.WriteConfigv2(data.ChannelID, "type", "guild") + //todo 完善频道ob信息 //调试 PrintStructWithFieldNames(onebotMsg) @@ -180,7 +181,8 @@ func (p *Processors) ProcessGuildNormalMessage(data *dto.WSMessageData) error { echo.AddMsgID(AppIDString, ChannelID64, data.ID) echo.AddMsgType(AppIDString, ChannelID64, "guild") //储存当前群或频道号的类型 - idmap.WriteConfigv2(data.ChannelID, "type", "guild") + idmap.WriteConfigv2(fmt.Sprint(ChannelID64), "type", "guild") + echo.AddMsgType(AppIDString, ChannelID64, "guild") //调试 PrintStructWithFieldNames(groupMsg) diff --git a/gensokyo.db b/gensokyo.db index 5ab9bd33..602ebfc1 100644 Binary files a/gensokyo.db and b/gensokyo.db differ diff --git a/handlers/send_group_msg.go b/handlers/send_group_msg.go index a962794a..1d9dd5b5 100644 --- a/handlers/send_group_msg.go +++ b/handlers/send_group_msg.go @@ -112,6 +112,12 @@ func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap case "guild": //用GroupID给ChannelID赋值,因为我们是把频道虚拟成了群 message.Params.ChannelID = message.Params.GroupID.(string) + // 使用RetrieveRowByIDv2还原真实的ChannelID + RChannelID, err := idmap.RetrieveRowByIDv2(message.Params.ChannelID) + if err != nil { + log.Printf("error retrieving real UserID: %v", err) + } + message.Params.ChannelID = RChannelID //这一句是group_private的逻辑,发频道信息用的是channelid //message.Params.GroupID = value handleSendGuildChannelMsg(client, api, apiv2, message) @@ -139,14 +145,20 @@ func handleSendGroupMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openap } } +// 不支持base64 func generateGroupMessage(id string, foundItems map[string][]string, messageText string) interface{} { if imageURLs, ok := foundItems["local_image"]; ok && len(imageURLs) > 0 { // 从本地路径读取图片 imageData, err := os.ReadFile(imageURLs[0]) if err != nil { - // 读入文件,如果是本地图,应用端和gensokyo需要在一台电脑 + // 读入文件失败 log.Printf("Error reading the image from path %s: %v", imageURLs[0], err) - return nil + // 返回文本信息,提示图片文件不存在 + return &dto.MessageToCreate{ + Content: "错误: 图片文件不存在", + MsgID: id, + MsgType: 0, // 默认文本类型 + } } // base64编码 @@ -156,7 +168,12 @@ func generateGroupMessage(id string, foundItems map[string][]string, messageText imageURL, err := images.UploadBase64ImageToServer(base64Encoded) if err != nil { log.Printf("Error uploading base64 encoded image: %v", err) - return nil + // 如果上传失败,也返回文本信息,提示上传失败 + return &dto.MessageToCreate{ + Content: "错误: 上传图片失败", + MsgID: id, + MsgType: 0, // 默认文本类型 + } } // 创建RichMediaMessage并返回,当作URL图片处理 diff --git a/handlers/send_guild_channel_msg.go b/handlers/send_guild_channel_msg.go index 5b6f46ab..0a9d5506 100644 --- a/handlers/send_guild_channel_msg.go +++ b/handlers/send_guild_channel_msg.go @@ -9,6 +9,7 @@ import ( "github.com/hoshinonyaruko/gensokyo/callapi" "github.com/hoshinonyaruko/gensokyo/config" + "github.com/hoshinonyaruko/gensokyo/idmap" "github.com/hoshinonyaruko/gensokyo/echo" @@ -50,6 +51,7 @@ func handleSendGuildChannelMsg(client callapi.Client, api openapi.OpenAPI, apiv2 messageText, foundItems := parseMessageContent(params) channelID := params.ChannelID + log.Printf("发送文本信息失败: %v,%v", channelID, channelID) // 使用 echo 获取消息ID var messageID string if echoStr, ok := message.Echo.(string); ok { @@ -63,8 +65,8 @@ func handleSendGuildChannelMsg(client callapi.Client, api openapi.OpenAPI, apiv2 } log.Println("频道发信息messageText:", messageText) log.Println("foundItems:", foundItems) - var err error // 优先发送文本信息 + var err error if messageText != "" { textMsg, _ := generateReplyMessage(messageID, nil, messageText) if _, err = api.PostMessage(context.TODO(), channelID, textMsg); err != nil { @@ -112,13 +114,18 @@ func handleSendGuildChannelMsg(client callapi.Client, api openapi.OpenAPI, apiv2 params := message.Params channelID := params.ChannelID guildID := params.GuildID - handleSendGuildChannelPrivateMsg(client, api, apiv2, message, &guildID, &channelID) + // 使用RetrieveRowByIDv2还原真实的ChannelID + RChannelID, err := idmap.RetrieveRowByIDv2(channelID) + if err != nil { + log.Printf("error retrieving real UserID: %v", err) + } + handleSendGuildChannelPrivateMsg(client, api, apiv2, message, &guildID, &RChannelID) default: log.Printf("2Unknown message type: %s", msgType) } } -// 组合发频道信息需要的MessageToCreate +// 组合发频道信息需要的MessageToCreate 支持base64 func generateReplyMessage(id string, foundItems map[string][]string, messageText string) (*dto.MessageToCreate, bool) { var reply dto.MessageToCreate var isBase64 bool @@ -129,7 +136,13 @@ func generateReplyMessage(id string, foundItems map[string][]string, messageText if err != nil { // 读入文件,如果是本地图,应用端和gensokyo需要在一台电脑 log.Printf("Error reading the image from path %s: %v", imageURLs[0], err) - return nil, false + // 发文本信息,提示图片文件不存在 + reply = dto.MessageToCreate{ + Content: "错误: 图片文件不存在", + MsgID: id, + MsgType: 0, // 默认文本类型 + } + return &reply, false } //base64编码 diff --git a/handlers/send_msg.go b/handlers/send_msg.go index 89dc8b19..c4db671f 100644 --- a/handlers/send_msg.go +++ b/handlers/send_msg.go @@ -2,10 +2,8 @@ package handlers import ( "context" - "encoding/base64" "fmt" "log" - "os" "strconv" "time" @@ -13,7 +11,6 @@ import ( "github.com/hoshinonyaruko/gensokyo/config" "github.com/hoshinonyaruko/gensokyo/echo" "github.com/hoshinonyaruko/gensokyo/idmap" - "github.com/hoshinonyaruko/gensokyo/images" "github.com/tencent-connect/botgo/dto" "github.com/tencent-connect/botgo/openapi" ) @@ -74,7 +71,7 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope // 优先发送文本信息 if messageText != "" { - groupReply := generateMessage(messageID, nil, messageText) + groupReply := generateGroupMessage(messageID, nil, messageText) // 进行类型断言 groupMessage, ok := groupReply.(*dto.MessageToCreate) @@ -97,7 +94,7 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope var singleItem = make(map[string][]string) singleItem[key] = urls - groupReply := generateMessage(messageID, singleItem, "") + groupReply := generateGroupMessage(messageID, singleItem, "") // 进行类型断言 richMediaMessage, ok := groupReply.(*dto.RichMediaMessage) @@ -117,6 +114,12 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope case "guild": //用GroupID给ChannelID赋值,因为我们是把频道虚拟成了群 message.Params.ChannelID = message.Params.GroupID.(string) + // 使用RetrieveRowByIDv2还原真实的ChannelID + RChannelID, err := idmap.RetrieveRowByIDv2(message.Params.ChannelID) + if err != nil { + log.Printf("error retrieving real UserID: %v", err) + } + message.Params.ChannelID = RChannelID handleSendGuildChannelMsg(client, api, apiv2, message) case "guild_private": //send_msg比具体的send_xxx少一层,其包含的字段类型在虚拟化场景已经失去作用 @@ -130,14 +133,8 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope // 先尝试将GroupID断言为字符串 if channelID, ok := message.Params.GroupID.(string); ok && channelID != "" { channelIDPtr = &channelID - // 使用RetrieveRowByIDv2还原真实的UserID - RChannelID, err := idmap.RetrieveRowByIDv2(*channelIDPtr) - if err != nil { - log.Printf("error retrieving real ChannelID: %v", err) - return - } // 读取bolt数据库 通过ChannelID取回之前储存的guild_id - if value, err := idmap.ReadConfigv2(RChannelID, "guild_id"); err == nil && value != "" { + if value, err := idmap.ReadConfigv2(*channelIDPtr, "guild_id"); err == nil && value != "" { GuildidPtr = &value } else { log.Printf("Error reading config: %v", err) @@ -178,7 +175,7 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope // 优先发送文本信息 if messageText != "" { - groupReply := generateMessage(messageID, nil, messageText) + groupReply := generateGroupMessage(messageID, nil, messageText) // 进行类型断言 groupMessage, ok := groupReply.(*dto.MessageToCreate) @@ -201,7 +198,7 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope var singleItem = make(map[string][]string) singleItem[key] = urls - groupReply := generateMessage(messageID, singleItem, "") + groupReply := generateGroupMessage(messageID, singleItem, "") // 进行类型断言 richMediaMessage, ok := groupReply.(*dto.RichMediaMessage) @@ -221,82 +218,6 @@ func handleSendMsg(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.Ope } } -func generateMessage(id string, foundItems map[string][]string, messageText string) interface{} { - if imageURLs, ok := foundItems["local_image"]; ok && len(imageURLs) > 0 { - // 从本地路径读取图片 - imageData, err := os.ReadFile(imageURLs[0]) - if err != nil { - // 读入文件,如果是本地图,应用端和gensokyo需要在一台电脑 - log.Printf("Error reading the image from path %s: %v", imageURLs[0], err) - return nil - } - - // base64编码 - base64Encoded := base64.StdEncoding.EncodeToString(imageData) - - // 上传base64编码的图片并获取其URL - imageURL, err := images.UploadBase64ImageToServer(base64Encoded) - if err != nil { - log.Printf("Error uploading base64 encoded image: %v", err) - return nil - } - - // 创建RichMediaMessage并返回,当作URL图片处理 - return &dto.RichMediaMessage{ - EventID: id, - FileType: 1, // 1代表图片 - URL: imageURL, - Content: "", // 这个字段文档没有了 - SrvSendMsg: true, - } - } else if imageURLs, ok := foundItems["url_image"]; ok && len(imageURLs) > 0 { - // 发链接图片 - return &dto.RichMediaMessage{ - EventID: id, - FileType: 1, // 1代表图片 - URL: "http://" + imageURLs[0], - Content: "", // 这个字段文档没有了 - SrvSendMsg: true, - } - } else if base64_image, ok := foundItems["base64_image"]; ok && len(base64_image) > 0 { - // todo 适配base64图片 - //因为QQ群没有 form方式上传,所以在gensokyo内置了图床,需公网,或以lotus方式连接位于公网的gensokyo - //要正确的开放对应的端口和设置正确的ip地址在config,这对于一般用户是有一些难度的 - if base64Image, ok := foundItems["base64_image"]; ok && len(base64Image) > 0 { - // 解码base64图片数据 - fileImageData, err := base64.StdEncoding.DecodeString(base64Image[0]) - if err != nil { - log.Printf("failed to decode base64 image: %v", err) - return nil - } - // 将解码的图片数据转换回base64格式并上传 - imageURL, err := images.UploadBase64ImageToServer(base64.StdEncoding.EncodeToString(fileImageData)) - if err != nil { - log.Printf("failed to upload base64 image: %v", err) - return nil - } - // 创建RichMediaMessage并返回 - return &dto.RichMediaMessage{ - EventID: id, - FileType: 1, // 1代表图片 - URL: imageURL, - Content: "", // 这个字段文档没有了 - SrvSendMsg: true, - } - } - } else if voiceURLs, ok := foundItems["base64_record"]; ok && len(voiceURLs) > 0 { - // 目前不支持发语音 todo 适配base64 slik - } else { - // 返回文本信息 - return &dto.MessageToCreate{ - Content: messageText, - MsgID: id, - MsgType: 0, // 默认文本类型 - } - } - return nil -} - // 通过user_id获取messageID func GetMessageIDByUseridOrGroupid(appID string, userID interface{}) string { // 从appID和userID生成key diff --git a/handlers/send_private_msg.go b/handlers/send_private_msg.go index b3b0cc80..e6fb7605 100644 --- a/handlers/send_private_msg.go +++ b/handlers/send_private_msg.go @@ -28,6 +28,21 @@ func handleSendPrivateMsg(client callapi.Client, api openapi.OpenAPI, apiv2 open msgType = echo.GetMsgTypeByKey(echoStr) } + //如果获取不到 就用group_id获取信息类型 + if msgType == "" { + appID := config.GetAppIDStr() + groupID := message.Params.GroupID + fmt.Printf("appID: %s, GroupID: %v\n", appID, groupID) + + msgType = GetMessageTypeByGroupid(appID, groupID) + fmt.Printf("msgType: %s\n", msgType) + } + + //如果获取不到 就用user_id获取信息类型 + if msgType == "" { + msgType = GetMessageTypeByUserid(config.GetAppIDStr(), message.Params.UserID) + } + switch msgType { case "group_private": //私聊信息 @@ -251,13 +266,8 @@ func getGuildIDFromMessage(message callapi.ActionMessage) (string, string, error if err != nil { return "", "", fmt.Errorf("error reading channel_id: %v", err) } - // 使用RetrieveRowByIDv2还原真实的ChannelID - RChannelID, err := idmap.RetrieveRowByIDv2(channelID) - if err != nil { - log.Printf("error retrieving real UserID: %v", err) - } - // 使用channelID作为sectionName从数据库中获取guild_id - guildID, err := idmap.ReadConfigv2(RChannelID, "guild_id") + //使用channelID作为sectionName从数据库中获取guild_id + guildID, err := idmap.ReadConfigv2(channelID, "guild_id") if err != nil { return "", "", fmt.Errorf("error reading guild_id: %v", err) } diff --git a/handlers/set_group_ban.go b/handlers/set_group_ban.go index 93cb0e20..621760e6 100644 --- a/handlers/set_group_ban.go +++ b/handlers/set_group_ban.go @@ -20,18 +20,12 @@ func setGroupBan(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenA // 从message中获取group_id和UserID groupID := message.Params.GroupID.(string) receivedUserID := message.Params.UserID.(string) - // 使用RetrieveRowByIDv2还原真实的ChannelID - RChannelID, err := idmap.RetrieveRowByIDv2(groupID) + //读取ini 通过ChannelID取回之前储存的guild_id + guildID, err := idmap.ReadConfigv2(groupID, "guild_id") if err != nil { - log.Printf("error retrieving real UserID: %v", err) - } - // 根据RChannelID读取guild_id - guildID, err := idmap.ReadConfigv2(RChannelID, "guild_id") - if err != nil { - log.Printf("Error reading config for guild_id: %v", err) + log.Printf("Error reading config: %v", err) return } - // 根据UserID读取真实的userid realUserID, err := idmap.RetrieveRowByIDv2(receivedUserID) if err != nil { diff --git a/handlers/set_group_whole_ban.go b/handlers/set_group_whole_ban.go index bf010ccc..5166ee24 100644 --- a/handlers/set_group_whole_ban.go +++ b/handlers/set_group_whole_ban.go @@ -17,18 +17,12 @@ func init() { func setGroupWholeBan(client callapi.Client, api openapi.OpenAPI, apiv2 openapi.OpenAPI, message callapi.ActionMessage) { // 从message中获取group_id groupID := message.Params.GroupID.(string) - // 使用RetrieveRowByIDv2还原真实的ChannelID - RChannelID, err := idmap.RetrieveRowByIDv2(groupID) + //读取ini 通过ChannelID取回之前储存的guild_id + guildID, err := idmap.ReadConfigv2(groupID, "guild_id") if err != nil { - log.Printf("error retrieving real UserID: %v", err) - } - // 根据group_id读取guild_id - guildID, err := idmap.ReadConfigv2(RChannelID, "guild_id") - if err != nil { - log.Printf("Error reading config for guild_id: %v", err) + log.Printf("Error reading config: %v", err) return } - // 读取消息类型 msgType, err := idmap.ReadConfigv2(groupID, "type") if err != nil {