all repos — telegram-bot-api @ a0a678302e8953531aa87a6a7e97a395c8954ada

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 TestSendWithMediaGroup(t *testing.T) {
513	bot, _ := getBot(t)
514
515	cfg := NewMediaGroup(ChatID, []interface{}{
516		NewInputMediaPhoto("https://i.imgur.com/unQLJIb.jpg"),
517		NewInputMediaPhoto("https://i.imgur.com/J5qweNZ.jpg"),
518		NewInputMediaVideo("https://i.imgur.com/F6RmI24.mp4"),
519	})
520	_, err := bot.Request(cfg)
521	if err != nil {
522		t.Error(err)
523	}
524}
525
526func ExampleNewBotAPI() {
527	bot, err := NewBotAPI("MyAwesomeBotToken")
528	if err != nil {
529		panic(err)
530	}
531
532	bot.Debug = true
533
534	log.Printf("Authorized on account %s", bot.Self.UserName)
535
536	u := NewUpdate(0)
537	u.Timeout = 60
538
539	updates := bot.GetUpdatesChan(u)
540
541	// Optional: wait for updates and clear them if you don't want to handle
542	// a large backlog of old messages
543	time.Sleep(time.Millisecond * 500)
544	updates.Clear()
545
546	for update := range updates {
547		if update.Message == nil {
548			continue
549		}
550
551		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
552
553		msg := NewMessage(update.Message.Chat.ID, update.Message.Text)
554		msg.ReplyToMessageID = update.Message.MessageID
555
556		bot.Send(msg)
557	}
558}
559
560func ExampleNewWebhook() {
561	bot, err := NewBotAPI("MyAwesomeBotToken")
562	if err != nil {
563		panic(err)
564	}
565
566	bot.Debug = true
567
568	log.Printf("Authorized on account %s", bot.Self.UserName)
569
570	_, err = bot.Request(NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
571	if err != nil {
572		panic(err)
573	}
574
575	info, err := bot.GetWebhookInfo()
576
577	if err != nil {
578		panic(err)
579	}
580
581	if info.LastErrorDate != 0 {
582		log.Printf("failed to set webhook: %s", info.LastErrorMessage)
583	}
584
585	updates := bot.ListenForWebhook("/" + bot.Token)
586	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
587
588	for update := range updates {
589		log.Printf("%+v\n", update)
590	}
591}
592
593func ExampleInlineConfig() {
594	bot, err := NewBotAPI("MyAwesomeBotToken") // create new bot
595	if err != nil {
596		panic(err)
597	}
598
599	log.Printf("Authorized on account %s", bot.Self.UserName)
600
601	u := NewUpdate(0)
602	u.Timeout = 60
603
604	updates := bot.GetUpdatesChan(u)
605
606	for update := range updates {
607		if update.InlineQuery == nil { // if no inline query, ignore it
608			continue
609		}
610
611		article := NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
612		article.Description = update.InlineQuery.Query
613
614		inlineConf := InlineConfig{
615			InlineQueryID: update.InlineQuery.ID,
616			IsPersonal:    true,
617			CacheTime:     0,
618			Results:       []interface{}{article},
619		}
620
621		if _, err := bot.Request(inlineConf); err != nil {
622			log.Println(err)
623		}
624	}
625}
626
627func TestDeleteMessage(t *testing.T) {
628	bot, _ := getBot(t)
629
630	msg := NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
631	msg.ParseMode = "markdown"
632	message, _ := bot.Send(msg)
633
634	deleteMessageConfig := DeleteMessageConfig{
635		ChatID:    message.Chat.ID,
636		MessageID: message.MessageID,
637	}
638	_, err := bot.Request(deleteMessageConfig)
639
640	if err != nil {
641		t.Error(err)
642		t.Fail()
643	}
644}
645
646func TestPinChatMessage(t *testing.T) {
647	bot, _ := getBot(t)
648
649	msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
650	msg.ParseMode = "markdown"
651	message, _ := bot.Send(msg)
652
653	pinChatMessageConfig := PinChatMessageConfig{
654		ChatID:              message.Chat.ID,
655		MessageID:           message.MessageID,
656		DisableNotification: false,
657	}
658	_, err := bot.Request(pinChatMessageConfig)
659
660	if err != nil {
661		t.Error(err)
662		t.Fail()
663	}
664}
665
666func TestUnpinChatMessage(t *testing.T) {
667	bot, _ := getBot(t)
668
669	msg := NewMessage(SupergroupChatID, "A test message from the test library in telegram-bot-api")
670	msg.ParseMode = "markdown"
671	message, _ := bot.Send(msg)
672
673	// We need pin message to unpin something
674	pinChatMessageConfig := PinChatMessageConfig{
675		ChatID:              message.Chat.ID,
676		MessageID:           message.MessageID,
677		DisableNotification: false,
678	}
679
680	if _, err := bot.Request(pinChatMessageConfig); err != nil {
681		t.Error(err)
682		t.Fail()
683	}
684
685	unpinChatMessageConfig := UnpinChatMessageConfig{
686		ChatID: message.Chat.ID,
687	}
688
689	if _, err := bot.Request(unpinChatMessageConfig); err != nil {
690		t.Error(err)
691		t.Fail()
692	}
693}