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