-
Notifications
You must be signed in to change notification settings - Fork 65
/
api.go
343 lines (305 loc) · 12.5 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package zero
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
"github.com/wdvxdr1123/ZeroBot/message"
"github.com/wdvxdr1123/ZeroBot/utils/helper"
)
// formatMessage 格式化消息数组
func formatMessage(msg interface{}) string {
switch m := msg.(type) {
case string:
return m
case fmt.Stringer:
return m.String()
default:
s, _ := json.Marshal(msg)
return helper.BytesToString(s)
}
}
// CallAction 调用 cqhttp API
func (ctx *Ctx) CallAction(action string, params Params) APIResponse {
req := APIRequest{
Action: action,
Params: params,
}
rsp, err := ctx.caller.CallApi(req)
if err != nil {
log.Errorf("调用 API: %v 时出现错误: %v", action, err.Error())
}
if err == nil && rsp.RetCode != 0 {
log.Errorf("调用 API: %v 时出现错误, RetCode: %v, Msg: %v, Wording: %v", action, rsp.RetCode, rsp.Msg, rsp.Wording)
}
return rsp
}
// SendGroupMessage 发送群消息
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#send_group_msg-%E5%8F%91%E9%80%81%E7%BE%A4%E6%B6%88%E6%81%AF
func (ctx *Ctx) SendGroupMessage(groupID int64, message interface{}) int64 {
rsp := ctx.CallAction("send_group_msg", Params{ // 调用并保存返回值
"group_id": groupID,
"message": message,
}).Data.Get("message_id")
if rsp.Exists() {
log.Infof("发送群消息(%v): %v (id=%v)", groupID, formatMessage(message), rsp.Int())
return rsp.Int()
}
return 0 // 无法获取返回值
}
// SendPrivateMessage 发送私聊消息
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#send_private_msg-%E5%8F%91%E9%80%81%E7%A7%81%E8%81%8A%E6%B6%88%E6%81%AF
func (ctx *Ctx) SendPrivateMessage(userID int64, message interface{}) int64 {
rsp := ctx.CallAction("send_private_msg", Params{
"user_id": userID,
"message": message,
}).Data.Get("message_id")
if rsp.Exists() {
log.Infof("发送私聊消息(%v): %v (id=%v)", userID, formatMessage(message), rsp.Int())
return rsp.Int()
}
return 0 // 无法获取返回值
}
// DeleteMessage 撤回消息
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#delete_msg-%E6%92%A4%E5%9B%9E%E6%B6%88%E6%81%AF
func (ctx *Ctx) DeleteMessage(messageID int64) {
ctx.CallAction("delete_msg", Params{
"message_id": messageID,
})
}
// GetMessage 获取消息
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_msg-%E8%8E%B7%E5%8F%96%E6%B6%88%E6%81%AF
func (ctx *Ctx) GetMessage(messageID int64) Message {
rsp := ctx.CallAction("get_msg", Params{
"message_id": messageID,
}).Data
m := Message{
Elements: message.ParseMessage(helper.StringToBytes(rsp.Get("message").Raw)),
MessageId: rsp.Get("message_id").Int(),
MessageType: rsp.Get("message_type").String(),
Sender: &User{},
}
err := json.Unmarshal(helper.StringToBytes(rsp.Get("sender").Raw), m.Sender)
if err != nil {
return Message{}
}
return m
}
// GetForwardMessage 获取合并转发消息
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_forward_msg-%E8%8E%B7%E5%8F%96%E5%90%88%E5%B9%B6%E8%BD%AC%E5%8F%91%E6%B6%88%E6%81%AF
func (ctx *Ctx) GetForwardMessage(id int64) gjson.Result {
rsp := ctx.CallAction("get_forward_msg", Params{
"id": id,
}).Data
return rsp
}
// SetGroupKick 群组踢人
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_kick-%E7%BE%A4%E7%BB%84%E8%B8%A2%E4%BA%BA
func (ctx *Ctx) SetGroupKick(groupID, userID int64, rejectAddRequest bool) {
ctx.CallAction("set_group_kick", Params{
"group_id": groupID,
"user_id": userID,
"reject_add_request": rejectAddRequest,
})
}
// SetGroupBan 群组单人禁言
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_ban-%E7%BE%A4%E7%BB%84%E5%8D%95%E4%BA%BA%E7%A6%81%E8%A8%80
func (ctx *Ctx) SetGroupBan(groupID, userID, duration int64) {
ctx.CallAction("set_group_ban", Params{
"group_id": groupID,
"user_id": userID,
"duration": duration,
})
}
// SetGroupWholeBan 群组全员禁言
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_whole_ban-%E7%BE%A4%E7%BB%84%E5%85%A8%E5%91%98%E7%A6%81%E8%A8%80
func (ctx *Ctx) SetGroupWholeBan(groupID int64, enable bool) {
ctx.CallAction("set_group_whole_ban", Params{
"group_id": groupID,
"enable": enable,
})
}
// SetGroupAdmin 群组设置管理员
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_whole_ban-%E7%BE%A4%E7%BB%84%E5%85%A8%E5%91%98%E7%A6%81%E8%A8%80
func (ctx *Ctx) SetGroupAdmin(groupID, userID int64, enable bool) {
ctx.CallAction("set_group_admin", Params{
"group_id": groupID,
"user_id": userID,
"enable": enable,
})
}
// SetGroupAnonymous 群组匿名
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_anonymous-%E7%BE%A4%E7%BB%84%E5%8C%BF%E5%90%8D
func (ctx *Ctx) SetGroupAnonymous(groupID int64, enable bool) {
ctx.CallAction("set_group_anonymous", Params{
"group_id": groupID,
"enable": enable,
})
}
// SetGroupCard 设置群名片(群备注)
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_card-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E5%90%8D%E7%89%87%E7%BE%A4%E5%A4%87%E6%B3%A8
func (ctx *Ctx) SetGroupCard(groupID, userID int64, card string) {
ctx.CallAction("set_group_card", Params{
"group_id": groupID,
"user_id": userID,
"card": card,
})
}
// SetGroupName 设置群名
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_name-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E5%90%8D
func (ctx *Ctx) SetGroupName(groupID int64, groupName string) {
ctx.CallAction("set_group_name", Params{
"group_id": groupID,
"group_name": groupName,
})
}
// SetGroupLeave 退出群组
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_leave-%E9%80%80%E5%87%BA%E7%BE%A4%E7%BB%84
func (ctx *Ctx) SetGroupLeave(groupID int64, isDismiss bool) {
ctx.CallAction("set_group_leave", Params{
"group_id": groupID,
"is_dismiss": isDismiss,
})
}
// SetGroupSpecialTitle 设置群组专属头衔
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_special_title-%E8%AE%BE%E7%BD%AE%E7%BE%A4%E7%BB%84%E4%B8%93%E5%B1%9E%E5%A4%B4%E8%A1%94
func (ctx *Ctx) SetGroupSpecialTitle(groupID int64, userID int64, specialTitle string) {
ctx.CallAction("set_group_special_title", Params{
"group_id": groupID,
"user_id": userID,
"special_title": specialTitle,
})
}
// SetFriendAddRequest 处理加好友请求
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#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 (ctx *Ctx) SetFriendAddRequest(flag string, approve bool, remark string) {
ctx.CallAction("set_friend_add_request", Params{
"flag": flag,
"approve": approve,
"remark": remark,
})
}
// SetGroupAddRequest 处理加群请求/邀请
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#set_group_add_request-%E5%A4%84%E7%90%86%E5%8A%A0%E7%BE%A4%E8%AF%B7%E6%B1%82%E9%82%80%E8%AF%B7
func (ctx *Ctx) SetGroupAddRequest(flag string, subType string, approve bool, reason string) {
ctx.CallAction("set_group_add_request", Params{
"flag": flag,
"sub_type": subType,
"approve": approve,
"reason": reason,
})
}
// GetLoginInfo 获取登录号信息
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_login_info-%E8%8E%B7%E5%8F%96%E7%99%BB%E5%BD%95%E5%8F%B7%E4%BF%A1%E6%81%AF
func (ctx *Ctx) GetLoginInfo() gjson.Result {
return ctx.CallAction("get_login_info", Params{}).Data
}
// GetStrangerInfo 获取陌生人信息
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_stranger_info-%E8%8E%B7%E5%8F%96%E9%99%8C%E7%94%9F%E4%BA%BA%E4%BF%A1%E6%81%AF
func (ctx *Ctx) GetStrangerInfo(userID int64, noCache bool) gjson.Result {
return ctx.CallAction("get_stranger_info", Params{
"user_id": userID,
"no_cache": noCache,
}).Data
}
// GetFriendList 获取好友列表
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_friend_list-%E8%8E%B7%E5%8F%96%E5%A5%BD%E5%8F%8B%E5%88%97%E8%A1%A8
func (ctx *Ctx) GetFriendList() gjson.Result {
return ctx.CallAction("get_friend_list", Params{}).Data
}
// GetGroupInfo 获取群信息
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_group_info-%E8%8E%B7%E5%8F%96%E7%BE%A4%E4%BF%A1%E6%81%AF
func (ctx *Ctx) GetGroupInfo(groupID int64, noCache bool) Group {
rsp := ctx.CallAction("get_group_info", Params{
"group_id": groupID,
"no_cache": noCache,
}).Data
group := Group{}
_ = json.Unmarshal([]byte(rsp.Raw), &group)
return group
}
// GetGroupList 获取群列表
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_group_list-%E8%8E%B7%E5%8F%96%E7%BE%A4%E5%88%97%E8%A1%A8
func (ctx *Ctx) GetGroupList() gjson.Result {
return ctx.CallAction("get_group_list", Params{}).Data
}
// GetGroupMemberInfo 获取群成员信息
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_group_member_info-%E8%8E%B7%E5%8F%96%E7%BE%A4%E6%88%90%E5%91%98%E4%BF%A1%E6%81%AF
func (ctx *Ctx) GetGroupMemberInfo(groupID int64, userID int64, noCache bool) gjson.Result {
return ctx.CallAction("get_group_member_info", Params{
"group_id": groupID,
"user_id": userID,
"no_cache": noCache,
}).Data
}
// GetGroupMemberList 获取群成员列表
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_group_member_list-%E8%8E%B7%E5%8F%96%E7%BE%A4%E6%88%90%E5%91%98%E5%88%97%E8%A1%A8
func (ctx *Ctx) GetGroupMemberList(groupID int64) gjson.Result {
return ctx.CallAction("get_group_member_list", Params{
"group_id": groupID,
}).Data
}
// GetGroupHonorInfo 获取群荣誉信息
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_group_honor_info-%E8%8E%B7%E5%8F%96%E7%BE%A4%E8%8D%A3%E8%AA%89%E4%BF%A1%E6%81%AF
func (ctx *Ctx) GetGroupHonorInfo(groupID int64, hType string) gjson.Result {
return ctx.CallAction("get_group_honor_info", Params{
"group_id": groupID,
"type": hType,
}).Data
}
// GetRecord 获取语音
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_record-%E8%8E%B7%E5%8F%96%E8%AF%AD%E9%9F%B3
func (ctx *Ctx) GetRecord(file string, outFormat string) gjson.Result {
return ctx.CallAction("get_record", Params{
"file": file,
"out_format": outFormat,
}).Data
}
// GetImage 获取图片
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_image-%E8%8E%B7%E5%8F%96%E5%9B%BE%E7%89%87
func (ctx *Ctx) GetImage(file string) gjson.Result {
return ctx.CallAction("get_image", Params{
"file": file,
}).Data
}
// GetVersionInfo 获取运行状态
// https://github.com/howmanybots/onebot/blob/master/v11/specs/api/public.md#get_status-%E8%8E%B7%E5%8F%96%E8%BF%90%E8%A1%8C%E7%8A%B6%E6%80%81
func (ctx *Ctx) GetVersionInfo() gjson.Result {
return ctx.CallAction("get_version_info", Params{}).Data
}
// Expand API
// SetGroupPortrait 设置群头像
// https://github.com/Mrs4s/go-cqhttp/blob/master/docs/cqhttp.md#%E8%AE%BE%E7%BD%AE%E7%BE%A4%E5%A4%B4%E5%83%8F
func (ctx *Ctx) SetGroupPortrait(groupID int64, file string) {
ctx.CallAction("set_group_portrait", Params{
"group_id": groupID,
"file": file,
})
}
// OCRImage 图片OCR
// https://github.com/Mrs4s/go-cqhttp/blob/master/docs/cqhttp.md#%E5%9B%BE%E7%89%87ocr
func (ctx *Ctx) OCRImage(file string) gjson.Result {
return ctx.CallAction("ocr_image", Params{
"image": file,
}).Data
}
// SendGroupForwardMessage 发送合并转发(群)
// https://github.com/Mrs4s/go-cqhttp/blob/master/docs/cqhttp.md#%E5%9B%BE%E7%89%87ocr
func (ctx *Ctx) SendGroupForwardMessage(groupID int64, message message.Message) gjson.Result {
return ctx.CallAction("send_group_forward_msg", Params{
"group_id": groupID,
"messages": message,
}).Data
}
// GetGroupSystemMessage 获取群系统消息
// https://github.com/Mrs4s/go-cqhttp/blob/master/docs/cqhttp.md#%E8%8E%B7%E5%8F%96%E7%BE%A4%E7%B3%BB%E7%BB%9F%E6%B6%88%E6%81%AF
func (ctx *Ctx) GetGroupSystemMessage() gjson.Result {
return ctx.CallAction("get_group_system_msg", Params{}).Data
}
// GetWordSlices 获取中文分词
// https://github.com/Mrs4s/go-cqhttp/blob/master/docs/cqhttp.md#%E8%8E%B7%E5%8F%96%E4%B8%AD%E6%96%87%E5%88%86%E8%AF%8D
func (ctx *Ctx) GetWordSlices(content string) gjson.Result {
return ctx.CallAction(".get_word_slices", Params{
"content": content,
}).Data
}