mirror of
https://github.com/Mrs4s/go-cqhttp.git
synced 2025-06-29 19:43:24 +00:00
Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
977337914d | |||
6780615d45 | |||
2670c84c8e | |||
e2b6c3d607 | |||
58a3f81142 | |||
64cb539c0d | |||
3bb844d1d4 | |||
0fdb01e82c | |||
7cf199189a | |||
3aea70e92a | |||
d1505ec15d | |||
750a01acdd | |||
a12dcfa2d7 | |||
86422c5a00 | |||
f3b43bc64c | |||
57153d2d4a | |||
b73aeb6666 | |||
a215028ac0 | |||
d076f174bb | |||
5c8fa7f9a0 | |||
9c95a906b6 |
24
README.md
24
README.md
@ -10,17 +10,32 @@
|
||||
- [x] HTTP API
|
||||
- [x] 反向HTTP POST
|
||||
- [x] 正向Websocket
|
||||
- [x] 反向Websocket (测试中)
|
||||
- [x] 反向Websocket
|
||||
|
||||
#### 拓展支持
|
||||
> 拓展API可前往 [文档](docs/cqhttp.md) 查看
|
||||
- [x] HTTP POST多点上报
|
||||
- [x] 反向WS多点连接
|
||||
- [x] 修改群名
|
||||
- [x] 消息撤回事件
|
||||
- [x] 解析/发送 回复消息
|
||||
- [x] 解析合并转发
|
||||
- [x] 解析/发送 合并转发
|
||||
- [ ] 使用代理请求网络图片
|
||||
|
||||
#### 实现
|
||||
<details>
|
||||
<summary>已实现CQ码</summary>
|
||||
|
||||
- [CQ:image]
|
||||
- [CQ:face]
|
||||
- [CQ:at]
|
||||
- [CQ:share]
|
||||
- [CQ:reply]
|
||||
- [CQ:forward]
|
||||
- [CQ:node]
|
||||
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>已实现API</summary>
|
||||
|
||||
@ -44,6 +59,7 @@
|
||||
| /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_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) |
|
||||
| /get_image | 获取图片信息(拓展API) |
|
||||
| /get_group_msg | 获取群组消息(拓展API) |
|
||||
@ -62,11 +78,13 @@
|
||||
| ------------------------------------------------------------ |
|
||||
| [私聊信息](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=加群请求/邀请) |
|
||||
|
||||
|
144
coolq/api.go
144
coolq/api.go
@ -1,7 +1,6 @@
|
||||
package coolq
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/Mrs4s/MiraiGo/binary"
|
||||
"github.com/Mrs4s/MiraiGo/client"
|
||||
"github.com/Mrs4s/MiraiGo/message"
|
||||
@ -13,6 +12,7 @@ import (
|
||||
"path"
|
||||
"runtime"
|
||||
"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
|
||||
@ -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
|
||||
func (bot *CQBot) CQSendGroupMessage(groupId int64, msg string) MSG {
|
||||
if msg == "" {
|
||||
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)
|
||||
}
|
||||
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)
|
||||
}
|
||||
elem := bot.ConvertStringMessage(msg, true)
|
||||
mid := bot.SendGroupMessage(groupId, &message.SendingMessage{Elements: elem})
|
||||
if mid == -1 {
|
||||
return Failed(100)
|
||||
var nodes []*message.ForwardNode
|
||||
ts := time.Now().Add(-time.Minute * 5)
|
||||
hasCustom := func() bool {
|
||||
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
|
||||
func (bot *CQBot) CQSendPrivateMessage(userId int64, msg string) MSG {
|
||||
if msg == "" {
|
||||
return Failed(100)
|
||||
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)
|
||||
}
|
||||
return OK(MSG{"message_id": mid})
|
||||
}
|
||||
elem := bot.ConvertStringMessage(msg, false)
|
||||
mid := bot.SendPrivateMessage(userId, &message.SendingMessage{Elements: elem})
|
||||
if mid == -1 {
|
||||
return Failed(100)
|
||||
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})
|
||||
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
|
||||
@ -182,6 +273,15 @@ func (bot *CQBot) CQSetGroupWholeBan(groupId int64, enable bool) MSG {
|
||||
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
|
||||
func (bot *CQBot) CQProcessFriendRequest(flag string, approve bool) MSG {
|
||||
req, ok := bot.friendReqCache.Load(flag)
|
||||
@ -241,16 +341,14 @@ func (bot *CQBot) CQHandleQuickOperation(context, operation gjson.Result) MSG {
|
||||
switch postType {
|
||||
case "message":
|
||||
msgType := context.Get("message_type").Str
|
||||
reply := operation.Get("reply").Str
|
||||
if reply != "" {
|
||||
reply := operation.Get("reply")
|
||||
if reply.Exists() {
|
||||
at := true
|
||||
if operation.Get("at_sender").Exists() {
|
||||
at = operation.Get("at_sender").Bool()
|
||||
}
|
||||
// TODO: 处理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)
|
||||
}
|
||||
if msgType == "private" {
|
||||
@ -317,8 +415,8 @@ func (bot *CQBot) CQGetForwardMessage(resId string) MSG {
|
||||
checkImage(n.Message)
|
||||
r = append(r, MSG{
|
||||
"sender": MSG{
|
||||
"user_id": n.SenderId,
|
||||
"nick": n.SenderName,
|
||||
"user_id": n.SenderId,
|
||||
"nickname": n.SenderName,
|
||||
},
|
||||
"time": n.Time,
|
||||
"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.OnGroupMuted(bot.groupMutedEvent)
|
||||
bot.Client.OnGroupMessageRecalled(bot.groupRecallEvent)
|
||||
bot.Client.OnFriendMessageRecalled(bot.friendRecallEvent)
|
||||
bot.Client.OnJoinGroup(bot.joinGroupEvent)
|
||||
bot.Client.OnLeaveGroup(bot.leaveGroupEvent)
|
||||
bot.Client.OnGroupMemberJoined(bot.memberJoinEvent)
|
||||
@ -75,7 +76,7 @@ func (bot *CQBot) GetGroupMessage(mid int32) MSG {
|
||||
return err
|
||||
}
|
||||
buff := new(bytes.Buffer)
|
||||
buff.Write(e.Value)
|
||||
buff.Write(binary.GZipUncompress(e.Value))
|
||||
return gob.NewDecoder(buff).Decode(&m)
|
||||
})
|
||||
if err == nil {
|
||||
@ -140,7 +141,7 @@ func (bot *CQBot) InsertGroupMessage(m *message.GroupMessage) int32 {
|
||||
if err := gob.NewEncoder(buf).Encode(val); err != nil {
|
||||
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 {
|
||||
log.Warnf("记录聊天数据时出现错误: %v", err)
|
||||
@ -154,7 +155,9 @@ func ToGlobalId(code int64, msgId int32) int32 {
|
||||
}
|
||||
|
||||
func (bot *CQBot) Release() {
|
||||
_ = bot.db.Close()
|
||||
if bot.db != nil {
|
||||
_ = bot.db.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (bot *CQBot) dispatchEventMessage(m MSG) {
|
||||
|
@ -8,6 +8,7 @@ import (
|
||||
"github.com/Mrs4s/MiraiGo/message"
|
||||
"github.com/Mrs4s/go-cqhttp/global"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/tidwall/gjson"
|
||||
"io/ioutil"
|
||||
"path"
|
||||
"regexp"
|
||||
@ -103,6 +104,58 @@ func (bot *CQBot) ConvertStringMessage(m string, group bool) (r []message.IMessa
|
||||
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) {
|
||||
switch t {
|
||||
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)
|
||||
return message.NewAt(t), nil
|
||||
case "share":
|
||||
return message.NewUrlShare(d["url"], d["title"], d["content"], d["image"]), nil
|
||||
default:
|
||||
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",
|
||||
"message_type": "private",
|
||||
"sub_type": "friend",
|
||||
"message_id": m.Id,
|
||||
"message_id": ToGlobalId(m.Sender.Uin, m.Id),
|
||||
"user_id": m.Sender.Uin,
|
||||
"message": ToStringMessage(m.Elements, 0, false),
|
||||
"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) {
|
||||
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)
|
||||
id := m.Id
|
||||
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) {
|
||||
log.Infof("Bot进入了群 %v.", formatGroupName(group))
|
||||
bot.dispatchEventMessage(bot.groupIncrease(group.Code, 0, c.Uin))
|
||||
|
193
docs/cqhttp.md
193
docs/cqhttp.md
@ -4,14 +4,128 @@
|
||||
|
||||
## CQCode
|
||||
|
||||
| Code | 示例 | 说明 |
|
||||
| ------- | -------------------- | ------------------------------------------------------------ |
|
||||
| reply | [CQ:reply,id=123456] | 回复ID为 `123456`的信息. 发送时一条 `message` 仅能使用一次 |
|
||||
| forward | [CQ:forward,id=abcd] | ID为abcd的转发消息, 暂时仅能接收. 可通过 `/get_forward_msg` API获取具体信息 |
|
||||
### 回复
|
||||
|
||||
Type : `reply`
|
||||
|
||||
范围: **发送/接收**
|
||||
|
||||
参数:
|
||||
|
||||
| 参数名 | 类型 | 说明 |
|
||||
| ------ | ---- | ------------------------------------- |
|
||||
| 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
|
||||
|
||||
`/set_group_name` **设置群名**
|
||||
### 设置群名
|
||||
|
||||
终结点: `/set_group_name`
|
||||
|
||||
**参数**
|
||||
|
||||
@ -20,7 +134,9 @@
|
||||
| group_id | int64 | 群号 |
|
||||
| name | string | 新名 |
|
||||
|
||||
`/get_image` **获取图片信息**
|
||||
### 获取图片信息
|
||||
|
||||
终结点: `/get_image`
|
||||
|
||||
> 该接口为 CQHTTP 接口修改
|
||||
|
||||
@ -38,7 +154,9 @@
|
||||
| `filename` | string | 图片文件原名 |
|
||||
| `url` | string | 图片下载地址 |
|
||||
|
||||
`/get_group_msg` **获取群消息**
|
||||
### 获取群消息
|
||||
|
||||
终结点: `/get_group_msg`
|
||||
|
||||
参数
|
||||
|
||||
@ -56,7 +174,9 @@
|
||||
| `time` | int32 | 发送时间 |
|
||||
| `content` | message | 消息内容 |
|
||||
|
||||
`/get_forward_msg` **获取转发消息信息**
|
||||
### 获取合并转发内容
|
||||
|
||||
终结点: `/get_forward_msg`
|
||||
|
||||
参数
|
||||
|
||||
@ -64,7 +184,53 @@
|
||||
| ------------ | ------ | ------ |
|
||||
| `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 |
|
||||
| `message_id` | int64 | | 被撤回的消息id |
|
||||
|
||||
#### 好友消息撤回
|
||||
|
||||
**上报数据**
|
||||
|
||||
| 字段 | 类型 | 可能的值 | 说明 |
|
||||
| ------------- | ------ | -------------- | -------------- |
|
||||
| `post_type` | string | `notice` | 上报类型 |
|
||||
| `notice_type` | string | `friend_recall`| 消息类型 |
|
||||
| `user_id` | int64 | | 好友id |
|
||||
| `message_id` | int64 | | 被撤回的消息id |
|
||||
|
||||
|
@ -15,6 +15,7 @@ type JsonConfig struct {
|
||||
HttpConfig *GoCQHttpConfig `json:"http_config"`
|
||||
WSConfig *GoCQWebsocketConfig `json:"ws_config"`
|
||||
ReverseServers []*GoCQReverseWebsocketConfig `json:"ws_reverse_servers"`
|
||||
Debug bool `json:"debug"`
|
||||
}
|
||||
|
||||
type CQHttpApiConfig struct {
|
||||
|
2
go.mod
2
go.mod
@ -3,7 +3,7 @@ module github.com/Mrs4s/go-cqhttp
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/Mrs4s/MiraiGo v0.0.0-20200726203306-14d741fe9383
|
||||
github.com/Mrs4s/MiraiGo v0.0.0-20200802003417-9cd1355853c7
|
||||
github.com/gin-gonic/gin v1.6.3
|
||||
github.com/gorilla/websocket v1.4.2
|
||||
github.com/guonaihong/gout v0.1.1
|
||||
|
27
go.sum
27
go.sum
@ -1,21 +1,7 @@
|
||||
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/Mrs4s/MiraiGo v0.0.0-20200717203209-5ead51215a01 h1:kG9Oj5/jI8PurVDu3M5DjytBgLaPpDcScIY4oo9YjYE=
|
||||
github.com/Mrs4s/MiraiGo v0.0.0-20200717203209-5ead51215a01/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-20200718181224-a45ad2e14770/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/Mrs4s/MiraiGo v0.0.0-20200802003417-9cd1355853c7 h1:El3V+TFChR7AdfzMa8YqVIc/HX6lp5/4gwgMix9pz7w=
|
||||
github.com/Mrs4s/MiraiGo v0.0.0-20200802003417-9cd1355853c7/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
|
||||
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
|
||||
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=
|
||||
@ -50,8 +36,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.1 h1:ZFgWrT+bLgsYPirOnRfKLYJLvssAegOj/hgyMFdJZe0=
|
||||
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.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
@ -65,7 +49,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/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.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.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
|
||||
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
|
||||
@ -81,9 +64,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/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-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 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
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/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
@ -138,7 +119,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-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-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-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
|
||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
@ -164,7 +144,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.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||
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.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
|
||||
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
|
||||
@ -174,7 +153,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.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
|
||||
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-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
|
49
main.go
49
main.go
@ -18,6 +18,7 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
@ -72,7 +73,34 @@ func init() {
|
||||
|
||||
func main() {
|
||||
console := bufio.NewReader(os.Stdin)
|
||||
conf := global.Load("config.json")
|
||||
var conf *global.JsonConfig
|
||||
if global.PathExists("config.json") || os.Getenv("UIN") == "" {
|
||||
conf = global.Load("config.json")
|
||||
} else if os.Getenv("UIN") != "" {
|
||||
log.Infof("将从环境变量加载配置.")
|
||||
uin, _ := strconv.ParseInt(os.Getenv("UIN"), 10, 64)
|
||||
pwd := os.Getenv("PASS")
|
||||
post := os.Getenv("HTTP_POST")
|
||||
conf = &global.JsonConfig{
|
||||
Uin: uin,
|
||||
Password: pwd,
|
||||
HttpConfig: &global.GoCQHttpConfig{
|
||||
Enabled: true,
|
||||
Host: "0.0.0.0",
|
||||
Port: 5700,
|
||||
PostUrls: map[string]string{},
|
||||
},
|
||||
WSConfig: &global.GoCQWebsocketConfig{
|
||||
Enabled: true,
|
||||
Host: "0.0.0.0",
|
||||
Port: 6700,
|
||||
},
|
||||
Debug: os.Getenv("DEBUG") == "true",
|
||||
}
|
||||
if post != "" {
|
||||
conf.HttpConfig.PostUrls[post] = os.Getenv("HTTP_SECRET")
|
||||
}
|
||||
}
|
||||
if conf == nil {
|
||||
err := global.DefaultConfig().Save("config.json")
|
||||
if err != nil {
|
||||
@ -80,14 +108,20 @@ func main() {
|
||||
return
|
||||
}
|
||||
log.Infof("默认配置文件已生成, 请编辑 config.json 后重启程序.")
|
||||
time.Sleep(time.Second * 5)
|
||||
return
|
||||
}
|
||||
if conf.Uin == 0 || conf.Password == "" {
|
||||
log.Fatal("请修改 config.json 以添加账号密码.")
|
||||
log.Warnf("请修改 config.json 以添加账号密码.")
|
||||
time.Sleep(time.Second * 5)
|
||||
return
|
||||
}
|
||||
if conf.Debug {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
log.Warnf("已开启Debug模式.")
|
||||
}
|
||||
if !global.PathExists("device.json") {
|
||||
log.Warn("虚拟设备信息不存在, 将自动生成随机设备,按 Enter 继续.")
|
||||
_, _ = console.ReadString('\n')
|
||||
log.Warn("虚拟设备信息不存在, 将自动生成随机设备.")
|
||||
client.GenRandomDevice()
|
||||
_ = ioutil.WriteFile("device.json", client.SystemDeviceInfo.ToJson(), 0777)
|
||||
log.Info("已生成设备信息并保存到 device.json 文件.")
|
||||
@ -115,11 +149,8 @@ func main() {
|
||||
rsp, err = cli.SubmitCaptcha(strings.ReplaceAll(text, "\n", ""), rsp.CaptchaSign)
|
||||
continue
|
||||
case client.UnsafeDeviceError:
|
||||
log.Warnf("账号已开启设备锁,请前往 -> %v <- 验证.", rsp.VerifyUrl)
|
||||
log.Info("按 Enter 继续")
|
||||
_, _ = console.ReadString('\n')
|
||||
rsp, err = cli.Login()
|
||||
continue
|
||||
log.Warnf("账号已开启设备锁,请前往 -> %v <- 验证并重启Bot.", rsp.VerifyUrl)
|
||||
return
|
||||
case client.OtherLoginError, client.UnknownLoginError:
|
||||
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_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_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_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_async", s.GetImage)
|
||||
|
||||
@ -229,13 +235,27 @@ func (s *httpServer) SendMessage(c *gin.Context) {
|
||||
func (s *httpServer) SendPrivateMessage(c *gin.Context) {
|
||||
uid, _ := strconv.ParseInt(getParam(c, "user_id"), 10, 64)
|
||||
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) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_id"), 10, 64)
|
||||
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) {
|
||||
@ -286,7 +306,7 @@ func (s *httpServer) SetGroupKick(c *gin.Context) {
|
||||
func (s *httpServer) SetGroupBan(c *gin.Context) {
|
||||
gid, _ := strconv.ParseInt(getParam(c, "group_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)))
|
||||
}
|
||||
|
||||
@ -300,6 +320,11 @@ func (s *httpServer) SetGroupName(c *gin.Context) {
|
||||
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) {
|
||||
resId := getParam(c, "message_id")
|
||||
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)
|
||||
if res.Exists() {
|
||||
switch res.Type {
|
||||
case gjson.JSON:
|
||||
return res.Raw
|
||||
case gjson.String:
|
||||
return res.Str
|
||||
case gjson.Number:
|
||||
|
@ -164,12 +164,15 @@ func (c *websocketClient) listenApi(conn *wsc.Conn, u bool) {
|
||||
}
|
||||
j := gjson.ParseBytes(buf[:l])
|
||||
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 {
|
||||
ret := f(c.bot, j.Get("params"))
|
||||
if j.Get("echo").Exists() {
|
||||
ret["echo"] = j.Get("echo").Value()
|
||||
}
|
||||
c.pushLock.Lock()
|
||||
_, _ = conn.Write([]byte(ret.ToJson()))
|
||||
c.pushLock.Unlock()
|
||||
}
|
||||
}
|
||||
if c.conf.ReverseReconnectInterval != 0 {
|
||||
@ -186,6 +189,7 @@ func (c *websocketClient) onBotPushEvent(m coolq.MSG) {
|
||||
c.pushLock.Lock()
|
||||
defer c.pushLock.Unlock()
|
||||
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 {
|
||||
_ = c.eventConn.Close()
|
||||
if c.conf.ReverseReconnectInterval != 0 {
|
||||
@ -195,6 +199,7 @@ func (c *websocketClient) onBotPushEvent(m coolq.MSG) {
|
||||
}
|
||||
}
|
||||
if c.universalConn != nil {
|
||||
log.Debugf("向WS服务器 %v 推送Event: %v", c.universalConn.RemoteAddr().String(), m.ToJson())
|
||||
_, _ = c.universalConn.Write([]byte(m.ToJson()))
|
||||
}
|
||||
}
|
||||
@ -267,13 +272,15 @@ func (s *websocketServer) listenApi(c *websocket.Conn) {
|
||||
if t == websocket.TextMessage {
|
||||
j := gjson.ParseBytes(payload)
|
||||
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 {
|
||||
ret := f(s.bot, j.Get("params"))
|
||||
if j.Get("echo").Exists() {
|
||||
ret["echo"] = j.Get("echo").Value()
|
||||
}
|
||||
s.pushLock.Lock()
|
||||
_ = c.WriteJSON(ret)
|
||||
s.pushLock.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -284,6 +291,7 @@ func (s *websocketServer) onBotPushEvent(m coolq.MSG) {
|
||||
defer s.pushLock.Unlock()
|
||||
pos := 0
|
||||
for _, conn := range s.eventConn {
|
||||
log.Debugf("向WS客户端 %v 推送Event: %v", conn.RemoteAddr().String(), m.ToJson())
|
||||
err := conn.WriteMessage(websocket.TextMessage, []byte(m.ToJson()))
|
||||
if err != nil {
|
||||
_ = 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 {
|
||||
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 {
|
||||
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{}
|
||||
},
|
||||
"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 {
|
||||
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 {
|
||||
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)
|
||||
},
|
||||
"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 {
|
||||
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 {
|
||||
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 {
|
||||
return bot.CQGetImage(p.Get("file").Str)
|
||||
},
|
||||
|
Reference in New Issue
Block a user