From 15085d0a6bc2417ad4470f1d0d364ae840c1ab9c Mon Sep 17 00:00:00 2001 From: scjtqs Date: Mon, 24 Aug 2020 02:04:32 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=BC=80=E6=94=BE=E8=87=AA=E7=94=B1xml?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=96=B9?= =?UTF-8?q?=E4=BE=BF=E5=BC=80=E5=8F=91=E8=80=85=E8=87=AA=E8=A1=8C=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E5=92=8C=E8=B0=83=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + coolq/api.go | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++ docs/cqhttp.md | 56 +++++++++++++++++++++++++++++ server/http.go | 25 +++++++++++++ 4 files changed, 180 insertions(+) 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)) +} From 8874ed039270879a14d39e8f9420c3d57a0f6fbb Mon Sep 17 00:00:00 2001 From: scjtqs Date: Mon, 24 Aug 2020 04:32:02 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20CQ:xml=E7=9A=84?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coolq/api.go | 98 ---------------------------------------------- coolq/cqcode.go | 26 +++++++++++++ docs/cqhttp.md | 101 +++++++++++++++++++++--------------------------- global/net.go | 18 +++++++++ server/http.go | 24 ------------ 5 files changed, 89 insertions(+), 178 deletions(-) diff --git a/coolq/api.go b/coolq/api.go index 7e212ae..f506725 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -622,101 +622,3 @@ 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/coolq/cqcode.go b/coolq/cqcode.go index 5e8f6ae..f7c6294 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -196,6 +196,22 @@ func (bot *CQBot) ConvertStringMessage(m string, group bool) (r []message.IMessa } } } + if(t == "xml"){ + //xml需要实体化,发送的时候不能转回去 + x := make(map[string]string) + //循环遍历Map + for _, p := range ps { + x[p[1]] = p[2] + } + resId := x["resid"] + template := x["data"] + println(template) + i, _ := strconv.ParseInt(resId, 10, 64) + msg :=global.NewXmlMsg(template,i) + r=append(r,msg) + continue + } + elem, err := bot.ToElement(t, d, group) if err != nil { log.Warnf("转换CQ码到MiraiGo Element时出现错误: %v 将原样发送.", err) @@ -236,6 +252,16 @@ func (bot *CQBot) ConvertObjectMessage(m gjson.Result, group bool) (r []message. } } } + if t =="xml"{ + //xml需要实体化,发送的时候不能转回去 + resId := e.Get("data").Get("resid").Str + template := e.Get("data").Get("data").Str + println(template) + i, _ := strconv.ParseInt(resId, 10, 64) + msg :=global.NewXmlMsg(template,i) + r=append(r,msg) + return + } d := make(map[string]string) e.Get("data").ForEach(func(key, value gjson.Result) bool { d[key.Str] = value.Str diff --git a/docs/cqhttp.md b/docs/cqhttp.md index 5737ac4..a0f5c42 100644 --- a/docs/cqhttp.md +++ b/docs/cqhttp.md @@ -119,6 +119,51 @@ Type: `node` ] ```` +### xml支持 + +Type: `xml` + +范围: **发送** + +参数: + +| 参数名 | 类型 | 说明 | +| ------ | ------ | ------------------------------------------------------------ | +| data | string | xml内容,xml中的value部分,记得实体化处理| +| resid | int32 | 可以不填| + +示例: `[CQ:xml,data=xxxx]` + +####一些xml样例 +####ps:重要:xml中的value部分,记得html实体化处理后,再打加入到cq码中 +#### qq音乐 +```xml + +``` +#### 网易音乐 +```xml + +``` + +#### 卡片消息1 +```xml + + +生死8秒!女司机高速急刹,他一个操作救下一车性命 + + +``` + +#### 卡片消息2 +```xml + + + +test title + + + +``` ## API @@ -258,59 +303,3 @@ 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/global/net.go b/global/net.go index 40aeab8..d3cf1a7 100644 --- a/global/net.go +++ b/global/net.go @@ -3,9 +3,11 @@ package global import ( "bytes" "compress/gzip" + "github.com/Mrs4s/MiraiGo/message" "github.com/tidwall/gjson" "io/ioutil" "net/http" + "strconv" "strings" ) @@ -41,3 +43,19 @@ func QQMusicSongInfo(id string) (gjson.Result, error) { } return gjson.ParseBytes(d).Get("songinfo.data"), nil } + +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", + } +} diff --git a/server/http.go b/server/http.go index ee13557..f6a229b 100644 --- a/server/http.go +++ b/server/http.go @@ -155,10 +155,6 @@ 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) @@ -444,23 +440,3 @@ 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)) -} From 03cc0dba9509d3dc044c2ec70c4cdbe4028d83f8 Mon Sep 17 00:00:00 2001 From: scjtqs Date: Mon, 24 Aug 2020 16:01:21 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E7=A7=BB=E5=8A=A8xml=E5=A4=84=E7=90=86?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coolq/cqcode.go | 33 +++++++-------------------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/coolq/cqcode.go b/coolq/cqcode.go index f7c6294..d59b097 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -196,22 +196,6 @@ func (bot *CQBot) ConvertStringMessage(m string, group bool) (r []message.IMessa } } } - if(t == "xml"){ - //xml需要实体化,发送的时候不能转回去 - x := make(map[string]string) - //循环遍历Map - for _, p := range ps { - x[p[1]] = p[2] - } - resId := x["resid"] - template := x["data"] - println(template) - i, _ := strconv.ParseInt(resId, 10, 64) - msg :=global.NewXmlMsg(template,i) - r=append(r,msg) - continue - } - elem, err := bot.ToElement(t, d, group) if err != nil { log.Warnf("转换CQ码到MiraiGo Element时出现错误: %v 将原样发送.", err) @@ -252,16 +236,6 @@ func (bot *CQBot) ConvertObjectMessage(m gjson.Result, group bool) (r []message. } } } - if t =="xml"{ - //xml需要实体化,发送的时候不能转回去 - resId := e.Get("data").Get("resid").Str - template := e.Get("data").Get("data").Str - println(template) - i, _ := strconv.ParseInt(resId, 10, 64) - msg :=global.NewXmlMsg(template,i) - r=append(r,msg) - return - } d := make(map[string]string) e.Get("data").ForEach(func(key, value gjson.Result) bool { d[key.Str] = value.Str @@ -483,6 +457,13 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message. }, nil } return nil, errors.New("unsupported music type: " + d["type"]) + case "xml": + resId := d["resid"] + template := CQCodeEscapeValue(d["data"]) + println(template) + i, _ := strconv.ParseInt(resId, 10, 64) + msg :=global.NewXmlMsg(template,i) + return msg,nil default: return nil, errors.New("unsupported cq code: " + t) } From cb9436601ff40bfb84ac25cf62855beb2e6ecec4 Mon Sep 17 00:00:00 2001 From: scjtqs Date: Mon, 24 Aug 2020 16:16:36 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E7=A7=BB=E5=8A=A8xml=E5=A4=84=E7=90=86?= =?UTF-8?q?=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coolq/cqcode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coolq/cqcode.go b/coolq/cqcode.go index d59b097..d22c56d 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -460,7 +460,7 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message. case "xml": resId := d["resid"] template := CQCodeEscapeValue(d["data"]) - println(template) + //println(template) i, _ := strconv.ParseInt(resId, 10, 64) msg :=global.NewXmlMsg(template,i) return msg,nil