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
26func getBot(t *testing.T) (*BotAPI, error) {
27 bot, err := NewBotAPI(TestToken)
28 bot.Debug = true
29
30 if err != nil {
31 t.Error(err)
32 t.Fail()
33 }
34
35 return bot, err
36}
37
38func TestNewBotAPI_notoken(t *testing.T) {
39 _, err := NewBotAPI("")
40
41 if err == nil {
42 t.Error(err)
43 t.Fail()
44 }
45}
46
47func TestGetUpdates(t *testing.T) {
48 bot, _ := getBot(t)
49
50 u := NewUpdate(0)
51
52 _, err := bot.GetUpdates(u)
53
54 if err != nil {
55 t.Error(err)
56 t.Fail()
57 }
58}
59
60func TestSendWithMessage(t *testing.T) {
61 bot, _ := getBot(t)
62
63 msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
64 msg.ParseMode = "markdown"
65 _, err := bot.Send(msg)
66
67 if err != nil {
68 t.Error(err)
69 t.Fail()
70 }
71}
72
73func TestSendWithMessageReply(t *testing.T) {
74 bot, _ := getBot(t)
75
76 msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
77 msg.ReplyToMessageID = ReplyToMessageID
78 _, err := bot.Send(msg)
79
80 if err != nil {
81 t.Error(err)
82 t.Fail()
83 }
84}
85
86func TestSendWithMessageForward(t *testing.T) {
87 bot, _ := getBot(t)
88
89 msg := NewForward(ChatID, ChatID, ReplyToMessageID)
90 _, err := bot.Send(msg)
91
92 if err != nil {
93 t.Error(err)
94 t.Fail()
95 }
96}
97
98func TestSendWithNewPhoto(t *testing.T) {
99 bot, _ := getBot(t)
100
101 msg := NewPhotoUpload(ChatID, "tests/image.jpg")
102 msg.Caption = "Test"
103 _, err := bot.Send(msg)
104
105 if err != nil {
106 t.Error(err)
107 t.Fail()
108 }
109}
110
111func TestSendWithNewPhotoWithFileBytes(t *testing.T) {
112 bot, _ := getBot(t)
113
114 data, _ := ioutil.ReadFile("tests/image.jpg")
115 b := FileBytes{Name: "image.jpg", Bytes: data}
116
117 msg := NewPhotoUpload(ChatID, b)
118 msg.Caption = "Test"
119 _, err := bot.Send(msg)
120
121 if err != nil {
122 t.Error(err)
123 t.Fail()
124 }
125}
126
127func TestSendWithNewPhotoWithFileReader(t *testing.T) {
128 bot, _ := getBot(t)
129
130 f, _ := os.Open("tests/image.jpg")
131 reader := FileReader{Name: "image.jpg", Reader: f, Size: -1}
132
133 msg := NewPhotoUpload(ChatID, reader)
134 msg.Caption = "Test"
135 _, err := bot.Send(msg)
136
137 if err != nil {
138 t.Error(err)
139 t.Fail()
140 }
141}
142
143func TestSendWithNewPhotoReply(t *testing.T) {
144 bot, _ := getBot(t)
145
146 msg := NewPhotoUpload(ChatID, "tests/image.jpg")
147 msg.ReplyToMessageID = ReplyToMessageID
148
149 _, err := bot.Send(msg)
150
151 if err != nil {
152 t.Error(err)
153 t.Fail()
154 }
155}
156
157func TestSendNewPhotoToChannel(t *testing.T) {
158 bot, _ := getBot(t)
159
160 msg := NewPhotoUploadToChannel(Channel, "tests/image.jpg")
161 msg.Caption = "Test"
162 _, err := bot.Send(msg)
163
164 if err != nil {
165 t.Error(err)
166 t.Fail()
167 }
168}
169
170func TestSendNewPhotoToChannelFileBytes(t *testing.T) {
171 bot, _ := getBot(t)
172
173 data, _ := ioutil.ReadFile("tests/image.jpg")
174 b := FileBytes{Name: "image.jpg", Bytes: data}
175
176 msg := NewPhotoUploadToChannel(Channel, b)
177 msg.Caption = "Test"
178 _, err := bot.Send(msg)
179
180 if err != nil {
181 t.Error(err)
182 t.Fail()
183 }
184}
185
186func TestSendNewPhotoToChannelFileReader(t *testing.T) {
187 bot, _ := getBot(t)
188
189 f, _ := os.Open("tests/image.jpg")
190 reader := FileReader{Name: "image.jpg", Reader: f, Size: -1}
191
192 msg := NewPhotoUploadToChannel(Channel, reader)
193 msg.Caption = "Test"
194 _, err := bot.Send(msg)
195
196 if err != nil {
197 t.Error(err)
198 t.Fail()
199 }
200}
201
202func TestSendWithExistingPhoto(t *testing.T) {
203 bot, _ := getBot(t)
204
205 msg := NewPhotoShare(ChatID, ExistingPhotoFileID)
206 msg.Caption = "Test"
207 _, err := bot.Send(msg)
208
209 if err != nil {
210 t.Error(err)
211 t.Fail()
212 }
213}
214
215func TestSendWithNewDocument(t *testing.T) {
216 bot, _ := getBot(t)
217
218 msg := NewDocumentUpload(ChatID, "tests/image.jpg")
219 _, err := bot.Send(msg)
220
221 if err != nil {
222 t.Error(err)
223 t.Fail()
224 }
225}
226
227func TestSendWithExistingDocument(t *testing.T) {
228 bot, _ := getBot(t)
229
230 msg := NewDocumentShare(ChatID, ExistingDocumentFileID)
231 _, err := bot.Send(msg)
232
233 if err != nil {
234 t.Error(err)
235 t.Fail()
236 }
237}
238
239func TestSendWithNewAudio(t *testing.T) {
240 bot, _ := getBot(t)
241
242 msg := NewAudioUpload(ChatID, "tests/audio.mp3")
243 msg.Title = "TEST"
244 msg.Duration = 10
245 msg.Performer = "TEST"
246 msg.MimeType = "audio/mpeg"
247 msg.FileSize = 688
248 _, err := bot.Send(msg)
249
250 if err != nil {
251 t.Error(err)
252 t.Fail()
253 }
254}
255
256func TestSendWithExistingAudio(t *testing.T) {
257 bot, _ := getBot(t)
258
259 msg := NewAudioShare(ChatID, ExistingAudioFileID)
260 msg.Title = "TEST"
261 msg.Duration = 10
262 msg.Performer = "TEST"
263
264 _, err := bot.Send(msg)
265
266 if err != nil {
267 t.Error(err)
268 t.Fail()
269 }
270}
271
272func TestSendWithNewVoice(t *testing.T) {
273 bot, _ := getBot(t)
274
275 msg := NewVoiceUpload(ChatID, "tests/voice.ogg")
276 msg.Duration = 10
277 _, err := bot.Send(msg)
278
279 if err != nil {
280 t.Error(err)
281 t.Fail()
282 }
283}
284
285func TestSendWithExistingVoice(t *testing.T) {
286 bot, _ := getBot(t)
287
288 msg := NewVoiceShare(ChatID, ExistingVoiceFileID)
289 msg.Duration = 10
290 _, err := bot.Send(msg)
291
292 if err != nil {
293 t.Error(err)
294 t.Fail()
295 }
296}
297
298func TestSendWithContact(t *testing.T) {
299 bot, _ := getBot(t)
300
301 contact := NewContact(ChatID, "5551234567", "Test")
302
303 if _, err := bot.Send(contact); err != nil {
304 t.Error(err)
305 t.Fail()
306 }
307}
308
309func TestSendWithLocation(t *testing.T) {
310 bot, _ := getBot(t)
311
312 _, err := bot.Send(NewLocation(ChatID, 40, 40))
313
314 if err != nil {
315 t.Error(err)
316 t.Fail()
317 }
318}
319
320func TestSendWithVenue(t *testing.T) {
321 bot, _ := getBot(t)
322
323 venue := NewVenue(ChatID, "A Test Location", "123 Test Street", 40, 40)
324
325 if _, err := bot.Send(venue); err != nil {
326 t.Error(err)
327 t.Fail()
328 }
329}
330
331func TestSendWithNewVideo(t *testing.T) {
332 bot, _ := getBot(t)
333
334 msg := NewVideoUpload(ChatID, "tests/video.mp4")
335 msg.Duration = 10
336 msg.Caption = "TEST"
337
338 _, err := bot.Send(msg)
339
340 if err != nil {
341 t.Error(err)
342 t.Fail()
343 }
344}
345
346func TestSendWithExistingVideo(t *testing.T) {
347 bot, _ := getBot(t)
348
349 msg := NewVideoShare(ChatID, ExistingVideoFileID)
350 msg.Duration = 10
351 msg.Caption = "TEST"
352
353 _, err := bot.Send(msg)
354
355 if err != nil {
356 t.Error(err)
357 t.Fail()
358 }
359}
360
361func TestSendWithNewVideoNote(t *testing.T) {
362 bot, _ := getBot(t)
363
364 msg := NewVideoNoteUpload(ChatID, 240, "tests/videonote.mp4")
365 msg.Duration = 10
366
367 _, err := bot.Send(msg)
368
369 if err != nil {
370 t.Error(err)
371 t.Fail()
372 }
373}
374
375func TestSendWithExistingVideoNote(t *testing.T) {
376 bot, _ := getBot(t)
377
378 msg := NewVideoNoteShare(ChatID, 240, ExistingVideoNoteFileID)
379 msg.Duration = 10
380
381 _, err := bot.Send(msg)
382
383 if err != nil {
384 t.Error(err)
385 t.Fail()
386 }
387}
388
389func TestSendWithNewSticker(t *testing.T) {
390 bot, _ := getBot(t)
391
392 msg := NewStickerUpload(ChatID, "tests/image.jpg")
393
394 _, err := bot.Send(msg)
395
396 if err != nil {
397 t.Error(err)
398 t.Fail()
399 }
400}
401
402func TestSendWithExistingSticker(t *testing.T) {
403 bot, _ := getBot(t)
404
405 msg := NewStickerShare(ChatID, ExistingStickerFileID)
406
407 _, err := bot.Send(msg)
408
409 if err != nil {
410 t.Error(err)
411 t.Fail()
412 }
413}
414
415func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
416 bot, _ := getBot(t)
417
418 msg := NewStickerUpload(ChatID, "tests/image.jpg")
419 msg.ReplyMarkup = ReplyKeyboardRemove{
420 RemoveKeyboard: true,
421 Selective: false,
422 }
423 _, err := bot.Send(msg)
424
425 if err != nil {
426 t.Error(err)
427 t.Fail()
428 }
429}
430
431func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
432 bot, _ := getBot(t)
433
434 msg := NewStickerShare(ChatID, ExistingStickerFileID)
435 msg.ReplyMarkup = ReplyKeyboardRemove{
436 RemoveKeyboard: true,
437 Selective: false,
438 }
439
440 _, err := bot.Send(msg)
441
442 if err != nil {
443 t.Error(err)
444 t.Fail()
445 }
446}
447
448func TestGetFile(t *testing.T) {
449 bot, _ := getBot(t)
450
451 file := FileConfig{
452 FileID: ExistingPhotoFileID,
453 }
454
455 _, err := bot.GetFile(file)
456
457 if err != nil {
458 t.Error(err)
459 t.Fail()
460 }
461}
462
463func TestSendChatConfig(t *testing.T) {
464 bot, _ := getBot(t)
465
466 _, err := bot.Request(NewChatAction(ChatID, ChatTyping))
467
468 if err != nil {
469 t.Error(err)
470 t.Fail()
471 }
472}
473
474func TestSendEditMessage(t *testing.T) {
475 bot, _ := getBot(t)
476
477 msg, err := bot.Send(NewMessage(ChatID, "Testing editing."))
478 if err != nil {
479 t.Error(err)
480 t.Fail()
481 }
482
483 edit := EditMessageTextConfig{
484 BaseEdit: BaseEdit{
485 ChatID: ChatID,
486 MessageID: msg.MessageID,
487 },
488 Text: "Updated text.",
489 }
490
491 _, err = bot.Send(edit)
492 if err != nil {
493 t.Error(err)
494 t.Fail()
495 }
496}
497
498func TestGetUserProfilePhotos(t *testing.T) {
499 bot, _ := getBot(t)
500
501 _, err := bot.GetUserProfilePhotos(NewUserProfilePhotos(ChatID))
502 if err != nil {
503 t.Error(err)
504 t.Fail()
505 }
506}
507
508func TestSetWebhookWithCert(t *testing.T) {
509 bot, _ := getBot(t)
510
511 time.Sleep(time.Second * 2)
512
513 bot.Request(RemoveWebhookConfig{})
514
515 wh := NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
516 _, err := bot.Request(wh)
517 if err != nil {
518 t.Error(err)
519 t.Fail()
520 }
521
522 _, err = bot.GetWebhookInfo()
523
524 if err != nil {
525 t.Error(err)
526 }
527
528 bot.Request(RemoveWebhookConfig{})
529}
530
531func TestSetWebhookWithoutCert(t *testing.T) {
532 bot, _ := getBot(t)
533
534 time.Sleep(time.Second * 2)
535
536 bot.Request(RemoveWebhookConfig{})
537
538 wh := NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
539 _, err := bot.Request(wh)
540 if err != nil {
541 t.Error(err)
542 t.Fail()
543 }
544
545 info, err := bot.GetWebhookInfo()
546
547 if err != nil {
548 t.Error(err)
549 }
550
551 if info.LastErrorDate != 0 {
552 t.Errorf("failed to set webhook: %s", info.LastErrorMessage)
553 }
554
555 bot.Request(RemoveWebhookConfig{})
556}
557
558func TestSendWithMediaGroup(t *testing.T) {
559 bot, _ := getBot(t)
560
561 cfg := NewMediaGroup(ChatID, []interface{}{
562 NewInputMediaPhoto("https://i.imgur.com/unQLJIb.jpg"),
563 NewInputMediaPhoto("https://i.imgur.com/J5qweNZ.jpg"),
564 NewInputMediaVideo("https://i.imgur.com/F6RmI24.mp4"),
565 })
566
567 messages, err := bot.SendMediaGroup(cfg)
568 if err != nil {
569 t.Error(err)
570 }
571
572 if messages == nil {
573 t.Error()
574 }
575
576 if len(messages) != 3 {
577 t.Error()
578 }
579}
580
581func ExampleNewBotAPI() {
582 bot, err := NewBotAPI("MyAwesomeBotToken")
583 if err != nil {
584 panic(err)
585 }
586
587 bot.Debug = true
588
589 log.Printf("Authorized on account %s", bot.Self.UserName)
590
591 u := NewUpdate(0)
592 u.Timeout = 60
593
594 updates := bot.GetUpdatesChan(u)
595
596 // Optional: wait for updates and clear them if you don't want to handle
597 // a large backlog of old messages
598 time.Sleep(time.Millisecond * 500)
599 updates.Clear()
600
601 for update := range updates {
602 if update.Message == nil {
603 continue
604 }
605
606 log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
607
608 msg := NewMessage(update.Message.Chat.ID, update.Message.Text)
609 msg.ReplyToMessageID = update.Message.MessageID
610
611 bot.Send(msg)
612 }
613}
614
615func ExampleNewWebhook() {
616 bot, err := NewBotAPI("MyAwesomeBotToken")
617 if err != nil {
618 panic(err)
619 }
620
621 bot.Debug = true
622
623 log.Printf("Authorized on account %s", bot.Self.UserName)
624
625 _, err = bot.Request(NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
626 if err != nil {
627 panic(err)
628 }
629
630 info, err := bot.GetWebhookInfo()
631
632 if err != nil {
633 panic(err)
634 }
635
636 if info.LastErrorDate != 0 {
637 log.Printf("failed to set webhook: %s", info.LastErrorMessage)
638 }
639
640 updates := bot.ListenForWebhook("/" + bot.Token)
641 go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
642
643 for update := range updates {
644 log.Printf("%+v\n", update)
645 }
646}
647
648func ExampleInlineConfig() {
649 bot, err := NewBotAPI("MyAwesomeBotToken") // create new bot
650 if err != nil {
651 panic(err)
652 }
653
654 log.Printf("Authorized on account %s", bot.Self.UserName)
655
656 u := NewUpdate(0)
657 u.Timeout = 60
658
659 updates := bot.GetUpdatesChan(u)
660
661 for update := range updates {
662 if update.InlineQuery == nil { // if no inline query, ignore it
663 continue
664 }
665
666 article := NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
667 article.Description = update.InlineQuery.Query
668
669 inlineConf := InlineConfig{
670 InlineQueryID: update.InlineQuery.ID,
671 IsPersonal: true,
672 CacheTime: 0,
673 Results: []interface{}{article},
674 }
675
676 if _, err := bot.Request(inlineConf); err != nil {
677 log.Println(err)
678 }
679 }
680}
681
682func TestDeleteMessage(t *testing.T) {
683 bot, _ := getBot(t)
684
685 msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
686 msg.ParseMode = "markdown"
687 message, _ := bot.Send(msg)
688
689 deleteMessageConfig := DeleteMessageConfig{
690 ChatID: message.Chat.ID,
691 MessageID: message.MessageID,
692 }
693 _, err := bot.Request(deleteMessageConfig)
694
695 if err != nil {
696 t.Error(err)
697 t.Fail()
698 }
699}
700
701func TestPinChatMessage(t *testing.T) {
702 bot, _ := getBot(t)
703
704 msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
705 msg.ParseMode = "markdown"
706 message, _ := bot.Send(msg)
707
708 pinChatMessageConfig := PinChatMessageConfig{
709 ChatID: message.Chat.ID,
710 MessageID: message.MessageID,
711 DisableNotification: false,
712 }
713 _, err := bot.Request(pinChatMessageConfig)
714
715 if err != nil {
716 t.Error(err)
717 t.Fail()
718 }
719}
720
721func TestUnpinChatMessage(t *testing.T) {
722 bot, _ := getBot(t)
723
724 msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
725 msg.ParseMode = "markdown"
726 message, _ := bot.Send(msg)
727
728 // We need pin message to unpin something
729 pinChatMessageConfig := PinChatMessageConfig{
730 ChatID: message.Chat.ID,
731 MessageID: message.MessageID,
732 DisableNotification: false,
733 }
734
735 if _, err := bot.Request(pinChatMessageConfig); err != nil {
736 t.Error(err)
737 t.Fail()
738 }
739
740 unpinChatMessageConfig := UnpinChatMessageConfig{
741 ChatID: message.Chat.ID,
742 }
743
744 if _, err := bot.Request(unpinChatMessageConfig); err != nil {
745 t.Error(err)
746 t.Fail()
747 }
748}
749
750func TestPolls(t *testing.T) {
751 bot, _ := getBot(t)
752
753 poll := NewPoll(SupergroupChatID, "Are polls working?", "Yes", "No")
754
755 msg, err := bot.Send(poll)
756 if err != nil {
757 t.Error(err)
758 t.Fail()
759 }
760
761 result, err := bot.StopPoll(NewStopPoll(SupergroupChatID, msg.MessageID))
762 if err != nil {
763 t.Error(err)
764 t.Fail()
765 }
766
767 if result.Question != "Are polls working?" {
768 t.Error("Poll question did not match")
769 t.Fail()
770 }
771
772 if !result.IsClosed {
773 t.Error("Poll did not end")
774 t.Fail()
775 }
776
777 if result.Options[0].Text != "Yes" || result.Options[0].VoterCount != 0 || result.Options[1].Text != "No" || result.Options[1].VoterCount != 0 {
778 t.Error("Poll options were incorrect")
779 t.Fail()
780 }
781}