From 2561d6570d22ce6e37682266dd100c7af0e52aa0 Mon Sep 17 00:00:00 2001 From: SanaeFox <36219542+Hoshinonyaruko@users.noreply.github.com> Date: Mon, 19 Aug 2024 01:36:40 +0800 Subject: [PATCH] Beta482 (#488) * beta447 * beta448 * beta449 * beta450 * beta451 * beta452 * beta453 * beta454 * beta455 * btea455 * beta456 * beta457 * beta458 * beta460 * beta460 * beta461 * beta462 * beta463 * beta464 * beta465 * beta467 * beta468 * beta469 * beta470 * beta471 * beta472 * beta473 * beta473 * beta475 * beta476 * beta478 * beta479 * beta479 * beta480 * beta481 * beta482 --- Processor/ProcessC2CMessage.go | 364 +++++++++++++++++++------------ handlers/send_msg.go | 22 ++ handlers/send_private_msg_sse.go | 69 +++--- httpapi/httpapi.go | 142 +++++++++++- 4 files changed, 420 insertions(+), 177 deletions(-) diff --git a/Processor/ProcessC2CMessage.go b/Processor/ProcessC2CMessage.go index 0bc4854b..2b62b304 100644 --- a/Processor/ProcessC2CMessage.go +++ b/Processor/ProcessC2CMessage.go @@ -182,158 +182,250 @@ func (p *Processors) ProcessC2CMessage(data *dto.WSC2CMessageData) error { idmap.StoreUserInfo(data.Author.ID, userdata) } else { //将私聊信息转化为群信息(特殊需求情况下) + if !config.GetStringOb11() { + //转换appid + AppIDString := strconv.FormatUint(p.Settings.AppID, 10) + // 获取当前时间的13位毫秒级时间戳 + currentTimeMillis := time.Now().UnixNano() / 1e6 + // 构造echostr,包括AppID,原始的s变量和当前时间戳 + echostr := fmt.Sprintf("%s_%d_%d", AppIDString, s, currentTimeMillis) + //把userid作为群号 + //映射str的userid到int + var userid64 int64 + var err error + var magic int64 + if config.GetIdmapPro() { + //将真实id转为int userid64 + magic, userid64, err = idmap.StoreIDv2Pro("group_private", data.Author.ID) + mylog.Printf("魔法数字:%v", magic) //690426430 + if err != nil { + mylog.Fatalf("Error storing ID: %v", err) + } + //当参数不全,降级时 + _, _ = idmap.StoreIDv2(data.Author.ID) + //补救措施 + idmap.SimplifiedStoreID(data.Author.ID) + } else { + //将真实id转为int userid64 + userid64, err = idmap.StoreIDv2(data.Author.ID) + if err != nil { + mylog.Fatalf("Error storing ID: %v", err) + } + } + //转换at + messageText := handlers.RevertTransformedText(data, "group_private", p.Api, p.Apiv2, userid64, userid64, config.GetWhiteEnable(5)) + if messageText == "" { + mylog.Printf("信息被自定义黑白名单拦截") + return nil + } + //框架内指令 + p.HandleFrameworkCommand(messageText, data, "group_private") + //映射str的messageID到int + var messageID64 int64 + if config.GetMemoryMsgid() { + messageID64, err = echo.StoreCacheInMemory(data.ID) + if err != nil { + log.Fatalf("Error storing ID: %v", err) + } + } else { + messageID64, err = idmap.StoreCachev2(data.ID) + if err != nil { + log.Fatalf("Error storing ID: %v", err) + } + } + messageID := int(messageID64) + //todo 判断array模式 然后对Message处理成array格式 + IsBindedUserId := idmap.CheckValue(data.Author.ID, userid64) - //转换appid - AppIDString := strconv.FormatUint(p.Settings.AppID, 10) - // 获取当前时间的13位毫秒级时间戳 - currentTimeMillis := time.Now().UnixNano() / 1e6 - // 构造echostr,包括AppID,原始的s变量和当前时间戳 - echostr := fmt.Sprintf("%s_%d_%d", AppIDString, s, currentTimeMillis) - //把userid作为群号 - //映射str的userid到int - var userid64 int64 - var err error - var magic int64 - if config.GetIdmapPro() { - //将真实id转为int userid64 - magic, userid64, err = idmap.StoreIDv2Pro("group_private", data.Author.ID) - mylog.Printf("魔法数字:%v", magic) //690426430 - if err != nil { - mylog.Fatalf("Error storing ID: %v", err) + var selfid64 int64 + if config.GetUseUin() { + selfid64 = config.GetUinint64() + } else { + selfid64 = int64(p.Settings.AppID) } - //当参数不全,降级时 - _, _ = idmap.StoreIDv2(data.Author.ID) - //补救措施 - idmap.SimplifiedStoreID(data.Author.ID) - } else { - //将真实id转为int userid64 - userid64, err = idmap.StoreIDv2(data.Author.ID) - if err != nil { - mylog.Fatalf("Error storing ID: %v", err) + + groupMsg := OnebotGroupMessage{ + RawMessage: messageText, + Message: messageText, + MessageID: messageID, + GroupID: userid64, + MessageType: "group", + PostType: "message", + SelfID: selfid64, + UserID: userid64, + Sender: Sender{ + UserID: userid64, + TinyID: "0", + Sex: "0", + Age: 0, + Area: "0", + Level: "0", + }, + SubType: "normal", + Time: time.Now().Unix(), } - } - //转换at - messageText := handlers.RevertTransformedText(data, "group_private", p.Api, p.Apiv2, userid64, userid64, config.GetWhiteEnable(5)) - if messageText == "" { - mylog.Printf("信息被自定义黑白名单拦截") - return nil - } - //框架内指令 - p.HandleFrameworkCommand(messageText, data, "group_private") - //映射str的messageID到int - var messageID64 int64 - if config.GetMemoryMsgid() { - messageID64, err = echo.StoreCacheInMemory(data.ID) - if err != nil { - log.Fatalf("Error storing ID: %v", err) + //增强配置 + if !config.GetNativeOb11() { + groupMsg.RealMessageType = "group_private" + groupMsg.IsBindedUserId = IsBindedUserId + groupMsg.RealUserID = data.Author.ID + groupMsg.Avatar, _ = GenerateAvatarURLV2(data.Author.ID) } - } else { - messageID64, err = idmap.StoreCachev2(data.ID) - if err != nil { - log.Fatalf("Error storing ID: %v", err) + //根据条件判断是否增加nick和card + var CaN = config.GetCardAndNick() + if CaN != "" { + groupMsg.Sender.Nickname = CaN + groupMsg.Sender.Card = CaN } - } - messageID := int(messageID64) - //todo 判断array模式 然后对Message处理成array格式 - IsBindedUserId := idmap.CheckValue(data.Author.ID, userid64) + // 根据条件判断是否添加Echo字段 + if config.GetTwoWayEcho() { + groupMsg.Echo = echostr + //用向应用端(如果支持)发送echo,来确定客户端的send_msg对应的触发词原文 + echo.AddMsgIDv3(AppIDString, echostr, messageText) + } + // 获取MasterID数组 + masterIDs := config.GetMasterID() - var selfid64 int64 - if config.GetUseUin() { - selfid64 = config.GetUinint64() - } else { - selfid64 = int64(p.Settings.AppID) - } + // 判断userid64是否在masterIDs数组里 + isMaster := false + for _, id := range masterIDs { + if strconv.FormatInt(userid64, 10) == id { + isMaster = true + break + } + } - groupMsg := OnebotGroupMessage{ - RawMessage: messageText, - Message: messageText, - MessageID: messageID, - GroupID: userid64, - MessageType: "group", - PostType: "message", - SelfID: selfid64, - UserID: userid64, - Sender: Sender{ - UserID: userid64, - TinyID: "0", - Sex: "0", - Age: 0, - Area: "0", - Level: "0", - }, - SubType: "normal", - Time: time.Now().Unix(), - } - //增强配置 - if !config.GetNativeOb11() { - groupMsg.RealMessageType = "group_private" - groupMsg.IsBindedUserId = IsBindedUserId - groupMsg.RealUserID = data.Author.ID - groupMsg.Avatar, _ = GenerateAvatarURLV2(data.Author.ID) - } - //根据条件判断是否增加nick和card - var CaN = config.GetCardAndNick() - if CaN != "" { - groupMsg.Sender.Nickname = CaN - groupMsg.Sender.Card = CaN - } - // 根据条件判断是否添加Echo字段 - if config.GetTwoWayEcho() { - groupMsg.Echo = echostr - //用向应用端(如果支持)发送echo,来确定客户端的send_msg对应的触发词原文 - echo.AddMsgIDv3(AppIDString, echostr, messageText) - } - // 获取MasterID数组 - masterIDs := config.GetMasterID() - - // 判断userid64是否在masterIDs数组里 - isMaster := false - for _, id := range masterIDs { - if strconv.FormatInt(userid64, 10) == id { - isMaster = true - break + // 根据isMaster的值为groupMsg的Sender赋值role字段 + if isMaster { + groupMsg.Sender.Role = "owner" + } else { + groupMsg.Sender.Role = "member" } - } + //将当前s和appid和message进行映射 + echo.AddMsgID(AppIDString, s, data.ID) + echo.AddMsgType(AppIDString, s, "group_private") + //为不支持双向echo的ob服务端映射 + echo.AddMsgID(AppIDString, userid64, data.ID) + //映射类型 + echo.AddMsgType(AppIDString, userid64, "group_private") + //储存当前群或频道号的类型 + idmap.WriteConfigv2(fmt.Sprint(userid64), "type", "group_private") + + //懒message_id池 + echo.AddLazyMessageId(strconv.FormatInt(userid64, 10), data.ID, time.Now()) - // 根据isMaster的值为groupMsg的Sender赋值role字段 - if isMaster { - groupMsg.Sender.Role = "owner" + //懒message_id池 + echo.AddLazyMessageId(data.Author.ID, data.ID, time.Now()) + + //调试 + PrintStructWithFieldNames(groupMsg) + + // Convert OnebotGroupMessage to map and send + groupMsgMap := structToMap(groupMsg) + //上报信息到onebotv11应用端(正反ws) + go p.BroadcastMessageToAll(groupMsgMap, p.Apiv2, data) + + //组合FriendData + struserid := strconv.FormatInt(userid64, 10) + userdata := structs.FriendData{ + Nickname: "", + Remark: "", + UserID: struserid, + } + //缓存私信好友列表 + idmap.StoreUserInfo(data.Author.ID, userdata) } else { - groupMsg.Sender.Role = "member" - } - //将当前s和appid和message进行映射 - echo.AddMsgID(AppIDString, s, data.ID) - echo.AddMsgType(AppIDString, s, "group_private") - //为不支持双向echo的ob服务端映射 - echo.AddMsgID(AppIDString, userid64, data.ID) - //映射类型 - echo.AddMsgType(AppIDString, userid64, "group_private") - //储存当前群或频道号的类型 - idmap.WriteConfigv2(fmt.Sprint(userid64), "type", "group_private") + //转换appid + AppIDString := strconv.FormatUint(p.Settings.AppID, 10) + // 获取当前时间的13位毫秒级时间戳 + currentTimeMillis := time.Now().UnixNano() / 1e6 + // 构造echostr,包括AppID,原始的s变量和当前时间戳 + echostr := fmt.Sprintf("%s_%d_%d", AppIDString, s, currentTimeMillis) - //懒message_id池 - echo.AddLazyMessageId(strconv.FormatInt(userid64, 10), data.ID, time.Now()) + var selfid64 int64 + if config.GetUseUin() { + selfid64 = config.GetUinint64() + } else { + selfid64 = int64(p.Settings.AppID) + } - //懒message_id池 - echo.AddLazyMessageId(data.Author.ID, data.ID, time.Now()) + //转换at + messageText := handlers.RevertTransformedText(data, "group_private", p.Api, p.Apiv2, 0, 0, config.GetWhiteEnable(5)) + if messageText == "" { + mylog.Printf("信息被自定义黑白名单拦截") + return nil + } - //调试 - PrintStructWithFieldNames(groupMsg) + groupMsg := OnebotGroupMessageS{ + RawMessage: messageText, + Message: messageText, + MessageID: data.ID, + GroupID: data.Author.ID, + MessageType: "group", + PostType: "message", + SelfID: selfid64, + UserID: data.Author.ID, + Sender: Sender{ + UserID: 0, + TinyID: "0", + Sex: "0", + Age: 0, + Area: "0", + Level: "0", + }, + SubType: "normal", + Time: time.Now().Unix(), + } - // Convert OnebotGroupMessage to map and send - groupMsgMap := structToMap(groupMsg) - //上报信息到onebotv11应用端(正反ws) - go p.BroadcastMessageToAll(groupMsgMap, p.Apiv2, data) + //增强配置 + if !config.GetNativeOb11() { + groupMsg.RealMessageType = "group_private" + groupMsg.RealUserID = data.Author.ID + groupMsg.Avatar, _ = GenerateAvatarURLV2(data.Author.ID) + } + //根据条件判断是否增加nick和card + var CaN = config.GetCardAndNick() + if CaN != "" { + groupMsg.Sender.Nickname = CaN + groupMsg.Sender.Card = CaN + } - //组合FriendData - struserid := strconv.FormatInt(userid64, 10) - userdata := structs.FriendData{ - Nickname: "", - Remark: "", - UserID: struserid, + // 根据条件判断是否添加Echo字段 + if config.GetTwoWayEcho() { + groupMsg.Echo = echostr + // 用向应用端(如果支持)发送echo,来确定客户端的send_msg对应的触发词原文 + echo.AddMsgIDv3(AppIDString, echostr, data.Content) + } + + //将当前s和appid和message进行映射 + echo.AddMsgID(AppIDString, s, data.ID) + echo.AddMsgType(AppIDString, s, "group_private") + + echo.AddMsgIDv3(AppIDString, data.Author.ID, data.ID) + + //储存当前群或频道号的类型 + idmap.WriteConfigv2(data.Author.ID, "type", "group_private") + + //懒message_id池 + echo.AddLazyMessageId(data.Author.ID, data.ID, time.Now()) + + //调试 + PrintStructWithFieldNames(groupMsg) + + // Convert OnebotGroupMessage to map and send + groupMsgMap := structToMap(groupMsg) + //上报信息到onebotv11应用端(正反ws) + go p.BroadcastMessageToAll(groupMsgMap, p.Apiv2, data) + + //组合FriendData + userdata := structs.FriendData{ + Nickname: "", + Remark: "", + UserID: data.Author.ID, + } + //缓存私信好友列表 + idmap.StoreUserInfo(data.Author.ID, userdata) } - //缓存私信好友列表 - idmap.StoreUserInfo(data.Author.ID, userdata) } return nil diff --git a/handlers/send_msg.go b/handlers/send_msg.go index ce61d397..cc06add9 100644 --- a/handlers/send_msg.go +++ b/handlers/send_msg.go @@ -218,6 +218,28 @@ func GetMessageIDByUseridOrGroupid(appID string, userID interface{}) string { return messageid } +// 通过user_id获取messageID +func GetMessageIDByUseridOrGroupidSP(appID string, userID interface{}) string { + // 从appID和userID生成key + var userIDStr string + switch u := userID.(type) { + case int: + userIDStr = strconv.Itoa(u) + case int64: + userIDStr = strconv.FormatInt(u, 10) + case float64: + userIDStr = strconv.FormatFloat(u, 'f', 0, 64) + case string: + userIDStr = u + default: + // 可能需要处理其他类型或报错 + return "" + } + + messageid := echo.GetMsgIDv3(appID, userIDStr) + return messageid +} + // 通过user_id获取EventID 私聊,群,频道,通用 userID可以是三者之一 这是不需要区分群+用户的 只需要精准到群 私聊只需要精准到用户 idmap不开启的用户使用 func GetEventIDByUseridOrGroupid(appID string, userID interface{}) string { // 从appID和userID生成key diff --git a/handlers/send_private_msg_sse.go b/handlers/send_private_msg_sse.go index 2a7dacd3..bdbe478c 100644 --- a/handlers/send_private_msg_sse.go +++ b/handlers/send_private_msg_sse.go @@ -8,7 +8,6 @@ import ( "github.com/hoshinonyaruko/gensokyo/callapi" "github.com/hoshinonyaruko/gensokyo/config" - "github.com/hoshinonyaruko/gensokyo/echo" "github.com/hoshinonyaruko/gensokyo/idmap" "github.com/hoshinonyaruko/gensokyo/mylog" "github.com/hoshinonyaruko/gensokyo/structs" @@ -72,24 +71,29 @@ func HandleSendPrivateMsgSSE(client callapi.Client, api openapi.OpenAPI, apiv2 o var resp *dto.C2CMessageResponse - //私聊信息 var UserID string - if config.GetIdmapPro() { - //还原真实的userid - //mylog.Printf("group_private:%v", message.Params.UserID.(string)) - _, UserID, err = idmap.RetrieveRowByIDv2Pro("690426430", message.Params.UserID.(string)) - if err != nil { - mylog.Printf("Error reading config: %v", err) - return "", nil + + if message.Params.UserID != nil && len(message.Params.UserID.(string)) != 32 { + //私聊信息 + if config.GetIdmapPro() { + //还原真实的userid + //mylog.Printf("group_private:%v", message.Params.UserID.(string)) + _, UserID, err = idmap.RetrieveRowByIDv2Pro("690426430", message.Params.UserID.(string)) + if err != nil { + mylog.Printf("Error reading config: %v", err) + return "", nil + } + mylog.Printf("测试,通过Proid获取的UserID:%v", UserID) + } else { + //还原真实的userid + UserID, err = idmap.RetrieveRowByIDv2(message.Params.UserID.(string)) + if err != nil { + mylog.Printf("Error reading config: %v", err) + return "", nil + } } - mylog.Printf("测试,通过Proid获取的UserID:%v", UserID) } else { - //还原真实的userid - UserID, err = idmap.RetrieveRowByIDv2(message.Params.UserID.(string)) - if err != nil { - mylog.Printf("Error reading config: %v", err) - return "", nil - } + UserID = message.Params.UserID.(string) } // 首先,将message.Params.Message序列化成JSON字符串 @@ -112,40 +116,27 @@ func HandleSendPrivateMsgSSE(client callapi.Client, api openapi.OpenAPI, apiv2 o // 使用 echo 获取消息ID var messageID string - if config.GetLazyMessageId() { - //由于实现了Params的自定义unmarshell 所以可以类型安全的断言为string - messageID = echo.GetLazyMessagesId(UserID) - mylog.Printf("GetLazyMessagesId: %v", messageID) - } - if messageID == "" { - if echoStr, ok := message.Echo.(string); ok { - messageID = echo.GetMsgIDByKey(echoStr) - mylog.Println("echo取私聊发信息对应的message_id:", messageID) - } - } + // 如果messageID仍然为空,尝试使用config.GetAppID和UserID的组合来获取messageID - // 如果messageID为空,通过函数获取 if messageID == "" { - messageID = GetMessageIDByUseridOrGroupid(config.GetAppIDStr(), UserID) - mylog.Println("通过GetMessageIDByUserid函数获取的message_id:", messageID) - } - if messageID == "2000" { - messageID = "" - mylog.Println("通过lazymsgid发送群私聊主动信息,每月可发送1次") + if config.GetStringOb11() { + messageID = GetMessageIDByUseridOrGroupidSP(config.GetAppIDStr(), UserID) + mylog.Errorf("通过GetMessageIDByUserid函数获取的message_id:" + messageID) + } else { + messageID = GetMessageIDByUseridOrGroupid(config.GetAppIDStr(), UserID) + mylog.Errorf("通过GetMessageIDByUserid函数获取的message_id:" + messageID) + } + } // 获取并打印相关ID relatedID := GetRelatedID(messageID) - //fmt.Println("相关ID:", relatedID) - dtoSSE := generateMessageSSE(messageBody, messageID, relatedID) - mylog.Printf("私聊发信息sse:%v", dtoSSE) - resp, err = apiv2.PostC2CMessageSSE(context.TODO(), UserID, dtoSSE) if err != nil { - mylog.Printf("发送文本私聊信息失败: %v", err) + mylog.Errorf("发送文本私聊信息失败: %v", err) //如果失败 防止进入递归 return "", nil } diff --git a/httpapi/httpapi.go b/httpapi/httpapi.go index 9f536cb0..bce146ca 100644 --- a/httpapi/httpapi.go +++ b/httpapi/httpapi.go @@ -29,9 +29,14 @@ func CombinedMiddleware(api openapi.OpenAPI, apiV2 openapi.OpenAPI) gin.HandlerF return } } + // 检查路径和处理对应的请求 if c.Request.URL.Path == "/send_group_msg" { - handleSendGroupMessage(c, api, apiV2) + if config.GetStringOb11() { + handleSendGroupMessageSP(c, api, apiV2) + } else { + handleSendGroupMessage(c, api, apiV2) + } return } if c.Request.URL.Path == "/send_group_msg_raw" { @@ -43,7 +48,11 @@ func CombinedMiddleware(api openapi.OpenAPI, apiV2 openapi.OpenAPI) gin.HandlerF return } if c.Request.URL.Path == "/send_private_msg_sse" { - handleSendPrivateMessageSSE(c, api, apiV2) + if config.GetStringOb11() { + handleSendPrivateMessageSSESP(c, api, apiV2) + } else { + handleSendPrivateMessageSSE(c, api, apiV2) + } return } if c.Request.URL.Path == "/send_guild_channel_msg" { @@ -131,6 +140,57 @@ func handleSendGroupMessage(c *gin.Context, api openapi.OpenAPI, apiV2 openapi.O c.String(http.StatusOK, retmsg) } +// handleSendGroupMessage 处理发送群聊消息的请求 +func handleSendGroupMessageSP(c *gin.Context, api openapi.OpenAPI, apiV2 openapi.OpenAPI) { + var retmsg string + var req struct { + GroupID string `json:"group_id" form:"group_id"` + UserID *string `json:"user_id,omitempty" form:"user_id"` + Message string `json:"message" form:"message"` + AutoEscape bool `json:"auto_escape" form:"auto_escape"` + } + + // 根据请求方法解析参数 + if c.Request.Method == http.MethodGet { + // 从URL查询参数解析 + if err := c.ShouldBindQuery(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + } else { + // 从JSON或表单数据解析 + if err := c.ShouldBind(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + } + + // 使用解析后的参数处理请求 + client := &HttpAPIClient{} + // 创建 ActionMessage 实例 + message := callapi.ActionMessage{ + Action: "send_group_msg", + Params: callapi.ParamsContent{ + GroupID: req.GroupID, // 注意这里需要转换类型,因为 GroupID 是 int64 + Message: req.Message, + }, + } + // 如果 UserID 存在,则加入到参数中 + if req.UserID != nil { + message.Params.UserID = *req.UserID + } + // 调用处理函数 + retmsg, err := handlers.HandleSendGroupMsg(client, api, apiV2, message) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + // 返回处理结果 + c.Header("Content-Type", "application/json") + c.String(http.StatusOK, retmsg) +} + // handleSendGroupMessageRaw 处理发送群聊消息的请求 func handleSendGroupMessageRaw(c *gin.Context, api openapi.OpenAPI, apiV2 openapi.OpenAPI) { var retmsg string @@ -234,6 +294,84 @@ func handleSendPrivateMessage(c *gin.Context, api openapi.OpenAPI, apiV2 openapi c.String(http.StatusOK, retmsg) } +// handleSendPrivateMessageSSE 处理发送私聊SSE消息的请求 +func handleSendPrivateMessageSSESP(c *gin.Context, api openapi.OpenAPI, apiV2 openapi.OpenAPI) { + // 根据请求方法解析参数 + if c.Request.Method == http.MethodGet { + var req struct { + GroupID string `json:"group_id" form:"group_id"` + UserID string `json:"user_id" form:"user_id"` + Message string `json:"message" form:"message"` + AutoEscape bool `json:"auto_escape" form:"auto_escape"` + } + // 从URL查询参数解析 + if err := c.ShouldBindQuery(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + var InterfaceBody structs.InterfaceBody + if err := json.Unmarshal([]byte(req.Message), &InterfaceBody); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid message format"}) + return + } + + client := &HttpAPIClient{} + // 创建 ActionMessage 实例 + message := callapi.ActionMessage{ + Action: "send_private_msg_sse", + Params: callapi.ParamsContent{ + GroupID: req.GroupID, // 注意这里需要转换类型,因为 GroupID 是 int64 + UserID: req.UserID, + Message: InterfaceBody, + }, + } + // 调用处理函数 + retmsg, err := handlers.HandleSendPrivateMsgSSE(client, api, apiV2, message) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + // 返回处理结果 + c.Header("Content-Type", "application/json") + c.String(http.StatusOK, retmsg) + } else { + var req struct { + GroupID string `json:"group_id" form:"group_id"` + UserID string `json:"user_id" form:"user_id"` + Message interface{} `json:"message" form:"message"` + AutoEscape bool `json:"auto_escape" form:"auto_escape"` + } + // 从JSON或表单数据解析 + if err := c.ShouldBind(&req); err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + client := &HttpAPIClient{} + // 创建 ActionMessage 实例 + message := callapi.ActionMessage{ + Action: "send_private_msg_sse", + Params: callapi.ParamsContent{ + GroupID: req.GroupID, // 注意这里需要转换类型,因为 GroupID 是 int64 + UserID: req.UserID, + Message: req.Message, + }, + } + // 调用处理函数 + retmsg, err := handlers.HandleSendPrivateMsgSSE(client, api, apiV2, message) + if err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) + return + } + + // 返回处理结果 + c.Header("Content-Type", "application/json") + c.String(http.StatusOK, retmsg) + } + +} + // handleSendPrivateMessageSSE 处理发送私聊SSE消息的请求 func handleSendPrivateMessageSSE(c *gin.Context, api openapi.OpenAPI, apiV2 openapi.OpenAPI) { // 根据请求方法解析参数