diff --git a/coolq/api.go b/coolq/api.go index 1bb3c39..bd9282d 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -1,7 +1,6 @@ package coolq import ( - "fmt" "github.com/Mrs4s/MiraiGo/binary" "github.com/Mrs4s/MiraiGo/client" "github.com/Mrs4s/MiraiGo/message" @@ -96,29 +95,50 @@ func (bot *CQBot) CQGetGroupMemberInfo(groupId, userId int64, noCache bool) MSG } // https://cqhttp.cc/docs/4.15/#/API?id=send_group_msg-%E5%8F%91%E9%80%81%E7%BE%A4%E6%B6%88%E6%81%AF -func (bot *CQBot) CQSendGroupMessage(groupId int64, msg string) MSG { - if msg == "" { - return Failed(100) +func (bot *CQBot) CQSendGroupMessage(groupId int64, m gjson.Result) MSG { + if m.Type == gjson.String { + str := m.Str + if str == "" { + return Failed(100) + } + elem := bot.ConvertStringMessage(str, true) + mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem}) + if mid == -1 { + return Failed(100) + } + return OK(MSG{"message_id": mid}) } - elem := bot.ConvertStringMessage(msg, true) - mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem}) - if mid == -1 { - return Failed(100) + if m.IsArray() { + elem := bot.ConvertArrayMessage(m, true) + mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem}) + if mid == -1 { + return Failed(100) + } + return OK(MSG{"message_id": mid}) } - return OK(MSG{"message_id": mid}) + return Failed(100) } // https://cqhttp.cc/docs/4.15/#/API?id=send_private_msg-%E5%8F%91%E9%80%81%E7%A7%81%E8%81%8A%E6%B6%88%E6%81%AF -func (bot *CQBot) CQSendPrivateMessage(userId int64, msg string) MSG { - if msg == "" { - return Failed(100) +func (bot *CQBot) CQSendPrivateMessage(userId int64, m gjson.Result) MSG { + if m.Type == gjson.String { + str := m.Str + elem := bot.ConvertStringMessage(str, false) + mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem}) + if mid == -1 { + return Failed(100) + } + return OK(MSG{"message_id": mid}) } - elem := bot.ConvertStringMessage(msg, false) - mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem}) - if mid == -1 { - return Failed(100) + if m.IsArray() { + elem := bot.ConvertArrayMessage(m, true) + mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem}) + if mid == -1 { + return Failed(100) + } + return OK(MSG{"message_id": mid}) } - return OK(MSG{"message_id": mid}) + return Failed(100) } // https://cqhttp.cc/docs/4.15/#/API?id=set_group_card-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E5%90%8D%E7%89%87%EF%BC%88%E7%BE%A4%E5%A4%87%E6%B3%A8%EF%BC%89 @@ -241,16 +261,13 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG { switch postType { case "message": msgType := context.Get("message_type").Str - reply := operation.Get("reply").Str - if reply != "" { + reply := operation.Get("reply") + if reply.Exists() { at := true if operation.Get("at_sender").Exists() { at = operation.Get("at_sender").Bool() } if msgType == "group" && at { - if at { - reply = fmt.Sprintf("[CQ:at,qq=%d]%s", context.Get("user_id").Int(), reply) - } bot.CQSendGroupMessage(context.Get("group_id").Int(), reply) } if msgType == "private" { diff --git a/coolq/cqcode.go b/coolq/cqcode.go index 1bc538d..90cd315 100644 --- a/coolq/cqcode.go +++ b/coolq/cqcode.go @@ -8,6 +8,7 @@ import ( "github.com/Mrs4s/MiraiGo/message" "github.com/Mrs4s/go-cqhttp/global" log "github.com/sirupsen/logrus" + "github.com/tidwall/gjson" "io/ioutil" "path" "regexp" @@ -103,6 +104,47 @@ func (bot *CQBot) ConvertStringMessage(m string, group bool) (r []message.IMessa return } +func (bot *CQBot) ConvertArrayMessage(m gjson.Result, group bool) (r []message.IMessageElement) { + for _, e := range m.Array() { + t := e.Get("type").Str + if t == "reply" && group { + if len(r) > 0 { + if _, ok := r[0].(*message.ReplyElement); ok { + log.Warnf("警告: 一条信息只能包含一个 Reply 元素.") + continue + } + } + mid, err := strconv.Atoi(e.Get("data").Get("id").Str) + if err == nil { + org := bot.GetGroupMessage(int32(mid)) + if org != nil { + r = append([]message.IMessageElement{ + &message.ReplyElement{ + ReplySeq: org["message-id"].(int32), + Sender: org["sender"].(message.Sender).Uin, + Time: org["time"].(int32), + Elements: bot.ConvertStringMessage(org["message"].(string), group), + }, + }, r...) + continue + } + } + } + d := make(map[string]string) + e.Get("data").ForEach(func(key, value gjson.Result) bool { + d[key.Str] = value.Str + return true + }) + elem, err := bot.ToElement(t, d, group) + if err != nil { + log.Warnf("转换CQ码到MiraiGo Element时出现错误: %v 将忽略本段CQ码.", err) + continue + } + r = append(r, elem) + } + return +} + func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message.IMessageElement, error) { switch t { case "text": diff --git a/global/config.go b/global/config.go index 8f88313..852d2e5 100644 --- a/global/config.go +++ b/global/config.go @@ -15,6 +15,7 @@ type JsonConfig struct { HttpConfig *GoCQHttpConfig `json:"http_config"` WSConfig *GoCQWebsocketConfig `json:"ws_config"` ReverseServers []*GoCQReverseWebsocketConfig `json:"ws_reverse_servers"` + Proxy string `json:"proxy"` } type CQHttpApiConfig struct { diff --git a/go.mod b/go.mod index 53865fc..68bb56c 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/Mrs4s/go-cqhttp go 1.14 require ( - github.com/Mrs4s/MiraiGo v0.0.0-20200727124316-9d432df098d9 + github.com/Mrs4s/MiraiGo v0.0.0-20200728112613-91aa75e8270d github.com/gin-gonic/gin v1.6.3 github.com/gorilla/websocket v1.4.2 github.com/guonaihong/gout v0.1.1 diff --git a/go.sum b/go.sum index 4f5f9d6..c9c5bcf 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Mrs4s/MiraiGo v0.0.0-20200727124316-9d432df098d9 h1:sgwJQnKKAJF/FCXyGv5HHpE5BcKGnwGyCSmPKTB7/vE= github.com/Mrs4s/MiraiGo v0.0.0-20200727124316-9d432df098d9/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU= +github.com/Mrs4s/MiraiGo v0.0.0-20200728112613-91aa75e8270d h1:Pv8lvODPBb+By7CU1crF0/KrmwIrUb4W/3jTs9WDSN4= +github.com/Mrs4s/MiraiGo v0.0.0-20200728112613-91aa75e8270d/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU= github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0= github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= diff --git a/server/http.go b/server/http.go index fa6f883..68dee66 100644 --- a/server/http.go +++ b/server/http.go @@ -229,13 +229,21 @@ func (s *httpServer) SendMessage(c *gin.Context) { func (s *httpServer) SendPrivateMessage(c *gin.Context) { uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64) msg := getParam(c, "message") - c.JSON(200, s.bot.CQSendPrivateMessage(uid, msg)) + if gjson.Valid(msg) { + c.JSON(200, s.bot.CQSendPrivateMessage(uid, gjson.Parse(msg))) + return + } + c.JSON(200, s.bot.CQSendPrivateMessage(uid, gjson.Result{Type: gjson.String, Str: msg})) } func (s *httpServer) SendGroupMessage(c *gin.Context) { gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64) msg := getParam(c, "message") - c.JSON(200, s.bot.CQSendGroupMessage(gid, msg)) + if gjson.Valid(msg) { + c.JSON(200, s.bot.CQSendGroupMessage(gid, gjson.Parse(msg))) + return + } + c.JSON(200, s.bot.CQSendGroupMessage(gid, gjson.Result{Type: gjson.String, Str: msg})) } func (s *httpServer) GetImage(c *gin.Context) { @@ -361,6 +369,8 @@ func getParam(c *gin.Context, k string) string { res := obj.(gjson.Result).Get(k) if res.Exists() { switch res.Type { + case gjson.JSON: + return res.Raw case gjson.String: return res.Str case gjson.Number: diff --git a/server/websocket.go b/server/websocket.go index f893bca..899723e 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -164,6 +164,7 @@ func (c *websocketClient) listenApi(conn *wsc.Conn, u bool) { } j := gjson.ParseBytes(buf[:l]) t := strings.ReplaceAll(j.Get("action").Str, "_async", "") + //log.Infof("调用API: %v p: %v", t, j.Get("params").Raw) if f, ok := wsApi[t]; ok { ret := f(c.bot, j.Get("params")) if j.Get("echo").Exists() { @@ -320,18 +321,18 @@ var wsApi = map[string]func(*coolq.CQBot, gjson.Result) coolq.MSG{ }, "send_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { if p.Get("group_id").Int() != 0 { - return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message").Str) + return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message")) } if p.Get("user_id").Int() != 0 { - return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message").Str) + return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message")) } return coolq.MSG{} }, "send_group_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { - return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message").Str) + return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message")) }, "send_private_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { - return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message").Str) + return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message")) }, "delete_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG { return bot.CQDeleteMessage(int32(p.Get("message_id").Int()))