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