all repos — telegram-bot-api @ b6130533a37705a96e236717c9baf6484c0e8c8c

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 TestSendWithLocation(t *testing.T) {
234	bot, _ := getBot(t)
235
236	_, err := bot.Send(tgbotapi.NewLocation(ChatID, 40, 40))
237
238	if err != nil {
239		t.Fail()
240	}
241}
242
243func TestSendWithNewVideo(t *testing.T) {
244	bot, _ := getBot(t)
245
246	msg := tgbotapi.NewVideoUpload(ChatID, "tests/video.mp4")
247	msg.Duration = 10
248	msg.Caption = "TEST"
249
250	_, err := bot.Send(msg)
251
252	if err != nil {
253		t.Fail()
254	}
255}
256
257func TestSendWithExistingVideo(t *testing.T) {
258	bot, _ := getBot(t)
259
260	msg := tgbotapi.NewVideoShare(ChatID, ExistingVideoFileID)
261	msg.Duration = 10
262	msg.Caption = "TEST"
263
264	_, err := bot.Send(msg)
265
266	if err != nil {
267		t.Fail()
268	}
269}
270
271func TestSendWithNewSticker(t *testing.T) {
272	bot, _ := getBot(t)
273
274	msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
275
276	_, err := bot.Send(msg)
277
278	if err != nil {
279		t.Fail()
280	}
281}
282
283func TestSendWithExistingSticker(t *testing.T) {
284	bot, _ := getBot(t)
285
286	msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
287
288	_, err := bot.Send(msg)
289
290	if err != nil {
291		t.Fail()
292	}
293}
294
295func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
296	bot, _ := getBot(t)
297
298	msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
299	msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
300	_, err := bot.Send(msg)
301
302	if err != nil {
303		t.Fail()
304	}
305}
306
307func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
308	bot, _ := getBot(t)
309
310	msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
311	msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
312
313	_, err := bot.Send(msg)
314
315	if err != nil {
316
317		t.Fail()
318	}
319}
320
321func TestGetFile(t *testing.T) {
322	bot, _ := getBot(t)
323
324	file := tgbotapi.FileConfig{ExistingPhotoFileID}
325
326	_, err := bot.GetFile(file)
327
328	if err != nil {
329		t.Fail()
330	}
331}
332
333func TestSendChatConfig(t *testing.T) {
334	bot, _ := getBot(t)
335
336	_, err := bot.Send(tgbotapi.NewChatAction(ChatID, tgbotapi.ChatTyping))
337
338	if err != nil {
339		t.Fail()
340	}
341}
342
343func TestSendEditMessage(t *testing.T) {
344	bot, _ := getBot(t)
345
346	msg, err := bot.Send(tgbotapi.NewMessage(ChatID, "Testing editing."))
347	if err != nil {
348		t.Fail()
349	}
350
351	edit := tgbotapi.EditMessageTextConfig{
352		BaseEdit: tgbotapi.BaseEdit{
353			ChatID:    ChatID,
354			MessageID: msg.MessageID,
355		},
356		Text: "Updated text.",
357	}
358
359	_, err = bot.Send(edit)
360	if err != nil {
361		t.Fail()
362	}
363}
364
365func TestGetUserProfilePhotos(t *testing.T) {
366	bot, _ := getBot(t)
367
368	_, err := bot.GetUserProfilePhotos(tgbotapi.NewUserProfilePhotos(ChatID))
369	if err != nil {
370		t.Fail()
371	}
372}
373
374func TestSetWebhookWithCert(t *testing.T) {
375	bot, _ := getBot(t)
376
377	bot.RemoveWebhook()
378
379	wh := tgbotapi.NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
380	_, err := bot.SetWebhook(wh)
381	if err != nil {
382		t.Fail()
383	}
384
385	bot.RemoveWebhook()
386}
387
388func TestSetWebhookWithoutCert(t *testing.T) {
389	bot, _ := getBot(t)
390
391	bot.RemoveWebhook()
392
393	wh := tgbotapi.NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
394	_, err := bot.SetWebhook(wh)
395	if err != nil {
396		t.Fail()
397	}
398
399	bot.RemoveWebhook()
400}
401
402func TestUpdatesChan(t *testing.T) {
403	bot, _ := getBot(t)
404
405	var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
406	ucfg.Timeout = 60
407	_, err := bot.GetUpdatesChan(ucfg)
408
409	if err != nil {
410		t.Fail()
411	}
412}
413
414func ExampleNewBotAPI() {
415	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
416	if err != nil {
417		log.Panic(err)
418	}
419
420	bot.Debug = true
421
422	log.Printf("Authorized on account %s", bot.Self.UserName)
423
424	u := tgbotapi.NewUpdate(0)
425	u.Timeout = 60
426
427	updates, err := bot.GetUpdatesChan(u)
428
429	for update := range updates {
430		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
431
432		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
433		msg.ReplyToMessageID = update.Message.MessageID
434
435		bot.Send(msg)
436	}
437}
438
439func ExampleNewWebhook() {
440	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
441	if err != nil {
442		log.Fatal(err)
443	}
444
445	bot.Debug = true
446
447	log.Printf("Authorized on account %s", bot.Self.UserName)
448
449	_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
450	if err != nil {
451		log.Fatal(err)
452	}
453
454	updates := bot.ListenForWebhook("/" + bot.Token)
455	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
456
457	for update := range updates {
458		log.Printf("%+v\n", update)
459	}
460}
461
462func ExampleAnswerInlineQuery() {
463	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken") // create new bot
464	if err != nil {
465		log.Panic(err)
466	}
467
468	log.Printf("Authorized on account %s", bot.Self.UserName)
469
470	u := tgbotapi.NewUpdate(0)
471	u.Timeout = 60
472
473	updates, err := bot.GetUpdatesChan(u)
474
475	for update := range updates {
476		if update.InlineQuery.Query == "" { // if no inline query, ignore it
477			continue
478		}
479
480		article := tgbotapi.NewInlineQueryResultArticle(update.InlineQuery.ID, "Echo", update.InlineQuery.Query)
481		article.Description = update.InlineQuery.Query
482
483		inlineConf := tgbotapi.InlineConfig{
484			InlineQueryID: update.InlineQuery.ID,
485			IsPersonal:    true,
486			CacheTime:     0,
487			Results:       []interface{}{article},
488		}
489
490		if _, err := bot.AnswerInlineQuery(inlineConf); err != nil {
491			log.Println(err)
492		}
493	}
494}