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