mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-06-30 11:53:25 +00:00
Compare commits
18 Commits
Author | SHA1 | Date | |
---|---|---|---|
e2b6c3d607 | |||
58a3f81142 | |||
64cb539c0d | |||
3bb844d1d4 | |||
0fdb01e82c | |||
7cf199189a | |||
3aea70e92a | |||
d1505ec15d | |||
750a01acdd | |||
a12dcfa2d7 | |||
86422c5a00 | |||
f3b43bc64c | |||
57153d2d4a | |||
b73aeb6666 | |||
a215028ac0 | |||
d076f174bb | |||
5c8fa7f9a0 | |||
9c95a906b6 |
11
README.md
11
README.md
@ -10,15 +10,17 @@
|
|||||||
- [x] HTTP API
|
- [x] HTTP API
|
||||||
- [x] 反向HTTP POST
|
- [x] 反向HTTP POST
|
||||||
- [x] 正向Websocket
|
- [x] 正向Websocket
|
||||||
- [x] 反向Websocket (测试中)
|
- [x] 反向Websocket
|
||||||
|
|
||||||
#### 拓展支持
|
#### 拓展支持
|
||||||
|
> 拓展API可前往 [文档](docs/cqhttp.md) 查看
|
||||||
- [x] HTTP POST多点上报
|
- [x] HTTP POST多点上报
|
||||||
- [x] 反向WS多点连接
|
- [x] 反向WS多点连接
|
||||||
- [x] 修改群名
|
- [x] 修改群名
|
||||||
- [x] 消息撤回事件
|
- [x] 消息撤回事件
|
||||||
- [x] 解析/发送 回复消息
|
- [x] 解析/发送 回复消息
|
||||||
- [x] 解析合并转发
|
- [x] 解析/发送 合并转发
|
||||||
|
- [ ] 使用代理请求网络图片
|
||||||
|
|
||||||
#### 实现
|
#### 实现
|
||||||
<details>
|
<details>
|
||||||
@ -44,6 +46,7 @@
|
|||||||
| /set_group_kick | [群组T人](https://cqhttp.cc/docs/4.15/#/API?id=set_group_kick-群组踢人) |
|
| /set_group_kick | [群组T人](https://cqhttp.cc/docs/4.15/#/API?id=set_group_kick-群组踢人) |
|
||||||
| /set_group_ban | [群组单人禁言](https://cqhttp.cc/docs/4.15/#/API?id=set_group_ban-群组单人禁言) |
|
| /set_group_ban | [群组单人禁言](https://cqhttp.cc/docs/4.15/#/API?id=set_group_ban-群组单人禁言) |
|
||||||
| /set_group_whole_ban | [群组全员禁言](https://cqhttp.cc/docs/4.15/#/API?id=set_group_whole_ban-群组全员禁言) |
|
| /set_group_whole_ban | [群组全员禁言](https://cqhttp.cc/docs/4.15/#/API?id=set_group_whole_ban-群组全员禁言) |
|
||||||
|
| /set_group_leave | [退出群组](https://cqhttp.cc/docs/4.15/#/API?id=set_group_leave-退出群组) |
|
||||||
| /set_group_name | 设置群组名(拓展API) |
|
| /set_group_name | 设置群组名(拓展API) |
|
||||||
| /get_image | 获取图片信息(拓展API) |
|
| /get_image | 获取图片信息(拓展API) |
|
||||||
| /get_group_msg | 获取群组消息(拓展API) |
|
| /get_group_msg | 获取群组消息(拓展API) |
|
||||||
@ -62,11 +65,13 @@
|
|||||||
| ------------------------------------------------------------ |
|
| ------------------------------------------------------------ |
|
||||||
| [私聊信息](https://cqhttp.cc/docs/4.15/#/Post?id=私聊消息) |
|
| [私聊信息](https://cqhttp.cc/docs/4.15/#/Post?id=私聊消息) |
|
||||||
| [群消息](https://cqhttp.cc/docs/4.15/#/Post?id=群消息) |
|
| [群消息](https://cqhttp.cc/docs/4.15/#/Post?id=群消息) |
|
||||||
| [群消息撤回(拓展Event)](docs/cqhttp.md#群消息撤回) |
|
| [群消息撤回(拓展Event)](docs/cqhttp.md#群消息撤回) |
|
||||||
|
| [好友消息撤回(拓展Event)](docs/cqhttp.md#好友消息撤回) |
|
||||||
| [群管理员变动](https://cqhttp.cc/docs/4.15/#/Post?id=群管理员变动) |
|
| [群管理员变动](https://cqhttp.cc/docs/4.15/#/Post?id=群管理员变动) |
|
||||||
| [群成员减少](https://cqhttp.cc/docs/4.15/#/Post?id=群成员减少) |
|
| [群成员减少](https://cqhttp.cc/docs/4.15/#/Post?id=群成员减少) |
|
||||||
| [群成员增加](https://cqhttp.cc/docs/4.15/#/Post?id=群成员增加) |
|
| [群成员增加](https://cqhttp.cc/docs/4.15/#/Post?id=群成员增加) |
|
||||||
| [群禁言](https://cqhttp.cc/docs/4.15/#/Post?id=群禁言) |
|
| [群禁言](https://cqhttp.cc/docs/4.15/#/Post?id=群禁言) |
|
||||||
|
| [群文件上传](https://cqhttp.cc/docs/4.15/#/Post?id=群文件上传)|
|
||||||
| [加好友请求](https://cqhttp.cc/docs/4.15/#/Post?id=加好友请求) |
|
| [加好友请求](https://cqhttp.cc/docs/4.15/#/Post?id=加好友请求) |
|
||||||
| [加群请求/邀请](https://cqhttp.cc/docs/4.15/#/Post?id=加群请求/邀请) |
|
| [加群请求/邀请](https://cqhttp.cc/docs/4.15/#/Post?id=加群请求/邀请) |
|
||||||
|
|
||||||
|
144
coolq/api.go
144
coolq/api.go
@ -1,7 +1,6 @@
|
|||||||
package coolq
|
package coolq
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/Mrs4s/MiraiGo/binary"
|
"github.com/Mrs4s/MiraiGo/binary"
|
||||||
"github.com/Mrs4s/MiraiGo/client"
|
"github.com/Mrs4s/MiraiGo/client"
|
||||||
"github.com/Mrs4s/MiraiGo/message"
|
"github.com/Mrs4s/MiraiGo/message"
|
||||||
@ -13,6 +12,7 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// https://cqhttp.cc/docs/4.15/#/API?id=get_login_info-%E8%8E%B7%E5%8F%96%E7%99%BB%E5%BD%95%E5%8F%B7%E4%BF%A1%E6%81%AF
|
// https://cqhttp.cc/docs/4.15/#/API?id=get_login_info-%E8%8E%B7%E5%8F%96%E7%99%BB%E5%BD%95%E5%8F%B7%E4%BF%A1%E6%81%AF
|
||||||
@ -96,29 +96,120 @@ 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, msg string) MSG {
|
func (bot *CQBot) CQSendGroupMessage(groupId int64, m gjson.Result) MSG {
|
||||||
if 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})
|
||||||
|
}
|
||||||
|
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 Failed(100)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) CQSendGroupForwardMessage(groupId int64, m gjson.Result) MSG {
|
||||||
|
if m.Type != gjson.JSON {
|
||||||
return Failed(100)
|
return Failed(100)
|
||||||
}
|
}
|
||||||
elem := bot.ConvertStringMessage(msg, true)
|
var nodes []*message.ForwardNode
|
||||||
mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem})
|
ts := time.Now().Add(-time.Minute * 5)
|
||||||
if mid == -1 {
|
hasCustom := func() bool {
|
||||||
return Failed(100)
|
for _, item := range m.Array() {
|
||||||
|
if item.Get("data.uin").Exists() {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}()
|
||||||
|
convert := func(e gjson.Result) {
|
||||||
|
if e.Get("type").Str != "node" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ts.Add(time.Second)
|
||||||
|
if e.Get("data.id").Exists() {
|
||||||
|
i, _ := strconv.Atoi(e.Get("data.id").Str)
|
||||||
|
m := bot.GetGroupMessage(int32(i))
|
||||||
|
if m != nil {
|
||||||
|
sender := m["sender"].(message.Sender)
|
||||||
|
nodes = append(nodes, &message.ForwardNode{
|
||||||
|
SenderId: sender.Uin,
|
||||||
|
SenderName: (&sender).DisplayName(),
|
||||||
|
Time: func() int32 {
|
||||||
|
if hasCustom {
|
||||||
|
return int32(ts.Unix())
|
||||||
|
}
|
||||||
|
return m["time"].(int32)
|
||||||
|
}(),
|
||||||
|
Message: bot.ConvertStringMessage(m["message"].(string), true),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Warnf("警告: 引用消息 %v 错误或数据库未开启.", e.Get("data.id").Str)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
uin, _ := strconv.ParseInt(e.Get("data.uin").Str, 10, 64)
|
||||||
|
name := e.Get("data.name").Str
|
||||||
|
content := bot.ConvertObjectMessage(e.Get("data.content"), true)
|
||||||
|
if uin != 0 && name != "" && len(content) > 0 {
|
||||||
|
nodes = append(nodes, &message.ForwardNode{
|
||||||
|
SenderId: uin,
|
||||||
|
SenderName: name,
|
||||||
|
Time: int32(ts.Unix()),
|
||||||
|
Message: content,
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Warnf("警告: 非法 Forward node 将跳过")
|
||||||
}
|
}
|
||||||
return OK(MSG{"message_id": mid})
|
if m.IsArray() {
|
||||||
|
for _, item := range m.Array() {
|
||||||
|
convert(item)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
convert(m)
|
||||||
|
}
|
||||||
|
if len(nodes) > 0 {
|
||||||
|
gm := bot.Client.SendGroupForwardMessage(groupId, &message.ForwardMessage{Nodes: nodes})
|
||||||
|
return OK(MSG{
|
||||||
|
"message_id": ToGlobalId(groupId, gm.Id),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
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
|
// 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 {
|
func (bot *CQBot) CQSendPrivateMessage(userId int64, m gjson.Result) MSG {
|
||||||
if msg == "" {
|
if m.Type == gjson.String {
|
||||||
return Failed(100)
|
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)
|
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})
|
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
|
// 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
|
||||||
@ -182,6 +273,15 @@ func (bot *CQBot) CQSetGroupWholeBan(groupId int64, enable bool) MSG {
|
|||||||
return Failed(100)
|
return Failed(100)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://cqhttp.cc/docs/4.15/#/API?id=set_group_leave-%E9%80%80%E5%87%BA%E7%BE%A4%E7%BB%84
|
||||||
|
func (bot *CQBot) CQSetGroupLeave(groupId int64) MSG {
|
||||||
|
if g := bot.Client.FindGroup(groupId); g != nil {
|
||||||
|
g.Quit()
|
||||||
|
return OK(nil)
|
||||||
|
}
|
||||||
|
return Failed(100)
|
||||||
|
}
|
||||||
|
|
||||||
// https://cqhttp.cc/docs/4.15/#/API?id=set_friend_add_request-%E5%A4%84%E7%90%86%E5%8A%A0%E5%A5%BD%E5%8F%8B%E8%AF%B7%E6%B1%82
|
// https://cqhttp.cc/docs/4.15/#/API?id=set_friend_add_request-%E5%A4%84%E7%90%86%E5%8A%A0%E5%A5%BD%E5%8F%8B%E8%AF%B7%E6%B1%82
|
||||||
func (bot *CQBot) CQProcessFriendRequest(flag string, approve bool) MSG {
|
func (bot *CQBot) CQProcessFriendRequest(flag string, approve bool) MSG {
|
||||||
req, ok := bot.friendReqCache.Load(flag)
|
req, ok := bot.friendReqCache.Load(flag)
|
||||||
@ -241,16 +341,14 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG {
|
|||||||
switch postType {
|
switch postType {
|
||||||
case "message":
|
case "message":
|
||||||
msgType := context.Get("message_type").Str
|
msgType := context.Get("message_type").Str
|
||||||
reply := operation.Get("reply").Str
|
reply := operation.Get("reply")
|
||||||
if reply != "" {
|
if reply.Exists() {
|
||||||
at := true
|
at := true
|
||||||
if operation.Get("at_sender").Exists() {
|
if operation.Get("at_sender").Exists() {
|
||||||
at = operation.Get("at_sender").Bool()
|
at = operation.Get("at_sender").Bool()
|
||||||
}
|
}
|
||||||
|
// TODO: 处理at字段
|
||||||
if msgType == "group" && at {
|
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)
|
bot.CQSendGroupMessage(context.Get("group_id").Int(), reply)
|
||||||
}
|
}
|
||||||
if msgType == "private" {
|
if msgType == "private" {
|
||||||
@ -317,8 +415,8 @@ func (bot *CQBot) CQGetForwardMessage(resId string) MSG {
|
|||||||
checkImage(n.Message)
|
checkImage(n.Message)
|
||||||
r = append(r, MSG{
|
r = append(r, MSG{
|
||||||
"sender": MSG{
|
"sender": MSG{
|
||||||
"user_id": n.SenderId,
|
"user_id": n.SenderId,
|
||||||
"nick": n.SenderName,
|
"nickname": n.SenderName,
|
||||||
},
|
},
|
||||||
"time": n.Time,
|
"time": n.Time,
|
||||||
"content": ToStringMessage(n.Message, 0, false),
|
"content": ToStringMessage(n.Message, 0, false),
|
||||||
|
@ -51,6 +51,7 @@ func NewQQBot(cli *client.QQClient, conf *global.JsonConfig) *CQBot {
|
|||||||
bot.Client.OnTempMessage(bot.tempMessageEvent)
|
bot.Client.OnTempMessage(bot.tempMessageEvent)
|
||||||
bot.Client.OnGroupMuted(bot.groupMutedEvent)
|
bot.Client.OnGroupMuted(bot.groupMutedEvent)
|
||||||
bot.Client.OnGroupMessageRecalled(bot.groupRecallEvent)
|
bot.Client.OnGroupMessageRecalled(bot.groupRecallEvent)
|
||||||
|
bot.Client.OnFriendMessageRecalled(bot.friendRecallEvent)
|
||||||
bot.Client.OnJoinGroup(bot.joinGroupEvent)
|
bot.Client.OnJoinGroup(bot.joinGroupEvent)
|
||||||
bot.Client.OnLeaveGroup(bot.leaveGroupEvent)
|
bot.Client.OnLeaveGroup(bot.leaveGroupEvent)
|
||||||
bot.Client.OnGroupMemberJoined(bot.memberJoinEvent)
|
bot.Client.OnGroupMemberJoined(bot.memberJoinEvent)
|
||||||
@ -75,7 +76,7 @@ func (bot *CQBot) GetGroupMessage(mid int32) MSG {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
buff := new(bytes.Buffer)
|
buff := new(bytes.Buffer)
|
||||||
buff.Write(e.Value)
|
buff.Write(binary.GZipUncompress(e.Value))
|
||||||
return gob.NewDecoder(buff).Decode(&m)
|
return gob.NewDecoder(buff).Decode(&m)
|
||||||
})
|
})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -140,7 +141,7 @@ func (bot *CQBot) InsertGroupMessage(m *message.GroupMessage) int32 {
|
|||||||
if err := gob.NewEncoder(buf).Encode(val); err != nil {
|
if err := gob.NewEncoder(buf).Encode(val); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return tx.Put("group-messages", binary.ToBytes(id), buf.Bytes(), 0)
|
return tx.Put("group-messages", binary.ToBytes(id), binary.GZipCompress(buf.Bytes()), 0)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("记录聊天数据时出现错误: %v", err)
|
log.Warnf("记录聊天数据时出现错误: %v", err)
|
||||||
@ -154,7 +155,9 @@ func ToGlobalId(code int64, msgId int32) int32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bot *CQBot) Release() {
|
func (bot *CQBot) Release() {
|
||||||
_ = bot.db.Close()
|
if bot.db != nil {
|
||||||
|
_ = bot.db.Close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bot *CQBot) dispatchEventMessage(m MSG) {
|
func (bot *CQBot) dispatchEventMessage(m MSG) {
|
||||||
|
@ -8,6 +8,7 @@ import (
|
|||||||
"github.com/Mrs4s/MiraiGo/message"
|
"github.com/Mrs4s/MiraiGo/message"
|
||||||
"github.com/Mrs4s/go-cqhttp/global"
|
"github.com/Mrs4s/go-cqhttp/global"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"path"
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
@ -103,6 +104,58 @@ func (bot *CQBot) ConvertStringMessage(m string, group bool) (r []message.IMessa
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) ConvertObjectMessage(m gjson.Result, group bool) (r []message.IMessageElement) {
|
||||||
|
convertElem := func(e gjson.Result) {
|
||||||
|
t := e.Get("type").Str
|
||||||
|
if t == "reply" && group {
|
||||||
|
if len(r) > 0 {
|
||||||
|
if _, ok := r[0].(*message.ReplyElement); ok {
|
||||||
|
log.Warnf("警告: 一条信息只能包含一个 Reply 元素.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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...)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r = append(r, elem)
|
||||||
|
}
|
||||||
|
if m.Type == gjson.String {
|
||||||
|
return bot.ConvertStringMessage(m.Str, group)
|
||||||
|
}
|
||||||
|
if m.IsArray() {
|
||||||
|
for _, e := range m.Array() {
|
||||||
|
convertElem(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if m.IsObject() {
|
||||||
|
convertElem(m)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message.IMessageElement, error) {
|
func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message.IMessageElement, error) {
|
||||||
switch t {
|
switch t {
|
||||||
case "text":
|
case "text":
|
||||||
@ -160,6 +213,8 @@ func (bot *CQBot) ToElement(t string, d map[string]string, group bool) (message.
|
|||||||
}
|
}
|
||||||
t, _ := strconv.ParseInt(qq, 10, 64)
|
t, _ := strconv.ParseInt(qq, 10, 64)
|
||||||
return message.NewAt(t), nil
|
return message.NewAt(t), nil
|
||||||
|
case "share":
|
||||||
|
return message.NewUrlShare(d["url"], d["title"], d["content"], d["image"]), nil
|
||||||
default:
|
default:
|
||||||
return nil, errors.New("unsupported cq code: " + t)
|
return nil, errors.New("unsupported cq code: " + t)
|
||||||
}
|
}
|
||||||
|
@ -21,7 +21,7 @@ func (bot *CQBot) privateMessageEvent(c *client.QQClient, m *message.PrivateMess
|
|||||||
"post_type": "message",
|
"post_type": "message",
|
||||||
"message_type": "private",
|
"message_type": "private",
|
||||||
"sub_type": "friend",
|
"sub_type": "friend",
|
||||||
"message_id": m.Id,
|
"message_id": ToGlobalId(m.Sender.Uin, m.Id),
|
||||||
"user_id": m.Sender.Uin,
|
"user_id": m.Sender.Uin,
|
||||||
"message": ToStringMessage(m.Elements, 0, false),
|
"message": ToStringMessage(m.Elements, 0, false),
|
||||||
"raw_message": cqm,
|
"raw_message": cqm,
|
||||||
@ -40,6 +40,26 @@ func (bot *CQBot) privateMessageEvent(c *client.QQClient, m *message.PrivateMess
|
|||||||
|
|
||||||
func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage) {
|
func (bot *CQBot) groupMessageEvent(c *client.QQClient, m *message.GroupMessage) {
|
||||||
checkImage(m.Elements)
|
checkImage(m.Elements)
|
||||||
|
for _, elem := range m.Elements {
|
||||||
|
if file, ok := elem.(*message.GroupFileElement); ok {
|
||||||
|
log.Infof("群 %v(%v) 内 %v(%v) 上传了文件: %v", m.GroupName, m.GroupCode, m.Sender.DisplayName(), m.Sender.Uin, file.Name)
|
||||||
|
bot.dispatchEventMessage(MSG{
|
||||||
|
"post_type": "notice",
|
||||||
|
"notice_type": "group_upload",
|
||||||
|
"group_id": m.GroupCode,
|
||||||
|
"user_id": m.Sender.Uin,
|
||||||
|
"file": MSG{
|
||||||
|
"id": file.Path,
|
||||||
|
"name": file.Name,
|
||||||
|
"size": file.Size,
|
||||||
|
"busid": file.Busid,
|
||||||
|
},
|
||||||
|
"self_id": c.Uin,
|
||||||
|
"time": time.Now().Unix(),
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
cqm := ToStringMessage(m.Elements, m.GroupCode, true)
|
cqm := ToStringMessage(m.Elements, m.GroupCode, true)
|
||||||
id := m.Id
|
id := m.Id
|
||||||
if bot.db != nil {
|
if bot.db != nil {
|
||||||
@ -164,6 +184,20 @@ func (bot *CQBot) groupRecallEvent(c *client.QQClient, e *client.GroupMessageRec
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bot *CQBot) friendRecallEvent(c *client.QQClient, e *client.FriendMessageRecalledEvent) {
|
||||||
|
f := c.FindFriend(e.FriendUin)
|
||||||
|
gid := ToGlobalId(e.FriendUin, e.MessageId)
|
||||||
|
log.Infof("好友 %v(%v) 撤回了消息: %v", f.Nickname, f.Uin, gid)
|
||||||
|
bot.dispatchEventMessage(MSG{
|
||||||
|
"post_type": "notice",
|
||||||
|
"notice_type": "friend_recall",
|
||||||
|
"self_id": c.Uin,
|
||||||
|
"user_id": f.Uin,
|
||||||
|
"time": e.Time,
|
||||||
|
"message_id": gid,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (bot *CQBot) joinGroupEvent(c *client.QQClient, group *client.GroupInfo) {
|
func (bot *CQBot) joinGroupEvent(c *client.QQClient, group *client.GroupInfo) {
|
||||||
log.Infof("Bot进入了群 %v.", formatGroupName(group))
|
log.Infof("Bot进入了群 %v.", formatGroupName(group))
|
||||||
bot.dispatchEventMessage(bot.groupIncrease(group.Code, 0, c.Uin))
|
bot.dispatchEventMessage(bot.groupIncrease(group.Code, 0, c.Uin))
|
||||||
|
193
docs/cqhttp.md
193
docs/cqhttp.md
@ -4,14 +4,128 @@
|
|||||||
|
|
||||||
## CQCode
|
## CQCode
|
||||||
|
|
||||||
| Code | 示例 | 说明 |
|
### 回复
|
||||||
| ------- | -------------------- | ------------------------------------------------------------ |
|
|
||||||
| reply | [CQ:reply,id=123456] | 回复ID为 `123456`的信息. 发送时一条 `message` 仅能使用一次 |
|
Type : `reply`
|
||||||
| forward | [CQ:forward,id=abcd] | ID为abcd的转发消息, 暂时仅能接收. 可通过 `/get_forward_msg` API获取具体信息 |
|
|
||||||
|
范围: **发送/接收**
|
||||||
|
|
||||||
|
参数:
|
||||||
|
|
||||||
|
| 参数名 | 类型 | 说明 |
|
||||||
|
| ------ | ---- | ------------------------------------- |
|
||||||
|
| id | int | 回复时所引用的消息id, 必须为本群消息. |
|
||||||
|
|
||||||
|
示例: `[CQ:reply,id=123456]`
|
||||||
|
|
||||||
|
### 合并转发
|
||||||
|
|
||||||
|
Type: `forward`
|
||||||
|
|
||||||
|
范围: **接收**
|
||||||
|
|
||||||
|
参数:
|
||||||
|
|
||||||
|
| 参数名 | 类型 | 说明 |
|
||||||
|
| ------ | ------ | ------------------------------------------------------------ |
|
||||||
|
| id | string | 合并转发ID, 需要通过 `/get_forward_msg` API获取转发的具体内容 |
|
||||||
|
|
||||||
|
示例: `[CQ:forward,id=xxxx]`
|
||||||
|
|
||||||
|
### 合并转发消息节点
|
||||||
|
|
||||||
|
Type: `node`
|
||||||
|
|
||||||
|
范围: **发送**
|
||||||
|
|
||||||
|
参数:
|
||||||
|
|
||||||
|
| 参数名 | 类型 | 说明 | 特殊说明 |
|
||||||
|
| ------- | ------- | -------------- | ------------------------------------------------------------ |
|
||||||
|
| id | int32 | 转发消息id | 直接引用他人的消息合并转发, 实际查看顺序为原消息发送顺序 **与下面的自定义消息二选一** |
|
||||||
|
| name | string | 发送者显示名字 | 用于自定义消息 (自定义消息并合并转发,实际查看顺序为自定义消息段顺序) |
|
||||||
|
| uin | int64 | 发送者QQ号 | 用于自定义消息 |
|
||||||
|
| content | message | 具体消息 | 用于自定义消息 **不支持转发套娃,不支持引用回复** |
|
||||||
|
|
||||||
|
特殊说明: **需要使用单独的API `/send_group_forward_msg` 发送,并且由于消息段较为复杂,仅支持Array形式入参。 如果引用消息和自定义消息同时出现,实际查看顺序将取消息段顺序. 另外按 [CQHTTP](https://cqhttp.cc/docs/4.15/#/Message?id=格式) 文档说明, `data` 应全为字符串, 但由于需要接收`message` 类型的消息, 所以 *仅限此Type的content字段* 支持Array套娃**
|
||||||
|
|
||||||
|
示例:
|
||||||
|
|
||||||
|
直接引用消息合并转发:
|
||||||
|
|
||||||
|
````json
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"type": "node",
|
||||||
|
"data": {
|
||||||
|
"id": "123"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "node",
|
||||||
|
"data": {
|
||||||
|
"id": "456"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
````
|
||||||
|
|
||||||
|
自定义消息合并转发:
|
||||||
|
|
||||||
|
````json
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"type": "node",
|
||||||
|
"data": {
|
||||||
|
"name": "消息发送者A",
|
||||||
|
"uin": "10086",
|
||||||
|
"content": [
|
||||||
|
{
|
||||||
|
"type": "text",
|
||||||
|
"data": {"text": "测试消息1"}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "node",
|
||||||
|
"data": {
|
||||||
|
"name": "消息发送者B",
|
||||||
|
"uin": "10087",
|
||||||
|
"content": "[CQ:image,file=xxxxx]测试消息2"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
````
|
||||||
|
|
||||||
|
引用自定义混合合并转发:
|
||||||
|
|
||||||
|
````json
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"type": "node",
|
||||||
|
"data": {
|
||||||
|
"name": "自定义发送者",
|
||||||
|
"uin": "10086",
|
||||||
|
"content": "我是自定义消息"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "node",
|
||||||
|
"data": {
|
||||||
|
"id": "123"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
````
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
`/set_group_name` **设置群名**
|
### 设置群名
|
||||||
|
|
||||||
|
终结点: `/set_group_name`
|
||||||
|
|
||||||
**参数**
|
**参数**
|
||||||
|
|
||||||
@ -20,7 +134,9 @@
|
|||||||
| group_id | int64 | 群号 |
|
| group_id | int64 | 群号 |
|
||||||
| name | string | 新名 |
|
| name | string | 新名 |
|
||||||
|
|
||||||
`/get_image` **获取图片信息**
|
### 获取图片信息
|
||||||
|
|
||||||
|
终结点: `/get_image`
|
||||||
|
|
||||||
> 该接口为 CQHTTP 接口修改
|
> 该接口为 CQHTTP 接口修改
|
||||||
|
|
||||||
@ -38,7 +154,9 @@
|
|||||||
| `filename` | string | 图片文件原名 |
|
| `filename` | string | 图片文件原名 |
|
||||||
| `url` | string | 图片下载地址 |
|
| `url` | string | 图片下载地址 |
|
||||||
|
|
||||||
`/get_group_msg` **获取群消息**
|
### 获取群消息
|
||||||
|
|
||||||
|
终结点: `/get_group_msg`
|
||||||
|
|
||||||
参数
|
参数
|
||||||
|
|
||||||
@ -56,7 +174,9 @@
|
|||||||
| `time` | int32 | 发送时间 |
|
| `time` | int32 | 发送时间 |
|
||||||
| `content` | message | 消息内容 |
|
| `content` | message | 消息内容 |
|
||||||
|
|
||||||
`/get_forward_msg` **获取转发消息信息**
|
### 获取合并转发内容
|
||||||
|
|
||||||
|
终结点: `/get_forward_msg`
|
||||||
|
|
||||||
参数
|
参数
|
||||||
|
|
||||||
@ -64,7 +184,53 @@
|
|||||||
| ------------ | ------ | ------ |
|
| ------------ | ------ | ------ |
|
||||||
| `message_id` | string | 消息id |
|
| `message_id` | string | 消息id |
|
||||||
|
|
||||||
|
响应数据
|
||||||
|
|
||||||
|
| 字段 | 类型 | 说明 |
|
||||||
|
| ---------- | ----------------- | -------- |
|
||||||
|
| `messages` | forward message[] | 消息列表 |
|
||||||
|
|
||||||
|
响应示例
|
||||||
|
|
||||||
|
````json
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"messages": [
|
||||||
|
{
|
||||||
|
"content": "合并转发1",
|
||||||
|
"sender": {
|
||||||
|
"nickname": "发送者A",
|
||||||
|
"user_id": 10086
|
||||||
|
},
|
||||||
|
"time": 1595694374
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"content": "合并转发2[CQ:image,file=xxxx,url=xxxx]",
|
||||||
|
"sender": {
|
||||||
|
"nickname": "发送者B",
|
||||||
|
"user_id": 10087
|
||||||
|
},
|
||||||
|
"time": 1595694393
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"retcode": 0,
|
||||||
|
"status": "ok"
|
||||||
|
}
|
||||||
|
````
|
||||||
|
|
||||||
|
### 发送合并转发(群)
|
||||||
|
|
||||||
|
终结点: `/send_group_forward_msg`
|
||||||
|
|
||||||
|
**参数**
|
||||||
|
|
||||||
|
| 字段 | 类型 | 说明 |
|
||||||
|
| ---------- | -------------- | ---------------------------- |
|
||||||
|
| `group_id` | int64 | 群号 |
|
||||||
|
| `messages` | forward node[] | 自定义转发消息, 具体看CQCode |
|
||||||
|
|
||||||
|
###
|
||||||
|
|
||||||
## 事件
|
## 事件
|
||||||
|
|
||||||
@ -81,3 +247,14 @@
|
|||||||
| `operator_id` | int64 | | 操作者id |
|
| `operator_id` | int64 | | 操作者id |
|
||||||
| `message_id` | int64 | | 被撤回的消息id |
|
| `message_id` | int64 | | 被撤回的消息id |
|
||||||
|
|
||||||
|
#### 好友消息撤回
|
||||||
|
|
||||||
|
**上报数据**
|
||||||
|
|
||||||
|
| 字段 | 类型 | 可能的值 | 说明 |
|
||||||
|
| ------------- | ------ | -------------- | -------------- |
|
||||||
|
| `post_type` | string | `notice` | 上报类型 |
|
||||||
|
| `notice_type` | string | `friend_recall`| 消息类型 |
|
||||||
|
| `user_id` | int64 | | 好友id |
|
||||||
|
| `message_id` | int64 | | 被撤回的消息id |
|
||||||
|
|
||||||
|
@ -15,6 +15,8 @@ type JsonConfig struct {
|
|||||||
HttpConfig *GoCQHttpConfig `json:"http_config"`
|
HttpConfig *GoCQHttpConfig `json:"http_config"`
|
||||||
WSConfig *GoCQWebsocketConfig `json:"ws_config"`
|
WSConfig *GoCQWebsocketConfig `json:"ws_config"`
|
||||||
ReverseServers []*GoCQReverseWebsocketConfig `json:"ws_reverse_servers"`
|
ReverseServers []*GoCQReverseWebsocketConfig `json:"ws_reverse_servers"`
|
||||||
|
Proxy string `json:"proxy"`
|
||||||
|
Debug bool `json:"debug"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type CQHttpApiConfig struct {
|
type CQHttpApiConfig struct {
|
||||||
|
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module github.com/Mrs4s/go-cqhttp
|
|||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200726203306-14d741fe9383
|
github.com/Mrs4s/MiraiGo v0.0.0-20200801022834-8f1c0c053a86
|
||||||
github.com/gin-gonic/gin v1.6.3
|
github.com/gin-gonic/gin v1.6.3
|
||||||
github.com/gorilla/websocket v1.4.2
|
github.com/gorilla/websocket v1.4.2
|
||||||
github.com/guonaihong/gout v0.1.1
|
github.com/guonaihong/gout v0.1.1
|
||||||
|
29
go.sum
29
go.sum
@ -1,21 +1,9 @@
|
|||||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200717203209-5ead51215a01 h1:kG9Oj5/jI8PurVDu3M5DjytBgLaPpDcScIY4oo9YjYE=
|
github.com/Mrs4s/MiraiGo v0.0.0-20200731150933-29a5a459fd85 h1:qLNIyNtxORtHJsBcuwFkKHSXgAffxCN5H9zYrWaAr+Y=
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200717203209-5ead51215a01/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
github.com/Mrs4s/MiraiGo v0.0.0-20200731150933-29a5a459fd85/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200718181224-a45ad2e14770 h1:sbEcdUqvUFQ5dGaXzJVlwc+Q5tsMORSDGs6vwEmHYDg=
|
github.com/Mrs4s/MiraiGo v0.0.0-20200801022834-8f1c0c053a86 h1:Rnfejw8W7MX3nW15a1tDW3ybAq2y95iQFGZmcQUKuRs=
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200718181224-a45ad2e14770/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
github.com/Mrs4s/MiraiGo v0.0.0-20200801022834-8f1c0c053a86/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200719130800-9d81397b5d91 h1:0es7eD8Mn2CzPX7c4Ig67zt8Izz/eNyZu2wa4p0aRfY=
|
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200719130800-9d81397b5d91/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200720175644-0a8fa220ea50 h1:phnnq/0GZXsLeoviernp6qD57M2XxBzAuWpHG8B9ESI=
|
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200720175644-0a8fa220ea50/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200720230213-9a7a28f9dcc7 h1:nzGG3nm4gJA7wyvvyxMEvmY7RAJA7HtMTPcCUbrh/v0=
|
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200720230213-9a7a28f9dcc7/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200720231612-a7e460246fbc h1:elEjdwOy2u+Gfz+1UvoverA/x3RKTenbbAuBMwizTGk=
|
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200720231612-a7e460246fbc/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200721195252-2accd73f8b8e h1:68ol9TpLBwbFQU+S6VQ0CY6nQqN2xIPxHWD/rvBZxVI=
|
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200721195252-2accd73f8b8e/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200726203306-14d741fe9383 h1:Z1z7pG9RJo5ynI1mW0r+PyK9wZ6xExY8cMS+kV0NUJ0=
|
|
||||||
github.com/Mrs4s/MiraiGo v0.0.0-20200726203306-14d741fe9383/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
|
||||||
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
|
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
|
||||||
github.com/bwmarrin/snowflake v0.3.0/go.mod h1:NdZxfVWX+oR6y2K0o6qAYv6gIOP9rjG0/E9WsDpxqwE=
|
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=
|
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||||
@ -50,8 +38,6 @@ github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:W
|
|||||||
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||||
github.com/golang/protobuf v1.4.1 h1:ZFgWrT+bLgsYPirOnRfKLYJLvssAegOj/hgyMFdJZe0=
|
github.com/golang/protobuf v1.4.1 h1:ZFgWrT+bLgsYPirOnRfKLYJLvssAegOj/hgyMFdJZe0=
|
||||||
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
|
github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8=
|
||||||
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
|
|
||||||
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
|
||||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||||
@ -65,7 +51,6 @@ github.com/guonaihong/gout v0.1.1 h1:2i3eqQ1KUhTlj7AFeIHqVUFku5QwUhwE2wNgYTVpbxQ
|
|||||||
github.com/guonaihong/gout v0.1.1/go.mod h1:vXvv5Kxr70eM5wrp4F0+t9lnLWmq+YPW2GByll2f/EA=
|
github.com/guonaihong/gout v0.1.1/go.mod h1:vXvv5Kxr70eM5wrp4F0+t9lnLWmq+YPW2GByll2f/EA=
|
||||||
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
||||||
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
|
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
|
||||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||||
@ -81,9 +66,7 @@ github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2y
|
|||||||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
|
||||||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
|
||||||
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
|
||||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
@ -138,7 +121,6 @@ golang.org/x/sys v0.0.0-20181221143128-b4a75ba826a6/go.mod h1:STP8DvDyc/dI5b8T5h
|
|||||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||||
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
|
|
||||||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
|
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
|
||||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||||
@ -164,7 +146,6 @@ google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQ
|
|||||||
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
|
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
|
||||||
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||||
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
|
||||||
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||||
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
|
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
|
||||||
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
||||||
@ -174,7 +155,5 @@ gopkg.in/go-playground/validator.v9 v9.29.1/go.mod h1:+c9/zcJMFNgbLvly1L1V+PpxWd
|
|||||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||||
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||||
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
|
|
||||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
|
||||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
11
main.go
11
main.go
@ -85,6 +85,10 @@ func main() {
|
|||||||
if conf.Uin == 0 || conf.Password == "" {
|
if conf.Uin == 0 || conf.Password == "" {
|
||||||
log.Fatal("请修改 config.json 以添加账号密码.")
|
log.Fatal("请修改 config.json 以添加账号密码.")
|
||||||
}
|
}
|
||||||
|
if conf.Debug {
|
||||||
|
log.SetLevel(log.DebugLevel)
|
||||||
|
log.Warnf("已开启Debug模式.")
|
||||||
|
}
|
||||||
if !global.PathExists("device.json") {
|
if !global.PathExists("device.json") {
|
||||||
log.Warn("虚拟设备信息不存在, 将自动生成随机设备,按 Enter 继续.")
|
log.Warn("虚拟设备信息不存在, 将自动生成随机设备,按 Enter 继续.")
|
||||||
_, _ = console.ReadString('\n')
|
_, _ = console.ReadString('\n')
|
||||||
@ -115,11 +119,8 @@ func main() {
|
|||||||
rsp, err = cli.SubmitCaptcha(strings.ReplaceAll(text, "\n", ""), rsp.CaptchaSign)
|
rsp, err = cli.SubmitCaptcha(strings.ReplaceAll(text, "\n", ""), rsp.CaptchaSign)
|
||||||
continue
|
continue
|
||||||
case client.UnsafeDeviceError:
|
case client.UnsafeDeviceError:
|
||||||
log.Warnf("账号已开启设备锁,请前往 -> %v <- 验证.", rsp.VerifyUrl)
|
log.Warnf("账号已开启设备锁,请前往 -> %v <- 验证并重启Bot.", rsp.VerifyUrl)
|
||||||
log.Info("按 Enter 继续")
|
return
|
||||||
_, _ = console.ReadString('\n')
|
|
||||||
rsp, err = cli.Login()
|
|
||||||
continue
|
|
||||||
case client.OtherLoginError, client.UnknownLoginError:
|
case client.OtherLoginError, client.UnknownLoginError:
|
||||||
log.Fatalf("登录失败: %v", rsp.ErrorMessage)
|
log.Fatalf("登录失败: %v", rsp.ErrorMessage)
|
||||||
}
|
}
|
||||||
|
@ -97,6 +97,9 @@ func (s *httpServer) Run(addr, authToken string, bot *coolq.CQBot) {
|
|||||||
s.engine.Any("/send_group_msg", s.SendGroupMessage)
|
s.engine.Any("/send_group_msg", s.SendGroupMessage)
|
||||||
s.engine.Any("/send_group_msg_async", s.SendGroupMessage)
|
s.engine.Any("/send_group_msg_async", s.SendGroupMessage)
|
||||||
|
|
||||||
|
s.engine.Any("/send_group_forward_msg", s.SendGroupForwardMessage)
|
||||||
|
s.engine.Any("/send_group_forward_msg_async", s.SendGroupForwardMessage)
|
||||||
|
|
||||||
s.engine.Any("/delete_msg", s.DeleteMessage)
|
s.engine.Any("/delete_msg", s.DeleteMessage)
|
||||||
s.engine.Any("/delete_msg_async", s.DeleteMessage)
|
s.engine.Any("/delete_msg_async", s.DeleteMessage)
|
||||||
|
|
||||||
@ -124,6 +127,9 @@ func (s *httpServer) Run(addr, authToken string, bot *coolq.CQBot) {
|
|||||||
s.engine.Any("/set_group_name", s.SetGroupName)
|
s.engine.Any("/set_group_name", s.SetGroupName)
|
||||||
s.engine.Any("/set_group_name_async", s.SetGroupName)
|
s.engine.Any("/set_group_name_async", s.SetGroupName)
|
||||||
|
|
||||||
|
s.engine.Any("/set_group_leave", s.SetGroupLeave)
|
||||||
|
s.engine.Any("/set_group_leave_async", s.SetGroupLeave)
|
||||||
|
|
||||||
s.engine.Any("/get_image", s.GetImage)
|
s.engine.Any("/get_image", s.GetImage)
|
||||||
s.engine.Any("/get_image_async", s.GetImage)
|
s.engine.Any("/get_image_async", s.GetImage)
|
||||||
|
|
||||||
@ -229,13 +235,27 @@ func (s *httpServer) SendMessage(c *gin.Context) {
|
|||||||
func (s *httpServer) SendPrivateMessage(c *gin.Context) {
|
func (s *httpServer) SendPrivateMessage(c *gin.Context) {
|
||||||
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||||
msg := getParam(c, "message")
|
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) {
|
func (s *httpServer) SendGroupMessage(c *gin.Context) {
|
||||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||||
msg := getParam(c, "message")
|
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) SendGroupForwardMessage(c *gin.Context) {
|
||||||
|
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||||
|
msg := getParam(c, "messages")
|
||||||
|
c.JSON(200, s.bot.CQSendGroupForwardMessage(gid, gjson.Parse(msg)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *httpServer) GetImage(c *gin.Context) {
|
func (s *httpServer) GetImage(c *gin.Context) {
|
||||||
@ -286,7 +306,7 @@ func (s *httpServer) SetGroupKick(c *gin.Context) {
|
|||||||
func (s *httpServer) SetGroupBan(c *gin.Context) {
|
func (s *httpServer) SetGroupBan(c *gin.Context) {
|
||||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||||
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||||
i, _ := strconv.ParseInt(getParam(c, "duration"), 10, 64)
|
i, _ := strconv.ParseInt(getParamOrDefault(c, "duration", "1800"), 10, 64)
|
||||||
c.JSON(200, s.bot.CQSetGroupBan(gid, uid, uint32(i)))
|
c.JSON(200, s.bot.CQSetGroupBan(gid, uid, uint32(i)))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -300,6 +320,11 @@ func (s *httpServer) SetGroupName(c *gin.Context) {
|
|||||||
c.JSON(200, s.bot.CQSetGroupName(gid, getParam(c, "name")))
|
c.JSON(200, s.bot.CQSetGroupName(gid, getParam(c, "name")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *httpServer) SetGroupLeave(c *gin.Context) {
|
||||||
|
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||||
|
c.JSON(200, s.bot.CQSetGroupLeave(gid))
|
||||||
|
}
|
||||||
|
|
||||||
func (s *httpServer) GetForwardMessage(c *gin.Context) {
|
func (s *httpServer) GetForwardMessage(c *gin.Context) {
|
||||||
resId := getParam(c, "message_id")
|
resId := getParam(c, "message_id")
|
||||||
c.JSON(200, s.bot.CQGetForwardMessage(resId))
|
c.JSON(200, s.bot.CQGetForwardMessage(resId))
|
||||||
@ -361,6 +386,8 @@ func getParam(c *gin.Context, k string) string {
|
|||||||
res := obj.(gjson.Result).Get(k)
|
res := obj.(gjson.Result).Get(k)
|
||||||
if res.Exists() {
|
if res.Exists() {
|
||||||
switch res.Type {
|
switch res.Type {
|
||||||
|
case gjson.JSON:
|
||||||
|
return res.Raw
|
||||||
case gjson.String:
|
case gjson.String:
|
||||||
return res.Str
|
return res.Str
|
||||||
case gjson.Number:
|
case gjson.Number:
|
||||||
|
@ -164,12 +164,15 @@ func (c *websocketClient) listenApi(conn *wsc.Conn, u bool) {
|
|||||||
}
|
}
|
||||||
j := gjson.ParseBytes(buf[:l])
|
j := gjson.ParseBytes(buf[:l])
|
||||||
t := strings.ReplaceAll(j.Get("action").Str, "_async", "")
|
t := strings.ReplaceAll(j.Get("action").Str, "_async", "")
|
||||||
|
log.Debugf("反向WS接收到API调用: %v 参数: %v", t, j.Get("params").Raw)
|
||||||
if f, ok := wsApi[t]; ok {
|
if f, ok := wsApi[t]; ok {
|
||||||
ret := f(c.bot, j.Get("params"))
|
ret := f(c.bot, j.Get("params"))
|
||||||
if j.Get("echo").Exists() {
|
if j.Get("echo").Exists() {
|
||||||
ret["echo"] = j.Get("echo").Value()
|
ret["echo"] = j.Get("echo").Value()
|
||||||
}
|
}
|
||||||
|
c.pushLock.Lock()
|
||||||
_, _ = conn.Write([]byte(ret.ToJson()))
|
_, _ = conn.Write([]byte(ret.ToJson()))
|
||||||
|
c.pushLock.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if c.conf.ReverseReconnectInterval != 0 {
|
if c.conf.ReverseReconnectInterval != 0 {
|
||||||
@ -186,6 +189,7 @@ func (c *websocketClient) onBotPushEvent(m coolq.MSG) {
|
|||||||
c.pushLock.Lock()
|
c.pushLock.Lock()
|
||||||
defer c.pushLock.Unlock()
|
defer c.pushLock.Unlock()
|
||||||
if c.eventConn != nil {
|
if c.eventConn != nil {
|
||||||
|
log.Debugf("向WS服务器 %v 推送Event: %v", c.eventConn.RemoteAddr().String(), m.ToJson())
|
||||||
if _, err := c.eventConn.Write([]byte(m.ToJson())); err != nil {
|
if _, err := c.eventConn.Write([]byte(m.ToJson())); err != nil {
|
||||||
_ = c.eventConn.Close()
|
_ = c.eventConn.Close()
|
||||||
if c.conf.ReverseReconnectInterval != 0 {
|
if c.conf.ReverseReconnectInterval != 0 {
|
||||||
@ -195,6 +199,7 @@ func (c *websocketClient) onBotPushEvent(m coolq.MSG) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if c.universalConn != nil {
|
if c.universalConn != nil {
|
||||||
|
log.Debugf("向WS服务器 %v 推送Event: %v", c.universalConn.RemoteAddr().String(), m.ToJson())
|
||||||
_, _ = c.universalConn.Write([]byte(m.ToJson()))
|
_, _ = c.universalConn.Write([]byte(m.ToJson()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -267,13 +272,15 @@ func (s *websocketServer) listenApi(c *websocket.Conn) {
|
|||||||
if t == websocket.TextMessage {
|
if t == websocket.TextMessage {
|
||||||
j := gjson.ParseBytes(payload)
|
j := gjson.ParseBytes(payload)
|
||||||
t := strings.ReplaceAll(j.Get("action").Str, "_async", "") //TODO: async support
|
t := strings.ReplaceAll(j.Get("action").Str, "_async", "") //TODO: async support
|
||||||
//log.Infof("API调用: %v", j.Get("action").Str)
|
log.Debugf("WS接收到API调用: %v 参数: %v", t, j.Get("params").Raw)
|
||||||
if f, ok := wsApi[t]; ok {
|
if f, ok := wsApi[t]; ok {
|
||||||
ret := f(s.bot, j.Get("params"))
|
ret := f(s.bot, j.Get("params"))
|
||||||
if j.Get("echo").Exists() {
|
if j.Get("echo").Exists() {
|
||||||
ret["echo"] = j.Get("echo").Value()
|
ret["echo"] = j.Get("echo").Value()
|
||||||
}
|
}
|
||||||
|
s.pushLock.Lock()
|
||||||
_ = c.WriteJSON(ret)
|
_ = c.WriteJSON(ret)
|
||||||
|
s.pushLock.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -284,6 +291,7 @@ func (s *websocketServer) onBotPushEvent(m coolq.MSG) {
|
|||||||
defer s.pushLock.Unlock()
|
defer s.pushLock.Unlock()
|
||||||
pos := 0
|
pos := 0
|
||||||
for _, conn := range s.eventConn {
|
for _, conn := range s.eventConn {
|
||||||
|
log.Debugf("向WS客户端 %v 推送Event: %v", conn.RemoteAddr().String(), m.ToJson())
|
||||||
err := conn.WriteMessage(websocket.TextMessage, []byte(m.ToJson()))
|
err := conn.WriteMessage(websocket.TextMessage, []byte(m.ToJson()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
_ = conn.Close()
|
_ = conn.Close()
|
||||||
@ -320,18 +328,21 @@ var wsApi = map[string]func(*coolq.CQBot, gjson.Result) coolq.MSG{
|
|||||||
},
|
},
|
||||||
"send_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
"send_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||||
if p.Get("group_id").Int() != 0 {
|
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 {
|
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{}
|
return coolq.MSG{}
|
||||||
},
|
},
|
||||||
"send_group_msg": func(bot *coolq.CQBot, p gjson.Result) 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_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 {
|
"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 {
|
"delete_msg": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||||
return bot.CQDeleteMessage(int32(p.Get("message_id").Int()))
|
return bot.CQDeleteMessage(int32(p.Get("message_id").Int()))
|
||||||
@ -364,7 +375,12 @@ var wsApi = map[string]func(*coolq.CQBot, gjson.Result) coolq.MSG{
|
|||||||
return bot.CQSetGroupKick(p.Get("group_id").Int(), p.Get("user_id").Int(), p.Get("message").Str)
|
return bot.CQSetGroupKick(p.Get("group_id").Int(), p.Get("user_id").Int(), p.Get("message").Str)
|
||||||
},
|
},
|
||||||
"set_group_ban": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
"set_group_ban": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||||
return bot.CQSetGroupBan(p.Get("group_id").Int(), p.Get("user_id").Int(), uint32(p.Get("duration").Int()))
|
return bot.CQSetGroupBan(p.Get("group_id").Int(), p.Get("user_id").Int(), func() uint32 {
|
||||||
|
if p.Get("duration").Exists() {
|
||||||
|
return uint32(p.Get("duration").Int())
|
||||||
|
}
|
||||||
|
return 1800
|
||||||
|
}())
|
||||||
},
|
},
|
||||||
"set_group_whole_ban": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
"set_group_whole_ban": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||||
return bot.CQSetGroupWholeBan(p.Get("group_id").Int(), func() bool {
|
return bot.CQSetGroupWholeBan(p.Get("group_id").Int(), func() bool {
|
||||||
@ -377,6 +393,9 @@ var wsApi = map[string]func(*coolq.CQBot, gjson.Result) coolq.MSG{
|
|||||||
"set_group_name": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
"set_group_name": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||||
return bot.CQSetGroupName(p.Get("group_id").Int(), p.Get("name").Str)
|
return bot.CQSetGroupName(p.Get("group_id").Int(), p.Get("name").Str)
|
||||||
},
|
},
|
||||||
|
"set_group_leave": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||||
|
return bot.CQSetGroupLeave(p.Get("group_id").Int())
|
||||||
|
},
|
||||||
"get_image": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
"get_image": func(bot *coolq.CQBot, p gjson.Result) coolq.MSG {
|
||||||
return bot.CQGetImage(p.Get("file").Str)
|
return bot.CQGetImage(p.Get("file").Str)
|
||||||
},
|
},
|
||||||
|
Reference in New Issue
Block a user