all repos — telegram-bot-api @ 3866c68e795c67225c59663bd0e84291512965f6

Golang bindings for the Telegram Bot API

bot_test.go (view raw)

  1package tgbotapi_test
  2
  3import (
  4	"io/ioutil"
  5	"log"
  6	"net/http"
  7	"os"
  8	"testing"
  9	"time"
 10
 11	"github.com/go-telegram-bot-api/telegram-bot-api"
 12)
 13
 14const (
 15	TestToken              = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I"
 16	ChatID                 = 76918703
 17	ReplyToMessageID       = 35
 18	ExistingPhotoFileID    = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC"
 19	ExistingDocumentFileID = "BQADAgADOQADjMcoCcioX1GrDvp3Ag"
 20	ExistingAudioFileID    = "BQADAgADRgADjMcoCdXg3lSIN49lAg"
 21	ExistingVoiceFileID    = "AwADAgADWQADjMcoCeul6r_q52IyAg"
 22	ExistingVideoFileID    = "BAADAgADZgADjMcoCav432kYe0FRAg"
 23	ExistingStickerFileID  = "BQADAgADcwADjMcoCbdl-6eB--YPAg"
 24)
 25
 26func getBot(t *testing.T) (*tgbotapi.BotAPI, error) {
 27	bot, err := tgbotapi.NewBotAPI(TestToken)
 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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.FileBytes{Name: "image.jpg", Bytes: data}
115
116	msg := tgbotapi.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 := tgbotapi.FileReader{Name: "image.jpg", Reader: f, Size: -1}
131
132	msg := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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(tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 TestSendWithNewSticker(t *testing.T) {
316	bot, _ := getBot(t)
317
318	msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
319
320	_, err := bot.Send(msg)
321
322	if err != nil {
323		t.Error(err)
324		t.Fail()
325	}
326}
327
328func TestSendWithExistingSticker(t *testing.T) {
329	bot, _ := getBot(t)
330
331	msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
332
333	_, err := bot.Send(msg)
334
335	if err != nil {
336		t.Error(err)
337		t.Fail()
338	}
339}
340
341func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
342	bot, _ := getBot(t)
343
344	msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
345	msg.ReplyMarkup = tgbotapi.ReplyKeyboardRemove{true, false}
346	_, err := bot.Send(msg)
347
348	if err != nil {
349		t.Error(err)
350		t.Fail()
351	}
352}
353
354func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
355	bot, _ := getBot(t)
356
357	msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
358	msg.ReplyMarkup = tgbotapi.ReplyKeyboardRemove{true, false}
359
360	_, err := bot.Send(msg)
361
362	if err != nil {
363		t.Error(err)
364		t.Fail()
365	}
366}
367
368func TestGetFile(t *testing.T) {
369	bot, _ := getBot(t)
370
371	file := tgbotapi.FileConfig{ExistingPhotoFileID}
372
373	_, err := bot.GetFile(file)
374
375	if err != nil {
376		t.Error(err)
377		t.Fail()
378	}
379}
380
381func TestSendChatConfig(t *testing.T) {
382	bot, _ := getBot(t)
383
384	_, err := bot.Send(tgbotapi.NewChatAction(ChatID, tgbotapi.ChatTyping))
385
386	if err != nil {
387		t.Error(err)
388		t.Fail()
389	}
390}
391
392func TestSendEditMessage(t *testing.T) {
393	bot, _ := getBot(t)
394
395	msg, err := bot.Send(tgbotapi.NewMessage(ChatID, "Testing editing."))
396	if err != nil {
397		t.Error(err)
398		t.Fail()
399	}
400
401	edit := tgbotapi.EditMessageTextConfig{
402		BaseEdit: tgbotapi.BaseEdit{
403			ChatID:    ChatID,
404			MessageID: msg.MessageID,
405		},
406		Text: "Updated text.",
407	}
408
409	_, err = bot.Send(edit)
410	if err != nil {
411		t.Error(err)
412		t.Fail()
413	}
414}
415
416func TestGetUserProfilePhotos(t *testing.T) {
417	bot, _ := getBot(t)
418
419	_, err := bot.GetUserProfilePhotos(tgbotapi.NewUserProfilePhotos(ChatID))
420	if err != nil {
421		t.Error(err)
422		t.Fail()
423	}
424}
425
426func TestSetWebhookWithCert(t *testing.T) {
427	bot, _ := getBot(t)
428
429	time.Sleep(time.Second * 2)
430
431	bot.RemoveWebhook()
432
433	wh := tgbotapi.NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
434	_, err := bot.SetWebhook(wh)
435	if err != nil {
436		t.Error(err)
437		t.Fail()
438	}
439
440	bot.RemoveWebhook()
441}
442
443func TestSetWebhookWithoutCert(t *testing.T) {
444	bot, _ := getBot(t)
445
446	time.Sleep(time.Second * 2)
447
448	bot.RemoveWebhook()
449
450	wh := tgbotapi.NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
451	_, err := bot.SetWebhook(wh)
452	if err != nil {
453		t.Error(err)
454		t.Fail()
455	}
456
457	bot.RemoveWebhook()
458}
459
460func TestUpdatesChan(t *testing.T) {
461	bot, _ := getBot(t)
462
463	var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
464	ucfg.Timeout = 60
465	_, err := bot.GetUpdatesChan(ucfg)
466
467	if err != nil {
468		t.Error(err)
469		t.Fail()
470	}
471}
472
473func ExampleNewBotAPI() {
474	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
475	if err != nil {
476		log.Panic(err)
477	}
478
479	bot.Debug = true
480
481	log.Printf("Authorized on account %s", bot.Self.UserName)
482
483	u := tgbotapi.NewUpdate(0)
484	u.Timeout = 60
485
486	updates, err := bot.GetUpdatesChan(u)
487
488	// Optional: wait for updates and clear them if you don't want to handle
489	// a large backlog of old messages
490	time.Sleep(time.Millisecond * 500)
491	updates.Clear()
492
493	for update := range updates {
494		if update.Message == nil {
495			continue
496		}
497
498		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
499
500		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
501		msg.ReplyToMessageID = update.Message.MessageID
502
503		bot.Send(msg)
504	}
505}
506
507func ExampleNewWebhook() {
508	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
509	if err != nil {
510		log.Fatal(err)
511	}
512
513	bot.Debug = true
514
515	log.Printf("Authorized on account %s", bot.Self.UserName)
516
517	_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
518	if err != nil {
519		log.Fatal(err)
520	}
521
522	updates := bot.ListenForWebhook("/" + bot.Token)
523	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
524
525	for update := range updates {
526		log.Printf("%+v\n", update)
527	}
528}
529
530func ExampleAnswerInlineQuery() {
531	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") // create new bot
532	if err != nil {
533		log.Panic(err)
534	}
535
536	log.Printf("Authorized on account %s", bot.Self.UserName)
537
538	u := tgbotapi.NewUpdate(0)
539	u.Timeout = 60
540
541	updates, err := bot.GetUpdatesChan(u)
542
543	for update := range updates {
544		if update.InlineQuery == nil { // if no inline query, ignore it
545			continue
546		}
547
548		article := tgbotapi.NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
549		article.Description = update.InlineQuery.Query
550
551		inlineConf := tgbotapi.InlineConfig{
552			InlineQueryID: update.InlineQuery.ID,
553			IsPersonal:    true,
554			CacheTime:     0,
555			Results:       []interface{}{article},
556		}
557
558		if _, err := bot.AnswerInlineQuery(inlineConf); err != nil {
559			log.Println(err)
560		}
561	}
562}