bot_test.go (view raw)
1package tgbotapi_test
2
3import (
4 "github.com/Syfaro/telegram-bot-api"
5 "io/ioutil"
6 "log"
7 "net/http"
8 "net/http/httptest"
9 "os"
10 "strings"
11 "testing"
12)
13
14const (
15 TestToken = "153667468:AAHlSHlMqSt1f_uFmVRJbm5gntu2HI4WW8I"
16 ChatID = 76918703
17 ReplyToMessageID = 35
18 ExistingPhotoFileID = "AgADAgADw6cxG4zHKAkr42N7RwEN3IFShCoABHQwXEtVks4EH2wBAAEC"
19 ExistingDocumentFileID = "BQADAgADOQADjMcoCcioX1GrDvp3Ag"
20 ExistingAudioFileID = "BQADAgADRgADjMcoCdXg3lSIN49lAg"
21 ExistingVoiceFileID = "AwADAgADWQADjMcoCeul6r_q52IyAg"
22 ExistingVideoFileID = "BAADAgADZgADjMcoCav432kYe0FRAg"
23 ExistingStickerFileID = "BQADAgADcwADjMcoCbdl-6eB--YPAg"
24)
25
26func getBot(t *testing.T) (*tgbotapi.BotAPI, error) {
27 bot, err := tgbotapi.NewBotAPI(TestToken)
28
29 if err != nil {
30 t.Fail()
31 }
32
33 return bot, err
34}
35
36func TestNewBotAPI_notoken(t *testing.T) {
37 _, err := tgbotapi.NewBotAPI("")
38
39 if err == nil {
40 t.Fail()
41 }
42}
43
44func TestGetUpdates(t *testing.T) {
45 bot, _ := getBot(t)
46
47 u := tgbotapi.NewUpdate(0)
48
49 _, err := bot.GetUpdates(u)
50
51 if err != nil {
52 t.Fail()
53 }
54}
55
56func TestSendWithMessage(t *testing.T) {
57 bot, _ := getBot(t)
58
59 msg := tgbotapi.NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
60 msg.ParseMode = "markdown"
61 _, err := bot.Send(msg)
62
63 if err != nil {
64 t.Fail()
65 }
66}
67
68func TestSendWithMessageReply(t *testing.T) {
69 bot, _ := getBot(t)
70
71 msg := tgbotapi.NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
72 msg.ReplyToMessageID = ReplyToMessageID
73 _, err := bot.Send(msg)
74
75 if err != nil {
76 t.Fail()
77 }
78}
79
80func TestSendWithMessageForward(t *testing.T) {
81 bot, _ := getBot(t)
82
83 msg := tgbotapi.NewForward(ChatID, ChatID, ReplyToMessageID)
84 _, err := bot.Send(msg)
85
86 if err != nil {
87 t.Fail()
88 }
89}
90
91func TestSendWithNewPhoto(t *testing.T) {
92 bot, _ := getBot(t)
93
94 msg := tgbotapi.NewPhotoUpload(ChatID, "tests/image.jpg")
95 msg.Caption = "Test"
96 _, err := bot.Send(msg)
97
98 if err != nil {
99 t.Fail()
100 }
101}
102
103func TestSendWithNewPhotoWithFileBytes(t *testing.T) {
104 bot, _ := getBot(t)
105
106 data, _ := ioutil.ReadFile("tests/image.jpg")
107 b := tgbotapi.FileBytes{Name: "image.jpg", Bytes: data}
108
109 msg := tgbotapi.NewPhotoUpload(ChatID, b)
110 msg.Caption = "Test"
111 _, err := bot.Send(msg)
112
113 if err != nil {
114 t.Fail()
115 }
116}
117
118func TestSendWithNewPhotoWithFileReader(t *testing.T) {
119 bot, _ := getBot(t)
120
121 f, _ := os.Open("tests/image.jpg")
122 reader := tgbotapi.FileReader{Name: "image.jpg", Reader: f, Size: -1}
123
124 msg := tgbotapi.NewPhotoUpload(ChatID, reader)
125 msg.Caption = "Test"
126 _, err := bot.Send(msg)
127
128 if err != nil {
129 t.Fail()
130 }
131}
132
133func TestSendWithNewPhotoReply(t *testing.T) {
134 bot, _ := getBot(t)
135
136 msg := tgbotapi.NewPhotoUpload(ChatID, "tests/image.jpg")
137 msg.ReplyToMessageID = ReplyToMessageID
138
139 _, err := bot.Send(msg)
140
141 if err != nil {
142 t.Fail()
143 }
144}
145
146func TestSendWithExistingPhoto(t *testing.T) {
147 bot, _ := getBot(t)
148
149 msg := tgbotapi.NewPhotoShare(ChatID, ExistingPhotoFileID)
150 msg.Caption = "Test"
151 _, err := bot.Send(msg)
152
153 if err != nil {
154 t.Fail()
155 }
156}
157
158func TestSendWithNewDocument(t *testing.T) {
159 bot, _ := getBot(t)
160
161 msg := tgbotapi.NewDocumentUpload(ChatID, "tests/image.jpg")
162 _, err := bot.Send(msg)
163
164 if err != nil {
165 t.Fail()
166 }
167}
168
169func TestSendWithExistingDocument(t *testing.T) {
170 bot, _ := getBot(t)
171
172 msg := tgbotapi.NewDocumentShare(ChatID, ExistingDocumentFileID)
173 _, err := bot.Send(msg)
174
175 if err != nil {
176 t.Fail()
177 }
178}
179
180func TestSendWithNewAudio(t *testing.T) {
181 bot, _ := getBot(t)
182
183 msg := tgbotapi.NewAudioUpload(ChatID, "tests/audio.mp3")
184 msg.Title = "TEST"
185 msg.Duration = 10
186 msg.Performer = "TEST"
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 TestGetUserProfilePhotos(t *testing.T) {
344 bot, _ := getBot(t)
345
346 _, err := bot.GetUserProfilePhotos(tgbotapi.NewUserProfilePhotos(ChatID))
347 if err != nil {
348 t.Fail()
349 }
350}
351
352func TestListenForWebhook(t *testing.T) {
353 bot, _ := getBot(t)
354
355 _, handler := bot.ListenForWebhook("/")
356
357 req, _ := http.NewRequest("GET", "", strings.NewReader("{}"))
358 w := httptest.NewRecorder()
359
360 handler.ServeHTTP(w, req)
361 if w.Code != http.StatusOK {
362 t.Errorf("Home page didn't return %v", http.StatusOK)
363 }
364}
365
366func TestSetWebhookWithCert(t *testing.T) {
367 bot, _ := getBot(t)
368
369 bot.RemoveWebhook()
370
371 wh := tgbotapi.NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
372 _, err := bot.SetWebhook(wh)
373 if err != nil {
374 t.Fail()
375 }
376
377 bot.RemoveWebhook()
378}
379
380func TestSetWebhookWithoutCert(t *testing.T) {
381 bot, _ := getBot(t)
382
383 bot.RemoveWebhook()
384
385 wh := tgbotapi.NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
386 _, err := bot.SetWebhook(wh)
387 if err != nil {
388 t.Fail()
389 }
390
391 bot.RemoveWebhook()
392}
393
394func TestUpdatesChan(t *testing.T) {
395 bot, _ := getBot(t)
396
397 var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
398 ucfg.Timeout = 60
399 _, err := bot.GetUpdatesChan(ucfg)
400
401 if err != nil {
402 t.Fail()
403 }
404}
405
406func ExampleNewBotAPI() {
407 bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
408 if err != nil {
409 log.Panic(err)
410 }
411
412 bot.Debug = true
413
414 log.Printf("Authorized on account %s", bot.Self.UserName)
415
416 u := tgbotapi.NewUpdate(0)
417 u.Timeout = 60
418
419 updates, err := bot.GetUpdatesChan(u)
420
421 for update := range updates {
422 log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
423
424 msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
425 msg.ReplyToMessageID = update.Message.MessageID
426
427 bot.Send(msg)
428 }
429}
430
431func ExampleNewWebhook() {
432 bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
433 if err != nil {
434 log.Fatal(err)
435 }
436
437 bot.Debug = true
438
439 log.Printf("Authorized on account %s", bot.Self.UserName)
440
441 _, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
442 if err != nil {
443 log.Fatal(err)
444 }
445
446 updates, _ := bot.ListenForWebhook("/" + bot.Token)
447 go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
448
449 for update := range updates {
450 log.Printf("%+v\n", update)
451 }
452}