all repos — telegram-bot-api @ 53dd9f6367ff5e28f99962463d58d2d298c8dba0

Golang bindings for the Telegram Bot API

bot_test.go (view raw)

  1package tgbotapi_test
  2
  3import (
  4	"github.com/zhulik/telegram-bot-api"
  5	"log"
  6	"net/http"
  7	"net/http/httptest"
  8	"os"
  9	"strings"
 10	"testing"
 11)
 12
 13func TestMain(m *testing.M) {
 14	botToken := os.Getenv("TELEGRAM_API_TOKEN")
 15
 16	if botToken == "" {
 17		log.Panic("You must provide a TELEGRAM_API_TOKEN env variable to test!")
 18	}
 19
 20	os.Exit(m.Run())
 21}
 22
 23func TestNewBotAPI_notoken(t *testing.T) {
 24	_, err := tgbotapi.NewBotAPI("")
 25
 26	if err == nil {
 27		t.Fail()
 28	}
 29}
 30
 31func TestNewBotAPI_token(t *testing.T) {
 32	_, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
 33
 34	if err != nil {
 35		t.Fail()
 36	}
 37}
 38
 39func TestGetUpdates(t *testing.T) {
 40	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
 41
 42	if err != nil {
 43		t.Fail()
 44	}
 45
 46	u := tgbotapi.NewUpdate(0)
 47
 48	_, err = bot.GetUpdates(u)
 49
 50	if err != nil {
 51		t.Log(err.Error())
 52		t.Fail()
 53	}
 54}
 55
 56func TestSendWithMessage(t *testing.T) {
 57	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
 58
 59	if err != nil {
 60		t.Fail()
 61	}
 62
 63	msg := tgbotapi.NewMessage(76918703, "A test message from the test library in telegram-bot-api")
 64	msg.ParseMode = "markdown"
 65	_, err = bot.Send(msg)
 66
 67	if err != nil {
 68		t.Fail()
 69	}
 70}
 71
 72func TestSendWithMessageReply(t *testing.T) {
 73	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
 74
 75	if err != nil {
 76		t.Fail()
 77	}
 78
 79	msg := tgbotapi.NewMessage(76918703, "A test message from the test library in telegram-bot-api")
 80	msg.ReplyToMessageID = 480
 81	_, err = bot.Send(msg)
 82
 83	if err != nil {
 84		t.Fail()
 85	}
 86}
 87
 88func TestSendWithMessageForward(t *testing.T) {
 89	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
 90
 91	if err != nil {
 92		t.Fail()
 93	}
 94
 95	msg := tgbotapi.NewForward(76918703, 76918703, 480)
 96	_, err = bot.Send(msg)
 97
 98	if err != nil {
 99		t.Fail()
100	}
101}
102
103func TestSendWithNewPhoto(t *testing.T) {
104	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
105
106	if err != nil {
107		t.Fail()
108	}
109
110	msg := tgbotapi.NewPhotoUpload(76918703, "tests/image.jpg")
111	msg.Caption = "Test"
112	_, err = bot.Send(msg)
113
114	if err != nil {
115		t.Fail()
116	}
117}
118
119func TestSendWithNewPhotoReply(t *testing.T) {
120	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
121
122	if err != nil {
123		t.Fail()
124	}
125
126	msg := tgbotapi.NewPhotoUpload(76918703, "tests/image.jpg")
127	msg.ReplyToMessageID = 480
128
129	_, err = bot.Send(msg)
130
131	if err != nil {
132		t.Fail()
133	}
134}
135
136func TestSendWithExistingPhoto(t *testing.T) {
137	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
138
139	if err != nil {
140		t.Fail()
141	}
142
143	msg := tgbotapi.NewPhotoShare(76918703, "AgADAgADxKcxG4cBswqt13DnHOgbmBxDhCoABC0h01_AL4SKe20BAAEC")
144	msg.Caption = "Test"
145	_, err = bot.Send(msg)
146
147	if err != nil {
148		t.Fail()
149	}
150}
151
152func TestSendWithNewDocument(t *testing.T) {
153	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
154
155	if err != nil {
156		t.Fail()
157	}
158
159	msg := tgbotapi.NewDocumentUpload(76918703, "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, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
169
170	if err != nil {
171		t.Fail()
172	}
173
174	msg := tgbotapi.NewDocumentShare(76918703, "BQADAgADBwADhwGzCjWgiUU4T8VNAg")
175	_, err = bot.Send(msg)
176
177	if err != nil {
178		t.Fail()
179	}
180}
181
182func TestSendWithNewAudio(t *testing.T) {
183	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
184
185	if err != nil {
186		t.Fail()
187	}
188
189	msg := tgbotapi.NewAudioUpload(76918703, "tests/audio.mp3")
190	msg.Title = "TEST"
191	msg.Duration = 10
192	msg.Performer = "TEST"
193	_, err = bot.Send(msg)
194
195	if err != nil {
196		t.Fail()
197	}
198}
199
200func TestSendWithExistingAudio(t *testing.T) {
201	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
202
203	if err != nil {
204		t.Fail()
205	}
206
207	msg := tgbotapi.NewAudioShare(76918703, "BQADAgADMwADhwGzCkYFlCTpxiP6Ag")
208	msg.Title = "TEST"
209	msg.Duration = 10
210	msg.Performer = "TEST"
211
212	_, err = bot.Send(msg)
213
214	if err != nil {
215		t.Fail()
216	}
217}
218
219func TestSendWithNewVoice(t *testing.T) {
220	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
221
222	if err != nil {
223		t.Fail()
224	}
225
226	msg := tgbotapi.NewVoiceUpload(76918703, "tests/voice.ogg")
227	msg.Duration = 10
228	_, err = bot.Send(msg)
229
230	if err != nil {
231		t.Fail()
232	}
233}
234
235func TestSendWithExistingVoice(t *testing.T) {
236	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
237
238	if err != nil {
239		t.Fail()
240	}
241
242	msg := tgbotapi.NewVoiceShare(76918703, "AwADAgADIgADhwGzCigyMW_GUtWIAg")
243	msg.Duration = 10
244	_, err = bot.Send(msg)
245
246	if err != nil {
247		t.Fail()
248	}
249}
250
251func TestSendWithLocation(t *testing.T) {
252	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
253
254	if err != nil {
255		t.Fail()
256	}
257
258	_, err = bot.Send(tgbotapi.NewLocation(76918703, 40, 40))
259
260	if err != nil {
261		t.Fail()
262	}
263}
264
265func TestSendWithNewVideo(t *testing.T) {
266	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
267
268	if err != nil {
269		t.Fail()
270	}
271
272	msg := tgbotapi.NewVideoUpload(76918703, "tests/video.mp4")
273	msg.Duration = 10
274	msg.Caption = "TEST"
275
276	_, err = bot.Send(msg)
277
278	if err != nil {
279
280		t.Fail()
281	}
282}
283
284func TestSendWithExistingVideo(t *testing.T) {
285	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
286
287	if err != nil {
288		t.Fail()
289	}
290
291	msg := tgbotapi.NewVideoShare(76918703, "BAADAgADRgADhwGzCopBeKJ7rv9SAg")
292	msg.Duration = 10
293	msg.Caption = "TEST"
294
295	_, err = bot.Send(msg)
296
297	if err != nil {
298
299		t.Fail()
300	}
301}
302
303func TestSendWithNewSticker(t *testing.T) {
304	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
305
306	if err != nil {
307		t.Fail()
308	}
309
310	msg := tgbotapi.NewStickerUpload(76918703, "tests/image.jpg")
311
312	_, err = bot.Send(msg)
313
314	if err != nil {
315
316		t.Fail()
317	}
318}
319
320func TestSendWithExistingSticker(t *testing.T) {
321	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
322
323	if err != nil {
324		t.Fail()
325	}
326
327	msg := tgbotapi.NewStickerShare(76918703, "BQADAgADUwADhwGzCmwtOypNFlrRAg")
328
329	_, err = bot.Send(msg)
330
331	if err != nil {
332
333		t.Fail()
334	}
335}
336
337func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
338	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
339
340	if err != nil {
341		t.Fail()
342	}
343
344	msg := tgbotapi.NewStickerUpload(76918703, "tests/image.jpg")
345	msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
346	_, err = bot.Send(msg)
347
348	if err != nil {
349
350		t.Fail()
351	}
352}
353
354func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
355	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
356
357	if err != nil {
358		t.Fail()
359	}
360
361	msg := tgbotapi.NewStickerShare(76918703, "BQADAgADUwADhwGzCmwtOypNFlrRAg")
362	msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
363
364	_, err = bot.Send(msg)
365
366	if err != nil {
367
368		t.Fail()
369	}
370}
371
372func TestGetFile(t *testing.T) {
373	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
374
375	if err != nil {
376		t.Fail()
377	}
378
379	file := tgbotapi.FileConfig{"BQADAgADBwADhwGzCjWgiUU4T8VNAg"}
380
381	_, err = bot.GetFile(file)
382
383	if err != nil {
384		t.Fail()
385	}
386}
387
388func TestSendChatConfig(t *testing.T) {
389	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
390
391	if err != nil {
392		t.Fail()
393	}
394
395	err = bot.SendChatAction(tgbotapi.NewChatAction(76918703, tgbotapi.ChatTyping))
396
397	if err != nil {
398		t.Fail()
399	}
400}
401
402func TestGetUserProfilePhotos(t *testing.T) {
403	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
404
405	if err != nil {
406		t.Fail()
407	}
408
409	_, err = bot.GetUserProfilePhotos(tgbotapi.NewUserProfilePhotos(76918703))
410	if err != nil {
411		t.Fail()
412	}
413}
414
415func TestListenForWebhook(t *testing.T) {
416	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
417
418	if err != nil {
419		t.Fail()
420	}
421
422	handler := bot.ListenForWebhook("/")
423
424	req, _ := http.NewRequest("GET", "", strings.NewReader("{}"))
425	w := httptest.NewRecorder()
426
427	handler.ServeHTTP(w, req)
428	if w.Code != http.StatusOK {
429		t.Errorf("Home page didn't return %v", http.StatusOK)
430	}
431}
432
433func TestSetWebhook(t *testing.T) {
434	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
435
436	if err != nil {
437		t.Fail()
438	}
439
440	wh := tgbotapi.WebhookConfig{Clear: true}
441	_, err = bot.SetWebhook(wh)
442
443	wh = tgbotapi.NewWebhookWithCert("https://example.com/tgbotapi-test/" + bot.Token, "tests/cert.pem")
444	_, err = bot.SetWebhook(wh)
445	if err != nil {
446		t.Fail()
447	}
448
449	wh = tgbotapi.WebhookConfig{Clear: true}
450	_, err = bot.SetWebhook(wh)
451}
452
453func TestUpdatesChan(t *testing.T) {
454	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
455
456	if err != nil {
457		t.Fail()
458	}
459
460	var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
461	ucfg.Timeout = 60
462	err = bot.UpdatesChan(ucfg)
463
464	if err != nil {
465		t.Fail()
466	}
467}
468
469func ExampleNewBotAPI() {
470	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
471	if err != nil {
472		log.Panic(err)
473	}
474
475	bot.Debug = true
476
477	log.Printf("Authorized on account %s", bot.Self.UserName)
478
479	u := tgbotapi.NewUpdate(0)
480	u.Timeout = 60
481
482	err = bot.UpdatesChan(u)
483
484	for update := range bot.Updates {
485		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
486
487		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
488		msg.ReplyToMessageID = update.Message.MessageID
489
490		bot.Send(msg)
491	}
492}