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