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 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 TestSetWebhookWithCert(t *testing.T) {
353 bot, _ := getBot(t)
354
355 bot.RemoveWebhook()
356
357 wh := tgbotapi.NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
358 _, err := bot.SetWebhook(wh)
359 if err != nil {
360 t.Fail()
361 }
362
363 bot.RemoveWebhook()
364}
365
366func TestSetWebhookWithoutCert(t *testing.T) {
367 bot, _ := getBot(t)
368
369 bot.RemoveWebhook()
370
371 wh := tgbotapi.NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
372 _, err := bot.SetWebhook(wh)
373 if err != nil {
374 t.Fail()
375 }
376
377 bot.RemoveWebhook()
378}
379
380func TestUpdatesChan(t *testing.T) {
381 bot, _ := getBot(t)
382
383 var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
384 ucfg.Timeout = 60
385 _, err := bot.GetUpdatesChan(ucfg)
386
387 if err != nil {
388 t.Fail()
389 }
390}
391
392func ExampleNewBotAPI() {
393 bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
394 if err != nil {
395 log.Panic(err)
396 }
397
398 bot.Debug = true
399
400 log.Printf("Authorized on account %s", bot.Self.UserName)
401
402 u := tgbotapi.NewUpdate(0)
403 u.Timeout = 60
404
405 updates, err := bot.GetUpdatesChan(u)
406
407 for update := range updates {
408 log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
409
410 msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
411 msg.ReplyToMessageID = update.Message.MessageID
412
413 bot.Send(msg)
414 }
415}
416
417func ExampleNewWebhook() {
418 bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
419 if err != nil {
420 log.Fatal(err)
421 }
422
423 bot.Debug = true
424
425 log.Printf("Authorized on account %s", bot.Self.UserName)
426
427 _, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
428 if err != nil {
429 log.Fatal(err)
430 }
431
432 updates := bot.ListenForWebhook("/" + bot.Token)
433 go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
434
435 for update := range updates {
436 log.Printf("%+v\n", update)
437 }
438}