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