This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
/
api.go
725 lines (661 loc) · 17.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
package gonebot
import (
"encoding/json"
log "github.com/sirupsen/logrus"
"github.com/tidwall/gjson"
)
type ApiParams map[string]interface{}
func (bot *Bot) CallApi(action string, params ApiParams) (*gjson.Result, error) {
log.Infof("正在调用接口%s", action)
rsp, err := bot.provider.Request(action, params)
if err != nil {
log.Errorf("调用接口%s失败: %s", action, err)
return nil, err
}
switch rsp := rsp.(type) {
case *gjson.Result:
return rsp, nil
case gjson.Result:
return &rsp, nil
case string:
if gjson.Valid(rsp) {
ret := gjson.Parse(rsp)
return &ret, nil
}
panic("返回的数据不是json格式")
case []byte:
if gjson.ValidBytes(rsp) {
ret := gjson.ParseBytes(rsp)
return &ret, nil
}
panic("返回的数据不是json格式")
default:
if rsp == nil {
return nil, nil
}
str, _ := json.Marshal(rsp)
ret := gjson.ParseBytes(str)
return &ret, nil
}
}
// 发送私聊消息
func (bot *Bot) SendPrivateMsg(userId int64, message Message, autoEscape bool) (int32, error) {
data, err := bot.CallApi("send_private_msg", ApiParams{
"user_id": userId,
"message": message.String(),
"auto_escape": autoEscape,
})
if err != nil {
return -1, err
}
messageId := int32(data.Get("message_id").Int())
return messageId, nil
}
// 发送群消息
func (bot *Bot) SendGroupMsg(groupId int64, message Message, autoEscape bool) (int32, error) {
data, err := bot.CallApi("send_group_msg", ApiParams{
"group_id": groupId,
"message": message.String(),
"auto_escape": autoEscape,
})
if err != nil {
return -1, err
}
messageId := int32(data.Get("message_id").Int())
return messageId, nil
}
// 发送消息
func (bot *Bot) SendMsg(messageType string, userId, groupId int64, message Message, autoEscape bool) (int32, error) {
params := ApiParams{
"message_type": messageType,
"message": message.String(),
"auto_escape": autoEscape,
}
switch messageType {
case "private":
params["user_id"] = userId
case "group":
params["group_id"] = groupId
default: // 纠正错误的消息类型
if groupId > 0 {
params["message_type"] = "group"
} else if userId > 0 {
params["message_type"] = "private"
} else {
return -1, ErrInvalidMessageType
}
}
data, err := bot.CallApi("send_msg", params)
if err != nil {
return -1, err
}
messageId := int32(data.Get("message_id").Int())
return messageId, nil
}
// 撤回消息
func (bot *Bot) DeleteMsg(messageId int32) error {
_, err := bot.CallApi("delete_msg", ApiParams{
"message_id": messageId,
})
return err
}
type ApiResponse_GetMsg struct {
Time int32
MessageType string
MessageId int32
RealId int32
privateSender *MessageEventSender
groupSender *GroupMessageEventSender
Message Message
}
func (r *ApiResponse_GetMsg) GetPrivateSender() *MessageEventSender {
return r.privateSender
}
func (r *ApiResponse_GetMsg) GetGroupSender() *GroupMessageEventSender {
return r.groupSender
}
// 获取消息
func (bot *Bot) GetMsg(messageId int32) (*ApiResponse_GetMsg, error) {
data, err := bot.CallApi("get_msg", ApiParams{
"message_id": messageId,
})
if err != nil {
return nil, err
}
rsp := &ApiResponse_GetMsg{
Time: int32(data.Get("time").Int()),
MessageType: data.Get("message_type").String(),
MessageId: int32(data.Get("message_id").Int()),
RealId: int32(data.Get("real_id").Int()),
Message: ConvertJsonArrayToMessage(data.Get("message").Array()),
}
sender := MessageEventSender{
UserId: data.Get("user_id").Int(),
Nickname: data.Get("nickname").String(),
Sex: data.Get("sex").String(),
Age: int32(data.Get("age").Int()),
}
switch rsp.MessageType {
case "private":
rsp.privateSender = &sender
case "group":
gsender := GroupMessageEventSender{
MessageEventSender: sender,
Card: data.Get("card").String(),
Area: data.Get("area").String(),
Level: data.Get("level").String(),
Role: data.Get("role").String(),
Title: data.Get("title").String(),
}
rsp.groupSender = &gsender
default:
return nil, ErrInvalidMessageType
}
return rsp, nil
}
// 获取合并转发消息
func (bot *Bot) GetForwardMsg(messageId int32) (*Message, error) {
data, err := bot.CallApi("get_forward_msg", ApiParams{
"message_id": messageId,
})
if err != nil {
return nil, err
}
ret := ConvertJsonArrayToMessage(data.Get("message").Array())
return &ret, nil
}
// 发送好友赞
func (bot *Bot) SendLike(userId int64, times int) error {
_, err := bot.CallApi("send_like", ApiParams{
"user_id": userId,
"times": times,
})
return err
}
// 群组踢人
func (bot *Bot) SetGroupKick(groupId int64, userId int64, rejectAddRequest bool) error {
_, err := bot.CallApi("set_group_kick", ApiParams{
"group_id": groupId,
"user_id": userId,
"reject_add_request": rejectAddRequest,
})
return err
}
// 群组单人禁言
func (bot *Bot) SetGroupBan(groupId int64, userId int64, duration int) error {
_, err := bot.CallApi("set_group_ban", ApiParams{
"group_id": groupId,
"user_id": userId,
"duration": duration,
})
return err
}
// 群组匿名用户禁言
func (bot *Bot) SetGroupAnonymousBan(groupId int64, anonymous *Anonymous, anonymousFlag string, duration int) error {
params := ApiParams{
"group_id": groupId,
"duration": duration,
}
if anonymous != nil {
params["anonymous"] = anonymous
} else if anonymousFlag != "" {
params["anonymous_flag"] = anonymousFlag
} else {
return ErrMissingAnonymous
}
_, err := bot.CallApi("set_group_anonymous_ban", params)
return err
}
// 群组全员禁言
func (bot *Bot) SetGroupWholeBan(groupId int64, enable bool) error {
_, err := bot.CallApi("set_group_whole_ban", ApiParams{
"group_id": groupId,
"enable": enable,
})
return err
}
// 群组设置管理员
func (bot *Bot) SetGroupAdmin(groupId int64, userId int64, enable bool) error {
_, err := bot.CallApi("set_group_admin", ApiParams{
"group_id": groupId,
"user_id": userId,
"enable": enable,
})
return err
}
// 群组开关匿名
func (bot *Bot) SetGroupAnonymous(groupId int64, enable bool) error {
_, err := bot.CallApi("set_group_anonymous", ApiParams{
"group_id": groupId,
"enable": enable,
})
return err
}
// 设置群名片(群备注)
func (bot *Bot) SetGroupCard(groupId int64, userId int64, card string) error {
_, err := bot.CallApi("set_group_card", ApiParams{
"group_id": groupId,
"user_id": userId,
"card": card,
})
return err
}
// 设置群名
func (bot *Bot) SetGroupName(groupId int64, groupName string) error {
_, err := bot.CallApi("set_group_name", ApiParams{
"group_id": groupId,
"group_name": groupName,
})
return err
}
// 退出群组
func (bot *Bot) SetGroupLeave(groupId int64) error {
_, err := bot.CallApi("set_group_leave", ApiParams{
"group_id": groupId,
})
return err
}
// 解散群组
func (bot *Bot) SetGroupDismiss(groupId int64) error {
_, err := bot.CallApi("set_group_leave", ApiParams{
"group_id": groupId,
"is_dismiss": true,
})
return err
}
// 设置群组专属头衔
func (bot *Bot) SetGroupSpecialTitle(groupId int64, userId int64, specialTitle string, duration int) error {
_, err := bot.CallApi("set_group_special_title", ApiParams{
"group_id": groupId,
"user_id": userId,
"special_title": specialTitle,
"duration": duration,
})
return err
}
// 处理加好友请求
func (bot *Bot) SetFriendAddRequest(flag string, approve bool, remark string) error {
_, err := bot.CallApi("set_friend_add_request", ApiParams{
"flag": flag,
"approve": approve,
"remark": remark,
})
return err
}
// 处理加群请求/邀请
func (bot *Bot) SetGroupAddRequest(flag string, subType int, approve bool, reason string) error {
_, err := bot.CallApi("set_group_add_request", ApiParams{
"flag": flag,
"sub_type": subType,
"approve": approve,
"reason": reason,
})
return err
}
type LoginInfo struct {
UserId int64
Nickname string
}
// 获取登录号信息
func (bot *Bot) GetLoginInfo() (*LoginInfo, error) {
resp, err := bot.CallApi("get_login_info", nil)
if err != nil {
return nil, err
}
info := LoginInfo{
UserId: resp.Get("user_id").Int(),
Nickname: resp.Get("nickname").String(),
}
return &info, nil
}
type StrangerInfo struct {
UserId int64
Nickname string
Sex string
Age int32
}
// 获取陌生人信息
func (bot *Bot) GetStrangerInfo(userId int64) (*StrangerInfo, error) {
resp, err := bot.CallApi("get_stranger_info", ApiParams{
"user_id": userId,
})
if err != nil {
return nil, err
}
info := StrangerInfo{
UserId: resp.Get("user_id").Int(),
Nickname: resp.Get("nickname").String(),
Sex: resp.Get("sex").String(),
Age: int32(resp.Get("age").Int()),
}
return &info, nil
}
type FriendInfo struct {
UserId int64
Nickname string
Remark string
}
// 获取好友列表
func (bot *Bot) GetFriendList() (*[]FriendInfo, error) {
resp, err := bot.CallApi("get_friend_list", nil)
if err != nil {
return nil, err
}
var friends []FriendInfo
for _, v := range resp.Get("friends").Array() {
friend := FriendInfo{
UserId: v.Get("user_id").Int(),
Nickname: v.Get("nickname").String(),
Remark: v.Get("remark").String(),
}
friends = append(friends, friend)
}
return &friends, nil
}
type GroupInfo struct {
GroupId int64
GroupName string
MemberCount int
MaxMemberCount int
}
// 获取群信息
func (bot *Bot) GetGroupInfo(groupId int64) (*GroupInfo, error) {
resp, err := bot.CallApi("get_group_info", ApiParams{
"group_id": groupId,
})
if err != nil {
return nil, err
}
info := GroupInfo{
GroupId: resp.Get("group_id").Int(),
GroupName: resp.Get("group_name").String(),
MemberCount: int(resp.Get("member_count").Int()),
MaxMemberCount: int(resp.Get("max_member_count").Int()),
}
return &info, nil
}
// 获取群列表
func (bot *Bot) GetGroupList() (*[]GroupInfo, error) {
resp, err := bot.CallApi("get_group_list", nil)
if err != nil {
return nil, err
}
var groups []GroupInfo
for _, v := range resp.Array() {
group := GroupInfo{
GroupId: v.Get("group_id").Int(),
GroupName: v.Get("group_name").String(),
MemberCount: int(v.Get("member_count").Int()),
MaxMemberCount: int(v.Get("max_member_count").Int()),
}
groups = append(groups, group)
}
return &groups, nil
}
type GroupMemberInfo struct {
GroupId int64
UserId int64
Nickname string
Card string
Sex string
Age int
Area string
JoinTime int
LastSentTime int
Level string
Role string
Unfriendly bool
Title string
TitleExpireTime int
CardChangeable bool
}
// 获取群成员信息
func (bot *Bot) GetGroupMemberInfo(groupId int64, userId int64) (*GroupMemberInfo, error) {
resp, err := bot.CallApi("get_group_member_info", ApiParams{
"group_id": groupId,
"user_id": userId,
})
if err != nil {
return nil, err
}
info := GroupMemberInfo{
GroupId: resp.Get("group_id").Int(),
UserId: resp.Get("user_id").Int(),
Nickname: resp.Get("nickname").String(),
Card: resp.Get("card").String(),
Sex: resp.Get("sex").String(),
Age: int(resp.Get("age").Int()),
Area: resp.Get("area").String(),
JoinTime: int(resp.Get("join_time").Int()),
LastSentTime: int(resp.Get("last_sent_time").Int()),
Level: resp.Get("level").String(),
Role: resp.Get("role").String(),
Unfriendly: resp.Get("unfriendly").Bool(),
Title: resp.Get("title").String(),
TitleExpireTime: int(resp.Get("title_expire_time").Int()),
CardChangeable: resp.Get("card_changeable").Bool(),
}
return &info, nil
}
// 获取群成员列表
func (bot *Bot) GetGroupMemberList(groupId int64) (*[]GroupMemberInfo, error) {
resp, err := bot.CallApi("get_group_member_list", ApiParams{
"group_id": groupId,
})
if err != nil {
return nil, err
}
var members []GroupMemberInfo
for _, v := range resp.Get("members").Array() {
member := GroupMemberInfo{
GroupId: v.Get("group_id").Int(),
UserId: v.Get("user_id").Int(),
Nickname: v.Get("nickname").String(),
Card: v.Get("card").String(),
Sex: v.Get("sex").String(),
Age: int(v.Get("age").Int()),
Area: v.Get("area").String(),
JoinTime: int(v.Get("join_time").Int()),
LastSentTime: int(v.Get("last_sent_time").Int()),
Level: v.Get("level").String(),
Role: v.Get("role").String(),
Unfriendly: v.Get("unfriendly").Bool(),
Title: v.Get("title").String(),
TitleExpireTime: int(v.Get("title_expire_time").Int()),
CardChangeable: v.Get("card_changeable").Bool(),
}
members = append(members, member)
}
return &members, nil
}
type CurrentTalkative struct {
UserId int64
Nickname string
Avatar string
DayCount int
}
type HonorListItem struct {
UserId int64
Nickname string
Avatar string
Description string
}
type GroupHonorInfo struct {
GroupId int64
CurrentTalkative *CurrentTalkative
TalkativeList []HonorListItem
PerformerList []HonorListItem
LegendList []HonorListItem
StrongNewbieList []HonorListItem
EmotionList []HonorListItem
}
// 获取群荣誉信息
func (bot *Bot) GetGroupHonorInfo(groupId int64, type_ string) (*GroupHonorInfo, error) {
resp, err := bot.CallApi("get_group_honor_info", ApiParams{
"group_id": groupId,
"type": type_,
})
if err != nil {
return nil, err
}
info := GroupHonorInfo{
GroupId: resp.Get("group_id").Int(),
}
if type_ == "current_talkative" {
talk := CurrentTalkative{
UserId: resp.Get("user_id").Int(),
Nickname: resp.Get("nickname").String(),
Avatar: resp.Get("avatar").String(),
DayCount: int(resp.Get("day_count").Int()),
}
info.CurrentTalkative = &talk
return &info, nil
}
key := type_ + "_list"
var honorList []HonorListItem
for _, v := range resp.Get(key).Array() {
item := HonorListItem{
UserId: v.Get("user_id").Int(),
Nickname: v.Get("nickname").String(),
Avatar: v.Get("avatar").String(),
Description: v.Get("description").String(),
}
honorList = append(honorList, item)
}
switch type_ {
case "talkative_list":
info.TalkativeList = honorList
case "performer_list":
info.PerformerList = honorList
case "legend_list":
info.LegendList = honorList
case "strong_newbie_list":
info.StrongNewbieList = honorList
case "emotion_list":
info.EmotionList = honorList
}
return &info, nil
}
// 获取 Cookies
func (bot *Bot) GetCookies(domain string) (string, error) {
resp, err := bot.CallApi("get_cookies", ApiParams{
"domain": domain,
})
if err != nil {
return "", err
}
return resp.Get("cookies").String(), nil
}
// 获取 CSRF Token
func (bot *Bot) GetCsrfToken() (int32, error) {
resp, err := bot.CallApi("get_csrf_token", nil)
if err != nil {
return 0, err
}
return int32(resp.Get("csrf_token").Int()), nil
}
type QQCredential struct {
Cookies string
CsrfToken int32
}
// 获取 QQ 相关接口凭证
func (bot *Bot) GetCredentials(domain string) (*QQCredential, error) {
resp, err := bot.CallApi("get_credentials", nil)
if err != nil {
return nil, err
}
return &QQCredential{
Cookies: resp.Get("cookies").String(),
CsrfToken: int32(resp.Get("csrf_token").Int()),
}, nil
}
// 获取语音
func (bot *Bot) GetRecord(file string, outFormat string) (string, error) {
resp, err := bot.CallApi("get_record", ApiParams{
"file": file,
"out_format": outFormat,
})
if err != nil {
return "", err
}
return resp.Get("file").String(), nil
}
// 获取图片
func (bot *Bot) GetImage(file string) (string, error) {
resp, err := bot.CallApi("get_image", ApiParams{
"file": file,
})
if err != nil {
return "", err
}
return resp.Get("file").String(), nil
}
// 检查是否可以发送图片
func (bot *Bot) CanSendImage() (bool, error) {
resp, err := bot.CallApi("can_send_image", nil)
if err != nil {
return false, err
}
return resp.Get("yes").Bool(), nil
}
// 检查是否可以发送语音
func (bot *Bot) CanSendRecord() (bool, error) {
resp, err := bot.CallApi("can_send_record", nil)
if err != nil {
return false, err
}
return resp.Get("yes").Bool(), nil
}
type Status struct {
Online bool
Good bool
}
// 获取运行状态
func (bot *Bot) GetStatus() (*Status, error) {
resp, err := bot.CallApi("get_status", nil)
if err != nil {
return nil, err
}
return &Status{
Online: resp.Get("online").Bool(),
Good: resp.Get("good").Bool(),
}, nil
}
type VesionInfo struct {
AppName string
AppVersion string
ProtocolVersion string
}
// 获取版本信息
func (bot *Bot) GetVersionInfo() (*VesionInfo, error) {
resp, err := bot.CallApi("get_version_info", nil)
if err != nil {
return nil, err
}
return &VesionInfo{
AppName: resp.Get("app_name").String(),
AppVersion: resp.Get("app_version").String(),
ProtocolVersion: resp.Get("protocol_version").String(),
}, nil
}
// 重启 OneBot 实现
func (bot *Bot) SetRestart(delay int) error {
_, err := bot.CallApi("set_restart", ApiParams{
"delay": delay,
})
return err
}
// 清理缓存
func (bot *Bot) ClearCache() error {
_, err := bot.CallApi("clear_cache", nil)
return err
}
type quickOperationParams map[string]interface{}
// 对事件执行快速操作
func (bot *Bot) handleQuickOperation(context interface{}, operation quickOperationParams) error {
_, err := bot.CallApi(".handle_quick_operation", ApiParams{
"context": context,
"operation": operation,
})
return err
}