Skip to content

Commit

Permalink
fix http api send raw msg error.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrs4s committed Aug 2, 2020
1 parent 81d2ad3 commit 06fccbd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 38 deletions.
89 changes: 54 additions & 35 deletions coolq/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
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)
func (bot *CQBot) CQSendGroupMessage(groupId int64, i interface{}) MSG {
var str string
if m, ok := i.(gjson.Result); ok {
if m.Type == gjson.JSON {
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})
}
return OK(MSG{"message_id": mid})
str = func() string {
if m.Str != "" {
return m.Str
}
return m.Raw
}()
}
if m.Type == gjson.JSON {
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})
if s, ok := i.(string); ok {
str = s
}
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 {
Expand Down Expand Up @@ -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
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)
func (bot *CQBot) CQSendPrivateMessage(userId int64, i interface{}) 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})
}
return OK(MSG{"message_id": mid})
str = func() string {
if m.Str != "" {
return m.Str
}
return m.Raw
}()
}
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})
if s, ok := i.(string); ok {
str = s
}
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
Expand Down
2 changes: 1 addition & 1 deletion coolq/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (bot *CQBot) SendGroupMessage(groupId int64, m *message.SendingMessage) int
if i, ok := elem.(*message.ImageElement); ok {
gm, err := bot.Client.UploadGroupImage(groupId, i.Data)
if err != nil {
log.Warnf("警告: 群 %v 消息图片上传失败.", groupId)
log.Warnf("警告: 群 %v 消息图片上传失败: %v", groupId, err)
continue
}
newElem = append(newElem, gm)
Expand Down
4 changes: 2 additions & 2 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func (s *httpServer) SendPrivateMessage(c *gin.Context) {
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}))
c.JSON(200, s.bot.CQSendPrivateMessage(uid, msg))
}

func (s *httpServer) SendGroupMessage(c *gin.Context) {
Expand All @@ -249,7 +249,7 @@ func (s *httpServer) SendGroupMessage(c *gin.Context) {
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}))
c.JSON(200, s.bot.CQSendGroupMessage(gid, msg))
}

func (s *httpServer) SendGroupForwardMessage(c *gin.Context) {
Expand Down

0 comments on commit 06fccbd

Please sign in to comment.