This repository has been archived by the owner on Jul 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
main.go
1438 lines (1375 loc) · 40.1 KB
/
main.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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package OPQBot
import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"math/big"
"os"
"os/exec"
"os/signal"
"reflect"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/sirupsen/logrus"
nested "github.com/antonfisher/nested-logrus-formatter"
"github.com/goinggo/mapstructure"
gosocketio "github.com/mcoo/OPQBot/golang-socketio-edit"
"github.com/mcoo/OPQBot/golang-socketio-edit/transport"
"github.com/mcoo/OPQBot/session"
_ "github.com/mcoo/OPQBot/session/provider"
"github.com/mcoo/requests"
)
type BotManager struct {
QQ int64
SendChan chan SendMsgPack
Running bool
OPQUrl string
MaxRetryCount int
Done chan int
wg sync.WaitGroup
myRecord map[string]MyRecord
myRecordLocker sync.RWMutex
onEvent map[string][][]reflect.Value
middleware []middleware
delayed int
locker sync.RWMutex
restart chan int
Session *session.Manager
}
type middleware struct {
priority int
fun func(m map[string]interface{}) map[string]interface{}
}
func (b *BotManager) SetMaxRetryCount(maxRetryCount int) {
b.MaxRetryCount = maxRetryCount
}
var interrupt chan os.Signal
func ParserGroupAtMsg(pack GroupMsgPack) (a AtMsg, e error) {
if pack.MsgType != "AtMsg" {
e = errors.New("Not AtMsg. ")
return
}
e = json.Unmarshal([]byte(pack.Content), &a)
if e != nil {
return
}
return
}
func (a AtMsg) Clean() AtMsg {
for _, v := range a.UserExt {
a.Content = strings.TrimSpace(strings.ReplaceAll(a.Content, "@"+v.QQNick, ""))
}
return a
}
func ParserGroupReplyMsg(pack GroupMsgPack) (a Reply, e error) {
// Why OPQbot returns "replay" ...???
if pack.MsgType != "ReplayMsg" {
e = errors.New("Not ReplyMsg. ")
return
}
e = json.Unmarshal([]byte(pack.Content), &a)
if e != nil {
return
}
return
}
func ParserGroupPicMsg(pack GroupMsgPack) (a PicMsg, e error) {
if pack.MsgType != "PicMsg" {
e = errors.New("Not PicMsg. ")
return
}
e = json.Unmarshal([]byte(pack.Content), &a)
if e != nil {
return
}
return
}
func ParserGroupFileMsg(pack GroupMsgPack) (a GroupFileMsg, e error) {
if pack.MsgType != "GroupFileMsg" {
e = errors.New("Not GroupFileMsg. ")
return
}
e = json.Unmarshal([]byte(pack.Content), &a)
if e != nil {
return
}
return
}
func ParserVideoMsg(pack GroupMsgPack) (a VideoMsg, e error) {
if pack.MsgType != "VideoMsg" {
e = errors.New("Not VideoMsg. ")
return
}
e = json.Unmarshal([]byte(pack.Content), &a)
if e != nil {
return
}
return
}
func (b *BotManager) Wait() {
home:
b.wg.Wait()
if b.MaxRetryCount > 0 {
for i := 0; i < b.MaxRetryCount; i++ {
log.Info("等待重试,要终止请按下Ctrl+C")
select {
case <-b.Done:
b.Running = false
log.Info("Bot结束")
return
case <-time.After(5 * time.Second):
log.Warn("正在重连")
}
log.Warningf("重连尝试第%d/%d次\n", i+1, b.MaxRetryCount)
err := b.Start()
if err != nil {
log.Error(err)
} else {
goto home
}
}
}
b.Running = false
log.Info("Bot结束")
}
// VoiceMp3ToSilk Mp3转Silk mp3->silk Output: base64 String
func VoiceMp3ToSilk(mp3Path string) (string, error) {
n, _ := rand.Int(rand.Reader, big.NewInt(100000))
name := n.String()
pcmFile := name + ".tmp"
silkFile := name + ".silk"
cmd := exec.Command("./ffmpeg", "-i", mp3Path, "-ac", "1", "-ar", "24000", "-f", "s16le", pcmFile)
var stderr bytes.Buffer
_, err := cmd.CombinedOutput()
if err != nil {
return "", err
}
defer os.Remove(pcmFile)
cmd = exec.Command("./encoder", pcmFile, silkFile, "-tencent")
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil { //获取输出对象,可以从该对象中读取输出结果
return "", err
}
defer os.Remove(silkFile)
tresult, _ := ioutil.ReadFile(silkFile)
return base64.StdEncoding.EncodeToString(tresult), nil
}
// VoiceSilkToMp3 Silk转Mp3 silk->mp3 Output: []byte
func VoiceSilkToMp3(base64EncodedSilk string) ([]byte, error) {
decodeBytes, err := base64.StdEncoding.DecodeString(base64EncodedSilk)
if err != nil {
return decodeBytes, errors.New("转码失败! ")
}
n, _ := rand.Int(rand.Reader, big.NewInt(100000))
name := n.String()
mp3file := "./" + name + ".mp3"
pcmfile := "./" + name + ".pcm"
silk := "./" + name + ".silk"
err = ioutil.WriteFile(silk, decodeBytes, os.FileMode(0777))
if err != nil {
return decodeBytes, errors.New("写文件失败!")
}
defer os.Remove(silk)
var stderr bytes.Buffer
cmd := exec.Command("./decoder", silk, pcmfile)
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
return decodeBytes, errors.New("silk转pcm失败! ")
}
cmd = exec.Command("./ffmpeg", "-f", "s16le", "-ar", "24000", "-i", pcmfile, "-ac", "1", mp3file)
_, err = cmd.CombinedOutput()
if err != nil {
return decodeBytes, errors.New("pcm转mp3失败! ")
}
defer os.Remove(mp3file)
tresult, _ := ioutil.ReadFile(mp3file)
defer os.Remove(mp3file)
return tresult, nil
}
var log *logrus.Entry
func init() {
interrupt = make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, os.Kill)
l := logrus.New()
l.SetLevel(logrus.InfoLevel)
l.SetFormatter(&nested.Formatter{
HideKeys: true,
FieldsOrder: []string{"component", "category"},
TimestampFormat: "2006-01-02 15:04:05",
})
log = l.WithField("Core", "OPQBOT")
}
func SetLog(l *logrus.Entry) {
log = l
}
func NewBotManager(QQ int64, OPQUrl string) *BotManager {
s, err := session.NewManager("qq", 3600)
if err != nil {
panic(err)
}
go s.GC()
b := &BotManager{restart: make(chan int, 1), Session: s, Done: make(chan int, 10), MaxRetryCount: 10, wg: sync.WaitGroup{}, QQ: QQ, OPQUrl: OPQUrl, SendChan: make(chan SendMsgPack, 1024), onEvent: make(map[string][][]reflect.Value), myRecord: map[string]MyRecord{}, myRecordLocker: sync.RWMutex{}, locker: sync.RWMutex{}, delayed: 1000}
go func() {
for {
select {
case <-interrupt:
log.Info("程序被用户终止,正在进行释放资源操作!")
b.MaxRetryCount = 0
b.Done <- 0
b.Done <- 0
b.Done <- 0
case <-b.restart:
log.Warn("程序重连尝试!")
b.Done <- 1
b.Done <- 2
}
}
}()
return b
}
// SetSendDelayed 设置发送消息的时延 单位毫秒 默认1000
func (b *BotManager) SetSendDelayed(Millisecond int) {
b.delayed = Millisecond
}
// Start 开始连接
func (b *BotManager) Start() error {
b.Running = true
b.wg.Add(2)
go b.receiveSendPack()
go func() {
for {
select {
case <-b.Done:
b.wg.Done()
return
case <-time.After(10 * time.Second):
go func() {
if len(b.myRecord) > 50 {
b.myRecordLocker.Lock()
for i, v := range b.myRecord {
if time.Since(time.Unix(int64(v.MsgTime), 0)) > time.Second*180 {
delete(b.myRecord, i)
}
}
b.myRecordLocker.Unlock()
}
}()
}
}
}()
c, err := gosocketio.Dial(strings.ReplaceAll(b.OPQUrl, "http://", "ws://")+"/socket.io/?EIO=3&transport=websocket", transport.GetDefaultWebsocketTransport())
if err != nil {
b.restart <- 1
return err
}
err = c.On(gosocketio.OnConnection, func(h *gosocketio.Channel) {
// log.Println("连接成功!")
f, ok := b.onEvent[EventNameOnConnected]
if ok && len(f) >= 1 {
f[0][0].Call([]reflect.Value{})
}
})
if err != nil {
b.restart <- 1
return err
}
err = c.On(gosocketio.OnDisconnection, func(h *gosocketio.Channel) {
// log.Println("连接断开!")
f, ok := b.onEvent[EventNameOnDisconnected]
if ok && len(f) >= 1 {
f[0][0].Call([]reflect.Value{})
}
b.restart <- 1
})
if err != nil {
b.restart <- 1
return err
}
err = c.On("OnGroupMsgs", func(h *gosocketio.Channel, args returnPack) {
//log.Println(args)
if args.CurrentQQ != b.QQ {
return
}
b.locker.RLock()
defer b.locker.RUnlock()
f, ok := b.onEvent["OnGroupMsgs"]
if ok && len(f) >= 1 {
result := GroupMsgPack{}
err = mapstructure.Decode(args.CurrentPacket.Data, &result)
if err != nil {
log.Error("解析包错误")
return
}
reg1, _ := regexp.Compile(`\[([0-9]{1,5})\]`)
id := reg1.FindStringSubmatch(result.Content)
if result.FromUserID == b.QQ && len(id) > 1 {
go func() {
record := MyRecord{
FromGroupID: result.FromGroupID,
MsgRandom: result.MsgRandom,
MsgSeq: result.MsgSeq,
MsgTime: result.MsgTime,
MsgType: result.MsgType,
Content: result.Content,
}
b.myRecordLocker.Lock()
b.myRecord[id[1]] = record
b.myRecordLocker.Unlock()
}()
}
for _, v := range f {
if result.Ban {
return
}
result.Bot = b
result.f = v
result.NowIndex = 0
result.MaxIndex = len(v) - 1
v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)})
}
}
//log.Println(args)
})
if err != nil {
return err
}
err = c.On("OnFriendMsgs", func(h *gosocketio.Channel, args returnPack) {
if args.CurrentQQ != b.QQ {
return
}
b.locker.RLock()
defer b.locker.RUnlock()
f, ok := b.onEvent["OnFriendMsgs"]
if ok && len(f) >= 1 {
result := FriendMsgPack{}
err = mapstructure.Decode(args.CurrentPacket.Data, &result)
if err != nil {
log.Error("解析包错误")
return
}
for _, v := range f {
if result.Ban {
return
}
result.Bot = b
result.f = v
result.NowIndex = 0
result.MaxIndex = len(v) - 1
v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)})
}
}
//log.Println(args)
})
if err != nil {
b.restart <- 1
return err
}
err = c.On("OnEvents", func(h *gosocketio.Channel, args eventPack) {
if args.CurrentQQ != b.QQ {
return
}
e, ok := args.CurrentPacket.Data.(map[string]interface{})
if !ok {
log.Error("解析出错")
return
}
e1, ok := e["EventName"].(string)
if !ok {
log.Error("解析出错")
return
}
switch e1 {
case EventNameOnGroupJoin:
b.locker.RLock()
defer b.locker.RUnlock()
f, ok := b.onEvent[EventNameOnGroupJoin]
if ok && len(f) >= 1 {
result := GroupJoinPack{}
err = mapstructure.Decode(args.CurrentPacket.Data, &result)
if err != nil {
log.Error("解析包错误")
return
}
for _, v := range f {
if result.Ban {
return
}
result.Bot = b
result.f = v
result.NowIndex = 0
result.MaxIndex = len(v) - 1
v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)})
}
}
case EventNameOnGroupAdmin:
b.locker.RLock()
defer b.locker.RUnlock()
f, ok := b.onEvent[EventNameOnGroupAdmin]
if ok && len(f) >= 1 {
result := GroupAdminPack{}
err = mapstructure.Decode(args.CurrentPacket.Data, &result)
if err != nil {
log.Error("解析包错误")
return
}
for _, v := range f {
if result.Ban {
return
}
result.Bot = b
result.f = v
result.NowIndex = 0
result.MaxIndex = len(v) - 1
v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)})
}
}
case EventNameOnGroupExit:
b.locker.RLock()
defer b.locker.RUnlock()
f, ok := b.onEvent[EventNameOnGroupExit]
if ok && len(f) >= 1 {
result := GroupExitPack{}
err = mapstructure.Decode(args.CurrentPacket.Data, &result)
if err != nil {
log.Error("解析包错误")
return
}
for _, v := range f {
if result.Ban {
return
}
result.Bot = b
result.f = v
result.NowIndex = 0
result.MaxIndex = len(v) - 1
v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)})
}
}
case EventNameOnGroupExitSuccess:
b.locker.RLock()
defer b.locker.RUnlock()
f, ok := b.onEvent[EventNameOnGroupExitSuccess]
if ok && len(f) >= 1 {
result := GroupExitSuccessPack{}
err = mapstructure.Decode(args.CurrentPacket.Data, &result)
if err != nil {
log.Error("解析包错误")
return
}
for _, v := range f {
if result.Ban {
return
}
result.Bot = b
result.f = v
result.NowIndex = 0
result.MaxIndex = len(v) - 1
v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)})
}
}
case EventNameOnGroupAdminSysNotify:
b.locker.RLock()
defer b.locker.RUnlock()
f, ok := b.onEvent[EventNameOnGroupAdminSysNotify]
if ok && len(f) >= 1 {
result := GroupAdminSysNotifyPack{}
err = mapstructure.Decode(args.CurrentPacket.Data, &result)
if err != nil {
log.Error("解析包错误")
return
}
for _, v := range f {
if result.Ban {
return
}
result.Bot = b
result.f = v
result.NowIndex = 0
result.MaxIndex = len(v) - 1
v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)})
}
}
case EventNameOnGroupRevoke:
b.locker.RLock()
defer b.locker.RUnlock()
f, ok := b.onEvent[EventNameOnGroupRevoke]
if ok && len(f) >= 1 {
result := GroupRevokePack{}
err = mapstructure.Decode(args.CurrentPacket.Data, &result)
if err != nil {
log.Error("解析包错误")
return
}
for _, v := range f {
if result.Ban {
return
}
result.Bot = b
result.f = v
result.NowIndex = 0
result.MaxIndex = len(v) - 1
v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)})
}
}
case EventNameOnGroupShut:
b.locker.RLock()
defer b.locker.RUnlock()
f, ok := b.onEvent[EventNameOnGroupShut]
if ok && len(f) >= 1 {
result := GroupShutPack{}
err = mapstructure.Decode(args.CurrentPacket.Data, &result)
if err != nil {
log.Error("解析包错误")
return
}
for _, v := range f {
if result.Ban {
return
}
result.Bot = b
result.f = v
result.NowIndex = 0
result.MaxIndex = len(v) - 1
v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)})
}
}
case EventNameOnGroupSystemNotify:
b.locker.RLock()
defer b.locker.RUnlock()
f, ok := b.onEvent[EventNameOnGroupSystemNotify]
if ok && len(f) >= 1 {
result := GroupSystemNotifyPack{}
err = mapstructure.Decode(args.CurrentPacket.Data, &result)
if err != nil {
log.Error("解析包错误")
return
}
for _, v := range f {
if result.Ban {
return
}
result.Bot = b
result.f = v
result.NowIndex = 0
result.MaxIndex = len(v) - 1
v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(&result)})
}
}
default:
b.locker.RLock()
defer b.locker.RUnlock()
f, ok := b.onEvent[EventNameOnOther]
if ok && len(f) >= 1 {
for _, v := range f {
v[0].Call([]reflect.Value{reflect.ValueOf(args.CurrentQQ), reflect.ValueOf(args)})
}
}
}
})
if err != nil {
b.restart <- 1
return err
}
return nil
}
// Send 发送消息函数
func (b *BotManager) Send(sendMsgPack SendMsgPack) {
select {
case b.SendChan <- sendMsgPack:
default:
}
}
// Stop 停止
func (b *BotManager) Stop() {
if !b.Running {
return
}
b.Running = false
close(b.SendChan)
}
// ReCallMsg 撤回消息
func (b *BotManager) ReCallMsg(GroupID, MsgRandom int64, MsgSeq int) error {
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=PbMessageSvc.PbMsgWithDraw&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"GroupID": GroupID, "MsgSeq": MsgSeq, "MsgRandom": MsgRandom})
if err != nil {
// log.Println(err.Error())
return err
}
var result Result
err = res.Json(&result)
if err != nil {
return err
}
if result.Ret != 0 {
return errors.New("Error 撤回失败")
} else {
return nil
}
}
// RefreshKey 刷新Key
func (b *BotManager) RefreshKey() error {
res, err := requests.Get(b.OPQUrl + "/v1/RefreshKeys?qq=" + strconv.FormatInt(b.QQ, 10))
if err != nil {
// log.Println(err.Error())
return err
}
var result Result
err = res.Json(&result)
if err != nil {
return err
}
if result.Ret != 0 {
return errors.New("Error 刷新Key失败")
} else {
return nil
}
}
// Announce 发公告 Pinned 1为置顶,0为普通公告 announceType 发布类型(10为使用弹窗公告,20为发送给新成员,其他暂未知)
func (b *BotManager) Announce(title, text string, pinned, announceType int, groupID int64) error {
var result Result
res, err := requests.PostJson(b.OPQUrl+"/v1/Group/Announce?qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"GroupID": groupID, "Title": title, "Text": text, "Pinned": pinned, "Type": announceType})
if err != nil {
return err
}
err = res.Json(&result)
if err != nil {
return err
}
if result.Ret != 0 {
return errors.New(result.Msg)
}
return nil
}
// UploadFileWithBase64 上传群文件
func (b *BotManager) UploadFileWithBase64(FileName, FileBase64 string, ToUserUid int64, Notify bool) error {
var result Result
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=SendMsgV2&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"ToUserUid": ToUserUid, "Notify": Notify, "FileName": FileName, "FileBase64": FileBase64, "SendMsgType": "UploadGroupFile"})
if err != nil {
return err
}
err = res.Json(&result)
if err != nil {
return err
}
if result.Ret != 0 {
return errors.New(result.Msg)
}
return nil
}
// UploadFileWithFileUrl 上传群文件
func (b *BotManager) UploadFileWithFileUrl(FileName, FileUrl string, ToUserUid int64, Notify bool) error {
var result Result
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=SendMsgV2&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"ToUserUid": ToUserUid, "Notify": Notify, "FileName": FileName, "FileUrl": FileUrl, "SendMsgType": "UploadGroupFile"})
if err != nil {
return err
}
err = res.Json(&result)
if err != nil {
return err
}
if result.Ret != 0 {
return errors.New(result.Msg)
}
return nil
}
// UploadFileWithFilePath 上传群文件
func (b *BotManager) UploadFileWithFilePath(FilePath string, ToUserUid int64, Notify bool) error {
var result Result
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=SendMsgV2&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"ToUserUid": ToUserUid, "Notify": Notify, "FilePath": FilePath, "SendMsgType": "UploadGroupFile"})
if err != nil {
return err
}
err = res.Json(&result)
if err != nil {
return err
}
if result.Ret != 0 {
return errors.New(result.Msg)
}
return nil
}
// Chuo 戳戳 sendType 0戳好友 1戳群友 sendType=0 时可以不填此字段 sendType=1 时不能为空
func (b *BotManager) Chuo(sendType int, groupID, userId int64) error {
var result Result
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=OidbSvc.0xed3_1&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"GroupID": groupID, "UserID": userId, "type": sendType})
if err != nil {
return err
}
err = res.Json(&result)
if err != nil {
return err
}
if result.Ret != 0 {
return errors.New(result.Msg)
}
return nil
}
// SetAdmin 设置管理员 flag 1为设置管理员 2为取消管理员
func (b *BotManager) SetAdmin(flag int, groupID, userId int64) error {
var result Result
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=OidbSvc.0x55c_1&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"GroupID": groupID, "UserID": userId, "Flag": flag})
if err != nil {
return err
}
err = res.Json(&result)
if err != nil {
return err
}
if result.Ret != 0 {
return errors.New(result.Msg)
}
return nil
}
// GetUserInfo 获取用户信息
func (b *BotManager) GetUserInfo(qq int64) (UserInfo, error) {
var result UserInfo
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=GetUserInfo&qq="+strconv.FormatInt(b.QQ, 10), map[string]int64{"UserID": qq})
if err != nil {
// log.Println(err.Error())
return result, err
}
err = res.Json(&result)
if err != nil {
return result, err
}
return result, nil
}
// GetUserCookie 获取QQ相关ck
func (b *BotManager) GetUserCookie() (Cookie, error) {
var result Cookie
res, err := requests.Get(b.OPQUrl + "/v1/LuaApiCaller?funcname=GetUserCook&qq=" + strconv.FormatInt(b.QQ, 10))
if err != nil {
// log.Println(err.Error())
return result, err
}
err = res.Json(&result)
if err != nil {
return result, err
}
return result, nil
}
// KickGroupMember 踢出群成员
func (b *BotManager) KickGroupMember(groupID, userId int64) error {
var result Result
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=GroupMgr&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"GroupID": groupID, "ActionUserID": userId, "ActionType": 3, "Content": ""})
if err != nil {
return err
}
err = res.Json(&result)
if err != nil {
return err
}
if result.Ret != 0 {
return errors.New(result.Msg)
}
return nil
}
// GetGroupMemberList 获取群成员列表
func (b *BotManager) GetGroupMemberList(groupID, LastUin int64) (GroupMemberList, error) {
var result GroupMemberList
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=friendlist.GetTroopMemberListReq&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"GroupUin": groupID, "LastUin": LastUin})
if err != nil {
return result, err
}
err = res.Json(&result)
if err != nil {
return result, err
}
return result, nil
}
// SetGroupNewNick 设置群名片
func (b *BotManager) SetGroupNewNick(newNick string, groupID, userId int64) error {
var result Result
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=friendlist.ModifyGroupCardReq&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"GroupID": groupID, "UserID": userId, "NewNick": newNick})
if err != nil {
return err
}
err = res.Json(&result)
if err != nil {
return err
}
if result.Ret != 0 {
return errors.New(result.Msg)
}
return nil
}
// SetGroupUniqueTitle 设置群头衔
func (b *BotManager) SetGroupUniqueTitle(newNick string, groupID, userId int64) error {
var result Result
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=OidbSvc.0x8fc_2&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"GroupID": groupID, "UserID": userId, "NewNick": newNick})
if err != nil {
return err
}
err = res.Json(&result)
if err != nil {
return err
}
if result.Ret != 0 {
return errors.New(result.Msg)
}
return nil
}
// GetFriendList 获取好友列表
func (b *BotManager) GetFriendList(startIndex int) (FriendList, error) {
var result FriendList
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=friendlist.GetFriendListReq&timeout=10&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"StartIndex": startIndex})
if err != nil {
return result, err
}
err = res.Json(&result)
if err != nil {
return result, err
}
return result, nil
}
// GetGroupList 获取群列表
func (b *BotManager) GetGroupList(nextToken string) (GroupList, error) {
var result GroupList
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=friendlist.GetTroopListReqV2&timeout=10&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"NextToken": nextToken})
if err != nil {
return result, err
}
err = res.Json(&result)
if err != nil {
return result, err
}
return result, nil
}
// SetForbidden 设置禁言 flag 0为设置全体禁言 1为设置某人禁言 ShutTime 0为取消禁言 >0为禁言分钟数 全体禁言>0为开启禁言
func (b *BotManager) SetForbidden(flag, ShutTime int, groupID, userId int64) error {
var result Result
if flag == 0 {
Switch := 0
if ShutTime > 0 {
Switch = 1
}
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=OidbSvc.0x89a_0&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"GroupID": groupID, "Switch": Switch})
if err != nil {
return err
}
err = res.Json(&result)
if err != nil {
return err
}
if result.Ret != 0 {
return errors.New(result.Msg)
}
} else {
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=OidbSvc.0x570_8&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"GroupID": groupID, "ShutUpUserID": userId, "ShutTime": ShutTime})
if err != nil {
return err
}
err = res.Json(&result)
if err != nil {
return err
}
if result.Ret != 0 {
return errors.New(result.Msg)
}
}
return nil
}
// GetFile 下载文件 groupId 为0 是下载好友分享文件
func (b *BotManager) GetFile(fileId string, groupID int64) (FriendFileResult, GroupFileResult, error) {
var friendFileResult FriendFileResult
var groupFileResult GroupFileResult
if groupID == 0 {
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=OfflineFilleHandleSvr.pb_ftn_CMD_REQ_APPLY_DOWNLOAD-1200&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"FileID": fileId})
if err != nil {
return friendFileResult, groupFileResult, err
}
err = res.Json(&friendFileResult)
if err != nil {
return friendFileResult, groupFileResult, err
}
} else {
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=OidbSvc.0x6d6_2&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"FileID": fileId, "GroupID": groupID})
if err != nil {
return friendFileResult, groupFileResult, err
}
err = res.Json(&groupFileResult)
if err != nil {
return friendFileResult, groupFileResult, err
}
}
return friendFileResult, groupFileResult, nil
}
// GetUserCardInfo 获取用户信息
func (b *BotManager) GetUserCardInfo(qq int64) (UserCardInfo, error) {
var result UserCardInfo
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=SummaryCard.ReqSummaryCard&qq="+strconv.FormatInt(b.QQ, 10), map[string]int64{"UserID": qq})
if err != nil {
// log.Println(err.Error())
return result, err
}
err = res.Json(&result)
if err != nil {
return result, err
}
return result, nil
}
// OldSendVoice 发送语音 旧版 将被移出
func (b *BotManager) OldSendVoice(userID int64, sendToType int, data string) error {
//var result Result
_, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=SendMsg&qq="+strconv.FormatInt(b.QQ, 10), map[string]interface{}{"toUser": userID, "sendToType": sendToType, "sendMsgType": "VoiceMsg", "content": "",
"groupid": 0,
"atUser": 0,
"voiceUrl": "",
"voiceBase64Buf": data,
})
if err != nil {
// log.Println(err.Error())
return err
}
//err = res.Json(&result)
//if err != nil {
// return result, err
//}
return nil
}
// Zan QQ赞 次数
func (b *BotManager) Zan(qq int64, num int) int {
var result Result
success := 0
for i := 0; i < num; i++ {
res, err := requests.PostJson(b.OPQUrl+"/v1/LuaApiCaller?funcname=OidbSvc.0x7e5_4&qq="+strconv.FormatInt(b.QQ, 10), map[string]int64{"UserID": qq})
if err == nil {
err = res.Json(&result)
if err != nil {
break
}
if result.Ret == 0 {
success += 1
}
}
time.Sleep(500 * time.Microsecond)
}
return success
}