1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-06-30 20:03:24 +00:00

Compare commits

..

7 Commits

Author SHA1 Message Date
f3b43bc64c doc update. 2020-07-29 21:46:59 +08:00
57153d2d4a forward message sending supported. 2020-07-29 21:45:43 +08:00
b73aeb6666 update MiraiGo for long message. 2020-07-29 12:53:54 +08:00
a215028ac0 object message supported. 2020-07-28 22:37:20 +08:00
d076f174bb array message supported. 2020-07-28 22:13:53 +08:00
5c8fa7f9a0 fix #4 Q2.
add db compress.
2020-07-28 20:55:51 +08:00
9c95a906b6 bug fix. 2020-07-27 21:12:17 +08:00
11 changed files with 391 additions and 73 deletions

View File

@ -10,7 +10,7 @@
- [x] HTTP API
- [x] 反向HTTP POST
- [x] 正向Websocket
- [x] 反向Websocket (测试中)
- [x] 反向Websocket
#### 拓展支持
- [x] HTTP POST多点上报
@ -18,7 +18,8 @@
- [x] 修改群名
- [x] 消息撤回事件
- [x] 解析/发送 回复消息
- [x] 解析合并转发
- [x] 解析/发送 合并转发
- [ ] 使用代理请求网络图片
#### 实现
<details>

View File

@ -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(msg, true)
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)
}
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 将跳过")
}
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)
}
elem := bot.ConvertStringMessage(msg, false)
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})
}
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 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
@ -241,16 +332,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" {

View File

@ -75,7 +75,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 +140,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 +154,9 @@ func ToGlobalId(code int64, msgId int32) int32 {
}
func (bot *CQBot) Release() {
if bot.db != nil {
_ = bot.db.Close()
}
}
func (bot *CQBot) dispatchEventMessage(m MSG) {

View File

@ -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":

View File

@ -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": {
"nick": "发送者A",
"user_id": 10086
},
"time": 1595694374
},
{
"content": "合并转发2[CQ:image,file=xxxx,url=xxxx]",
"sender": {
"nick": "发送者B",
"user_id": 10087
},
"time": 1595694393
}
]
},
"retcode": 0,
"status": "ok"
}
````
### 发送合并转发(群)
终结点: `/send_group_forward_msg`
**参数**
| 字段 | 类型 | 说明 |
| ---------- | -------------- | ---------------------------- |
| `group_id` | int64 | 群号 |
| `messages` | forward node[] | 自定义转发消息, 具体看CQCode |
###
## 事件

View File

@ -15,6 +15,7 @@ type JsonConfig struct {
HttpConfig *GoCQHttpConfig `json:"http_config"`
WSConfig *GoCQWebsocketConfig `json:"ws_config"`
ReverseServers []*GoCQReverseWebsocketConfig `json:"ws_reverse_servers"`
Proxy string `json:"proxy"`
}
type CQHttpApiConfig struct {

2
go.mod
View File

@ -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-20200729044443-98a77a6cf354
github.com/gin-gonic/gin v1.6.3
github.com/gorilla/websocket v1.4.2
github.com/guonaihong/gout v0.1.1

31
go.sum
View File

@ -1,21 +1,11 @@
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-20200727124316-9d432df098d9 h1:sgwJQnKKAJF/FCXyGv5HHpE5BcKGnwGyCSmPKTB7/vE=
github.com/Mrs4s/MiraiGo v0.0.0-20200727124316-9d432df098d9/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
github.com/Mrs4s/MiraiGo v0.0.0-20200728112613-91aa75e8270d h1:Pv8lvODPBb+By7CU1crF0/KrmwIrUb4W/3jTs9WDSN4=
github.com/Mrs4s/MiraiGo v0.0.0-20200728112613-91aa75e8270d/go.mod h1:M9wh1hjd0rie3+wm27tjPZkYMbD+MBV76CGqp2G7WSU=
github.com/Mrs4s/MiraiGo v0.0.0-20200729044443-98a77a6cf354 h1:f2Cv15Nir9tcefrvwD0+imVx1RQAw2TQI7LFipPUzrI=
github.com/Mrs4s/MiraiGo v0.0.0-20200729044443-98a77a6cf354/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 +40,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 +53,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 +68,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 +123,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 +148,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 +157,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=

View File

@ -115,11 +115,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)
}

View File

@ -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)
@ -229,13 +232,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 +303,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)))
}
@ -361,6 +378,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:

View File

@ -164,6 +164,7 @@ func (c *websocketClient) listenApi(conn *wsc.Conn, u bool) {
}
j := gjson.ParseBytes(buf[:l])
t := strings.ReplaceAll(j.Get("action").Str, "_async", "")
//log.Infof("调用API: %v p: %v", t, j.Get("params").Raw)
if f, ok := wsApi[t]; ok {
ret := f(c.bot, j.Get("params"))
if j.Get("echo").Exists() {
@ -320,18 +321,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 +368,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 {