all repos — telegram-bot-api @ c0eb5db6c3f5289bcbb06bdd0e68ef5ea6216371

Golang bindings for the Telegram Bot API

bot_test.go (view raw)

  1package tgbotapi_test
  2
  3import (
  4	"github.com/go-telegram-bot-api/telegram-bot-api"
  5	"io/ioutil"
  6	"log"
  7	"net/http"
  8	"os"
  9	"testing"
 10)
 11
 12const (
 13	TestToken              = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I"
 14	ChatID                 = 76918703
 15	ReplyToMessageID       = 35
 16	ExistingPhotoFileID    = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC"
 17	ExistingDocumentFileID = "BQADAgADOQADjMcoCcioX1GrDvp3Ag"
 18	ExistingAudioFileID    = "BQADAgADRgADjMcoCdXg3lSIN49lAg"
 19	ExistingVoiceFileID    = "AwADAgADWQADjMcoCeul6r_q52IyAg"
 20	ExistingVideoFileID    = "BAADAgADZgADjMcoCav432kYe0FRAg"
 21	ExistingStickerFileID  = "BQADAgADcwADjMcoCbdl-6eB--YPAg"
 22)
 23
 24func getBot(t *testing.T) (*tgbotapi.BotAPI, error) {
 25	bot, err := tgbotapi.NewBotAPI(TestToken)
 26
 27	if err != nil {
 28		t.Fail()
 29	}
 30
 31	return bot, err
 32}
 33
 34func TestNewBotAPI_notoken(t *testing.T) {
 35	_, err := tgbotapi.NewBotAPI("")
 36
 37	if err == nil {
 38		t.Fail()
 39	}
 40}
 41
 42func TestGetUpdates(t *testing.T) {
 43	bot, _ := getBot(t)
 44
 45	u := tgbotapi.NewUpdate(0)
 46
 47	_, err := bot.GetUpdates(u)
 48
 49	if err != nil {
 50		t.Fail()
 51	}
 52}
 53
 54func TestSendWithMessage(t *testing.T) {
 55	bot, _ := getBot(t)
 56
 57	msg := tgbotapi.NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
 58	msg.ParseMode = "markdown"
 59	_, err := bot.Send(msg)
 60
 61	if err != nil {
 62		t.Fail()
 63	}
 64}
 65
 66func TestSendWithMessageReply(t *testing.T) {
 67	bot, _ := getBot(t)
 68
 69	msg := tgbotapi.NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
 70	msg.ReplyToMessageID = ReplyToMessageID
 71	_, err := bot.Send(msg)
 72
 73	if err != nil {
 74		t.Fail()
 75	}
 76}
 77
 78func TestSendWithMessageForward(t *testing.T) {
 79	bot, _ := getBot(t)
 80
 81	msg := tgbotapi.NewForward(ChatID, ChatID, ReplyToMessageID)
 82	_, err := bot.Send(msg)
 83
 84	if err != nil {
 85		t.Fail()
 86	}
 87}
 88
 89func TestSendWithNewPhoto(t *testing.T) {
 90	bot, _ := getBot(t)
 91
 92	msg := tgbotapi.NewPhotoUpload(ChatID, "tests/image.jpg")
 93	msg.Caption = "Test"
 94	_, err := bot.Send(msg)
 95
 96	if err != nil {
 97		t.Fail()
 98	}
 99}
100
101func TestSendWithNewPhotoWithFileBytes(t *testing.T) {
102	bot, _ := getBot(t)
103
104	data, _ := ioutil.ReadFile("tests/image.jpg")
105	b := tgbotapi.FileBytes{Name: "image.jpg", Bytes: data}
106
107	msg := tgbotapi.NewPhotoUpload(ChatID, b)
108	msg.Caption = "Test"
109	_, err := bot.Send(msg)
110
111	if err != nil {
112		t.Fail()
113	}
114}
115
116func TestSendWithNewPhotoWithFileReader(t *testing.T) {
117	bot, _ := getBot(t)
118
119	f, _ := os.Open("tests/image.jpg")
120	reader := tgbotapi.FileReader{Name: "image.jpg", Reader: f, Size: -1}
121
122	msg := tgbotapi.NewPhotoUpload(ChatID, reader)
123	msg.Caption = "Test"
124	_, err := bot.Send(msg)
125
126	if err != nil {
127		t.Fail()
128	}
129}
130
131func TestSendWithNewPhotoReply(t *testing.T) {
132	bot, _ := getBot(t)
133
134	msg := tgbotapi.NewPhotoUpload(ChatID, "tests/image.jpg")
135	msg.ReplyToMessageID = ReplyToMessageID
136
137	_, err := bot.Send(msg)
138
139	if err != nil {
140		t.Fail()
141	}
142}
143
144func TestSendWithExistingPhoto(t *testing.T) {
145	bot, _ := getBot(t)
146
147	msg := tgbotapi.NewPhotoShare(ChatID, ExistingPhotoFileID)
148	msg.Caption = "Test"
149	_, err := bot.Send(msg)
150
151	if err != nil {
152		t.Fail()
153	}
154}
155
156func TestSendWithNewDocument(t *testing.T) {
157	bot, _ := getBot(t)
158
159	msg := tgbotapi.NewDocumentUpload(ChatID, "tests/image.jpg")
160	_, err := bot.Send(msg)
161
162	if err != nil {
163		t.Fail()
164	}
165}
166
167func TestSendWithExistingDocument(t *testing.T) {
168	bot, _ := getBot(t)
169
170	msg := tgbotapi.NewDocumentShare(ChatID, ExistingDocumentFileID)
171	_, err := bot.Send(msg)
172
173	if err != nil {
174		t.Fail()
175	}
176}
177
178func TestSendWithNewAudio(t *testing.T) {
179	bot, _ := getBot(t)
180
181	msg := tgbotapi.NewAudioUpload(ChatID, "tests/audio.mp3")
182	msg.Title = "TEST"
183	msg.Duration = 10
184	msg.Performer = "TEST"
185	msg.MimeType = "audio/mpeg"
186	msg.FileSize = 688
187	_, err := bot.Send(msg)
188
189	if err != nil {
190		t.Fail()
191	}
192}
193
194func TestSendWithExistingAudio(t *testing.T) {
195	bot, _ := getBot(t)
196
197	msg := tgbotapi.NewAudioShare(ChatID, ExistingAudioFileID)
198	msg.Title = "TEST"
199	msg.Duration = 10
200	msg.Performer = "TEST"
201
202	_, err := bot.Send(msg)
203
204	if err != nil {
205		t.Fail()
206	}
207}
208
209func TestSendWithNewVoice(t *testing.T) {
210	bot, _ := getBot(t)
211
212	msg := tgbotapi.NewVoiceUpload(ChatID, "tests/voice.ogg")
213	msg.Duration = 10
214	_, err := bot.Send(msg)
215
216	if err != nil {
217		t.Fail()
218	}
219}
220
221func TestSendWithExistingVoice(t *testing.T) {
222	bot, _ := getBot(t)
223
224	msg := tgbotapi.NewVoiceShare(ChatID, ExistingVoiceFileID)
225	msg.Duration = 10
226	_, err := bot.Send(msg)
227
228	if err != nil {
229		t.Fail()
230	}
231}
232
233func TestSendWithContact(t *testing.T) {
234	bot, _ := getBot(t)
235
236	contact := tgbotapi.NewContact(ChatID, "5551234567", "Test")
237
238	if _, err := bot.Send(contact); err != nil {
239		t.Fail()
240	}
241}
242
243func TestSendWithLocation(t *testing.T) {
244	bot, _ := getBot(t)
245
246	_, err := bot.Send(tgbotapi.NewLocation(ChatID, 40, 40))
247
248	if err != nil {
249		t.Fail()
250	}
251}
252
253func TestSendWithVenue(t *testing.T) {
254	bot, _ := getBot(t)
255
256	venue := tgbotapi.NewVenue(ChatID, "A Test Location", "123 Test Street", 40, 40)
257
258	if _, err := bot.Send(venue); err != nil {
259		t.Fail()
260	}
261}
262
263func TestSendWithNewVideo(t *testing.T) {
264	bot, _ := getBot(t)
265
266	msg := tgbotapi.NewVideoUpload(ChatID, "tests/video.mp4")
267	msg.Duration = 10
268	msg.Caption = "TEST"
269
270	_, err := bot.Send(msg)
271
272	if err != nil {
273		t.Fail()
274	}
275}
276
277func TestSendWithExistingVideo(t *testing.T) {
278	bot, _ := getBot(t)
279
280	msg := tgbotapi.NewVideoShare(ChatID, ExistingVideoFileID)
281	msg.Duration = 10
282	msg.Caption = "TEST"
283
284	_, err := bot.Send(msg)
285
286	if err != nil {
287		t.Fail()
288	}
289}
290
291func TestSendWithNewSticker(t *testing.T) {
292	bot, _ := getBot(t)
293
294	msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
295
296	_, err := bot.Send(msg)
297
298	if err != nil {
299		t.Fail()
300	}
301}
302
303func TestSendWithExistingSticker(t *testing.T) {
304	bot, _ := getBot(t)
305
306	msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
307
308	_, err := bot.Send(msg)
309
310	if err != nil {
311		t.Fail()
312	}
313}
314
315func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
316	bot, _ := getBot(t)
317
318	msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
319	msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
320	_, err := bot.Send(msg)
321
322	if err != nil {
323		t.Fail()
324	}
325}
326
327func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
328	bot, _ := getBot(t)
329
330	msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
331	msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
332
333	_, err := bot.Send(msg)
334
335	if err != nil {
336
337		t.Fail()
338	}
339}
340
341func TestGetFile(t *testing.T) {
342	bot, _ := getBot(t)
343
344	file := tgbotapi.FileConfig{ExistingPhotoFileID}
345
346	_, err := bot.GetFile(file)
347
348	if err != nil {
349		t.Fail()
350	}
351}
352
353func TestSendChatConfig(t *testing.T) {
354	bot, _ := getBot(t)
355
356	_, err := bot.Send(tgbotapi.NewChatAction(ChatID, tgbotapi.ChatTyping))
357
358	if err != nil {
359		t.Fail()
360	}
361}
362
363func TestSendEditMessage(t *testing.T) {
364	bot, _ := getBot(t)
365
366	msg, err := bot.Send(tgbotapi.NewMessage(ChatID, "Testing editing."))
367	if err != nil {
368		t.Fail()
369	}
370
371	edit := tgbotapi.EditMessageTextConfig{
372		BaseEdit: tgbotapi.BaseEdit{
373			ChatID:    ChatID,
374			MessageID: msg.MessageID,
375		},
376		Text: "Updated text.",
377	}
378
379	_, err = bot.Send(edit)
380	if err != nil {
381		t.Fail()
382	}
383}
384
385func TestGetUserProfilePhotos(t *testing.T) {
386	bot, _ := getBot(t)
387
388	_, err := bot.GetUserProfilePhotos(tgbotapi.NewUserProfilePhotos(ChatID))
389	if err != nil {
390		t.Fail()
391	}
392}
393
394func TestSetWebhookWithCert(t *testing.T) {
395	bot, _ := getBot(t)
396
397	bot.RemoveWebhook()
398
399	wh := tgbotapi.NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
400	_, err := bot.SetWebhook(wh)
401	if err != nil {
402		t.Fail()
403	}
404
405	bot.RemoveWebhook()
406}
407
408func TestSetWebhookWithoutCert(t *testing.T) {
409	bot, _ := getBot(t)
410
411	bot.RemoveWebhook()
412
413	wh := tgbotapi.NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
414	_, err := bot.SetWebhook(wh)
415	if err != nil {
416		t.Fail()
417	}
418
419	bot.RemoveWebhook()
420}
421
422func TestNewInlineQueryResultArticle(t *testing.T) {
423	result := tgbotapi.NewInlineQueryResultArticle("id", "title", "message")
424
425	if result.Type != "article" ||
426		result.ID != "id" ||
427		result.Title != "title" ||
428		result.InputMessageContent.(tgbotapi.InputTextMessageContent).Text != "message" {
429		t.Fail()
430	}
431}
432
433func TestNewInlineQueryResultGIF(t *testing.T) {
434	result := tgbotapi.NewInlineQueryResultGIF("id", "google.com")
435
436	if result.Type != "gif" ||
437		result.ID != "id" ||
438		result.URL != "google.com" {
439		t.Fail()
440	}
441}
442
443func TestNewInlineQueryResultMPEG4GIF(t *testing.T) {
444	result := tgbotapi.NewInlineQueryResultMPEG4GIF("id", "google.com")
445
446	if result.Type != "mpeg4_gif" ||
447		result.ID != "id" ||
448		result.URL != "google.com" {
449		t.Fail()
450	}
451}
452
453func TestNewInlineQueryResultPhoto(t *testing.T) {
454	result := tgbotapi.NewInlineQueryResultPhoto("id", "google.com")
455
456	if result.Type != "photo" ||
457		result.ID != "id" ||
458		result.URL != "google.com" {
459		t.Fail()
460	}
461}
462
463func TestNewInlineQueryResultVideo(t *testing.T) {
464	result := tgbotapi.NewInlineQueryResultVideo("id", "google.com")
465
466	if result.Type != "video" ||
467		result.ID != "id" ||
468		result.URL != "google.com" {
469	}
470}
471
472func TestNewEditMessageText(t *testing.T) {
473	edit := tgbotapi.NewEditMessageText(ChatID, ReplyToMessageID, "new text")
474
475	if edit.Text != "new text" ||
476		edit.BaseEdit.ChatID != ChatID ||
477		edit.BaseEdit.MessageID != ReplyToMessageID {
478		t.Fail()
479	}
480}
481
482func TestNewEditMessageCaption(t *testing.T) {
483	edit := tgbotapi.NewEditMessageCaption(ChatID, ReplyToMessageID, "new caption")
484
485	if edit.Caption != "new caption" ||
486		edit.BaseEdit.ChatID != ChatID ||
487		edit.BaseEdit.MessageID != ReplyToMessageID {
488		t.Fail()
489	}
490}
491
492func TestNewEditMessageReplyMarkup(t *testing.T) {
493	markup := tgbotapi.InlineKeyboardMarkup{
494		InlineKeyboard: [][]tgbotapi.InlineKeyboardButton{
495			[]tgbotapi.InlineKeyboardButton{
496				tgbotapi.InlineKeyboardButton{Text: "test"},
497			},
498		},
499	}
500
501	edit := tgbotapi.NewEditMessageReplyMarkup(ChatID, ReplyToMessageID, markup)
502
503	if edit.ReplyMarkup.InlineKeyboard[0][0].Text != "test" ||
504		edit.BaseEdit.ChatID != ChatID ||
505		edit.BaseEdit.MessageID != ReplyToMessageID {
506		t.Fail()
507	}
508
509}
510
511func TestUpdatesChan(t *testing.T) {
512	bot, _ := getBot(t)
513
514	var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
515	ucfg.Timeout = 60
516	_, err := bot.GetUpdatesChan(ucfg)
517
518	if err != nil {
519		t.Fail()
520	}
521}
522
523func ExampleNewBotAPI() {
524	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
525	if err != nil {
526		log.Panic(err)
527	}
528
529	bot.Debug = true
530
531	log.Printf("Authorized on account %s", bot.Self.UserName)
532
533	u := tgbotapi.NewUpdate(0)
534	u.Timeout = 60
535
536	updates, err := bot.GetUpdatesChan(u)
537
538	for update := range updates {
539		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
540
541		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
542		msg.ReplyToMessageID = update.Message.MessageID
543
544		bot.Send(msg)
545	}
546}
547
548func ExampleNewWebhook() {
549	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
550	if err != nil {
551		log.Fatal(err)
552	}
553
554	bot.Debug = true
555
556	log.Printf("Authorized on account %s", bot.Self.UserName)
557
558	_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
559	if err != nil {
560		log.Fatal(err)
561	}
562
563	updates := bot.ListenForWebhook("/" + bot.Token)
564	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
565
566	for update := range updates {
567		log.Printf("%+v\n", update)
568	}
569}
570
571func ExampleAnswerInlineQuery() {
572	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") // create new bot
573	if err != nil {
574		log.Panic(err)
575	}
576
577	log.Printf("Authorized on account %s", bot.Self.UserName)
578
579	u := tgbotapi.NewUpdate(0)
580	u.Timeout = 60
581
582	updates, err := bot.GetUpdatesChan(u)
583
584	for update := range updates {
585		if update.InlineQuery.Query == "" { // if no inline query, ignore it
586			continue
587		}
588
589		article := tgbotapi.NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
590		article.Description = update.InlineQuery.Query
591
592		inlineConf := tgbotapi.InlineConfig{
593			InlineQueryID: update.InlineQuery.ID,
594			IsPersonal:    true,
595			CacheTime:     0,
596			Results:       []interface{}{article},
597		}
598
599		if _, err := bot.AnswerInlineQuery(inlineConf); err != nil {
600			log.Println(err)
601		}
602	}
603}