all repos — telegram-bot-api @ 9d6e221a006b09fb3435a69e7c2a5bc05f96892e

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