bot_test.go (view raw)
1package tgbotapi
2
3import (
4 "io/ioutil"
5 "net/http"
6 "os"
7 "testing"
8 "time"
9)
10
11const (
12 TestToken = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I"
13 ChatID = 76918703
14 Channel = "@tgbotapitest"
15 SupergroupChatID = -1001120141283
16 ReplyToMessageID = 35
17 ExistingPhotoFileID = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC"
18 ExistingDocumentFileID = "BQADAgADOQADjMcoCcioX1GrDvp3Ag"
19 ExistingAudioFileID = "BQADAgADRgADjMcoCdXg3lSIN49lAg"
20 ExistingVoiceFileID = "AwADAgADWQADjMcoCeul6r_q52IyAg"
21 ExistingVideoFileID = "BAADAgADZgADjMcoCav432kYe0FRAg"
22 ExistingVideoNoteFileID = "DQADAgADdQAD70cQSUK41dLsRMqfAg"
23 ExistingStickerFileID = "BQADAgADcwADjMcoCbdl-6eB--YPAg"
24)
25
26type testLogger struct {
27 t *testing.T
28}
29
30func (t testLogger) Println(v ...interface{}) {
31 t.t.Log(v...)
32}
33
34func (t testLogger) Printf(format string, v ...interface{}) {
35 t.t.Logf(format, v...)
36}
37
38func getBot(t *testing.T) (*BotAPI, error) {
39 bot, err := NewBotAPI(TestToken)
40 bot.Debug = true
41
42 logger := testLogger{t}
43 SetLogger(logger)
44
45 if err != nil {
46 t.Error(err)
47 }
48
49 return bot, err
50}
51
52func TestNewBotAPI_notoken(t *testing.T) {
53 _, err := NewBotAPI("")
54
55 if err == nil {
56 t.Error(err)
57 }
58}
59
60func TestGetUpdates(t *testing.T) {
61 bot, _ := getBot(t)
62
63 u := NewUpdate(0)
64
65 _, err := bot.GetUpdates(u)
66
67 if err != nil {
68 t.Error(err)
69 }
70}
71
72func TestSendWithMessage(t *testing.T) {
73 bot, _ := getBot(t)
74
75 msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
76 msg.ParseMode = ModeMarkdown
77 _, err := bot.Send(msg)
78
79 if err != nil {
80 t.Error(err)
81 }
82}
83
84func TestSendWithMessageReply(t *testing.T) {
85 bot, _ := getBot(t)
86
87 msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
88 msg.ReplyToMessageID = ReplyToMessageID
89 _, err := bot.Send(msg)
90
91 if err != nil {
92 t.Error(err)
93 }
94}
95
96func TestSendWithMessageForward(t *testing.T) {
97 bot, _ := getBot(t)
98
99 msg := NewForward(ChatID, ChatID, ReplyToMessageID)
100 _, err := bot.Send(msg)
101
102 if err != nil {
103 t.Error(err)
104 }
105}
106
107func TestCopyMessage(t *testing.T) {
108 bot, _ := getBot(t)
109
110 msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
111 message, err := bot.Send(msg)
112 if err != nil {
113 t.Error(err)
114 }
115
116 copyMessageConfig := NewCopyMessage(SupergroupChatID, message.Chat.ID, message.MessageID)
117 messageID, err := bot.CopyMessage(copyMessageConfig)
118 if err != nil {
119 t.Error(err)
120 }
121
122 if messageID.MessageID == message.MessageID {
123 t.Error("copied message ID was the same as original message")
124 }
125}
126
127func TestSendWithNewPhoto(t *testing.T) {
128 bot, _ := getBot(t)
129
130 msg := NewPhoto(ChatID, "tests/image.jpg")
131 msg.Caption = "Test"
132 _, err := bot.Send(msg)
133
134 if err != nil {
135 t.Error(err)
136 }
137}
138
139func TestSendWithNewPhotoWithFileBytes(t *testing.T) {
140 bot, _ := getBot(t)
141
142 data, _ := ioutil.ReadFile("tests/image.jpg")
143 b := FileBytes{Name: "image.jpg", Bytes: data}
144
145 msg := NewPhoto(ChatID, b)
146 msg.Caption = "Test"
147 _, err := bot.Send(msg)
148
149 if err != nil {
150 t.Error(err)
151 }
152}
153
154func TestSendWithNewPhotoWithFileReader(t *testing.T) {
155 bot, _ := getBot(t)
156
157 f, _ := os.Open("tests/image.jpg")
158 reader := FileReader{Name: "image.jpg", Reader: f}
159
160 msg := NewPhoto(ChatID, reader)
161 msg.Caption = "Test"
162 _, err := bot.Send(msg)
163
164 if err != nil {
165 t.Error(err)
166 }
167}
168
169func TestSendWithNewPhotoReply(t *testing.T) {
170 bot, _ := getBot(t)
171
172 msg := NewPhoto(ChatID, "tests/image.jpg")
173 msg.ReplyToMessageID = ReplyToMessageID
174
175 _, err := bot.Send(msg)
176
177 if err != nil {
178 t.Error(err)
179 }
180}
181
182func TestSendNewPhotoToChannel(t *testing.T) {
183 bot, _ := getBot(t)
184
185 msg := NewPhotoToChannel(Channel, "tests/image.jpg")
186 msg.Caption = "Test"
187 _, err := bot.Send(msg)
188
189 if err != nil {
190 t.Error(err)
191 t.Fail()
192 }
193}
194
195func TestSendNewPhotoToChannelFileBytes(t *testing.T) {
196 bot, _ := getBot(t)
197
198 data, _ := ioutil.ReadFile("tests/image.jpg")
199 b := FileBytes{Name: "image.jpg", Bytes: data}
200
201 msg := NewPhotoToChannel(Channel, b)
202 msg.Caption = "Test"
203 _, err := bot.Send(msg)
204
205 if err != nil {
206 t.Error(err)
207 t.Fail()
208 }
209}
210
211func TestSendNewPhotoToChannelFileReader(t *testing.T) {
212 bot, _ := getBot(t)
213
214 f, _ := os.Open("tests/image.jpg")
215 reader := FileReader{Name: "image.jpg", Reader: f}
216
217 msg := NewPhotoToChannel(Channel, reader)
218 msg.Caption = "Test"
219 _, err := bot.Send(msg)
220
221 if err != nil {
222 t.Error(err)
223 t.Fail()
224 }
225}
226
227func TestSendWithExistingPhoto(t *testing.T) {
228 bot, _ := getBot(t)
229
230 msg := NewPhoto(ChatID, FileID(ExistingPhotoFileID))
231 msg.Caption = "Test"
232 _, err := bot.Send(msg)
233
234 if err != nil {
235 t.Error(err)
236 }
237}
238
239func TestSendWithNewDocument(t *testing.T) {
240 bot, _ := getBot(t)
241
242 msg := NewDocument(ChatID, "tests/image.jpg")
243 _, err := bot.Send(msg)
244
245 if err != nil {
246 t.Error(err)
247 }
248}
249
250func TestSendWithNewDocumentAndThumb(t *testing.T) {
251 bot, _ := getBot(t)
252
253 msg := NewDocument(ChatID, "tests/voice.ogg")
254 msg.Thumb = "tests/image.jpg"
255 _, err := bot.Send(msg)
256
257 if err != nil {
258 t.Error(err)
259 }
260}
261
262func TestSendWithExistingDocument(t *testing.T) {
263 bot, _ := getBot(t)
264
265 msg := NewDocument(ChatID, FileID(ExistingDocumentFileID))
266 _, err := bot.Send(msg)
267
268 if err != nil {
269 t.Error(err)
270 }
271}
272
273func TestSendWithNewAudio(t *testing.T) {
274 bot, _ := getBot(t)
275
276 msg := NewAudio(ChatID, "tests/audio.mp3")
277 msg.Title = "TEST"
278 msg.Duration = 10
279 msg.Performer = "TEST"
280 _, err := bot.Send(msg)
281
282 if err != nil {
283 t.Error(err)
284 }
285}
286
287func TestSendWithExistingAudio(t *testing.T) {
288 bot, _ := getBot(t)
289
290 msg := NewAudio(ChatID, FileID(ExistingAudioFileID))
291 msg.Title = "TEST"
292 msg.Duration = 10
293 msg.Performer = "TEST"
294
295 _, err := bot.Send(msg)
296
297 if err != nil {
298 t.Error(err)
299 }
300}
301
302func TestSendWithNewVoice(t *testing.T) {
303 bot, _ := getBot(t)
304
305 msg := NewVoice(ChatID, "tests/voice.ogg")
306 msg.Duration = 10
307 _, err := bot.Send(msg)
308
309 if err != nil {
310 t.Error(err)
311 }
312}
313
314func TestSendWithExistingVoice(t *testing.T) {
315 bot, _ := getBot(t)
316
317 msg := NewVoice(ChatID, FileID(ExistingVoiceFileID))
318 msg.Duration = 10
319 _, err := bot.Send(msg)
320
321 if err != nil {
322 t.Error(err)
323 }
324}
325
326func TestSendWithContact(t *testing.T) {
327 bot, _ := getBot(t)
328
329 contact := NewContact(ChatID, "5551234567", "Test")
330
331 if _, err := bot.Send(contact); err != nil {
332 t.Error(err)
333 }
334}
335
336func TestSendWithLocation(t *testing.T) {
337 bot, _ := getBot(t)
338
339 _, err := bot.Send(NewLocation(ChatID, 40, 40))
340
341 if err != nil {
342 t.Error(err)
343 }
344}
345
346func TestSendWithVenue(t *testing.T) {
347 bot, _ := getBot(t)
348
349 venue := NewVenue(ChatID, "A Test Location", "123 Test Street", 40, 40)
350
351 if _, err := bot.Send(venue); err != nil {
352 t.Error(err)
353 }
354}
355
356func TestSendWithNewVideo(t *testing.T) {
357 bot, _ := getBot(t)
358
359 msg := NewVideo(ChatID, "tests/video.mp4")
360 msg.Duration = 10
361 msg.Caption = "TEST"
362
363 _, err := bot.Send(msg)
364
365 if err != nil {
366 t.Error(err)
367 }
368}
369
370func TestSendWithExistingVideo(t *testing.T) {
371 bot, _ := getBot(t)
372
373 msg := NewVideo(ChatID, FileID(ExistingVideoFileID))
374 msg.Duration = 10
375 msg.Caption = "TEST"
376
377 _, err := bot.Send(msg)
378
379 if err != nil {
380 t.Error(err)
381 }
382}
383
384func TestSendWithNewVideoNote(t *testing.T) {
385 bot, _ := getBot(t)
386
387 msg := NewVideoNote(ChatID, 240, "tests/videonote.mp4")
388 msg.Duration = 10
389
390 _, err := bot.Send(msg)
391
392 if err != nil {
393 t.Error(err)
394 }
395}
396
397func TestSendWithExistingVideoNote(t *testing.T) {
398 bot, _ := getBot(t)
399
400 msg := NewVideoNote(ChatID, 240, FileID(ExistingVideoNoteFileID))
401 msg.Duration = 10
402
403 _, err := bot.Send(msg)
404
405 if err != nil {
406 t.Error(err)
407 }
408}
409
410func TestSendWithNewSticker(t *testing.T) {
411 bot, _ := getBot(t)
412
413 msg := NewSticker(ChatID, "tests/image.jpg")
414
415 _, err := bot.Send(msg)
416
417 if err != nil {
418 t.Error(err)
419 }
420}
421
422func TestSendWithExistingSticker(t *testing.T) {
423 bot, _ := getBot(t)
424
425 msg := NewSticker(ChatID, FileID(ExistingStickerFileID))
426
427 _, err := bot.Send(msg)
428
429 if err != nil {
430 t.Error(err)
431 }
432}
433
434func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
435 bot, _ := getBot(t)
436
437 msg := NewSticker(ChatID, "tests/image.jpg")
438 msg.ReplyMarkup = ReplyKeyboardRemove{
439 RemoveKeyboard: true,
440 Selective: false,
441 }
442 _, err := bot.Send(msg)
443
444 if err != nil {
445 t.Error(err)
446 }
447}
448
449func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
450 bot, _ := getBot(t)
451
452 msg := NewSticker(ChatID, FileID(ExistingStickerFileID))
453 msg.ReplyMarkup = ReplyKeyboardRemove{
454 RemoveKeyboard: true,
455 Selective: false,
456 }
457
458 _, err := bot.Send(msg)
459
460 if err != nil {
461 t.Error(err)
462 }
463}
464
465func TestSendWithDice(t *testing.T) {
466 bot, _ := getBot(t)
467
468 msg := NewDice(ChatID)
469 _, err := bot.Send(msg)
470
471 if err != nil {
472 t.Error(err)
473 t.Fail()
474 }
475
476}
477
478func TestSendWithDiceWithEmoji(t *testing.T) {
479 bot, _ := getBot(t)
480
481 msg := NewDiceWithEmoji(ChatID, "🏀")
482 _, err := bot.Send(msg)
483
484 if err != nil {
485 t.Error(err)
486 t.Fail()
487 }
488
489}
490
491func TestGetFile(t *testing.T) {
492 bot, _ := getBot(t)
493
494 file := FileConfig{
495 FileID: ExistingPhotoFileID,
496 }
497
498 _, err := bot.GetFile(file)
499
500 if err != nil {
501 t.Error(err)
502 }
503}
504
505func TestSendChatConfig(t *testing.T) {
506 bot, _ := getBot(t)
507
508 _, err := bot.Request(NewChatAction(ChatID, ChatTyping))
509
510 if err != nil {
511 t.Error(err)
512 }
513}
514
515func TestSendEditMessage(t *testing.T) {
516 bot, _ := getBot(t)
517
518 msg, err := bot.Send(NewMessage(ChatID, "Testing editing."))
519 if err != nil {
520 t.Error(err)
521 }
522
523 edit := EditMessageTextConfig{
524 BaseEdit: BaseEdit{
525 ChatID: ChatID,
526 MessageID: msg.MessageID,
527 },
528 Text: "Updated text.",
529 }
530
531 _, err = bot.Send(edit)
532 if err != nil {
533 t.Error(err)
534 }
535}
536
537func TestGetUserProfilePhotos(t *testing.T) {
538 bot, _ := getBot(t)
539
540 _, err := bot.GetUserProfilePhotos(NewUserProfilePhotos(ChatID))
541 if err != nil {
542 t.Error(err)
543 }
544}
545
546func TestSetWebhookWithCert(t *testing.T) {
547 bot, _ := getBot(t)
548
549 time.Sleep(time.Second * 2)
550
551 bot.Request(DeleteWebhookConfig{})
552
553 wh := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
554 _, err := bot.Request(wh)
555 if err != nil {
556 t.Error(err)
557 }
558
559 _, err = bot.GetWebhookInfo()
560
561 if err != nil {
562 t.Error(err)
563 }
564
565 bot.Request(DeleteWebhookConfig{})
566}
567
568func TestSetWebhookWithoutCert(t *testing.T) {
569 bot, _ := getBot(t)
570
571 time.Sleep(time.Second * 2)
572
573 bot.Request(DeleteWebhookConfig{})
574
575 wh := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
576 _, err := bot.Request(wh)
577 if err != nil {
578 t.Error(err)
579 }
580
581 info, err := bot.GetWebhookInfo()
582
583 if err != nil {
584 t.Error(err)
585 }
586 if info.MaxConnections == 0 {
587 t.Errorf("Expected maximum connections to be greater than 0")
588 }
589 if info.LastErrorDate != 0 {
590 t.Errorf("failed to set webhook: %s", info.LastErrorMessage)
591 }
592
593 bot.Request(DeleteWebhookConfig{})
594}
595
596func TestSendWithMediaGroupPhotoVideo(t *testing.T) {
597 bot, _ := getBot(t)
598
599 cfg := NewMediaGroup(ChatID, []interface{}{
600 NewInputMediaPhoto(FileURL("https://github.com/go-telegram-bot-api/telegram-bot-api/raw/0a3a1c8716c4cd8d26a262af9f12dcbab7f3f28c/tests/image.jpg")),
601 NewInputMediaPhoto("tests/image.jpg"),
602 NewInputMediaVideo("tests/video.mp4"),
603 })
604
605 messages, err := bot.SendMediaGroup(cfg)
606 if err != nil {
607 t.Error(err)
608 }
609
610 if messages == nil {
611 t.Error("No received messages")
612 }
613
614 if len(messages) != len(cfg.Media) {
615 t.Errorf("Different number of messages: %d", len(messages))
616 }
617}
618
619func TestSendWithMediaGroupDocument(t *testing.T) {
620 bot, _ := getBot(t)
621
622 cfg := NewMediaGroup(ChatID, []interface{}{
623 NewInputMediaDocument(FileURL("https://i.imgur.com/unQLJIb.jpg")),
624 NewInputMediaDocument("tests/image.jpg"),
625 })
626
627 messages, err := bot.SendMediaGroup(cfg)
628 if err != nil {
629 t.Error(err)
630 }
631
632 if messages == nil {
633 t.Error("No received messages")
634 }
635
636 if len(messages) != len(cfg.Media) {
637 t.Errorf("Different number of messages: %d", len(messages))
638 }
639}
640
641func TestSendWithMediaGroupAudio(t *testing.T) {
642 bot, _ := getBot(t)
643
644 cfg := NewMediaGroup(ChatID, []interface{}{
645 NewInputMediaAudio("tests/audio.mp3"),
646 NewInputMediaAudio("tests/audio.mp3"),
647 })
648
649 messages, err := bot.SendMediaGroup(cfg)
650 if err != nil {
651 t.Error(err)
652 }
653
654 if messages == nil {
655 t.Error("No received messages")
656 }
657
658 if len(messages) != len(cfg.Media) {
659 t.Errorf("Different number of messages: %d", len(messages))
660 }
661}
662
663func ExampleNewBotAPI() {
664 bot, err := NewBotAPI("MyAwesomeBotToken")
665 if err != nil {
666 panic(err)
667 }
668
669 bot.Debug = true
670
671 log.Printf("Authorized on account %s", bot.Self.UserName)
672
673 u := NewUpdate(0)
674 u.Timeout = 60
675
676 updates := bot.GetUpdatesChan(u)
677
678 // Optional: wait for updates and clear them if you don't want to handle
679 // a large backlog of old messages
680 time.Sleep(time.Millisecond * 500)
681 updates.Clear()
682
683 for update := range updates {
684 if update.Message == nil {
685 continue
686 }
687
688 log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
689
690 msg := NewMessage(update.Message.Chat.ID, update.Message.Text)
691 msg.ReplyToMessageID = update.Message.MessageID
692
693 bot.Send(msg)
694 }
695}
696
697func ExampleNewWebhook() {
698 bot, err := NewBotAPI("MyAwesomeBotToken")
699 if err != nil {
700 panic(err)
701 }
702
703 bot.Debug = true
704
705 log.Printf("Authorized on account %s", bot.Self.UserName)
706
707 _, err = bot.Request(NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
708 if err != nil {
709 panic(err)
710 }
711
712 info, err := bot.GetWebhookInfo()
713
714 if err != nil {
715 panic(err)
716 }
717
718 if info.LastErrorDate != 0 {
719 log.Printf("failed to set webhook: %s", info.LastErrorMessage)
720 }
721
722 updates := bot.ListenForWebhook("/" + bot.Token)
723 go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
724
725 for update := range updates {
726 log.Printf("%+v\n", update)
727 }
728}
729
730func ExampleWebhookHandler() {
731 bot, err := NewBotAPI("MyAwesomeBotToken")
732 if err != nil {
733 panic(err)
734 }
735
736 bot.Debug = true
737
738 log.Printf("Authorized on account %s", bot.Self.UserName)
739
740 _, err = bot.Request(NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
741 if err != nil {
742 panic(err)
743 }
744 info, err := bot.GetWebhookInfo()
745 if err != nil {
746 panic(err)
747 }
748 if info.LastErrorDate != 0 {
749 log.Printf("[Telegram callback failed]%s", info.LastErrorMessage)
750 }
751
752 http.HandleFunc("/"+bot.Token, func(w http.ResponseWriter, r *http.Request) {
753 update, err := bot.HandleUpdate(r)
754 if err != nil {
755 log.Printf("%+v\n", err.Error())
756 } else {
757 log.Printf("%+v\n", *update)
758 }
759 })
760
761 go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
762}
763
764func ExampleInlineConfig() {
765 bot, err := NewBotAPI("MyAwesomeBotToken") // create new bot
766 if err != nil {
767 panic(err)
768 }
769
770 log.Printf("Authorized on account %s", bot.Self.UserName)
771
772 u := NewUpdate(0)
773 u.Timeout = 60
774
775 updates := bot.GetUpdatesChan(u)
776
777 for update := range updates {
778 if update.InlineQuery == nil { // if no inline query, ignore it
779 continue
780 }
781
782 article := NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
783 article.Description = update.InlineQuery.Query
784
785 inlineConf := InlineConfig{
786 InlineQueryID: update.InlineQuery.ID,
787 IsPersonal: true,
788 CacheTime: 0,
789 Results: []interface{}{article},
790 }
791
792 if _, err := bot.Request(inlineConf); err != nil {
793 log.Println(err)
794 }
795 }
796}
797
798func TestDeleteMessage(t *testing.T) {
799 bot, _ := getBot(t)
800
801 msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
802 msg.ParseMode = ModeMarkdown
803 message, _ := bot.Send(msg)
804
805 deleteMessageConfig := DeleteMessageConfig{
806 ChatID: message.Chat.ID,
807 MessageID: message.MessageID,
808 }
809 _, err := bot.Request(deleteMessageConfig)
810
811 if err != nil {
812 t.Error(err)
813 }
814}
815
816func TestPinChatMessage(t *testing.T) {
817 bot, _ := getBot(t)
818
819 msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
820 msg.ParseMode = ModeMarkdown
821 message, _ := bot.Send(msg)
822
823 pinChatMessageConfig := PinChatMessageConfig{
824 ChatID: message.Chat.ID,
825 MessageID: message.MessageID,
826 DisableNotification: false,
827 }
828 _, err := bot.Request(pinChatMessageConfig)
829
830 if err != nil {
831 t.Error(err)
832 }
833}
834
835func TestUnpinChatMessage(t *testing.T) {
836 bot, _ := getBot(t)
837
838 msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
839 msg.ParseMode = ModeMarkdown
840 message, _ := bot.Send(msg)
841
842 // We need pin message to unpin something
843 pinChatMessageConfig := PinChatMessageConfig{
844 ChatID: message.Chat.ID,
845 MessageID: message.MessageID,
846 DisableNotification: false,
847 }
848
849 if _, err := bot.Request(pinChatMessageConfig); err != nil {
850 t.Error(err)
851 }
852
853 unpinChatMessageConfig := UnpinChatMessageConfig{
854 ChatID: message.Chat.ID,
855 MessageID: message.MessageID,
856 }
857
858 if _, err := bot.Request(unpinChatMessageConfig); err != nil {
859 t.Error(err)
860 }
861}
862
863func TestUnpinAllChatMessages(t *testing.T) {
864 bot, _ := getBot(t)
865
866 msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
867 msg.ParseMode = ModeMarkdown
868 message, _ := bot.Send(msg)
869
870 pinChatMessageConfig := PinChatMessageConfig{
871 ChatID: message.Chat.ID,
872 MessageID: message.MessageID,
873 DisableNotification: true,
874 }
875
876 if _, err := bot.Request(pinChatMessageConfig); err != nil {
877 t.Error(err)
878 }
879
880 unpinAllChatMessagesConfig := UnpinAllChatMessagesConfig{
881 ChatID: message.Chat.ID,
882 }
883
884 if _, err := bot.Request(unpinAllChatMessagesConfig); err != nil {
885 t.Error(err)
886 }
887}
888
889func TestPolls(t *testing.T) {
890 bot, _ := getBot(t)
891
892 poll := NewPoll(SupergroupChatID, "Are polls working?", "Yes", "No")
893
894 msg, err := bot.Send(poll)
895 if err != nil {
896 t.Error(err)
897 }
898
899 result, err := bot.StopPoll(NewStopPoll(SupergroupChatID, msg.MessageID))
900 if err != nil {
901 t.Error(err)
902 }
903
904 if result.Question != "Are polls working?" {
905 t.Error("Poll question did not match")
906 }
907
908 if !result.IsClosed {
909 t.Error("Poll did not end")
910 }
911
912 if result.Options[0].Text != "Yes" || result.Options[0].VoterCount != 0 || result.Options[1].Text != "No" || result.Options[1].VoterCount != 0 {
913 t.Error("Poll options were incorrect")
914 }
915}
916
917func TestSendDice(t *testing.T) {
918 bot, _ := getBot(t)
919
920 dice := NewSendDice(ChatID)
921
922 msg, err := bot.Send(dice)
923 if err != nil {
924 t.Error("Unable to send dice roll")
925 }
926
927 if msg.Dice == nil {
928 t.Error("Dice roll was not received")
929 }
930}
931
932func TestSetCommands(t *testing.T) {
933 bot, _ := getBot(t)
934
935 setCommands := NewSetMyCommands(BotCommand{
936 Command: "test",
937 Description: "a test command",
938 })
939
940 if _, err := bot.Request(setCommands); err != nil {
941 t.Error("Unable to set commands")
942 }
943
944 commands, err := bot.GetMyCommands()
945 if err != nil {
946 t.Error("Unable to get commands")
947 }
948
949 if len(commands) != 1 {
950 t.Error("Incorrect number of commands returned")
951 }
952
953 if commands[0].Command != "test" || commands[0].Description != "a test command" {
954 t.Error("Commands were incorrectly set")
955 }
956}
957
958func TestEditMessageMedia(t *testing.T) {
959 bot, _ := getBot(t)
960
961 msg := NewPhoto(ChatID, "tests/image.jpg")
962 msg.Caption = "Test"
963 m, err := bot.Send(msg)
964
965 if err != nil {
966 t.Error(err)
967 }
968
969 edit := EditMessageMediaConfig{
970 BaseEdit: BaseEdit{
971 ChatID: ChatID,
972 MessageID: m.MessageID,
973 },
974 Media: NewInputMediaVideo("tests/video.mp4"),
975 }
976
977 _, err = bot.Request(edit)
978 if err != nil {
979 t.Error(err)
980 }
981}
982
983func TestPrepareInputMediaForParams(t *testing.T) {
984 media := []interface{}{
985 NewInputMediaPhoto("tests/image.jpg"),
986 NewInputMediaVideo(FileID("test")),
987 }
988
989 prepared := prepareInputMediaForParams(media)
990
991 if media[0].(InputMediaPhoto).Media != "tests/image.jpg" {
992 t.Error("Original media was changed")
993 }
994
995 if prepared[0].(InputMediaPhoto).Media != "attach://file-0" {
996 t.Error("New media was not replaced")
997 }
998
999 if prepared[1].(InputMediaVideo).Media != FileID("test") {
1000 t.Error("Passthrough value was not the same")
1001 }
1002}