diff --git a/.gitignore b/.gitignore index 48b8bf9..31e3ac6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ vendor/ +.idea diff --git a/coolq/api.go b/coolq/api.go index f506725..7e212ae 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -622,3 +622,101 @@ func convertGroupMemberInfo(groupId int64, m *client.GroupMemberInfo) MSG { "card_changeable": false, } } + +func (bot *CQBot) Send_private_msg_xml(userId int64, i interface{}, ResId int64) MSG { + var str string + if m, ok := i.(gjson.Result); ok { + if m.Type == gjson.JSON { + elem := bot.ConvertObjectMessage(m, true) + mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem}) + if mid == -1 { + return Failed(100) + } + return OK(MSG{"message_id": mid}) + } + str = func() string { + if m.Str != "" { + return m.Str + } + return m.Raw + }() + } else if s, ok := i.(string); ok { + str = s + } + if str == "" { + return Failed(100) + } + var elem []message.IMessageElement + println(str) + elem = append(elem, NewXmlMsg(str,ResId)) + + mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem}) + if mid == -1 { + return Failed(100) + } + return OK(MSG{"message_id": mid}) +} + +func NewXmlMsg(template string,ResId int64) *message.ServiceElement{ + var serviceid string + if ResId == 0{ + serviceid ="2" //默认值2 + }else{ + serviceid = strconv.FormatInt(ResId,10) + } + //println(serviceid) + return &message.ServiceElement{ + Id: int32(ResId), + Content: template, + ResId: serviceid, + SubType: "xml", + } +} + +func (bot *CQBot) CQSendGroupMessage_XML(groupId int64, i interface{}, ResId int64) MSG { + var str string + fixAt := func(elem []message.IMessageElement) { + for _, e := range elem { + if at, ok := e.(*message.AtElement); ok && at.Target != 0 { + at.Display = "@" + func() string { + mem := bot.Client.FindGroup(groupId).FindMember(at.Target) + if mem != nil { + return mem.DisplayName() + } + return strconv.FormatInt(at.Target, 10) + }() + } + } + } + if m, ok := i.(gjson.Result); ok { + if m.Type == gjson.JSON { + elem := bot.ConvertObjectMessage(m, true) + fixAt(elem) + mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem}) + if mid == -1 { + return Failed(100) + } + return OK(MSG{"message_id": mid}) + } + str = func() string { + if m.Str != "" { + return m.Str + } + return m.Raw + }() + } else if s, ok := i.(string); ok { + str = s + } + if str == "" { + return Failed(100) + } + var elem []message.IMessageElement + println(str) + elem = append(elem, NewXmlMsg(str,ResId)) + //fixAt(elem) + mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem}) + if mid == -1 { + return Failed(100) + } + return OK(MSG{"message_id": mid}) +} diff --git a/docs/cqhttp.md b/docs/cqhttp.md index 8cddcc7..5737ac4 100644 --- a/docs/cqhttp.md +++ b/docs/cqhttp.md @@ -258,3 +258,59 @@ Type: `node` | `user_id` | int64 | | 好友id | | `message_id` | int64 | | 被撤回的消息id | + +## 自定义xml结构化消息发送,请自行承担风险 +### 发送好友消息 + +终结点: `/send_private_msg_xml` 或者 `send_private_msg_xml_async` + +**参数** + +| 字段 | 类型 | 说明 | +| -------- | ------ | ---- | +| user_id | int64 | qq号 | +| message | string | xml数据 | +| serviceid | int | 服务Id,一般xml中也有,不填默认为2 | + +### 发送群消息 + +终结点: `/send_group_msg_xml` 或者 `send_group_msg_xml_async` + +**参数** + +| 字段 | 类型 | 说明 | +| -------- | ------ | ---- | +| group_id | int64 | 群号 | +| message | string | xml数据 | +| serviceid | int | 服务Id,一般xml中也有,不填默认为2 | + + +###一些xml样例 +#### qq音乐 +```xml + +``` +#### 网易音乐 +```xml + +``` + +#### 卡片消息1 +```xml + + +生死8秒!女司机高速急刹,他一个操作救下一车性命 + + +``` + +#### 卡片消息2 +```xml + + + +test title + + + +``` \ No newline at end of file diff --git a/server/http.go b/server/http.go index fcf2935..ee13557 100644 --- a/server/http.go +++ b/server/http.go @@ -155,6 +155,10 @@ func (s *httpServer) Run(addr, authToken string, bot *coolq.CQBot) { s.engine.Any("/get_version_info_async", s.GetVersionInfo) s.engine.Any("/.handle_quick_operation", s.HandleQuickOperation) + s.engine.Any("/send_private_msg_xml", s.Send_private_msg_xml) + s.engine.Any("/send_private_msg_xml_async", s.Send_private_msg_xml) + s.engine.Any("/send_group_msg_xml", s.SendGroupMessage_xml) + s.engine.Any("/send_group_msg_xml_async", s.SendGroupMessage_xml) go func() { log.Infof("CQ HTTP 服务器已启动: %v", addr) @@ -400,6 +404,7 @@ func getParamOrDefault(c *gin.Context, k, def string) string { return def } + func getParam(c *gin.Context, k string) string { p, _ := getParamWithType(c, k) return p @@ -439,3 +444,23 @@ func getParamWithType(c *gin.Context, k string) (string, gjson.Type) { } return "", gjson.Null } +func (s *httpServer) Send_private_msg_xml(c *gin.Context) { + uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64) + msg, t := getParamWithType(c, "message") + resId, _ := strconv.ParseInt(getParam(c, "serviceid"), 10, 64) + if t == gjson.JSON { + c.JSON(200, s.bot.Send_private_msg_xml(uid, gjson.Parse(msg), resId)) + return + } + c.JSON(200, s.bot.Send_private_msg_xml(uid, msg, resId)) +} +func (s *httpServer) SendGroupMessage_xml(c *gin.Context) { + gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64) + msg, t := getParamWithType(c, "message") + resId, _ := strconv.ParseInt(getParam(c, "serviceid"), 10, 64) + if t == gjson.JSON { + c.JSON(200, s.bot.CQSendGroupMessage_XML(gid, gjson.Parse(msg), resId)) + return + } + c.JSON(200, s.bot.CQSendGroupMessage_XML(gid, msg, resId)) +}