mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-05-04 19:17:37 +08:00
Add support for post_message_format: array
This commit is contained in:
parent
d5a8f3ead2
commit
c27ebadbc4
@ -446,7 +446,7 @@ func (bot *CQBot) CQGetForwardMessage(resId string) MSG {
|
||||
"nickname": n.SenderName,
|
||||
},
|
||||
"time": n.Time,
|
||||
"content": ToStringMessage(n.Message, 0, false),
|
||||
"content": ToFormattedMessage(n.Message, 0, false),
|
||||
})
|
||||
}
|
||||
return OK(MSG{
|
||||
|
@ -154,7 +154,7 @@ func (bot *CQBot) InsertGroupMessage(m *message.GroupMessage) int32 {
|
||||
"group-name": m.GroupName,
|
||||
"sender": m.Sender,
|
||||
"time": m.Time,
|
||||
"message": ToStringMessage(m.Elements, m.GroupCode, true),
|
||||
"message": ToFormattedMessage(m.Elements, m.GroupCode, true),
|
||||
}
|
||||
id := ToGlobalId(m.GroupCode, m.Id)
|
||||
if bot.db != nil {
|
||||
|
@ -23,6 +23,76 @@ var matchReg = regexp.MustCompile(`\[CQ:\w+?.*?]`)
|
||||
var typeReg = regexp.MustCompile(`\[CQ:(\w+)`)
|
||||
var paramReg = regexp.MustCompile(`,([\w\-.]+?)=([^,\]]+)`)
|
||||
|
||||
func ToArrayMessage(e []message.IMessageElement, code int64, raw ...bool) (r []MSG) {
|
||||
ur := false
|
||||
if len(raw) != 0 {
|
||||
ur = raw[0]
|
||||
}
|
||||
for _, elem := range e {
|
||||
m := MSG{}
|
||||
switch o := elem.(type) {
|
||||
case *message.TextElement:
|
||||
m = MSG{
|
||||
"type": "text",
|
||||
"data": map[string]string{"text": CQCodeEscapeText(o.Content)},
|
||||
}
|
||||
case *message.AtElement:
|
||||
if o.Target == 0 {
|
||||
m = MSG{
|
||||
"type": "at",
|
||||
"data": map[string]string{"qq": "all"},
|
||||
}
|
||||
} else {
|
||||
m = MSG{
|
||||
"type": "at",
|
||||
"data": map[string]string{"qq": fmt.Sprint(o.Target)},
|
||||
}
|
||||
}
|
||||
case *message.ReplyElement:
|
||||
m = MSG{
|
||||
"type": "reply",
|
||||
"data": map[string]string{"id": fmt.Sprint(ToGlobalId(code, o.ReplySeq))},
|
||||
}
|
||||
case *message.ForwardElement:
|
||||
m = MSG{
|
||||
"type": "forward",
|
||||
"data": map[string]string{"id": o.ResId},
|
||||
}
|
||||
case *message.FaceElement:
|
||||
m = MSG{
|
||||
"type": "face",
|
||||
"data": map[string]string{"id": fmt.Sprint(o.Index)},
|
||||
}
|
||||
case *message.VoiceElement:
|
||||
if ur {
|
||||
m = MSG{
|
||||
"type": "record",
|
||||
"data": map[string]string{"file": o.Name},
|
||||
}
|
||||
} else {
|
||||
m = MSG{
|
||||
"type": "record",
|
||||
"data": map[string]string{"file": o.Name, "url": o.Url},
|
||||
}
|
||||
}
|
||||
case *message.ImageElement:
|
||||
if ur {
|
||||
m = MSG{
|
||||
"type": "image",
|
||||
"data": map[string]string{"file": o.Filename},
|
||||
}
|
||||
} else {
|
||||
m = MSG{
|
||||
"type": "image",
|
||||
"data": map[string]string{"file": o.Filename, "url": o.Url},
|
||||
}
|
||||
}
|
||||
}
|
||||
r = append(r, m)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func ToStringMessage(e []message.IMessageElement, code int64, raw ...bool) (r string) {
|
||||
ur := false
|
||||
if len(raw) != 0 {
|
||||
|
@ -14,6 +14,21 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var format = "string"
|
||||
|
||||
func SetMessageFormat(f string) {
|
||||
format = f
|
||||
}
|
||||
|
||||
func ToFormattedMessage(e []message.IMessageElement, code int64, raw ...bool) (r interface{}) {
|
||||
if format == "string" {
|
||||
r = ToStringMessage(e, code, raw...)
|
||||
} else if format == "array" {
|
||||
r = ToArrayMessage(e, code, raw...)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (bot *CQBot) privateMessageEvent(c *client.QQClient, m *message.PrivateMessage) {
|
||||
checkMedia(m.Elements)
|
||||
cqm := ToStringMessage(m.Elements, 0, true)
|
||||
@ -24,7 +39,7 @@ func (bot *CQBot) privateMessageEvent(c *client.QQClient, m *message.PrivateMess
|
||||
"sub_type": "friend",
|
||||
"message_id": ToGlobalId(m.Sender.Uin, m.Id),
|
||||
"user_id": m.Sender.Uin,
|
||||
"message": ToStringMessage(m.Elements, 0, false),
|
||||
"message": ToFormattedMessage(m.Elements, 0, false),
|
||||
"raw_message": cqm,
|
||||
"font": 0,
|
||||
"self_id": c.Uin,
|
||||
@ -72,7 +87,7 @@ func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage)
|
||||
"anonymous": nil,
|
||||
"font": 0,
|
||||
"group_id": m.GroupCode,
|
||||
"message": ToStringMessage(m.Elements, m.GroupCode, false),
|
||||
"message": ToFormattedMessage(m.Elements, m.GroupCode, false),
|
||||
"message_id": id,
|
||||
"message_type": "group",
|
||||
"post_type": "message",
|
||||
@ -128,7 +143,7 @@ func (bot *CQBot) tempMessageEvent(c *client.QQClient, m *message.TempMessage) {
|
||||
"sub_type": "group",
|
||||
"message_id": m.Id,
|
||||
"user_id": m.Sender.Uin,
|
||||
"message": ToStringMessage(m.Elements, 0, false),
|
||||
"message": ToFormattedMessage(m.Elements, 0, false),
|
||||
"raw_message": cqm,
|
||||
"font": 0,
|
||||
"self_id": c.Uin,
|
||||
|
@ -28,7 +28,8 @@ go-cqhttp 支持导入CQHTTP的配置文件, 具体步骤为:
|
||||
"enabled": true,
|
||||
"host": "0.0.0.0",
|
||||
"port": 5700,
|
||||
"post_urls": {"url:port": "secret"}
|
||||
"post_urls": {"url:port": "secret"},
|
||||
"post_message_format": "string"
|
||||
},
|
||||
"ws_config": {
|
||||
"enabled": true,
|
||||
|
@ -40,10 +40,11 @@ type CQHttpApiConfig struct {
|
||||
}
|
||||
|
||||
type GoCQHttpConfig struct {
|
||||
Enabled bool `json:"enabled"`
|
||||
Host string `json:"host"`
|
||||
Port uint16 `json:"port"`
|
||||
PostUrls map[string]string `json:"post_urls"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Host string `json:"host"`
|
||||
Port uint16 `json:"port"`
|
||||
PostUrls map[string]string `json:"post_urls"`
|
||||
PostMessageFormat string `json:"post_message_format"`
|
||||
}
|
||||
|
||||
type GoCQWebsocketConfig struct {
|
||||
@ -66,10 +67,11 @@ func DefaultConfig() *JsonConfig {
|
||||
ReLogin: true,
|
||||
ReLoginDelay: 3,
|
||||
HttpConfig: &GoCQHttpConfig{
|
||||
Enabled: true,
|
||||
Host: "0.0.0.0",
|
||||
Port: 5700,
|
||||
PostUrls: map[string]string{},
|
||||
Enabled: true,
|
||||
Host: "0.0.0.0",
|
||||
Port: 5700,
|
||||
PostUrls: map[string]string{},
|
||||
PostMessageFormat: "string",
|
||||
},
|
||||
WSConfig: &GoCQWebsocketConfig{
|
||||
Enabled: true,
|
||||
|
15
main.go
15
main.go
@ -91,10 +91,11 @@ func main() {
|
||||
Uin: uin,
|
||||
Password: pwd,
|
||||
HttpConfig: &global.GoCQHttpConfig{
|
||||
Enabled: true,
|
||||
Host: "0.0.0.0",
|
||||
Port: 5700,
|
||||
PostUrls: map[string]string{},
|
||||
Enabled: true,
|
||||
Host: "0.0.0.0",
|
||||
Port: 5700,
|
||||
PostUrls: map[string]string{},
|
||||
PostMessageFormat: "string",
|
||||
},
|
||||
WSConfig: &global.GoCQWebsocketConfig{
|
||||
Enabled: true,
|
||||
@ -193,6 +194,12 @@ func main() {
|
||||
b := coolq.NewQQBot(cli, conf)
|
||||
if conf.HttpConfig != nil && conf.HttpConfig.Enabled {
|
||||
server.HttpServer.Run(fmt.Sprintf("%s:%d", conf.HttpConfig.Host, conf.HttpConfig.Port), conf.AccessToken, b)
|
||||
if conf.HttpConfig.PostMessageFormat != "string" && conf.HttpConfig.PostMessageFormat != "array" {
|
||||
log.Errorf("http_config.post_message_format 配置错误")
|
||||
return
|
||||
} else {
|
||||
coolq.SetMessageFormat(conf.HttpConfig.PostMessageFormat)
|
||||
}
|
||||
for k, v := range conf.HttpConfig.PostUrls {
|
||||
server.NewHttpClient().Run(k, v, b)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user