-
Notifications
You must be signed in to change notification settings - Fork 26
/
api.go
1021 lines (825 loc) · 44.4 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
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
/*
* Echotron
* Copyright (C) 2018 The Echotron Contributors
*
* Echotron is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Echotron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package echotron
import (
"encoding/json"
"fmt"
"net/url"
"strings"
)
// API is the object that contains all the functions that wrap those of the Telegram Bot API.
type API struct {
token string
base string
}
// NewAPI returns a new API object.
func NewAPI(token string) API {
return API{
token: token,
base: fmt.Sprintf("https://api.telegram.org/bot%s/", token),
}
}
// NewLocalAPI is like NewAPI but allows to use a local API server.
func NewLocalAPI(url, token string) API {
return API{
token: token,
base: url,
}
}
// GetUpdates is used to receive incoming updates using long polling.
func (a API) GetUpdates(opts *UpdateOptions) (res APIResponseUpdate, err error) {
return res, client.get(a.base, "getUpdates", urlValues(opts), &res)
}
// SetWebhook is used to specify a url and receive incoming updates via an outgoing webhook.
func (a API) SetWebhook(webhookURL string, dropPendingUpdates bool, opts *WebhookOptions) (res APIResponseBase, err error) {
var (
vals = make(url.Values)
keyVal = map[string]string{"url": webhookURL}
)
url, err := url.JoinPath(a.base, "setWebhook")
if err != nil {
return res, err
}
vals.Set("drop_pending_updates", btoa(dropPendingUpdates))
addValues(vals, opts)
url = fmt.Sprintf("%s?%s", strings.TrimSuffix(url, "/"), vals.Encode())
cnt, err := client.doPostForm(url, keyVal)
if err != nil {
return
}
if err = json.Unmarshal(cnt, &res); err != nil {
return
}
err = check(res)
return
}
// DeleteWebhook is used to remove webhook integration if you decide to switch back to GetUpdates.
func (a API) DeleteWebhook(dropPendingUpdates bool) (res APIResponseBase, err error) {
var vals = make(url.Values)
vals.Set("drop_pending_updates", btoa(dropPendingUpdates))
return res, client.get(a.base, "deleteWebhook", vals, &res)
}
// GetWebhookInfo is used to get current webhook status.
func (a API) GetWebhookInfo() (res APIResponseWebhook, err error) {
return res, client.get(a.base, "getWebhookInfo", nil, &res)
}
// GetMe is a simple method for testing your bot's auth token.
func (a API) GetMe() (res APIResponseUser, err error) {
return res, client.get(a.base, "getMe", nil, &res)
}
// LogOut is used to log out from the cloud Bot API server before launching the bot locally.
// You MUST log out the bot before running it locally, otherwise there is no guarantee that the bot will receive updates.
// After a successful call, you can immediately log in on a local server,
// but will not be able to log in back to the cloud Bot API server for 10 minutes.
func (a API) LogOut() (res APIResponseBool, err error) {
return res, client.get(a.base, "logOut", nil, &res)
}
// Close is used to close the bot instance before moving it from one local server to another.
// You need to delete the webhook before calling this method to ensure that the bot isn't launched again after server restart.
// The method will return error 429 in the first 10 minutes after the bot is launched.
func (a API) Close() (res APIResponseBool, err error) {
return res, client.get(a.base, "close", nil, &res)
}
// SendMessage is used to send text messages.
func (a API) SendMessage(text string, chatID int64, opts *MessageOptions) (res APIResponseMessage, err error) {
var vals = make(url.Values)
vals.Set("text", text)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "sendMessage", addValues(vals, opts), &res)
}
// ForwardMessage is used to forward messages of any kind.
// Service messages can't be forwarded.
func (a API) ForwardMessage(chatID, fromChatID int64, messageID int, opts *ForwardOptions) (res APIResponseMessage, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("from_chat_id", itoa(fromChatID))
vals.Set("message_id", itoa(int64(messageID)))
return res, client.get(a.base, "forwardMessage", addValues(vals, opts), &res)
}
// ForwardMessages is used to forward multiple messages of any kind.
// If some of the specified messages can't be found or forwarded, they are skipped.
// Service messages and messages with protected content can't be forwarded.
// Album grouping is kept for forwarded messages.
func (a API) ForwardMessages(chatID, fromChatID int64, messageIDs []int, opts *ForwardOptions) (res APIResponseMessageIDs, err error) {
var vals = make(url.Values)
msgIDs, err := json.Marshal(messageIDs)
if err != nil {
return res, err
}
vals.Set("chat_id", itoa(chatID))
vals.Set("from_chat_id", itoa(fromChatID))
vals.Set("message_ids", string(msgIDs))
return res, client.get(a.base, "forwardMessages", addValues(vals, opts), &res)
}
// CopyMessage is used to copy messages of any kind.
// Service messages, paid media mesages, giveaway messages, giveaway winners messages, and invoice messages can't be copied.
// The method is analogous to the method ForwardMessage,
// but the copied message doesn't have a link to the original message.
func (a API) CopyMessage(chatID, fromChatID int64, messageID int, opts *CopyOptions) (res APIResponseMessageID, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("from_chat_id", itoa(fromChatID))
vals.Set("message_id", itoa(int64(messageID)))
return res, client.get(a.base, "copyMessage", addValues(vals, opts), &res)
}
// CopyMessages is used to copy messages of any kind.
// If some of the specified messages can't be found or copied, they are skipped.
// Service messages, paid media mesages, giveaway messages, giveaway winners messages, and invoice messages can't be copied.
// A quiz poll can be copied only if the value of the field correct_option_id is known to the bot.
// The method is analogous to the method forwardMessages, but the copied messages don't have a link to the original message.
// Album grouping is kept for copied messages.
func (a API) CopyMessages(chatID, fromChatID int64, messageIDs []int, opts *CopyMessagesOptions) (res APIResponseMessageIDs, err error) {
var vals = make(url.Values)
msgIDs, err := json.Marshal(messageIDs)
if err != nil {
return res, err
}
vals.Set("chat_id", itoa(chatID))
vals.Set("from_chat_id", itoa(fromChatID))
vals.Set("message_ids", string(msgIDs))
return res, client.get(a.base, "copyMessages", addValues(vals, opts), &res)
}
// SendPhoto is used to send photos.
func (a API) SendPhoto(file InputFile, chatID int64, opts *PhotoOptions) (res APIResponseMessage, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.postFile(a.base, "sendPhoto", "photo", file, InputFile{}, addValues(vals, opts), &res)
}
// SendAudio is used to send audio files,
// if you want Telegram clients to display them in the music player.
// Your audio must be in the .MP3 or .M4A format.
func (a API) SendAudio(file InputFile, chatID int64, opts *AudioOptions) (res APIResponseMessage, err error) {
var (
thumbnail InputFile
vals = make(url.Values)
)
if opts != nil {
thumbnail = opts.Thumbnail
}
vals.Set("chat_id", itoa(chatID))
return res, client.postFile(a.base, "sendAudio", "audio", file, thumbnail, addValues(vals, opts), &res)
}
// SendDocument is used to send general files.
func (a API) SendDocument(file InputFile, chatID int64, opts *DocumentOptions) (res APIResponseMessage, err error) {
var (
thumbnail InputFile
vals = make(url.Values)
)
if opts != nil {
thumbnail = opts.Thumbnail
}
vals.Set("chat_id", itoa(chatID))
return res, client.postFile(a.base, "sendDocument", "document", file, thumbnail, addValues(vals, opts), &res)
}
// SendVideo is used to send video files.
// Telegram clients support mp4 videos (other formats may be sent with SendDocument).
func (a API) SendVideo(file InputFile, chatID int64, opts *VideoOptions) (res APIResponseMessage, err error) {
var (
thumbnail InputFile
vals = make(url.Values)
)
if opts != nil {
thumbnail = opts.Thumbnail
}
vals.Set("chat_id", itoa(chatID))
return res, client.postFile(a.base, "sendVideo", "video", file, thumbnail, addValues(vals, opts), &res)
}
// SendAnimation is used to send animation files (GIF or H.264/MPEG-4 AVC video without sound).
func (a API) SendAnimation(file InputFile, chatID int64, opts *AnimationOptions) (res APIResponseMessage, err error) {
var (
thumbnail InputFile
vals = make(url.Values)
)
if opts != nil {
thumbnail = opts.Thumbnail
}
vals.Set("chat_id", itoa(chatID))
return res, client.postFile(a.base, "sendAnimation", "animation", file, thumbnail, addValues(vals, opts), &res)
}
// SendVoice is used to send audio files, if you want Telegram clients to display the file as a playable voice message.
// For this to work, your audio must be in an .OGG file encoded with OPUS (other formats may be sent as Audio or Document).
func (a API) SendVoice(file InputFile, chatID int64, opts *VoiceOptions) (res APIResponseMessage, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.postFile(a.base, "sendVoice", "voice", file, InputFile{}, addValues(vals, opts), &res)
}
// SendVideoNote is used to send video messages.
func (a API) SendVideoNote(file InputFile, chatID int64, opts *VideoNoteOptions) (res APIResponseMessage, err error) {
var (
thumbnail InputFile
vals = make(url.Values)
)
if opts != nil {
thumbnail = opts.Thumbnail
}
vals.Set("chat_id", itoa(chatID))
return res, client.postFile(a.base, "sendVideoNote", "video_note", file, thumbnail, addValues(vals, opts), &res)
}
// SendPaidMedia is used to send paid media to channel chats.
func (a API) SendPaidMedia(chatID int64, starCount int64, media []GroupableInputMedia, opts *PaidMediaOptions) (res APIResponseMessage, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("star_count", itoa(starCount))
return res, client.postMedia(a.base, "sendPaidMedia", false, addValues(vals, opts), &res, toInputMedia(media)...)
}
// SendMediaGroup is used to send a group of photos, videos, documents or audios as an album.
// Documents and audio files can be only grouped in an album with messages of the same type.
func (a API) SendMediaGroup(chatID int64, media []GroupableInputMedia, opts *MediaGroupOptions) (res APIResponseMessageArray, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.postMedia(a.base, "sendMediaGroup", false, addValues(vals, opts), &res, toInputMedia(media)...)
}
// SendLocation is used to send point on the map.
func (a API) SendLocation(chatID int64, latitude, longitude float64, opts *LocationOptions) (res APIResponseMessage, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("latitude", ftoa(latitude))
vals.Set("longitude", ftoa(longitude))
return res, client.get(a.base, "sendLocation", addValues(vals, opts), &res)
}
// EditMessageLiveLocation is used to edit live location messages.
// A location can be edited until its `LivePeriod` expires or editing is explicitly disabled by a call to `StopMessageLiveLocation`.
func (a API) EditMessageLiveLocation(msg MessageIDOptions, latitude, longitude float64, opts *EditLocationOptions) (res APIResponseMessage, err error) {
var vals = make(url.Values)
vals.Set("latitude", ftoa(latitude))
vals.Set("longitude", ftoa(longitude))
return res, client.get(a.base, "editMessageLiveLocation", addValues(addValues(vals, msg), opts), &res)
}
// StopMessageLiveLocation is used to stop updating a live location message before `LivePeriod` expires.
func (a API) StopMessageLiveLocation(msg MessageIDOptions, opts *StopLocationOptions) (res APIResponseMessage, err error) {
return res, client.get(a.base, "stopMessageLiveLocation", addValues(urlValues(msg), opts), &res)
}
// SendVenue is used to send information about a venue.
func (a API) SendVenue(chatID int64, latitude, longitude float64, title, address string, opts *VenueOptions) (res APIResponseMessage, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("latitude", ftoa(latitude))
vals.Set("longitude", ftoa(longitude))
vals.Set("title", title)
vals.Set("address", address)
return res, client.get(a.base, "sendVenue", addValues(vals, opts), &res)
}
// SendContact is used to send phone contacts.
func (a API) SendContact(phoneNumber, firstName string, chatID int64, opts *ContactOptions) (res APIResponseMessage, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("phone_number", phoneNumber)
vals.Set("first_name", firstName)
return res, client.get(a.base, "sendContact", addValues(vals, opts), &res)
}
// SendPoll is used to send a native poll.
func (a API) SendPoll(chatID int64, question string, options []InputPollOption, opts *PollOptions) (res APIResponseMessage, err error) {
var vals = make(url.Values)
pollOpts, err := json.Marshal(options)
if err != nil {
return res, err
}
vals.Set("chat_id", itoa(chatID))
vals.Set("question", question)
vals.Set("options", string(pollOpts))
return res, client.get(a.base, "sendPoll", addValues(vals, opts), &res)
}
// SendDice is used to send an animated emoji that will display a random value.
func (a API) SendDice(chatID int64, emoji DiceEmoji, opts *BaseOptions) (res APIResponseMessage, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("emoji", string(emoji))
return res, client.get(a.base, "sendDice", addValues(vals, opts), &res)
}
// SendChatAction is used to tell the user that something is happening on the bot's side.
// The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status).
func (a API) SendChatAction(action ChatAction, chatID int64, opts *ChatActionOptions) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("action", string(action))
return res, client.get(a.base, "sendChatAction", addValues(vals, opts), &res)
}
// SetMessageReaction is used to change the chosen reactions on a message.
// Service messages can't be reacted to.
// Automatically forwarded messages from a channel to its discussion group have the same available reactions as messages in the channel.
// In albums, bots must react to the first message.
func (a API) SetMessageReaction(chatID int64, messageID int, opts *MessageReactionOptions) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("message_id", itoa(int64(messageID)))
return res, client.get(a.base, "setMessageReaction", addValues(vals, opts), &res)
}
// GetUserProfilePhotos is used to get a list of profile pictures for a user.
func (a API) GetUserProfilePhotos(userID int64, opts *UserProfileOptions) (res APIResponseUserProfile, err error) {
var vals = make(url.Values)
vals.Set("user_id", itoa(userID))
return res, client.get(a.base, "getUserProfilePhotos", addValues(vals, opts), &res)
}
// GetFile returns the basic info about a file and prepares it for downloading.
// For the moment, bots can download files of up to 20MB in size.
// The file can then be downloaded with DownloadFile where filePath is taken from the response.
// It is guaranteed that the file will be downloadable for at least 1 hour.
// When the download file expires, a new one can be requested by calling GetFile again.
func (a API) GetFile(fileID string) (res APIResponseFile, err error) {
var vals = make(url.Values)
vals.Set("file_id", fileID)
return res, client.get(a.base, "getFile", vals, &res)
}
// DownloadFile returns the bytes of the file corresponding to the given filePath.
// This function is callable for at least 1 hour since the call to GetFile.
// When the download expires a new one can be requested by calling GetFile again.
func (a API) DownloadFile(filePath string) ([]byte, error) {
return client.doGet(fmt.Sprintf(
"https://api.telegram.org/file/bot%s/%s",
a.token,
filePath,
))
}
// BanChatMember is used to ban a user in a group, a supergroup or a channel.
// In the case of supergroups or channels, the user will not be able to return to the chat
// on their own using invite links, etc., unless unbanned first (through the UnbanChatMember method).
// The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
func (a API) BanChatMember(chatID, userID int64, opts *BanOptions) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("user_id", itoa(userID))
return res, client.get(a.base, "banChatMember", addValues(vals, opts), &res)
}
// UnbanChatMember is used to unban a previously banned user in a supergroup or channel.
// The user will NOT return to the group or channel automatically, but will be able to join via link, etc.
// The bot must be an administrator for this to work.
// By default, this method guarantees that after the call the user is not a member of the chat, but will be able to join it.
// So if the user is a member of the chat they will also be REMOVED from the chat.
// If you don't want this, use the parameter `OnlyIfBanned`.
func (a API) UnbanChatMember(chatID, userID int64, opts *UnbanOptions) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("user_id", itoa(userID))
return res, client.get(a.base, "unbanChatMember", addValues(vals, opts), &res)
}
// RestrictChatMember is used to restrict a user in a supergroup.
// The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
func (a API) RestrictChatMember(chatID, userID int64, permissions ChatPermissions, opts *RestrictOptions) (res APIResponseBool, err error) {
var vals = make(url.Values)
perm, err := json.Marshal(permissions)
if err != nil {
return
}
vals.Set("chat_id", itoa(chatID))
vals.Set("user_id", itoa(userID))
vals.Set("permissions", string(perm))
return res, client.get(a.base, "restrictChatMember", addValues(vals, opts), &res)
}
// PromoteChatMember is used to promote or demote a user in a supergroup or a channel.
// The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
func (a API) PromoteChatMember(chatID, userID int64, opts *PromoteOptions) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("user_id", itoa(userID))
return res, client.get(a.base, "promoteChatMember", addValues(vals, opts), &res)
}
// SetChatAdministratorCustomTitle is used to set a custom title for an administrator in a supergroup promoted by the bot.
func (a API) SetChatAdministratorCustomTitle(chatID, userID int64, customTitle string) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("user_id", itoa(userID))
vals.Set("custom_title", customTitle)
return res, client.get(a.base, "setChatAdministratorCustomTitle", vals, &res)
}
// BanChatSenderChat is used to ban a channel chat in a supergroup or a channel.
// The owner of the chat will not be able to send messages and join live streams on behalf of the chat, unless it is unbanned first.
// The bot must be an administrator in the supergroup or channel for this to work and must have the appropriate administrator rights.
func (a API) BanChatSenderChat(chatID, senderChatID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("sender_chat_id", itoa(senderChatID))
return res, client.get(a.base, "banChatSenderChat", vals, &res)
}
// UnbanChatSenderChat is used to unban a previously channel chat in a supergroup or channel.
// The bot must be an administrator for this to work and must have the appropriate administrator rights.
func (a API) UnbanChatSenderChat(chatID, senderChatID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("sender_chat_id", itoa(senderChatID))
return res, client.get(a.base, "unbanChatSenderChat", vals, &res)
}
// SetChatPermissions is used to set default chat permissions for all members.
// The bot must be an administrator in the supergroup for this to work and must have the can_restrict_members admin rights.
func (a API) SetChatPermissions(chatID int64, permissions ChatPermissions, opts *ChatPermissionsOptions) (res APIResponseBool, err error) {
var vals = make(url.Values)
perm, err := json.Marshal(permissions)
if err != nil {
return
}
vals.Set("chat_id", itoa(chatID))
vals.Set("permissions", string(perm))
return res, client.get(a.base, "setChatPermissions", addValues(vals, opts), &res)
}
// ExportChatInviteLink is used to generate a new primary invite link for a chat;
// any previously generated primary link is revoked.
// The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
func (a API) ExportChatInviteLink(chatID int64) (res APIResponseString, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "exportChatInviteLink", vals, &res)
}
// CreateChatInviteLink is used to create an additional invite link for a chat.
// The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
// The link can be revoked using the method RevokeChatInviteLink.
func (a API) CreateChatInviteLink(chatID int64, opts *InviteLinkOptions) (res APIResponseInviteLink, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "createChatInviteLink", addValues(vals, opts), &res)
}
// EditChatInviteLink is used to edit a non-primary invite link created by the bot.
// The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
func (a API) EditChatInviteLink(chatID int64, inviteLink string, opts *InviteLinkOptions) (res APIResponseInviteLink, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("invite_link", inviteLink)
return res, client.get(a.base, "editChatInviteLink", addValues(vals, opts), &res)
}
// CreateChatSubscriptionInviteLink is used to create a subscription invite link for a channel chat.
// The bot must have the can_invite_users administrator rights.
// The link can be edited using the method editChatSubscriptionInviteLink or revoked using the method revokeChatInviteLink.
func (a API) CreateChatSubscriptionInviteLink(chatID int64, subscriptionPeriod, subscriptionPrice int, opts *ChatSubscriptionInviteOptions) (res APIResponseInviteLink, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("subscription_period", itoa(int64(subscriptionPeriod)))
vals.Set("subscription_price", itoa(int64(subscriptionPrice)))
return res, client.get(a.base, "createChatSubscriptionInviteLink", addValues(vals, opts), &res)
}
// EditChatSubscriptionInviteLink is used to creeditate a subscription invite link for a channel chat.
// The bot must have the can_invite_users administrator rights.
func (a API) EditChatSubscriptionInviteLink(chatID int64, inviteLink string, opts *ChatSubscriptionInviteOptions) (res APIResponseInviteLink, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("invite_link", inviteLink)
return res, client.get(a.base, "editChatSubscriptionInviteLink", addValues(vals, opts), &res)
}
// RevokeChatInviteLink is used to revoke an invite link created by the bot.
// If the primary link is revoked, a new link is automatically generated.
// The bot must be an administrator in the supergroup for this to work and must have the appropriate admin rights.
func (a API) RevokeChatInviteLink(chatID int64, inviteLink string) (res APIResponseInviteLink, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("invite_link", inviteLink)
return res, client.get(a.base, "editChatInviteLink", vals, &res)
}
// ApproveChatJoinRequest is used to approve a chat join request.
// The bot must be an administrator in the chat for this to work and must have the CanInviteUsers administrator right.
func (a API) ApproveChatJoinRequest(chatID, userID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("user_id", itoa(userID))
return res, client.get(a.base, "approveChatJoinRequest", vals, &res)
}
// DeclineChatJoinRequest is used to decline a chat join request.
// The bot must be an administrator in the chat for this to work and must have the CanInviteUsers administrator right.
func (a API) DeclineChatJoinRequest(chatID, userID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("user_id", itoa(userID))
return res, client.get(a.base, "declineChatJoinRequest", vals, &res)
}
// SetChatPhoto is used to set a new profile photo for the chat.
// Photos can't be changed for private chats.
// The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
func (a API) SetChatPhoto(file InputFile, chatID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.postFile(a.base, "setChatPhoto", "photo", file, InputFile{}, vals, &res)
}
// DeleteChatPhoto is used to delete a chat photo.
// Photos can't be changed for private chats.
// The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
func (a API) DeleteChatPhoto(chatID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "deleteChatPhoto", vals, &res)
}
// SetChatTitle is used to change the title of a chat.
// Titles can't be changed for private chats.
// The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
func (a API) SetChatTitle(chatID int64, title string) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("title", title)
return res, client.get(a.base, "setChatTitle", vals, &res)
}
// SetChatDescription is used to change the description of a group, a supergroup or a channel.
// The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
func (a API) SetChatDescription(chatID int64, description string) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("description", description)
return res, client.get(a.base, "setChatDescription", vals, &res)
}
// PinChatMessage is used to add a message to the list of pinned messages in the chat.
// If the chat is not a private chat, the bot must be an administrator in the chat for this to work
// and must have the 'can_pin_messages' admin right in a supergroup or 'can_edit_messages' admin right in a channel.
func (a API) PinChatMessage(chatID int64, messageID int, opts *PinMessageOptions) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("message_id", itoa(int64(messageID)))
return res, client.get(a.base, "pinChatMessage", addValues(vals, opts), &res)
}
// UnpinChatMessage is used to remove a message from the list of pinned messages in the chat.
// If the chat is not a private chat, the bot must be an administrator in the chat for this to work
// and must have the 'can_pin_messages' admin right in a supergroup or 'can_edit_messages' admin right in a channel.
func (a API) UnpinChatMessage(chatID int64, opts *UnpinMessageOptions) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "unpinChatMessage", addValues(vals, opts), &res)
}
// UnpinAllChatMessages is used to clear the list of pinned messages in a chat.
// If the chat is not a private chat, the bot must be an administrator in the chat for this to work
// and must have the 'can_pin_messages' admin right in a supergroup or 'can_edit_messages' admin right in a channel.
func (a API) UnpinAllChatMessages(chatID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "unpinAllChatMessages", vals, &res)
}
// LeaveChat is used to make the bot leave a group, supergroup or channel.
func (a API) LeaveChat(chatID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "leaveChat", vals, &res)
}
// GetChat is used to get up to date information about the chat.
// (current name of the user for one-on-one conversations, current username of a user, group or channel, etc.)
func (a API) GetChat(chatID int64) (res APIResponseChat, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "getChat", vals, &res)
}
// GetChatAdministrators is used to get a list of administrators in a chat.
func (a API) GetChatAdministrators(chatID int64) (res APIResponseAdministrators, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "getChatAdministrators", vals, &res)
}
// GetChatMemberCount is used to get the number of members in a chat.
func (a API) GetChatMemberCount(chatID int64) (res APIResponseInteger, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "getChatMemberCount", vals, &res)
}
// GetChatMember is used to get information about a member of a chat.
func (a API) GetChatMember(chatID, userID int64) (res APIResponseChatMember, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("user_id", itoa(userID))
return res, client.get(a.base, "getChatMember", vals, &res)
}
// SetChatStickerSet is used to set a new group sticker set for a supergroup.
// The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
// Use the field `CanSetStickerSet` optionally returned in GetChat requests to check if the bot can use this method.
func (a API) SetChatStickerSet(chatID int64, stickerSetName string) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("sticker_set_name", stickerSetName)
return res, client.get(a.base, "setChatStickerSet", vals, &res)
}
// DeleteChatStickerSet is used to delete a group sticker set for a supergroup.
// The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
// Use the field `CanSetStickerSet` optionally returned in GetChat requests to check if the bot can use this method.
func (a API) DeleteChatStickerSet(chatID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "deleteChatStickerSet", vals, &res)
}
// CreateForumTopic is used to create a topic in a forum supergroup chat.
// The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights.
func (a API) CreateForumTopic(chatID int64, name string, opts *CreateTopicOptions) (res APIResponseForumTopic, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("name", name)
return res, client.get(a.base, "createForumTopic", addValues(vals, opts), &res)
}
// EditForumTopic is used to edit name and icon of a topic in a forum supergroup chat.
// The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights.
func (a API) EditForumTopic(chatID, messageThreadID int64, opts *EditTopicOptions) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("message_thread_id", itoa(messageThreadID))
return res, client.get(a.base, "editForumTopic", addValues(vals, opts), &res)
}
// CloseForumTopic is used to close an open topic in a forum supergroup chat.
// The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights.
func (a API) CloseForumTopic(chatID, messageThreadID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("message_thread_id", itoa(messageThreadID))
return res, client.get(a.base, "closeForumTopic", vals, &res)
}
// ReopenForumTopic is used to reopen a closed topic in a forum supergroup chat.
// The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights.
func (a API) ReopenForumTopic(chatID, messageThreadID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("message_thread_id", itoa(messageThreadID))
return res, client.get(a.base, "reopenForumTopic", vals, &res)
}
// DeleteForumTopic is used to delete a forum topic along with all its messages in a forum supergroup chat.
// The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights.
func (a API) DeleteForumTopic(chatID, messageThreadID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("message_thread_id", itoa(messageThreadID))
return res, client.get(a.base, "deleteForumTopic", vals, &res)
}
// UnpinAllForumTopicMessages is used to clear the list of pinned messages in a forum topic.
// The bot must be an administrator in the chat for this to work and must have the can_manage_topics administrator rights.
func (a API) UnpinAllForumTopicMessages(chatID, messageThreadID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("message_thread_id", itoa(messageThreadID))
return res, client.get(a.base, "unpinAllForumTopicMessages", vals, &res)
}
// EditGeneralForumTopic is used to edit the name of the 'General' topic in a forum supergroup chat.
// The bot must be an administrator in the chat for this to work and must have can_manage_topics administrator rights.
func (a API) EditGeneralForumTopic(chatID int64, name string) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("name", name)
return res, client.get(a.base, "editGeneralForumTopic", vals, &res)
}
// CloseGeneralForumTopic is used to close an open 'General' topic in a forum supergroup chat.
// The bot must be an administrator in the chat for this to work and must have can_manage_topics administrator rights.
func (a API) CloseGeneralForumTopic(chatID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "closeGeneralForumTopic", vals, &res)
}
// ReopenGeneralForumTopic is used to reopen a closed 'General' topic in a forum supergroup chat.
// The bot must be an administrator in the chat for this to work and must have can_manage_topics administrator rights.
// The topic will be automatically unhidden if it was hidden.
func (a API) ReopenGeneralForumTopic(chatID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "reopenGeneralForumTopic", vals, &res)
}
// HideGeneralForumTopic is used to hide the 'General' topic in a forum supergroup chat.
// The bot must be an administrator in the chat for this to work and must have can_manage_topics administrator rights.
// The topic will be automatically closed if it was open.
func (a API) HideGeneralForumTopic(chatID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "hideGeneralForumTopic", vals, &res)
}
// UnhideGeneralForumTopic is used to unhide the 'General' topic in a forum supergroup chat.
// The bot must be an administrator in the chat for this to work and must have can_manage_topics administrator rights.
func (a API) UnhideGeneralForumTopic(chatID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "unhideGeneralForumTopic", vals, &res)
}
// UnpinAllGeneralForumTopicMessages is used to clear the list of pinned messages in a General forum topic.
// The bot must be an administrator in the chat for this to work and must have can_pin_messages administrator right in the supergroup.
func (a API) UnpinAllGeneralForumTopicMessages(chatID int64) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
return res, client.get(a.base, "unpinAllGeneralForumTopicMessages", vals, &res)
}
// AnswerCallbackQuery is used to send answers to callback queries sent from inline keyboards.
// The answer will be displayed to the user as a notification at the top of the chat screen or as an alert.
func (a API) AnswerCallbackQuery(callbackID string, opts *CallbackQueryOptions) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("callback_query_id", callbackID)
return res, client.get(a.base, "answerCallbackQuery", addValues(vals, opts), &res)
}
// GetUserChatBoosts is used to get the list of boosts added to a chat by a user.
// Requires administrator rights in the chat.
func (a API) GetUserChatBoosts(chatID, userID int64) (res APIResponseUserChatBoosts, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("user_id", itoa(userID))
return res, client.get(a.base, "getUserChatBoosts", vals, &res)
}
// GetBusinessConnection is used to get information about the connection of the bot with a business account.
func (a API) GetBusinessConnection(business_connection_id string) (res APIResponseBusinessConnection, err error) {
var vals = make(url.Values)
vals.Set("business_connection_id", business_connection_id)
return res, client.get(a.base, "getBusinessConnection", vals, &res)
}
// SetMyCommands is used to change the list of the bot's commands for the given scope and user language.
func (a API) SetMyCommands(opts *CommandOptions, commands ...BotCommand) (res APIResponseBool, err error) {
var vals = make(url.Values)
jsn, _ := json.Marshal(commands)
vals.Set("commands", string(jsn))
return res, client.get(a.base, "setMyCommands", addValues(vals, opts), &res)
}
// DeleteMyCommands is used to delete the list of the bot's commands for the given scope and user language.
func (a API) DeleteMyCommands(opts *CommandOptions) (res APIResponseBool, err error) {
return res, client.get(a.base, "deleteMyCommands", urlValues(opts), &res)
}
// GetMyCommands is used to get the current list of the bot's commands for the given scope and user language.
func (a API) GetMyCommands(opts *CommandOptions) (res APIResponseCommands, err error) {
return res, client.get(a.base, "getMyCommands", urlValues(opts), &res)
}
// SetMyName is used to change the bot's name.
func (a API) SetMyName(name, languageCode string) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("name", name)
vals.Set("language_code", languageCode)
return res, client.get(a.base, "setMyName", vals, &res)
}
// GetMyName is used to get the current bot name for the given user language.
func (a API) GetMyName(languageCode string) (res APIResponseBotName, err error) {
var vals = make(url.Values)
vals.Set("language_code", languageCode)
return res, client.get(a.base, "getMyName", vals, &res)
}
// SetMyDescription is used to to change the bot's description, which is shown in the chat with the bot if the chat is empty.
func (a API) SetMyDescription(description, languageCode string) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("description", description)
vals.Set("language_code", languageCode)
return res, client.get(a.base, "setMyDescription", vals, &res)
}
// GetMyDescription is used to get the current bot description for the given user language.
func (a API) GetMyDescription(languageCode string) (res APIResponseBotDescription, err error) {
var vals = make(url.Values)
vals.Set("language_code", languageCode)
return res, client.get(a.base, "getMyDescription", vals, &res)
}
// SetMyShortDescription is used to to change the bot's short description,
// which is shown on the bot's profile page and is sent together with the link when users share the bot.
func (a API) SetMyShortDescription(shortDescription, languageCode string) (res APIResponseBool, err error) {
var vals = make(url.Values)
vals.Set("short_description", shortDescription)
vals.Set("language_code", languageCode)
return res, client.get(a.base, "setMyShortDescription", vals, &res)
}
// GetMyShortDescription is used to get the current bot short description for the given user language.
func (a API) GetMyShortDescription(languageCode string) (res APIResponseBotShortDescription, err error) {
var vals = make(url.Values)
vals.Set("language_code", languageCode)
return res, client.get(a.base, "getMyDescription", vals, &res)
}
// EditMessageText is used to edit text and game messages.
func (a API) EditMessageText(text string, msg MessageIDOptions, opts *MessageTextOptions) (res APIResponseMessage, err error) {
var vals = make(url.Values)
vals.Set("text", text)
return res, client.get(a.base, "editMessageText", addValues(addValues(vals, msg), opts), &res)
}
// EditMessageCaption is used to edit captions of messages.
func (a API) EditMessageCaption(msg MessageIDOptions, opts *MessageCaptionOptions) (res APIResponseMessage, err error) {
return res, client.get(a.base, "editMessageCaption", addValues(urlValues(msg), opts), &res)
}
// EditMessageMedia is used to edit animation, audio, document, photo or video messages.
// If a message is part of a message album, then it can be edited only to an audio for audio albums,
// only to a document for document albums and to a photo or a video otherwise.
// When an inline message is edited, a new file can't be uploaded.
// Use a previously uploaded file via its file_id or specify a URL.
func (a API) EditMessageMedia(msg MessageIDOptions, media InputMedia, opts *MessageMediaOptions) (res APIResponseMessage, err error) {
return res, client.postMedia(a.base, "editMessageMedia", true, addValues(urlValues(msg), opts), &res, media)
}
// EditMessageReplyMarkup is used to edit only the reply markup of messages.
func (a API) EditMessageReplyMarkup(msg MessageIDOptions, opts *MessageReplyMarkupOptions) (res APIResponseMessage, err error) {
return res, client.get(a.base, "editMessageReplyMarkup", addValues(urlValues(msg), opts), &res)
}
// StopPoll is used to stop a poll which was sent by the bot.
func (a API) StopPoll(chatID int64, messageID int, opts *StopPollOptions) (res APIResponsePoll, err error) {
var vals = make(url.Values)
vals.Set("chat_id", itoa(chatID))
vals.Set("message_id", itoa(int64(messageID)))
return res, client.get(a.base, "stopPoll", addValues(vals, opts), &res)
}
// DeleteMessage is used to delete a message, including service messages, with the following limitations:
// - A message can only be deleted if it was sent less than 48 hours ago.
// - A dice message in a private chat can only be deleted if it was sent more than 24 hours ago.
// - Bots can delete outgoing messages in private chats, groups, and supergroups.
// - Bots can delete incoming messages in private chats.
// - Bots granted can_post_messages permissions can delete outgoing messages in channels.
// - If the bot is an administrator of a group, it can delete any message there.
// - If the bot has can_delete_messages permission in a supergroup or a channel, it can delete any message there.
func (a API) DeleteMessage(chatID int64, messageID int) (res APIResponseBase, err error) {