forked from THE-cattail/qq-bot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.go
859 lines (750 loc) · 21.6 KB
/
bot.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
// Package qqbotapi has functions and types used for interacting with
// the Coolq HTTP API.
package qqbotapi
import (
"crypto/hmac"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"github.com/catsworld/qq-bot-api/cqcode"
"github.com/mitchellh/mapstructure"
"golang.org/x/net/websocket"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"sync"
"time"
)
// BotAPI allows you to interact with the Coolq HTTP API.
type BotAPI struct {
Token string `json:"token"`
Secret string `json:"secret"`
Debug bool `json:"debug"`
Buffer int `json:"buffer"`
APIEndpoint string `json:"api_endpoint"`
Self User `json:"-"`
Client *http.Client `json:"-"`
WSAPIClient *websocket.Conn `json:"-"`
WSEventClient *websocket.Conn `json:"-"`
WSPendingRequests map[int]chan APIResponse `json:"-"`
WSPendingMux sync.Mutex `json:"-"`
WSRequestTimeout time.Duration `json:"-"`
Echo int `json:"-"`
EchoMux sync.Mutex `json:"-"`
}
// NewBotAPI creates a new BotAPI instance.
//
// token: access_token, api: API Endpoint of Coolq-http, example: http://host:port.
// secret: the secret key of HMAC SHA1 signature of Coolq-HTTP, won't be validated if left blank.
func NewBotAPI(token string, api string, secret string) (*BotAPI, error) {
u, err := url.Parse(api)
if err != nil {
return nil, err
}
switch u.Scheme {
case "wss":
fallthrough
case "ws":
return NewBotAPIWithWSClient(token, api)
case "https":
fallthrough
case "http":
return NewBotAPIWithClient(token, api, secret)
default:
return nil, errors.New("bad api url scheme")
}
}
// NewBotAPIWithClient creates a new BotAPI instance
//
// It requires a token, an API endpoint and a secret which you
// set in Coolq HTTP API.
func NewBotAPIWithClient(token string, api string, secret string) (*BotAPI, error) {
bot := &BotAPI{
Token: token,
Client: &http.Client{},
Buffer: 100,
APIEndpoint: api,
Secret: secret,
}
self, err := bot.GetMe()
if err != nil {
return nil, err
}
bot.Self = self
return bot, nil
}
// NewBotAPIWithWSClient creates a new BotAPI instance
//
// It requires a token, an API endpoint which you
// set in Coolq HTTP API.
func NewBotAPIWithWSClient(token string, api string) (*BotAPI, error) {
bot := &BotAPI{
Token: token,
Buffer: 100,
APIEndpoint: api,
}
var err error
// Dial /api/ ws
apiConfig, err := websocket.NewConfig(api+"/api/", "http://localhost/")
if err != nil {
return nil, errors.New("invalid websocket address")
}
apiConfig.Header.Add("Authorization", fmt.Sprintf("Token %s", token))
bot.WSAPIClient, err = websocket.DialConfig(apiConfig)
if err != nil {
return nil, errors.New("failed to dial cqhttp api websocket")
}
bot.debugLog("Dial /api/ ws", "dial cqhttp api websocket success")
// Dial /event/ ws
eventConfig, err := websocket.NewConfig(api+"/event/", "http://localhost/")
if err != nil {
return nil, errors.New("invalid websocket address")
}
eventConfig.Header.Add("Authorization", fmt.Sprintf("Token %s", token))
bot.WSEventClient, err = websocket.DialConfig(eventConfig)
if err != nil {
return nil, errors.New("failed to dial cqhttp event websocket")
}
bot.debugLog("Dial /event/ ws", "dial cqhttp event websocket success")
bot.WSPendingRequests = make(map[int]chan APIResponse)
bot.WSRequestTimeout = time.Second * 10
go func() {
for {
// get api response
resp := APIResponse{}
if err := websocket.JSON.Receive(bot.WSAPIClient, &resp); err != nil {
bot.debugLog("WS APIResponse", "failed to read apiresponse (%v)", err)
continue
}
echo, ok := resp.Echo.(float64)
if !ok {
continue
}
e := int(echo)
bot.WSPendingMux.Lock()
if ch, ok := bot.WSPendingRequests[e]; ok {
ch <- resp
close(ch)
delete(bot.WSPendingRequests, e)
}
bot.WSPendingMux.Unlock()
}
}()
self, err := bot.GetMe()
if err != nil {
return nil, err
}
bot.Self = self
return bot, nil
}
// MakeRequest makes a request to a specific endpoint with our token.
func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) {
if bot.Client != nil {
return bot.makeHTTPRequest(endpoint, params)
} else {
return bot.makeWSRequest(endpoint, params)
}
}
func (bot *BotAPI) makeHTTPRequest(endpoint string, params url.Values) (APIResponse, error) {
method := fmt.Sprintf("%s/%s?access_token=%s", bot.APIEndpoint, endpoint, bot.Token)
resp, err := bot.Client.PostForm(method, params)
if err != nil {
return APIResponse{}, err
}
defer resp.Body.Close()
var apiResp APIResponse
bytes, err := bot.decodeAPIResponse(resp.Body, &apiResp)
if err != nil {
return apiResp, err
}
bot.debugLog("MakeRequest", "%s resp: %s", endpoint, bytes)
if apiResp.Status != "ok" {
return apiResp, errors.New(apiResp.Status + " " + strconv.Itoa(apiResp.RetCode))
}
return apiResp, nil
}
func (bot *BotAPI) decodeAPIResponse(responseBody io.Reader, resp *APIResponse) (_ []byte, err error) {
if !bot.Debug {
dec := json.NewDecoder(responseBody)
err = dec.Decode(resp)
return
}
// if debug, read reponse body
data, err := ioutil.ReadAll(responseBody)
if err != nil {
return
}
err = json.Unmarshal(data, resp)
if err != nil {
return
}
return data, nil
}
func (bot *BotAPI) makeWSRequest(endpoint string, params url.Values) (APIResponse, error) {
bot.EchoMux.Lock()
bot.Echo++
echo := bot.Echo
bot.EchoMux.Unlock()
p := make(map[string]interface{})
if params != nil {
for k, vs := range params {
if len(vs) != 0 {
p[k] = vs[0]
}
}
}
req := WebSocketRequest{
Echo: echo,
Action: endpoint,
Params: p,
}
ch := make(chan APIResponse)
bot.WSPendingRequests[echo] = ch
err := websocket.JSON.Send(bot.WSAPIClient, req)
if err != nil {
delete(bot.WSPendingRequests, echo)
return APIResponse{}, err
}
t := time.After(bot.WSRequestTimeout)
select {
case resp := <-ch:
return resp, nil
case <-t:
bot.WSPendingMux.Lock()
delete(bot.WSPendingRequests, echo)
close(ch)
bot.WSPendingMux.Unlock()
return APIResponse{}, errors.New("request timeout")
}
}
func (bot *BotAPI) makeMessageRequest(endpoint string, params url.Values) (Message, error) {
resp, err := bot.MakeRequest(endpoint, params)
if err != nil {
return Message{}, err
}
var message Message
json.Unmarshal(resp.Data, &message)
bot.debugLog(endpoint, params, message)
return message, nil
}
// GetMe fetches the currently authenticated bot.
//
// This method is called upon creation to validate the token,
// and so you may get this data from BotAPI.Self without the need for
// another request.
func (bot *BotAPI) GetMe() (User, error) {
resp, err := bot.MakeRequest("get_login_info", nil)
if err != nil {
return User{}, err
}
var user User
json.Unmarshal(resp.Data, &user)
bot.debugLog("GetMe", nil, user)
return user, nil
}
// GetStrangerInfo fetches a stranger's user info.
func (bot *BotAPI) GetStrangerInfo(userID int64) (User, error) {
v := url.Values{}
v.Add("user_id", strconv.FormatInt(userID, 10))
resp, err := bot.MakeRequest("get_stranger_info", v)
if err != nil {
return User{}, err
}
var user User
json.Unmarshal(resp.Data, &user)
bot.debugLog("GetStrangerInfo", nil, user)
return user, nil
}
// GetGroupMemberInfo fetches a group member's user info.
//
// Using cache may result in not updating in time, but will be responded faster
func (bot *BotAPI) GetGroupMemberInfo(groupID int64, userID int64, noCache bool) (User, error) {
v := url.Values{}
v.Add("group_id", strconv.FormatInt(groupID, 10))
v.Add("user_id", strconv.FormatInt(userID, 10))
v.Add("no_cache", strconv.FormatBool(noCache))
resp, err := bot.MakeRequest("get_group_member_info", v)
if err != nil {
return User{}, err
}
var user User
json.Unmarshal(resp.Data, &user)
bot.debugLog("GetGroupMemberInfo", nil, user)
return user, nil
}
// GetGroupMemberList fetches a group all member's user info.
//
// This information might be not full or accurate enough.
func (bot *BotAPI) GetGroupMemberList(groupID int64) ([]User, error) {
v := url.Values{}
v.Add("group_id", strconv.FormatInt(groupID, 10))
resp, err := bot.MakeRequest("get_group_member_list", v)
if err != nil {
return nil, err
}
users := make([]User, 0)
json.Unmarshal(resp.Data, &users)
bot.debugLog("GetGroupMemberInfo", nil, users)
return users, nil
}
// GetGroupList fetches all groups
func (bot *BotAPI) GetGroupList() ([]Group, error) {
v := url.Values{}
resp, err := bot.MakeRequest("get_group_list", v)
if err != nil {
return nil, err
}
groups := make([]Group, 0)
json.Unmarshal(resp.Data, &groups)
bot.debugLog("GetGroupList", nil, groups)
return groups, nil
}
// IsMessageToMe returns true if message directed to this bot.
//
// It requires the Message.
func (bot *BotAPI) IsMessageToMe(message Message) bool {
for _, media := range *message.Message {
at, ok := media.(*cqcode.At)
if !ok {
continue
}
if at.QQ == strconv.FormatInt(bot.Self.ID, 10) {
return true
}
}
return false
}
// Send will send a Chattable item to Coolq.
// The response will be regarded as Message, often with a MessageID in it.
//
// It requires the Chattable to send.
func (bot *BotAPI) Send(c Chattable) (Message, error) {
v, err := c.values()
if err != nil {
return Message{}, err
}
message, err := bot.makeMessageRequest(c.method(), v)
if err != nil {
return Message{}, err
}
return message, nil
}
func (bot *BotAPI) debugLog(context string, message ...interface{}) {
if bot.Debug {
for i, v := range message {
log.Printf("%s [%d]: %+v\n", context, i, v)
}
}
}
// Do will send a Chattable item to Coolq.
//
// It requires the Chattable to send.
func (bot *BotAPI) Do(c Chattable) (APIResponse, error) {
v, err := c.values()
if err != nil {
return APIResponse{}, err
}
resp, err := bot.MakeRequest(c.method(), v)
if err != nil {
return APIResponse{}, err
}
return resp, nil
}
// ParseRawMessage parses message
func (update *Update) ParseRawMessage() {
text, ok := update.RawMessage.(string)
if update.PostType != "message" {
update.Text = text
return
}
messageSubType := "normal"
chat := Chat{
Type: update.MessageType,
}
if chat.IsPrivate() {
chat.ID = int64(update.UserID)
chat.SubType = update.SubType
}
if chat.IsGroup() {
chat.ID = int64(update.GroupID)
messageSubType = update.SubType
}
if chat.IsDiscuss() {
chat.ID = int64(update.DiscussID)
}
message, _ := cqcode.ParseMessage(update.RawMessage)
if !ok {
text = message.CQString()
}
var user User
if messageSubType == "anonymous" {
anonymousName, ok := update.Anonymous.(string)
if !ok {
config := &mapstructure.DecoderConfig{
Metadata: nil,
Result: &user,
WeaklyTypedInput: true,
TagName: "anonymous",
}
decoder, _ := mapstructure.NewDecoder(config)
decoder.Decode(update.Anonymous)
} else {
user.AnonymousID = update.UserID
user.AnonymousName = anonymousName
user.AnonymousFlag = update.AnonymousFlag
}
}
user.ID = update.UserID
update.Message = &Message{
Message: &message,
MessageID: update.MessageID,
From: &user,
Chat: &chat,
Text: text,
SubType: messageSubType,
}
update.Text = text
if update.PostType == "event" {
update.NoticeType = update.Event
} else if update.PostType == "notice" {
update.Event = update.NoticeType
}
if update.Sender != nil {
update.Message.From = update.Sender
}
}
// PreloadUserInfo fills in the information in update.Message.From
func (bot *BotAPI) PreloadUserInfo(update *Update) {
if update.Message == nil || update.Message.IsAnonymous() {
return
}
var user User
var err error
if update.Message.Chat.Type == "group" {
user, err = bot.GetGroupMemberInfo(update.GroupID, update.UserID, false)
if err != nil {
return
}
} else {
user, err = bot.GetStrangerInfo(update.UserID)
if err != nil {
return
}
}
update.Message.From = &user
}
// GetUpdates fetches updates over long polling or websocket.
// https://github.com/richardchien/cqhttp-ext-long-polling
//
// Offset, Limit, and Timeout are optional.
// To avoid stale items, set Offset to one higher than the previous item.
// Set Timeout to a large number to reduce requests so you can get updates
// instantly instead of having to wait between requests.
func (bot *BotAPI) GetUpdates(config UpdateConfig) ([]Update, error) {
if bot.Client != nil {
return bot.getUpdatesViaHTTP(config)
} else {
return bot.getUpdatesViaWebSocket(config)
}
}
func (bot *BotAPI) getUpdatesViaHTTP(config UpdateConfig) ([]Update, error) {
v := url.Values{}
if config.Offset != 0 {
v.Add("offset", strconv.Itoa(config.Offset))
}
if config.Limit > 0 {
v.Add("limit", strconv.Itoa(config.Limit))
}
if config.Timeout > 0 {
v.Add("timeout", strconv.Itoa(config.Timeout))
}
resp, err := bot.MakeRequest("get_updates", v)
if err != nil {
return []Update{}, err
}
var updates []Update
json.Unmarshal(resp.Data, &updates)
for i := range updates {
updates[i].ParseRawMessage()
if config.PreloadUserInfo && updates[i].Sender == nil {
bot.PreloadUserInfo(&updates[i])
}
}
bot.debugLog("getUpdates", v, updates)
return updates, nil
}
func (bot *BotAPI) getUpdatesViaWebSocket(config UpdateConfig) ([]Update, error) {
var update Update
if err := websocket.JSON.Receive(bot.WSEventClient, &update); err != nil {
return nil, err
}
update.ParseRawMessage()
if config.PreloadUserInfo && update.Sender == nil {
bot.PreloadUserInfo(&update)
}
return []Update{update}, nil
}
// GetUpdatesChan starts and returns a channel that gets updates over long polling or websocket.
// https://github.com/richardchien/cqhttp-ext-long-polling
func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) {
ch := make(chan Update, bot.Buffer)
go func() {
for {
updates, err := bot.GetUpdates(config)
if err != nil {
log.Println(err)
log.Println("Failed to get updates, retrying in 3 seconds...")
time.Sleep(time.Second * 3)
continue
}
for _, update := range updates {
ch <- update
}
}
}()
return ch, nil
}
// ListenForWebSocket registers a http handler for a websocket and returns a channel that gets updates.
func (bot *BotAPI) ListenForWebSocket(config WebhookConfig) UpdatesChannel {
ch := make(chan Update, bot.Buffer)
server := websocket.Server{
Handshake: func(c *websocket.Config, r *http.Request) error {
if bot.Token != "" {
token := r.Header.Get("Authorization")[len("Token "):]
if token != bot.Token {
return errors.New("Invalid access token")
}
}
return nil
},
Handler: func(ws *websocket.Conn) {
connectionClose := make(chan bool)
go func() {
for {
select {
case <-time.After(20 * time.Second):
ws.PayloadType = byte(0x9)
ws.Write([]byte("keepalive"))
case <-connectionClose:
return
}
}
}()
for {
var update Update
err := websocket.JSON.Receive(ws, &update)
if err != nil {
bot.debugLog("ListenForWebSocket", "failed to read event (%v)", err)
connectionClose <- true
ws.Close()
return
}
update.ParseRawMessage()
if config.PreloadUserInfo {
bot.PreloadUserInfo(&update)
}
bot.debugLog("ListenForWebSocket", update)
ch <- update
}
},
}
http.Handle(config.Pattern, server)
return ch
}
// ListenForWebhook registers a http handler for a webhook and returns a channel that gets updates.
func (bot *BotAPI) ListenForWebhook(config WebhookConfig) UpdatesChannel {
ch := make(chan Update, bot.Buffer)
http.HandleFunc(config.Pattern, func(w http.ResponseWriter, r *http.Request) {
bytes, _ := ioutil.ReadAll(r.Body)
if bot.Secret != "" {
mac := hmac.New(sha1.New, []byte(bot.Secret))
mac.Write(bytes)
expectedMac := r.Header.Get("X-Signature")[len("sha1="):]
messageMac := hex.EncodeToString(mac.Sum(nil))
if expectedMac != messageMac {
bot.debugLog("ListenForWebhook HMAC", expectedMac, messageMac)
return
}
}
var update Update
json.Unmarshal(bytes, &update)
update.ParseRawMessage()
if config.PreloadUserInfo {
bot.PreloadUserInfo(&update)
}
bot.debugLog("ListenForWebhook", update)
ch <- update
w.WriteHeader(http.StatusNoContent)
})
return ch
}
// ListenForWebhookSync registers a http handler for a webhook.
//
// handler receives a update and returns a key-value dictionary.
func (bot *BotAPI) ListenForWebhookSync(config WebhookConfig, handler func(update Update) interface{}) {
http.HandleFunc(config.Pattern, func(w http.ResponseWriter, r *http.Request) {
bytes, _ := ioutil.ReadAll(r.Body)
if bot.Secret != "" {
mac := hmac.New(sha1.New, []byte(bot.Secret))
mac.Write(bytes)
expectedMac := r.Header.Get("X-Signature")[len("sha1="):]
messageMac := hex.EncodeToString(mac.Sum(nil))
if expectedMac != messageMac {
bot.debugLog("ListenForWebhook HMAC", expectedMac, messageMac)
return
}
}
var update Update
json.Unmarshal(bytes, &update)
update.ParseRawMessage()
if config.PreloadUserInfo {
bot.PreloadUserInfo(&update)
}
bot.debugLog("ListenForWebhook", update)
resp, _ := json.Marshal(handler(update))
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(resp)
})
}
// SendMessage sends message to a chat.
func (bot *BotAPI) SendMessage(chatID int64, chatType string, message interface{}) (Message, error) {
return bot.Send(NewMessage(chatID, chatType, message))
}
// NewMessage sends message to a chat.
func (bot *BotAPI) NewMessage(chatID int64, chatType string) *Sender {
return NewSender(bot, chatID, chatType)
}
// DeleteMessage deletes a message in a chat.
func (bot *BotAPI) DeleteMessage(messageID int64) (APIResponse, error) {
return bot.Do(DeleteMessageConfig{
MessageID: messageID,
})
}
// Like sends like (displayed in one's profile page) to a user.
func (bot *BotAPI) Like(userID int64, times int) (APIResponse, error) {
return bot.Do(LikeConfig{
UserID: userID,
Times: times,
})
}
// KickChatMember kick a chat member in a group.
func (bot *BotAPI) KickChatMember(groupID int64, userID int64, rejectAddRequest bool) (APIResponse, error) {
return bot.Do(KickChatMemberConfig{
ChatMemberConfig: ChatMemberConfig{
GroupID: groupID,
UserID: userID,
},
RejectAddRequest: rejectAddRequest,
})
}
// RestrictChatMember bans a chat member from sending messages.
func (bot *BotAPI) RestrictChatMember(groupID int64, userID int64, duration time.Duration) (APIResponse, error) {
return bot.Do(RestrictChatMemberConfig{
ChatMemberConfig: ChatMemberConfig{
GroupID: groupID,
UserID: userID,
},
Duration: duration,
})
}
// RestrictAnonymousChatMember bans an anonymous chat member from sending messages.
func (bot *BotAPI) RestrictAnonymousChatMember(groupID int64, flag string, duration time.Duration) (APIResponse, error) {
return bot.Do(RestrictChatMemberConfig{
ChatMemberConfig: ChatMemberConfig{
GroupID: groupID,
AnonymousFlag: flag,
},
Duration: duration,
})
}
// RestrictAllChatMembers : By this enabled, only administrators in a group will be able to send messages.
func (bot *BotAPI) RestrictAllChatMembers(groupID int64, enable bool) (APIResponse, error) {
return bot.Do(RestrictAllChatMembersConfig{
GroupControlConfig: GroupControlConfig{
GroupID: groupID,
Enable: enable,
},
})
}
// PromoteChatMember add admin rights to user.
func (bot *BotAPI) PromoteChatMember(groupID int64, userID int64, enable bool) (APIResponse, error) {
return bot.Do(PromoteChatMemberConfig{
ChatMemberConfig: ChatMemberConfig{
GroupID: groupID,
UserID: userID,
},
Enable: enable,
})
}
// EnableAnonymousChat : By this enabled, members in a group will be able to send messages with an anonymous identity.
func (bot *BotAPI) EnableAnonymousChat(groupID int64, enable bool) (APIResponse, error) {
return bot.Do(EnableAnonymousChatConfig{
GroupControlConfig: GroupControlConfig{
GroupID: groupID,
Enable: enable,
},
})
}
// SetChatMemberCard sets a chat member's 群名片 in the group.
func (bot *BotAPI) SetChatMemberCard(groupID int64, userID int64, card string) (APIResponse, error) {
return bot.Do(SetChatMemberCardConfig{
ChatMemberConfig: ChatMemberConfig{
GroupID: groupID,
UserID: userID,
},
Card: card,
})
}
// SetChatMemberTitle sets a chat member's 专属头衔 in the group.
func (bot *BotAPI) SetChatMemberTitle(groupID int64, userID int64, title string, duration time.Duration) (APIResponse, error) {
return bot.Do(SetChatMemberTitleConfig{
ChatMemberConfig: ChatMemberConfig{
GroupID: groupID,
UserID: userID,
},
SpecialTitle: title,
Duration: duration,
})
}
// LeaveChat makes the bot leave the chat.
func (bot *BotAPI) LeaveChat(chatID int64, chatType string, dismiss bool) (APIResponse, error) {
return bot.Do(LeaveChatConfig{
BaseChat: BaseChat{
ChatID: chatID,
ChatType: chatType,
},
IsDismiss: dismiss,
})
}
// HandleFriendRequest handles a friend request.
//
// remark: 备注
func (bot *BotAPI) HandleFriendRequest(flag string, approve bool, remark string) (APIResponse, error) {
return bot.Do(HandleFriendRequestConfig{
HandleRequestConfig: HandleRequestConfig{
RequestFlag: flag,
Approve: approve,
},
Remark: remark,
})
}
// HandleGroupRequest handles a group adding request.
//
// typ: sub_type in Update
// reason: Reason if you rejects this request.
func (bot *BotAPI) HandleGroupRequest(flag string, typ string, approve bool, reason string) (APIResponse, error) {
return bot.Do(HandleGroupRequestConfig{
HandleRequestConfig: HandleRequestConfig{
RequestFlag: flag,
Approve: approve,
},
Type: typ,
Reason: reason,
})
}