all repos — telegram-bot-api @ a746f39d224dc1070eb527fac4fdb70fd0ac0964

Golang bindings for the Telegram Bot API

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