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 = "AgACAgIAAxkDAAEBFUZhIALQ9pZN4BUe8ZSzUU_2foSo1AACnrMxG0BucEhezsBWOgcikQEAAwIAA20AAyAE"
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, FilePath("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, FilePath("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, FilePath("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, FilePath("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, FilePath("tests/voice.ogg"))
254 msg.Thumb = FilePath("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, FilePath("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, FilePath("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, FilePath("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, FilePath("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, FilePath("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, FilePath("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
515// TODO: identify why this isn't working
516// func TestSendEditMessage(t *testing.T) {
517// bot, _ := getBot(t)
518
519// msg, err := bot.Send(NewMessage(ChatID, "Testing editing."))
520// if err != nil {
521// t.Error(err)
522// }
523
524// edit := EditMessageTextConfig{
525// BaseEdit: BaseEdit{
526// ChatID: ChatID,
527// MessageID: msg.MessageID,
528// },
529// Text: "Updated text.",
530// }
531
532// _, err = bot.Send(edit)
533// if err != nil {
534// t.Error(err)
535// }
536// }
537
538func TestGetUserProfilePhotos(t *testing.T) {
539 bot, _ := getBot(t)
540
541 _, err := bot.GetUserProfilePhotos(NewUserProfilePhotos(ChatID))
542 if err != nil {
543 t.Error(err)
544 }
545}
546
547func TestSetWebhookWithCert(t *testing.T) {
548 bot, _ := getBot(t)
549
550 time.Sleep(time.Second * 2)
551
552 bot.Request(DeleteWebhookConfig{})
553
554 wh, err := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, FilePath("tests/cert.pem"))
555
556 if err != nil {
557 t.Error(err)
558 }
559 _, err = bot.Request(wh)
560
561 if err != nil {
562 t.Error(err)
563 }
564
565 _, err = bot.GetWebhookInfo()
566
567 if err != nil {
568 t.Error(err)
569 }
570
571 bot.Request(DeleteWebhookConfig{})
572}
573
574func TestSetWebhookWithoutCert(t *testing.T) {
575 bot, _ := getBot(t)
576
577 time.Sleep(time.Second * 2)
578
579 bot.Request(DeleteWebhookConfig{})
580
581 wh, err := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
582
583 if err != nil {
584 t.Error(err)
585 }
586
587 _, err = bot.Request(wh)
588
589 if err != nil {
590 t.Error(err)
591 }
592
593 info, err := bot.GetWebhookInfo()
594
595 if err != nil {
596 t.Error(err)
597 }
598 if info.MaxConnections == 0 {
599 t.Errorf("Expected maximum connections to be greater than 0")
600 }
601 if info.LastErrorDate != 0 {
602 t.Errorf("failed to set webhook: %s", info.LastErrorMessage)
603 }
604
605 bot.Request(DeleteWebhookConfig{})
606}
607
608func TestSendWithMediaGroupPhotoVideo(t *testing.T) {
609 bot, _ := getBot(t)
610
611 cfg := NewMediaGroup(ChatID, []interface{}{
612 NewInputMediaPhoto(FileURL("https://github.com/go-telegram-bot-api/telegram-bot-api/raw/0a3a1c8716c4cd8d26a262af9f12dcbab7f3f28c/tests/image.jpg")),
613 NewInputMediaPhoto(FilePath("tests/image.jpg")),
614 NewInputMediaVideo(FilePath("tests/video.mp4")),
615 })
616
617 messages, err := bot.SendMediaGroup(cfg)
618 if err != nil {
619 t.Error(err)
620 }
621
622 if messages == nil {
623 t.Error("No received messages")
624 }
625
626 if len(messages) != len(cfg.Media) {
627 t.Errorf("Different number of messages: %d", len(messages))
628 }
629}
630
631func TestSendWithMediaGroupDocument(t *testing.T) {
632 bot, _ := getBot(t)
633
634 cfg := NewMediaGroup(ChatID, []interface{}{
635 NewInputMediaDocument(FileURL("https://i.imgur.com/unQLJIb.jpg")),
636 NewInputMediaDocument(FilePath("tests/image.jpg")),
637 })
638
639 messages, err := bot.SendMediaGroup(cfg)
640 if err != nil {
641 t.Error(err)
642 }
643
644 if messages == nil {
645 t.Error("No received messages")
646 }
647
648 if len(messages) != len(cfg.Media) {
649 t.Errorf("Different number of messages: %d", len(messages))
650 }
651}
652
653func TestSendWithMediaGroupAudio(t *testing.T) {
654 bot, _ := getBot(t)
655
656 cfg := NewMediaGroup(ChatID, []interface{}{
657 NewInputMediaAudio(FilePath("tests/audio.mp3")),
658 NewInputMediaAudio(FilePath("tests/audio.mp3")),
659 })
660
661 messages, err := bot.SendMediaGroup(cfg)
662 if err != nil {
663 t.Error(err)
664 }
665
666 if messages == nil {
667 t.Error("No received messages")
668 }
669
670 if len(messages) != len(cfg.Media) {
671 t.Errorf("Different number of messages: %d", len(messages))
672 }
673}
674
675func ExampleNewBotAPI() {
676 bot, err := NewBotAPI("MyAwesomeBotToken")
677 if err != nil {
678 panic(err)
679 }
680
681 bot.Debug = true
682
683 log.Printf("Authorized on account %s", bot.Self.UserName)
684
685 u := NewUpdate(0)
686 u.Timeout = 60
687
688 updates := bot.GetUpdatesChan(u)
689
690 // Optional: wait for updates and clear them if you don't want to handle
691 // a large backlog of old messages
692 time.Sleep(time.Millisecond * 500)
693 updates.Clear()
694
695 for update := range updates {
696 if update.Message == nil {
697 continue
698 }
699
700 log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
701
702 msg := NewMessage(update.Message.Chat.ID, update.Message.Text)
703 msg.ReplyToMessageID = update.Message.MessageID
704
705 bot.Send(msg)
706 }
707}
708
709func ExampleNewWebhook() {
710 bot, err := NewBotAPI("MyAwesomeBotToken")
711 if err != nil {
712 panic(err)
713 }
714
715 bot.Debug = true
716
717 log.Printf("Authorized on account %s", bot.Self.UserName)
718
719 wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, FilePath("cert.pem"))
720
721 if err != nil {
722 panic(err)
723 }
724
725 _, err = bot.Request(wh)
726
727 if err != nil {
728 panic(err)
729 }
730
731 info, err := bot.GetWebhookInfo()
732
733 if err != nil {
734 panic(err)
735 }
736
737 if info.LastErrorDate != 0 {
738 log.Printf("failed to set webhook: %s", info.LastErrorMessage)
739 }
740
741 updates := bot.ListenForWebhook("/" + bot.Token)
742 go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
743
744 for update := range updates {
745 log.Printf("%+v\n", update)
746 }
747}
748
749func ExampleWebhookHandler() {
750 bot, err := NewBotAPI("MyAwesomeBotToken")
751 if err != nil {
752 panic(err)
753 }
754
755 bot.Debug = true
756
757 log.Printf("Authorized on account %s", bot.Self.UserName)
758
759 wh, err := NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, FilePath("cert.pem"))
760
761 if err != nil {
762 panic(err)
763 }
764
765 _, err = bot.Request(wh)
766 if err != nil {
767 panic(err)
768 }
769 info, err := bot.GetWebhookInfo()
770 if err != nil {
771 panic(err)
772 }
773 if info.LastErrorDate != 0 {
774 log.Printf("[Telegram callback failed]%s", info.LastErrorMessage)
775 }
776
777 http.HandleFunc("/"+bot.Token, func(w http.ResponseWriter, r *http.Request) {
778 update, err := bot.HandleUpdate(r)
779 if err != nil {
780 log.Printf("%+v\n", err.Error())
781 } else {
782 log.Printf("%+v\n", *update)
783 }
784 })
785
786 go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
787}
788
789func ExampleInlineConfig() {
790 bot, err := NewBotAPI("MyAwesomeBotToken") // create new bot
791 if err != nil {
792 panic(err)
793 }
794
795 log.Printf("Authorized on account %s", bot.Self.UserName)
796
797 u := NewUpdate(0)
798 u.Timeout = 60
799
800 updates := bot.GetUpdatesChan(u)
801
802 for update := range updates {
803 if update.InlineQuery == nil { // if no inline query, ignore it
804 continue
805 }
806
807 article := NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
808 article.Description = update.InlineQuery.Query
809
810 inlineConf := InlineConfig{
811 InlineQueryID: update.InlineQuery.ID,
812 IsPersonal: true,
813 CacheTime: 0,
814 Results: []interface{}{article},
815 }
816
817 if _, err := bot.Request(inlineConf); err != nil {
818 log.Println(err)
819 }
820 }
821}
822
823func TestDeleteMessage(t *testing.T) {
824 bot, _ := getBot(t)
825
826 msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
827 msg.ParseMode = ModeMarkdown
828 message, _ := bot.Send(msg)
829
830 deleteMessageConfig := DeleteMessageConfig{
831 ChatID: message.Chat.ID,
832 MessageID: message.MessageID,
833 }
834 _, err := bot.Request(deleteMessageConfig)
835
836 if err != nil {
837 t.Error(err)
838 }
839}
840
841func TestPinChatMessage(t *testing.T) {
842 bot, _ := getBot(t)
843
844 msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
845 msg.ParseMode = ModeMarkdown
846 message, _ := bot.Send(msg)
847
848 pinChatMessageConfig := PinChatMessageConfig{
849 ChatID: message.Chat.ID,
850 MessageID: message.MessageID,
851 DisableNotification: false,
852 }
853 _, err := bot.Request(pinChatMessageConfig)
854
855 if err != nil {
856 t.Error(err)
857 }
858}
859
860func TestUnpinChatMessage(t *testing.T) {
861 bot, _ := getBot(t)
862
863 msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
864 msg.ParseMode = ModeMarkdown
865 message, _ := bot.Send(msg)
866
867 // We need pin message to unpin something
868 pinChatMessageConfig := PinChatMessageConfig{
869 ChatID: message.Chat.ID,
870 MessageID: message.MessageID,
871 DisableNotification: false,
872 }
873
874 if _, err := bot.Request(pinChatMessageConfig); err != nil {
875 t.Error(err)
876 }
877
878 unpinChatMessageConfig := UnpinChatMessageConfig{
879 ChatID: message.Chat.ID,
880 MessageID: message.MessageID,
881 }
882
883 if _, err := bot.Request(unpinChatMessageConfig); err != nil {
884 t.Error(err)
885 }
886}
887
888func TestUnpinAllChatMessages(t *testing.T) {
889 bot, _ := getBot(t)
890
891 msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
892 msg.ParseMode = ModeMarkdown
893 message, _ := bot.Send(msg)
894
895 pinChatMessageConfig := PinChatMessageConfig{
896 ChatID: message.Chat.ID,
897 MessageID: message.MessageID,
898 DisableNotification: true,
899 }
900
901 if _, err := bot.Request(pinChatMessageConfig); err != nil {
902 t.Error(err)
903 }
904
905 unpinAllChatMessagesConfig := UnpinAllChatMessagesConfig{
906 ChatID: message.Chat.ID,
907 }
908
909 if _, err := bot.Request(unpinAllChatMessagesConfig); err != nil {
910 t.Error(err)
911 }
912}
913
914func TestPolls(t *testing.T) {
915 bot, _ := getBot(t)
916
917 poll := NewPoll(SupergroupChatID, "Are polls working?", "Yes", "No")
918
919 msg, err := bot.Send(poll)
920 if err != nil {
921 t.Error(err)
922 }
923
924 result, err := bot.StopPoll(NewStopPoll(SupergroupChatID, msg.MessageID))
925 if err != nil {
926 t.Error(err)
927 }
928
929 if result.Question != "Are polls working?" {
930 t.Error("Poll question did not match")
931 }
932
933 if !result.IsClosed {
934 t.Error("Poll did not end")
935 }
936
937 if result.Options[0].Text != "Yes" || result.Options[0].VoterCount != 0 || result.Options[1].Text != "No" || result.Options[1].VoterCount != 0 {
938 t.Error("Poll options were incorrect")
939 }
940}
941
942func TestSendDice(t *testing.T) {
943 bot, _ := getBot(t)
944
945 dice := NewDice(ChatID)
946
947 msg, err := bot.Send(dice)
948 if err != nil {
949 t.Error("Unable to send dice roll")
950 }
951
952 if msg.Dice == nil {
953 t.Error("Dice roll was not received")
954 }
955}
956
957func TestCommands(t *testing.T) {
958 bot, _ := getBot(t)
959
960 setCommands := NewSetMyCommands(BotCommand{
961 Command: "test",
962 Description: "a test command",
963 })
964
965 if _, err := bot.Request(setCommands); err != nil {
966 t.Error("Unable to set commands")
967 }
968
969 commands, err := bot.GetMyCommands()
970 if err != nil {
971 t.Error("Unable to get commands")
972 }
973
974 if len(commands) != 1 {
975 t.Error("Incorrect number of commands returned")
976 }
977
978 if commands[0].Command != "test" || commands[0].Description != "a test command" {
979 t.Error("Commands were incorrectly set")
980 }
981
982 setCommands = NewSetMyCommandsWithScope(NewBotCommandScopeAllPrivateChats(), BotCommand{
983 Command: "private",
984 Description: "a private command",
985 })
986
987 if _, err := bot.Request(setCommands); err != nil {
988 t.Error("Unable to set commands")
989 }
990
991 commands, err = bot.GetMyCommandsWithConfig(NewGetMyCommandsWithScope(NewBotCommandScopeAllPrivateChats()))
992 if err != nil {
993 t.Error("Unable to get commands")
994 }
995
996 if len(commands) != 1 {
997 t.Error("Incorrect number of commands returned")
998 }
999
1000 if commands[0].Command != "private" || commands[0].Description != "a private command" {
1001 t.Error("Commands were incorrectly set")
1002 }
1003}
1004
1005// TODO: figure out why test is failing
1006//
1007// func TestEditMessageMedia(t *testing.T) {
1008// bot, _ := getBot(t)
1009
1010// msg := NewPhoto(ChatID, "tests/image.jpg")
1011// msg.Caption = "Test"
1012// m, err := bot.Send(msg)
1013
1014// if err != nil {
1015// t.Error(err)
1016// }
1017
1018// edit := EditMessageMediaConfig{
1019// BaseEdit: BaseEdit{
1020// ChatID: ChatID,
1021// MessageID: m.MessageID,
1022// },
1023// Media: NewInputMediaVideo(FilePath("tests/video.mp4")),
1024// }
1025
1026// _, err = bot.Request(edit)
1027// if err != nil {
1028// t.Error(err)
1029// }
1030// }
1031
1032func TestPrepareInputMediaForParams(t *testing.T) {
1033 media := []interface{}{
1034 NewInputMediaPhoto(FilePath("tests/image.jpg")),
1035 NewInputMediaVideo(FileID("test")),
1036 }
1037
1038 prepared := prepareInputMediaForParams(media)
1039
1040 if media[0].(InputMediaPhoto).Media != FilePath("tests/image.jpg") {
1041 t.Error("Original media was changed")
1042 }
1043
1044 if prepared[0].(InputMediaPhoto).Media != fileAttach("attach://file-0") {
1045 t.Error("New media was not replaced")
1046 }
1047
1048 if prepared[1].(InputMediaVideo).Media != FileID("test") {
1049 t.Error("Passthrough value was not the same")
1050 }
1051}