1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-06 03:53:50 +08:00

fix http api send raw msg error.

This commit is contained in:
Mrs4s 2020-08-02 15:21:19 +08:00
parent 81d2ad3e79
commit 06fccbd9ef
3 changed files with 57 additions and 38 deletions

View File

@ -96,28 +96,36 @@ 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 // 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, m gjson.Result) MSG { func (bot *CQBot) CQSendGroupMessage(groupId int64, i interface{}) MSG {
if m.Type == gjson.String { var str string
str := m.Str if m, ok := i.(gjson.Result); ok {
if str == "" { if m.Type == gjson.JSON {
return Failed(100) elem := bot.ConvertObjectMessage(m, true)
mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem})
if mid == -1 {
return Failed(100)
}
return OK(MSG{"message_id": mid})
} }
elem := bot.ConvertStringMessage(str, true) str = func() string {
mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem}) if m.Str != "" {
if mid == -1 { return m.Str
return Failed(100) }
} return m.Raw
return OK(MSG{"message_id": mid}) }()
} }
if m.Type == gjson.JSON { if s, ok := i.(string); ok {
elem := bot.ConvertObjectMessage(m, true) str = s
mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem})
if mid == -1 {
return Failed(100)
}
return OK(MSG{"message_id": mid})
} }
return Failed(100) 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})
} }
func (bot *CQBot) CQSendGroupForwardMessage(groupId int64, m gjson.Result) MSG { func (bot *CQBot) CQSendGroupForwardMessage(groupId int64, m gjson.Result) MSG {
@ -191,25 +199,36 @@ func (bot *CQBot) CQSendGroupForwardMessage(groupId int64, m gjson.Result) MSG {
} }
// 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 // 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, m gjson.Result) MSG { func (bot *CQBot) CQSendPrivateMessage(userId int64, i interface{}) MSG {
if m.Type == gjson.String { var str string
str := m.Str if m, ok := i.(gjson.Result); ok {
elem := bot.ConvertStringMessage(str, false) if m.Type == gjson.JSON {
mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem}) elem := bot.ConvertObjectMessage(m, true)
if mid == -1 { mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem})
return Failed(100) if mid == -1 {
return Failed(100)
}
return OK(MSG{"message_id": mid})
} }
return OK(MSG{"message_id": mid}) str = func() string {
if m.Str != "" {
return m.Str
}
return m.Raw
}()
} }
if m.Type == gjson.JSON { if s, ok := i.(string); ok {
elem := bot.ConvertObjectMessage(m, true) str = s
mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem})
if mid == -1 {
return Failed(100)
}
return OK(MSG{"message_id": mid})
} }
return Failed(100) if str == "" {
return Failed(100)
}
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})
} }
// 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 // 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

View File

@ -93,7 +93,7 @@ func (bot *CQBot) SendGroupMessage(groupId int64, m *message.SendingMessage) int
if i, ok := elem.(*message.ImageElement); ok { if i, ok := elem.(*message.ImageElement); ok {
gm, err := bot.Client.UploadGroupImage(groupId, i.Data) gm, err := bot.Client.UploadGroupImage(groupId, i.Data)
if err != nil { if err != nil {
log.Warnf("警告: 群 %v 消息图片上传失败.", groupId) log.Warnf("警告: 群 %v 消息图片上传失败: %v", groupId, err)
continue continue
} }
newElem = append(newElem, gm) newElem = append(newElem, gm)

View File

@ -239,7 +239,7 @@ func (s *httpServer) SendPrivateMessage(c *gin.Context) {
c.JSON(200, s.bot.CQSendPrivateMessage(uid, gjson.Parse(msg))) c.JSON(200, s.bot.CQSendPrivateMessage(uid, gjson.Parse(msg)))
return return
} }
c.JSON(200, s.bot.CQSendPrivateMessage(uid, gjson.Result{Type: gjson.String, Str: msg})) c.JSON(200, s.bot.CQSendPrivateMessage(uid, msg))
} }
func (s *httpServer) SendGroupMessage(c *gin.Context) { func (s *httpServer) SendGroupMessage(c *gin.Context) {
@ -249,7 +249,7 @@ func (s *httpServer) SendGroupMessage(c *gin.Context) {
c.JSON(200, s.bot.CQSendGroupMessage(gid, gjson.Parse(msg))) c.JSON(200, s.bot.CQSendGroupMessage(gid, gjson.Parse(msg)))
return return
} }
c.JSON(200, s.bot.CQSendGroupMessage(gid, gjson.Result{Type: gjson.String, Str: msg})) c.JSON(200, s.bot.CQSendGroupMessage(gid, msg))
} }
func (s *httpServer) SendGroupForwardMessage(c *gin.Context) { func (s *httpServer) SendGroupForwardMessage(c *gin.Context) {