mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-05 03:23:49 +08:00
support "auto_escape" in api and quick operation
This commit is contained in:
parent
00d80d5dfc
commit
4820eb2fec
30
coolq/api.go
30
coolq/api.go
@ -96,7 +96,7 @@ 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, i interface{}) MSG {
|
||||
func (bot *CQBot) CQSendGroupMessage(groupId int64, i interface{}, autoEscape bool) MSG {
|
||||
var str string
|
||||
if m, ok := i.(gjson.Result); ok {
|
||||
if m.Type == gjson.JSON {
|
||||
@ -113,14 +113,18 @@ func (bot *CQBot) CQSendGroupMessage(groupId int64, i interface{}) MSG {
|
||||
}
|
||||
return m.Raw
|
||||
}()
|
||||
}
|
||||
if s, ok := i.(string); ok {
|
||||
} else if s, ok := i.(string); ok {
|
||||
str = s
|
||||
}
|
||||
if str == "" {
|
||||
return Failed(100)
|
||||
}
|
||||
elem := bot.ConvertStringMessage(str, true)
|
||||
var elem []message.IMessageElement
|
||||
if autoEscape {
|
||||
elem = append(elem, message.NewText(str))
|
||||
} else {
|
||||
elem = bot.ConvertStringMessage(str, true)
|
||||
}
|
||||
// fix at display
|
||||
for _, e := range elem {
|
||||
if at, ok := e.(*message.AtElement); ok && at.Target != 0 {
|
||||
@ -211,7 +215,7 @@ 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, i interface{}) MSG {
|
||||
func (bot *CQBot) CQSendPrivateMessage(userId int64, i interface{}, autoEscape bool) MSG {
|
||||
var str string
|
||||
if m, ok := i.(gjson.Result); ok {
|
||||
if m.Type == gjson.JSON {
|
||||
@ -228,14 +232,18 @@ func (bot *CQBot) CQSendPrivateMessage(userId int64, i interface{}) MSG {
|
||||
}
|
||||
return m.Raw
|
||||
}()
|
||||
}
|
||||
if s, ok := i.(string); ok {
|
||||
} else if s, ok := i.(string); ok {
|
||||
str = s
|
||||
}
|
||||
if str == "" {
|
||||
return Failed(100)
|
||||
}
|
||||
elem := bot.ConvertStringMessage(str, false)
|
||||
var elem []message.IMessageElement
|
||||
if autoEscape {
|
||||
elem = append(elem, message.NewText(str))
|
||||
} else {
|
||||
elem = bot.ConvertStringMessage(str, false)
|
||||
}
|
||||
mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem})
|
||||
if mid == -1 {
|
||||
return Failed(100)
|
||||
@ -374,6 +382,7 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG {
|
||||
msgType := context.Get("message_type").Str
|
||||
reply := operation.Get("reply")
|
||||
if reply.Exists() {
|
||||
autoEscape := global.EnsureBool(operation.Get("auto_escape"), false)
|
||||
/*
|
||||
at := true
|
||||
if operation.Get("at_sender").Exists() {
|
||||
@ -382,10 +391,11 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG {
|
||||
*/
|
||||
// TODO: 处理at字段
|
||||
if msgType == "group" {
|
||||
bot.CQSendGroupMessage(context.Get("group_id").Int(), reply)
|
||||
bot.CQSendGroupMessage(context.Get("group_id").Int(), reply, autoEscape)
|
||||
}
|
||||
if msgType == "private" {
|
||||
bot.CQSendPrivateMessage(context.Get("user_id").Int(), reply)
|
||||
// TODO: 处理auto_escape
|
||||
bot.CQSendPrivateMessage(context.Get("user_id").Int(), reply, autoEscape)
|
||||
}
|
||||
}
|
||||
if msgType == "group" {
|
||||
|
50
global/param.go
Normal file
50
global/param.go
Normal file
@ -0,0 +1,50 @@
|
||||
package global
|
||||
|
||||
import (
|
||||
"github.com/tidwall/gjson"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var trueSet = map[string]struct{}{
|
||||
"true": struct{}{},
|
||||
"yes": struct{}{},
|
||||
"1": struct{}{},
|
||||
}
|
||||
|
||||
var falseSet = map[string]struct{}{
|
||||
"false": struct{}{},
|
||||
"no": struct{}{},
|
||||
"0": struct{}{},
|
||||
}
|
||||
|
||||
func EnsureBool(p interface{}, defaultVal bool) bool {
|
||||
var str string
|
||||
if b, ok := p.(bool); ok {
|
||||
return b
|
||||
}
|
||||
if j, ok := p.(gjson.Result); ok {
|
||||
if !j.Exists() {
|
||||
return defaultVal
|
||||
}
|
||||
if j.Type == gjson.True {
|
||||
return true
|
||||
}
|
||||
if j.Type == gjson.False {
|
||||
return false
|
||||
}
|
||||
if j.Type != gjson.String {
|
||||
return defaultVal
|
||||
}
|
||||
str = j.Str
|
||||
} else if s, ok := p.(string); ok {
|
||||
str = s
|
||||
}
|
||||
str = strings.ToLower(str)
|
||||
if _, ok := trueSet[str]; ok {
|
||||
return true
|
||||
}
|
||||
if _, ok := falseSet[str]; ok {
|
||||
return false
|
||||
}
|
||||
return defaultVal
|
||||
}
|
@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/Mrs4s/go-cqhttp/coolq"
|
||||
"github.com/Mrs4s/go-cqhttp/global"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/guonaihong/gout"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@ -249,21 +250,23 @@ func (s *httpServer) SendMessage(c *gin.Context) {
|
||||
func (s *httpServer) SendPrivateMessage(c *gin.Context) {
|
||||
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||
msg, t := getParamWithType(c, "message")
|
||||
autoEscape := global.EnsureBool(getParam(c, "auto_escape"), false)
|
||||
if t == gjson.JSON {
|
||||
c.JSON(200, s.bot.CQSendPrivateMessage(uid, gjson.Parse(msg)))
|
||||
c.JSON(200, s.bot.CQSendPrivateMessage(uid, gjson.Parse(msg), autoEscape))
|
||||
return
|
||||
}
|
||||
c.JSON(200, s.bot.CQSendPrivateMessage(uid, msg))
|
||||
c.JSON(200, s.bot.CQSendPrivateMessage(uid, msg, autoEscape))
|
||||
}
|
||||
|
||||
func (s *httpServer) SendGroupMessage(c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
msg, t := getParamWithType(c, "message")
|
||||
autoEscape := global.EnsureBool(getParam(c, "auto_escape"), false)
|
||||
if t == gjson.JSON {
|
||||
c.JSON(200, s.bot.CQSendGroupMessage(gid, gjson.Parse(msg)))
|
||||
c.JSON(200, s.bot.CQSendGroupMessage(gid, gjson.Parse(msg), autoEscape))
|
||||
return
|
||||
}
|
||||
c.JSON(200, s.bot.CQSendGroupMessage(gid, msg))
|
||||
c.JSON(200, s.bot.CQSendGroupMessage(gid, msg, autoEscape))
|
||||
}
|
||||
|
||||
func (s *httpServer) SendGroupForwardMessage(c *gin.Context) {
|
||||
|
@ -337,28 +337,29 @@ var wsApi = map[string]func(*coolq.CQBot, gjson.Result) coolq.MSG{
|
||||
)
|
||||
},
|
||||
"send_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
autoEscape := global.EnsureBool(p.Get("auto_escape"), false)
|
||||
if p.Get("message_type").Str == "private" {
|
||||
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message"))
|
||||
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message"), autoEscape)
|
||||
}
|
||||
if p.Get("message_type").Str == "group" {
|
||||
return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message"))
|
||||
return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message"), autoEscape)
|
||||
}
|
||||
if p.Get("group_id").Int() != 0 {
|
||||
return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message"))
|
||||
return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message"), autoEscape)
|
||||
}
|
||||
if p.Get("user_id").Int() != 0 {
|
||||
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message"))
|
||||
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message"), autoEscape)
|
||||
}
|
||||
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"))
|
||||
return bot.CQSendGroupMessage(p.Get("group_id").Int(), p.Get("message"), global.EnsureBool(p.Get("auto_escape"), false))
|
||||
},
|
||||
"send_group_forward_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQSendGroupForwardMessage(p.Get("group_id").Int(), p.Get("messages"))
|
||||
},
|
||||
"send_private_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message"))
|
||||
return bot.CQSendPrivateMessage(p.Get("user_id").Int(), p.Get("message"), global.EnsureBool(p.Get("auto_escape"), false))
|
||||
},
|
||||
"delete_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||
return bot.CQDeleteMessage(int32(p.Get("message_id").Int()))
|
||||
|
Loading…
x
Reference in New Issue
Block a user