all repos — telegram-bot-api @ 41814af2a03c7d8f8ed5d098c63c9e953f197528

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	ExistingVideoNoteFileID = "DQADAgADdQAD70cQSUK41dLsRMqfAg"
 24	ExistingStickerFileID   = "BQADAgADcwADjMcoCbdl-6eB--YPAg"
 25)
 26
 27func getBot(t *testing.T) (*tgbotapi.BotAPI, error) {
 28	bot, err := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.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 := tgbotapi.FileBytes{Name: "image.jpg", Bytes: data}
117
118	msg := tgbotapi.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 := tgbotapi.FileReader{Name: "image.jpg", Reader: f, Size: -1}
133
134	msg := tgbotapi.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 := tgbotapi.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 TestSendWithExistingPhoto(t *testing.T) {
159	bot, _ := getBot(t)
160
161	msg := tgbotapi.NewPhotoShare(ChatID, ExistingPhotoFileID)
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 TestSendWithNewDocument(t *testing.T) {
172	bot, _ := getBot(t)
173
174	msg := tgbotapi.NewDocumentUpload(ChatID, "tests/image.jpg")
175	_, err := bot.Send(msg)
176
177	if err != nil {
178		t.Error(err)
179		t.Fail()
180	}
181}
182
183func TestSendWithExistingDocument(t *testing.T) {
184	bot, _ := getBot(t)
185
186	msg := tgbotapi.NewDocumentShare(ChatID, ExistingDocumentFileID)
187	_, err := bot.Send(msg)
188
189	if err != nil {
190		t.Error(err)
191		t.Fail()
192	}
193}
194
195func TestSendWithNewAudio(t *testing.T) {
196	bot, _ := getBot(t)
197
198	msg := tgbotapi.NewAudioUpload(ChatID, "tests/audio.mp3")
199	msg.Title = "TEST"
200	msg.Duration = 10
201	msg.Performer = "TEST"
202	msg.MimeType = "audio/mpeg"
203	msg.FileSize = 688
204	_, err := bot.Send(msg)
205
206	if err != nil {
207		t.Error(err)
208		t.Fail()
209	}
210}
211
212func TestSendWithExistingAudio(t *testing.T) {
213	bot, _ := getBot(t)
214
215	msg := tgbotapi.NewAudioShare(ChatID, ExistingAudioFileID)
216	msg.Title = "TEST"
217	msg.Duration = 10
218	msg.Performer = "TEST"
219
220	_, err := bot.Send(msg)
221
222	if err != nil {
223		t.Error(err)
224		t.Fail()
225	}
226}
227
228func TestSendWithNewVoice(t *testing.T) {
229	bot, _ := getBot(t)
230
231	msg := tgbotapi.NewVoiceUpload(ChatID, "tests/voice.ogg")
232	msg.Duration = 10
233	_, err := bot.Send(msg)
234
235	if err != nil {
236		t.Error(err)
237		t.Fail()
238	}
239}
240
241func TestSendWithExistingVoice(t *testing.T) {
242	bot, _ := getBot(t)
243
244	msg := tgbotapi.NewVoiceShare(ChatID, ExistingVoiceFileID)
245	msg.Duration = 10
246	_, err := bot.Send(msg)
247
248	if err != nil {
249		t.Error(err)
250		t.Fail()
251	}
252}
253
254func TestSendWithContact(t *testing.T) {
255	bot, _ := getBot(t)
256
257	contact := tgbotapi.NewContact(ChatID, "5551234567", "Test")
258
259	if _, err := bot.Send(contact); err != nil {
260		t.Error(err)
261		t.Fail()
262	}
263}
264
265func TestSendWithLocation(t *testing.T) {
266	bot, _ := getBot(t)
267
268	_, err := bot.Send(tgbotapi.NewLocation(ChatID, 40, 40))
269
270	if err != nil {
271		t.Error(err)
272		t.Fail()
273	}
274}
275
276func TestSendWithVenue(t *testing.T) {
277	bot, _ := getBot(t)
278
279	venue := tgbotapi.NewVenue(ChatID, "A Test Location", "123 Test Street", 40, 40)
280
281	if _, err := bot.Send(venue); err != nil {
282		t.Error(err)
283		t.Fail()
284	}
285}
286
287func TestSendWithNewVideo(t *testing.T) {
288	bot, _ := getBot(t)
289
290	msg := tgbotapi.NewVideoUpload(ChatID, "tests/video.mp4")
291	msg.Duration = 10
292	msg.Caption = "TEST"
293
294	_, err := bot.Send(msg)
295
296	if err != nil {
297		t.Error(err)
298		t.Fail()
299	}
300}
301
302func TestSendWithExistingVideo(t *testing.T) {
303	bot, _ := getBot(t)
304
305	msg := tgbotapi.NewVideoShare(ChatID, ExistingVideoFileID)
306	msg.Duration = 10
307	msg.Caption = "TEST"
308
309	_, err := bot.Send(msg)
310
311	if err != nil {
312		t.Error(err)
313		t.Fail()
314	}
315}
316
317func TestSendWithNewVideoNote(t *testing.T) {
318	bot, _ := getBot(t)
319
320	msg := tgbotapi.NewVideoNoteUpload(ChatID, 240, "tests/videonote.mp4")
321	msg.Duration = 10
322
323	_, err := bot.Send(msg)
324
325	if err != nil {
326		t.Error(err)
327		t.Fail()
328	}
329}
330
331func TestSendWithExistingVideoNote(t *testing.T) {
332	bot, _ := getBot(t)
333
334	msg := tgbotapi.NewVideoNoteShare(ChatID, 240, ExistingVideoNoteFileID)
335	msg.Duration = 10
336
337	_, err := bot.Send(msg)
338
339	if err != nil {
340		t.Error(err)
341		t.Fail()
342	}
343}
344
345func TestSendWithNewSticker(t *testing.T) {
346	bot, _ := getBot(t)
347
348	msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
349
350	_, err := bot.Send(msg)
351
352	if err != nil {
353		t.Error(err)
354		t.Fail()
355	}
356}
357
358func TestSendWithExistingSticker(t *testing.T) {
359	bot, _ := getBot(t)
360
361	msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
362
363	_, err := bot.Send(msg)
364
365	if err != nil {
366		t.Error(err)
367		t.Fail()
368	}
369}
370
371func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
372	bot, _ := getBot(t)
373
374	msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
375	msg.ReplyMarkup = tgbotapi.ReplyKeyboardRemove{true, false}
376	_, err := bot.Send(msg)
377
378	if err != nil {
379		t.Error(err)
380		t.Fail()
381	}
382}
383
384func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
385	bot, _ := getBot(t)
386
387	msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
388	msg.ReplyMarkup = tgbotapi.ReplyKeyboardRemove{true, false}
389
390	_, err := bot.Send(msg)
391
392	if err != nil {
393		t.Error(err)
394		t.Fail()
395	}
396}
397
398func TestGetFile(t *testing.T) {
399	bot, _ := getBot(t)
400
401	file := tgbotapi.FileConfig{ExistingPhotoFileID}
402
403	_, err := bot.GetFile(file)
404
405	if err != nil {
406		t.Error(err)
407		t.Fail()
408	}
409}
410
411func TestSendChatConfig(t *testing.T) {
412	bot, _ := getBot(t)
413
414	_, err := bot.Send(tgbotapi.NewChatAction(ChatID, tgbotapi.ChatTyping))
415
416	if err != nil {
417		t.Error(err)
418		t.Fail()
419	}
420}
421
422func TestSendEditMessage(t *testing.T) {
423	bot, _ := getBot(t)
424
425	msg, err := bot.Send(tgbotapi.NewMessage(ChatID, "Testing editing."))
426	if err != nil {
427		t.Error(err)
428		t.Fail()
429	}
430
431	edit := tgbotapi.EditMessageTextConfig{
432		BaseEdit: tgbotapi.BaseEdit{
433			ChatID:    ChatID,
434			MessageID: msg.MessageID,
435		},
436		Text: "Updated text.",
437	}
438
439	_, err = bot.Send(edit)
440	if err != nil {
441		t.Error(err)
442		t.Fail()
443	}
444}
445
446func TestGetUserProfilePhotos(t *testing.T) {
447	bot, _ := getBot(t)
448
449	_, err := bot.GetUserProfilePhotos(tgbotapi.NewUserProfilePhotos(ChatID))
450	if err != nil {
451		t.Error(err)
452		t.Fail()
453	}
454}
455
456func TestSetWebhookWithCert(t *testing.T) {
457	bot, _ := getBot(t)
458
459	time.Sleep(time.Second * 2)
460
461	bot.RemoveWebhook()
462
463	wh := tgbotapi.NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
464	_, err := bot.SetWebhook(wh)
465	if err != nil {
466		t.Error(err)
467		t.Fail()
468	}
469
470	bot.RemoveWebhook()
471}
472
473func TestSetWebhookWithoutCert(t *testing.T) {
474	bot, _ := getBot(t)
475
476	time.Sleep(time.Second * 2)
477
478	bot.RemoveWebhook()
479
480	wh := tgbotapi.NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
481	_, err := bot.SetWebhook(wh)
482	if err != nil {
483		t.Error(err)
484		t.Fail()
485	}
486
487	bot.RemoveWebhook()
488}
489
490func TestUpdatesChan(t *testing.T) {
491	bot, _ := getBot(t)
492
493	var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
494	ucfg.Timeout = 60
495	_, err := bot.GetUpdatesChan(ucfg)
496
497	if err != nil {
498		t.Error(err)
499		t.Fail()
500	}
501}
502
503func ExampleNewBotAPI() {
504	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
505	if err != nil {
506		log.Panic(err)
507	}
508
509	bot.Debug = true
510
511	log.Printf("Authorized on account %s", bot.Self.UserName)
512
513	u := tgbotapi.NewUpdate(0)
514	u.Timeout = 60
515
516	updates, err := bot.GetUpdatesChan(u)
517
518	// Optional: wait for updates and clear them if you don't want to handle
519	// a large backlog of old messages
520	time.Sleep(time.Millisecond * 500)
521	updates.Clear()
522
523	for update := range updates {
524		if update.Message == nil {
525			continue
526		}
527
528		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
529
530		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
531		msg.ReplyToMessageID = update.Message.MessageID
532
533		bot.Send(msg)
534	}
535}
536
537func ExampleNewWebhook() {
538	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
539	if err != nil {
540		log.Fatal(err)
541	}
542
543	bot.Debug = true
544
545	log.Printf("Authorized on account %s", bot.Self.UserName)
546
547	_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
548	if err != nil {
549		log.Fatal(err)
550	}
551
552	updates := bot.ListenForWebhook("/" + bot.Token)
553	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
554
555	for update := range updates {
556		log.Printf("%+v\n", update)
557	}
558}
559
560func ExampleAnswerInlineQuery() {
561	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") // create new bot
562	if err != nil {
563		log.Panic(err)
564	}
565
566	log.Printf("Authorized on account %s", bot.Self.UserName)
567
568	u := tgbotapi.NewUpdate(0)
569	u.Timeout = 60
570
571	updates, err := bot.GetUpdatesChan(u)
572
573	for update := range updates {
574		if update.InlineQuery == nil { // if no inline query, ignore it
575			continue
576		}
577
578		article := tgbotapi.NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
579		article.Description = update.InlineQuery.Query
580
581		inlineConf := tgbotapi.InlineConfig{
582			InlineQueryID: update.InlineQuery.ID,
583			IsPersonal:    true,
584			CacheTime:     0,
585			Results:       []interface{}{article},
586		}
587
588		if _, err := bot.AnswerInlineQuery(inlineConf); err != nil {
589			log.Println(err)
590		}
591	}
592}