-
Notifications
You must be signed in to change notification settings - Fork 55
/
client.go
2557 lines (2353 loc) · 81.9 KB
/
client.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 tbot
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
// Client is a low-level Telegram client
type Client struct {
token string
baseURL string
url string
httpClient *http.Client
nextOffset int
logger Logger
bufferSize int
timeout int
updatesParams url.Values
}
// NewClient creates new Telegram API client
func NewClient(token string, httpClient *http.Client, baseURL string) *Client {
return &Client{
token: token,
httpClient: httpClient,
baseURL: baseURL,
url: fmt.Sprintf("%s/bot%s/", baseURL, token) + "%s",
}
}
type inputFile struct {
field string
name string
}
type sendOption func(url.Values)
// Generic message options
var (
OptParseModeHTML = func(r url.Values) {
r.Set("parse_mode", "HTML")
}
OptParseModeMarkdown = func(r url.Values) {
r.Set("parse_mode", "MarkdownV2")
}
OptDisableNotification = func(r url.Values) {
r.Set("disable_notification", "true")
}
OptReplyToMessageID = func(id int) sendOption {
return func(r url.Values) {
r.Set("reply_to_message_id", strconv.Itoa(id))
}
}
)
func structString(s interface{}) string {
str, _ := json.Marshal(s)
return string(str)
}
// GetMe returns info about bot as a User object
func (c *Client) GetMe() (*User, error) {
me := &User{}
err := c.doRequest("getMe", nil, me)
return me, err
}
type forceReply struct {
ForceReply bool `json:"force_reply"`
Selective bool `json:"selective"`
}
type replyKeyboardRemove struct {
RemoveKeyboard bool `json:"remove_keyboard"`
Selective bool `json:"selective"`
}
// InlineKeyboardMarkup represents an inline keyboard that appears right next to the message it belongs to
type InlineKeyboardMarkup struct {
InlineKeyboard [][]InlineKeyboardButton `json:"inline_keyboard"`
}
// InlineKeyboardButton represents one button of an inline keyboard
type InlineKeyboardButton struct {
Text string `json:"text"`
URL string `json:"url,omitempty"`
LoginURL *LoginURL `json:"login_url,omitempty"`
CallbackData string `json:"callback_data,omitempty"`
SwitchInlineQuery *string `json:"switch_inline_query,omitempty"`
SwitchInlineQueryCurrentChat *string `json:"switch_inline_query_current_chat,omitempty"`
}
// LoginURL is a property of InlineKeyboardButton for Seamless Login feature
type LoginURL struct {
URL string `json:"url"`
ForwardText *string `json:"forward_text,omitempty"`
BotUsername *string `json:"bot_username,omitempty"`
RequestWriteAccess *string `json:"request_write_access,omitempty"`
}
// ReplyKeyboardMarkup represents a custom keyboard with reply options
type ReplyKeyboardMarkup struct {
Keyboard [][]KeyboardButton `json:"keyboard"`
ResizeKeyboard bool `json:"resize_keyboard"`
OneTimeKeyboard bool `json:"one_time_keyboard"`
Selective bool `json:"selective"`
}
// KeyboardButton represents one button of the reply keyboard
type KeyboardButton struct {
Text string `json:"text"`
RequestContact bool `json:"request_contact"`
RequestLocation bool `json:"request_location"`
RequestPoll *KeyboardButtonPollType `json:"request_poll,omitempty"`
}
// KeyboardButtonPollType represents type of a poll,
// which is allowed to be created and sent when the corresponding button is pressed
type KeyboardButtonPollType struct {
Type string `json:"type"`
}
func (c *Client) setWebhook(webhookURL string) error {
req := url.Values{}
req.Set("url", webhookURL)
var set bool
return c.doRequest("setWebhook", req, &set)
}
func (c *Client) deleteWebhook() error {
var ok bool
return c.doRequest("deleteWebhook", url.Values{}, &ok)
}
// SendMessage options
var (
OptDisableWebPagePreview = func(r url.Values) {
r.Set("disable_web_page_preview", "true")
}
OptInlineKeyboardMarkup = func(markup *InlineKeyboardMarkup) sendOption {
return func(r url.Values) {
r.Set("reply_markup", structString(markup))
}
}
OptReplyKeyboardMarkup = func(markup *ReplyKeyboardMarkup) sendOption {
return func(r url.Values) {
r.Set("reply_markup", structString(markup))
}
}
OptReplyKeyboardRemove = func(r url.Values) {
r.Set("reply_markup", structString(&replyKeyboardRemove{RemoveKeyboard: true}))
}
OptReplyKeyboardRemoveSelective = func(r url.Values) {
r.Set("reply_markup", structString(&replyKeyboardRemove{RemoveKeyboard: true, Selective: true}))
}
OptForceReply = func(r url.Values) {
r.Set("reply_markup", structString(&forceReply{ForceReply: true}))
}
OptForceReplySelective = func(r url.Values) {
r.Set("reply_markup", structString(&forceReply{ForceReply: true, Selective: true}))
}
)
/*
SendMessage sends message to telegram chat. Available options:
- OptParseModeHTML
- OptParseModeMarkdown
- OptDisableWebPagePreview
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendMessage(chatID string, text string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("text", text)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequest("sendMessage", req, msg)
return msg, err
}
/*
ForwardMessage forwards message from one chat to another. Available options:
- OptDisableNotification
*/
func (c *Client) ForwardMessage(chatID, fromChatID string, messageID int, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("from_chat_id", fromChatID)
req.Set("message_id", strconv.Itoa(messageID))
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequest("forwardMessage", req, msg)
return msg, err
}
// SendAudio options
var (
OptDuration = func(duration int) sendOption {
return func(r url.Values) {
r.Set("duration", strconv.Itoa(duration))
}
}
OptPerformer = func(performer string) sendOption {
return func(r url.Values) {
r.Set("performer", performer)
}
}
OptTitle = func(title string) sendOption {
return func(r url.Values) {
r.Set("title", title)
}
}
)
/*
SendAudio sends pre-uploaded audio to the chat. Pass fileID of the uploaded file. Available options:
- OptCaption(caption string)
- OptDuration(duration int)
- OptPerformer(performer string)
- OptTitle(title string)
- OptParseModeHTML
- OptParseModeMarkdown
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendAudio(chatID string, fileID string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("audio", fileID)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequest("sendAudio", req, msg)
return msg, err
}
/*
SendAudioFile sends file contents as an audio to the chat. Pass filename to send. Available options:
- OptCaption(caption string)
- OptDuration(duration int)
- OptPerformer(performer string)
- OptTitle(title string)
- OptParseModeHTML
- OptParseModeMarkdown
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendAudioFile(chatID string, filename string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequestWithFiles("sendAudio", req, msg, inputFile{field: "audio", name: filename})
return msg, err
}
// SendPhoto options
var (
OptCaption = func(caption string) sendOption {
return func(r url.Values) {
r.Set("caption", caption)
}
}
)
/*
SendPhoto sends pre-uploaded photo to the chat. Pass fileID of the photo. Available options:
- OptCaption(caption string)
- OptParseModeHTML
- OptParseModeMarkdown
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendPhoto(chatID string, fileID string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("photo", fileID)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequest("sendPhoto", req, msg)
return msg, err
}
/*
SendPhotoFile sends photo file contents to the chat. Pass filename to send. Available options:
- OptCaption(caption string)
- OptParseModeHTML
- OptParseModeMarkdown
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendPhotoFile(chatID string, filename string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequestWithFiles("sendPhoto", req, msg, inputFile{field: "photo", name: filename})
return msg, err
}
/*
SendDocument sends document to the chat. Pass fileID of the document. Available options:
- OptCaption(caption string)
- OptParseModeHTML
- OptParseModeMarkdown
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendDocument(chatID string, fileID string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("document", fileID)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequest("sendDocument", req, msg)
return msg, err
}
/*
SendDocumentFile sends document file contents to the chat. Pass filename to send. Available options:
- OptCaption(caption string)
- OptParseModeHTML
- OptParseModeMarkdown
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendDocumentFile(chatID string, filename string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequestWithFiles("sendDocument", req, msg, inputFile{field: "document", name: filename})
return msg, err
}
// SendVideo options
var (
OptWidth = func(width int) sendOption {
return func(r url.Values) {
r.Set("width", strconv.Itoa(width))
}
}
OptHeight = func(height int) sendOption {
return func(r url.Values) {
r.Set("height", strconv.Itoa(height))
}
}
OptSupportsStreaming = func(r url.Values) {
r.Set("supports_streaming", "true")
}
)
/*
SendVideo sends pre-uploaded video to chat. Pass fileID of the uploaded video. Available options:
- OptDuration(duration int)
- OptWidth(width int)
- OptHeight(height int)
- OptSupportsStreaming
- OptCaption(caption string)
- OptParseModeHTML
- OptParseModeMarkdown
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendVideo(chatID string, fileID string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("video", fileID)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequest("sendVideo", req, msg)
return msg, err
}
/*
SendVideoFile sends video file contents to the chat. Pass filename to send. Available options:
- OptDuration(duration int)
- OptWidth(width int)
- OptHeight(height int)
- OptSupportsStreaming
- OptCaption(caption string)
- OptParseModeHTML
- OptParseModeMarkdown
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendVideoFile(chatID string, filename string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequestWithFiles("sendVideo", req, msg, inputFile{field: "video", name: filename})
return msg, err
}
// SendAnimation options
var (
OptThumb = func(filename string) sendOption {
return func(v url.Values) {
v.Set("thumb", filename)
}
}
)
/*
SendAnimation sends animation to chat. Pass fileID to send. Available options:
- OptDuration(duration int)
- OptWidth(width int)
- OptHeight(height int)
- OptThumb(filename string)
- OptCaption(caption string)
- OptParseModeHTML
- OptParseModeMarkdown
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendAnimation(chatID string, fileID string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("animation", fileID)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
var err error
if len(req.Get("thumb")) > 0 {
thumb := req.Get("thumb")
req.Del("thumb")
err = c.doRequestWithFiles("sendAnimation", req, msg, inputFile{field: "thumb", name: thumb})
} else {
err = c.doRequest("sendAnimation", req, msg)
}
return msg, err
}
/*
SendAnimationFile sends animation file contents to the chat. Pass filename to send. Available options:
- OptDuration(duration int)
- OptWidth(width int)
- OptHeight(height int)
- OptThumb(filename string)
- OptCaption(caption string)
- OptParseModeHTML
- OptParseModeMarkdown
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendAnimationFile(chatID string, filename string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
files := []inputFile{{field: "animation", name: filename}}
if len(req.Get("thumb")) > 0 {
thumb := req.Get("thumb")
req.Del("thumb")
files = append(files, inputFile{field: "thumb", name: thumb})
}
err := c.doRequestWithFiles("sendAnimation", req, msg, files...)
return msg, err
}
/*
SendVoice sends audio file as a voice message. Pass file_id of previously uploaded file. Available options:
- OptCaption(caption string)
- OptDuration(duration int)
- OptParseModeHTML
- OptParseModeMarkdown
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendVoice(chatID string, fileID string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("voice", fileID)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequest("sendVoice", req, msg)
return msg, err
}
/*
SendVoiceFile sends the audio file as a voice message. Pass filename to send. Available options:
- OptCaption(caption string)
- OptDuration(duration int)
- OptParseModeHTML
- OptParseModeMarkdown
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendVoiceFile(chatID string, filename string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequestWithFiles("sendVoice", req, msg, inputFile{field: "voice", name: filename})
return msg, err
}
// SendVideoNote options
var (
OptLength = func(length int) sendOption {
return func(v url.Values) {
v.Set("length", fmt.Sprint(length))
}
}
)
/*
SendVideoNote sends video note. Pass fileID of previously uploaded video note. Available options:
- OptDuration(duration int)
- OptLength(length int)
- OptThumb(filename string)
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendVideoNote(chatID string, fileID string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("video_note", fileID)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
var err error
if len(req.Get("thumb")) > 0 {
thumb := req.Get("thumb")
req.Del("thumb")
err = c.doRequestWithFiles("sendVideoNote", req, msg, inputFile{field: "thumb", name: thumb})
} else {
err = c.doRequest("sendVideoNote", req, msg)
}
return msg, err
}
/*
SendVideoNoteFile sends video note to chat. Pass filename to upload. Available options:
- OptDuration(duration int)
- OptLength(length int)
- OptThumb(filename string)
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendVideoNoteFile(chatID string, filename string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
for _, opt := range opts {
opt(req)
}
files := []inputFile{{field: "video_note", name: filename}}
if len(req.Get("thumb")) > 0 {
thumb := req.Get("thumb")
req.Del("thumb")
files = append(files, inputFile{field: "thumb", name: thumb})
}
msg := &Message{}
err := c.doRequestWithFiles("sendVideoNote", req, msg, files...)
return msg, err
}
// InputMedia file
type InputMedia interface {
inputMedia()
}
var (
_ InputMedia = InputMediaPhoto{}
_ InputMedia = InputMediaVideo{}
)
// InputMediaPhoto represents a photo to be sent
type InputMediaPhoto struct {
Type string `json:"type"`
Media string `json:"media"`
Caption string `json:"caption,omitempty"`
ParseMode string `json:"parse_mode,omitempty"`
}
func (InputMediaPhoto) inputMedia() {}
// InputMediaVideo represents a video to be sent
type InputMediaVideo struct {
Type string `json:"type"`
Media string `json:"media"`
Thumb string `json:"thumb,omitempty"`
Caption string `json:"caption,omitempty"`
ParseMode string `json:"parse_mode,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
Duration int `json:"duration,omitempty"`
SupportsStreaming bool `json:"supports_streaming,omitempty"`
}
func (InputMediaVideo) inputMedia() {}
// SendMediaGroup send a group of photos or videos as an album
func (c *Client) SendMediaGroup(chatID string, media []InputMedia, opts ...sendOption) ([]*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
m, _ := json.Marshal(media)
req.Set("media", string(m))
for _, opt := range opts {
opt(req)
}
var msgs []*Message
err := c.doRequest("sendMediaGroup", req, &msgs)
return msgs, err
}
// SendLocation options
var (
OptLivePeriod = func(period int) sendOption {
return func(v url.Values) {
v.Set("live_period", fmt.Sprint(period))
}
}
)
/*
SendLocation sends point on the map to chat. Available options:
- OptLivePeriod(period int)
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendLocation(chatID string, latitude, longitude float64, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("latitude", fmt.Sprint(latitude))
req.Set("longitude", fmt.Sprint(longitude))
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequest("sendLocation", req, msg)
return msg, err
}
/*
EditMessageLiveLocation edits location in message sent by the bot. Available options:
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
*/
func (c *Client) EditMessageLiveLocation(chatID string, messageID int, latitude, longitude float64, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("message_id", fmt.Sprint(messageID))
req.Set("latitude", fmt.Sprint(latitude))
req.Set("longitude", fmt.Sprint(longitude))
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequest("editMessageLiveLocation", req, msg)
return msg, err
}
/*
EditInlineMessageLiveLocation edits location in message sent via the bot (using inline mode). Available options:
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
*/
func (c *Client) EditInlineMessageLiveLocation(inlineMessageID string, latitude, longitude float64, opts ...sendOption) error {
req := url.Values{}
req.Set("inline_message_id", inlineMessageID)
req.Set("latitude", fmt.Sprint(latitude))
req.Set("longitude", fmt.Sprint(longitude))
for _, opt := range opts {
opt(req)
}
var edited bool
err := c.doRequest("editMessageLiveLocation", req, &edited)
return err
}
/*
StopMessageLiveLocation stop updating a live location message sent by the bot. Available options:
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
*/
func (c *Client) StopMessageLiveLocation(chatID string, messageID int, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("message_id", fmt.Sprint(messageID))
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequest("stopMessageLiveLocation", req, msg)
return msg, err
}
/*
StopInlineMessageLiveLocation stop updating a live location message sent via the bot (using inline mode). Available options:
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
*/
func (c *Client) StopInlineMessageLiveLocation(inlineMessageID string, opts ...sendOption) error {
req := url.Values{}
req.Set("inline_message_id", inlineMessageID)
for _, opt := range opts {
opt(req)
}
var stopped bool
return c.doRequest("stopMessageLiveLocation", req, &stopped)
}
// SendVenue options
var (
OptFoursquareID = func(foursquareID string) sendOption {
return func(v url.Values) {
v.Set("foursquare_id", foursquareID)
}
}
OptFoursquareType = func(foursquareType string) sendOption {
return func(v url.Values) {
v.Set("foursquare_type", foursquareType)
}
}
)
/*
SendVenue sends information about a venue. Available options:
- OptFoursquareID(foursquareID string)
- OptFoursquareType(foursquareType string)
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendVenue(chatID string, latitude, longitude float64, title, address string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("latitude", fmt.Sprint(latitude))
req.Set("longitude", fmt.Sprint(longitude))
req.Set("title", title)
req.Set("address", address)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequest("sendVenue", req, msg)
return msg, err
}
// SendContact options
var (
OptLastName = func(lastName string) sendOption {
return func(v url.Values) {
v.Set("last_name", lastName)
}
}
OptVCard = func(vCard string) sendOption {
return func(v url.Values) {
v.Set("vcard", vCard)
}
}
)
/*
SendContact sends phone contact. Available options:
- OptLastName(lastName string)
- OptVCard(vCard string) TODO: implement vCard support (https://tools.ietf.org/html/rfc6350)
- OptDisableNotification
- OptReplyToMessageID(id int)
- OptInlineKeyboardMarkup(markup *InlineKeyboardMarkup)
- OptReplyKeyboardMarkup(markup *ReplyKeyboardMarkup)
- OptReplyKeyboardRemove
- OptReplyKeyboardRemoveSelective
- OptForceReply
- OptForceReplySelective
*/
func (c *Client) SendContact(chatID, phoneNumber, firstName string, opts ...sendOption) (*Message, error) {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("phone_number", phoneNumber)
req.Set("first_name", firstName)
for _, opt := range opts {
opt(req)
}
msg := &Message{}
err := c.doRequest("sendContact", req, msg)
return msg, err
}
type chatAction string
// Actions for SendChatAction
const (
ActionTyping chatAction = "typing"
ActionUploadPhoto chatAction = "upload_photo"
ActionRecordVideo chatAction = "record_video"
ActionUploadVideo chatAction = "upload_video"
ActionRecordAudio chatAction = "record_audio"
ActionUploadAudio chatAction = "upload_audio"
ActionUploadDocument chatAction = "upload_document"
ActionFindLocation chatAction = "find_location"
ActionRecordVideoNote chatAction = "record_video_note"
ActionUploadVideoNote chatAction = "upload_video_note"
)
/*
SendChatAction sends bot chat action. Available actions:
- ActionTyping
- ActionUploadPhoto
- ActionRecordVideo
- ActionUploadVideo
- ActionRecordAudio
- ActionUploadAudio
- ActionUploadDocument
- ActionFindLocation
- ActionRecordVideoNote
- ActionUploadVideoNote
*/
func (c *Client) SendChatAction(chatID string, action chatAction) error {
req := url.Values{}
req.Set("chat_id", chatID)
req.Set("action", string(action))
var sent bool
return c.doRequest("sendChatAction", req, &sent)
}
// UserProfilePhotos represent a user's profile pictures
type UserProfilePhotos struct {
TotalCount int `json:"total_count"`
Photos [][]PhotoSize `json:"photos"`
}
// GetUserProfilePhotos options
var (
OptOffset = func(offset int) sendOption {
return func(v url.Values) {
v.Set("offset", fmt.Sprint(offset))
}
}
OptLimit = func(limit int) sendOption {
return func(v url.Values) {
v.Set("limit", fmt.Sprint(limit))
}
}
)
/*
GetUserProfilePhotos returs user's profile pictures. Available options:
- OptOffset(offset int)
- OptLimit(limit int)
*/
func (c *Client) GetUserProfilePhotos(userID int, opts ...sendOption) (*UserProfilePhotos, error) {
req := url.Values{}
req.Set("user_id", fmt.Sprint(userID))
for _, opt := range opts {
opt(req)
}
photos := &UserProfilePhotos{}
err := c.doRequest("getUserProfilePhotos", req, photos)
return photos, err
}
// File object represents a file ready to be downloaded
type File struct {
FileID string `json:"file_id"`
FileUniqueID string `json:"file_unique_id"`
FileSize int `json:"file_size"`
FilePath string `json:"file_path"` // use https://api.telegram.org/file/bot<token>/<file_path> to download
}
/*
GetFile returns File object by fileID.
*/
func (c *Client) GetFile(fileID string) (*File, error) {
req := url.Values{}
req.Set("file_id", fileID)
file := &File{}
err := c.doRequest("getFile", req, file)
return file, err