all repos — telegram-bot-api @ 653939995c6ca92d40a9aa0cdad74378c598dd67

Golang bindings for the Telegram Bot API

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
 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.Log(err.Error())
 53		t.Fail()
 54	}
 55}
 56
 57func TestSendWithMessage(t *testing.T) {
 58	bot, _ := getBot(t)
 59
 60	msg := tgbotapi.NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
 61	msg.ParseMode = "markdown"
 62	_, err := bot.Send(msg)
 63
 64	if err != nil {
 65		t.Fail()
 66	}
 67}
 68
 69func TestSendWithMessageReply(t *testing.T) {
 70	bot, _ := getBot(t)
 71
 72	msg := tgbotapi.NewMessage(ChatID, "A test message from the test library in telegram-bot-api")
 73	msg.ReplyToMessageID = ReplyToMessageID
 74	_, err := bot.Send(msg)
 75
 76	if err != nil {
 77		t.Fail()
 78	}
 79}
 80
 81func TestSendWithMessageForward(t *testing.T) {
 82	bot, _ := getBot(t)
 83
 84	msg := tgbotapi.NewForward(ChatID, ChatID, ReplyToMessageID)
 85	_, err := bot.Send(msg)
 86
 87	if err != nil {
 88		t.Fail()
 89	}
 90}
 91
 92func TestSendWithNewPhoto(t *testing.T) {
 93	bot, _ := getBot(t)
 94
 95	msg := tgbotapi.NewPhotoUpload(ChatID, "tests/image.jpg")
 96	msg.Caption = "Test"
 97	_, err := bot.Send(msg)
 98
 99	if err != nil {
100		t.Fail()
101	}
102}
103
104func TestSendWithNewPhotoWithFileBytes(t *testing.T) {
105	bot, _ := getBot(t)
106
107	data, _ := ioutil.ReadFile("tests/image.jpg")
108	b := tgbotapi.FileBytes{Name: "image.jpg", Bytes: data}
109
110	msg := tgbotapi.NewPhotoUpload(ChatID, b)
111	msg.Caption = "Test"
112	_, err := bot.Send(msg)
113
114	if err != nil {
115		t.Fail()
116	}
117}
118
119func TestSendWithNewPhotoWithFileReader(t *testing.T) {
120	bot, _ := getBot(t)
121
122	f, _ := os.Open("tests/image.jpg")
123	reader := tgbotapi.FileReader{Name: "image.jpg", Reader: f, Size: -1}
124
125	msg := tgbotapi.NewPhotoUpload(ChatID, reader)
126	msg.Caption = "Test"
127	_, err := bot.Send(msg)
128
129	if err != nil {
130		t.Fail()
131	}
132}
133
134func TestSendWithNewPhotoReply(t *testing.T) {
135	bot, _ := getBot(t)
136
137	msg := tgbotapi.NewPhotoUpload(ChatID, "tests/image.jpg")
138	msg.ReplyToMessageID = ReplyToMessageID
139
140	_, err := bot.Send(msg)
141
142	if err != nil {
143		t.Fail()
144	}
145}
146
147func TestSendWithExistingPhoto(t *testing.T) {
148	bot, _ := getBot(t)
149
150	msg := tgbotapi.NewPhotoShare(ChatID, ExistingPhotoFileID)
151	msg.Caption = "Test"
152	_, err := bot.Send(msg)
153
154	if err != nil {
155		t.Fail()
156	}
157}
158
159func TestSendWithNewDocument(t *testing.T) {
160	bot, _ := getBot(t)
161
162	msg := tgbotapi.NewDocumentUpload(ChatID, "tests/image.jpg")
163	_, err := bot.Send(msg)
164
165	if err != nil {
166		t.Fail()
167	}
168}
169
170func TestSendWithExistingDocument(t *testing.T) {
171	bot, _ := getBot(t)
172
173	msg := tgbotapi.NewDocumentShare(ChatID, ExistingDocumentFileID)
174	_, err := bot.Send(msg)
175
176	if err != nil {
177		t.Fail()
178	}
179}
180
181func TestSendWithNewAudio(t *testing.T) {
182	bot, _ := getBot(t)
183
184	msg := tgbotapi.NewAudioUpload(ChatID, "tests/audio.mp3")
185	msg.Title = "TEST"
186	msg.Duration = 10
187	msg.Performer = "TEST"
188	_, err := bot.Send(msg)
189
190	if err != nil {
191		t.Fail()
192	}
193}
194
195func TestSendWithExistingAudio(t *testing.T) {
196	bot, _ := getBot(t)
197
198	msg := tgbotapi.NewAudioShare(ChatID, ExistingAudioFileID)
199	msg.Title = "TEST"
200	msg.Duration = 10
201	msg.Performer = "TEST"
202
203	_, err := bot.Send(msg)
204
205	if err != nil {
206		t.Fail()
207	}
208}
209
210func TestSendWithNewVoice(t *testing.T) {
211	bot, _ := getBot(t)
212
213	msg := tgbotapi.NewVoiceUpload(ChatID, "tests/voice.ogg")
214	msg.Duration = 10
215	_, err := bot.Send(msg)
216
217	if err != nil {
218		t.Fail()
219	}
220}
221
222func TestSendWithExistingVoice(t *testing.T) {
223	bot, _ := getBot(t)
224
225	msg := tgbotapi.NewVoiceShare(ChatID, ExistingVoiceFileID)
226	msg.Duration = 10
227	_, err := bot.Send(msg)
228
229	if err != nil {
230		t.Fail()
231	}
232}
233
234func TestSendWithLocation(t *testing.T) {
235	bot, _ := getBot(t)
236
237	_, err := bot.Send(tgbotapi.NewLocation(ChatID, 40, 40))
238
239	if err != nil {
240		t.Fail()
241	}
242}
243
244func TestSendWithNewVideo(t *testing.T) {
245	bot, _ := getBot(t)
246
247	msg := tgbotapi.NewVideoUpload(ChatID, "tests/video.mp4")
248	msg.Duration = 10
249	msg.Caption = "TEST"
250
251	_, err := bot.Send(msg)
252
253	if err != nil {
254		t.Fail()
255	}
256}
257
258func TestSendWithExistingVideo(t *testing.T) {
259	bot, _ := getBot(t)
260
261	msg := tgbotapi.NewVideoShare(ChatID, ExistingVideoFileID)
262	msg.Duration = 10
263	msg.Caption = "TEST"
264
265	_, err := bot.Send(msg)
266
267	if err != nil {
268		t.Fail()
269	}
270}
271
272func TestSendWithNewSticker(t *testing.T) {
273	bot, _ := getBot(t)
274
275	msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
276
277	resp, err := bot.Send(msg)
278
279	t.Log(resp)
280
281	if err != nil {
282		t.Fail()
283	}
284}
285
286func TestSendWithExistingSticker(t *testing.T) {
287	bot, _ := getBot(t)
288
289	msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
290
291	_, err := bot.Send(msg)
292
293	if err != nil {
294		t.Fail()
295	}
296}
297
298func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
299	bot, _ := getBot(t)
300
301	msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
302	msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
303	_, err := bot.Send(msg)
304
305	if err != nil {
306		t.Fail()
307	}
308}
309
310func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
311	bot, _ := getBot(t)
312
313	msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
314	msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
315
316	_, err := bot.Send(msg)
317
318	if err != nil {
319
320		t.Fail()
321	}
322}
323
324func TestGetFile(t *testing.T) {
325	bot, _ := getBot(t)
326
327	file := tgbotapi.FileConfig{ExistingPhotoFileID}
328
329	_, err := bot.GetFile(file)
330
331	if err != nil {
332		t.Fail()
333	}
334}
335
336func TestSendChatConfig(t *testing.T) {
337	bot, _ := getBot(t)
338
339	_, err := bot.Send(tgbotapi.NewChatAction(ChatID, tgbotapi.ChatTyping))
340
341	if err != nil {
342		t.Fail()
343	}
344}
345
346func TestGetUserProfilePhotos(t *testing.T) {
347	bot, _ := getBot(t)
348
349	_, err := bot.GetUserProfilePhotos(tgbotapi.NewUserProfilePhotos(ChatID))
350	if err != nil {
351		t.Fail()
352	}
353}
354
355func TestListenForWebhook(t *testing.T) {
356	bot, _ := getBot(t)
357
358	handler := bot.ListenForWebhook("/")
359
360	req, _ := http.NewRequest("GET", "", strings.NewReader("{}"))
361	w := httptest.NewRecorder()
362
363	handler.ServeHTTP(w, req)
364	if w.Code != http.StatusOK {
365		t.Errorf("Home page didn't return %v", http.StatusOK)
366	}
367}
368
369func TestSetWebhookWithCert(t *testing.T) {
370	bot, _ := getBot(t)
371
372	bot.RemoveWebhook()
373
374	wh := tgbotapi.NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
375	_, err := bot.SetWebhook(wh)
376	if err != nil {
377		t.Fail()
378	}
379
380	bot.RemoveWebhook()
381}
382
383func TestSetWebhookWithoutCert(t *testing.T) {
384	bot, _ := getBot(t)
385
386	bot.RemoveWebhook()
387
388	wh := tgbotapi.NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
389	_, err := bot.SetWebhook(wh)
390	if err != nil {
391		t.Fail()
392	}
393
394	bot.RemoveWebhook()
395}
396
397func TestUpdatesChan(t *testing.T) {
398	bot, _ := getBot(t)
399
400	var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
401	ucfg.Timeout = 60
402	err := bot.UpdatesChan(ucfg)
403
404	if err != nil {
405		t.Fail()
406	}
407}
408
409func ExampleNewBotAPI() {
410	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
411	if err != nil {
412		log.Panic(err)
413	}
414
415	bot.Debug = true
416
417	log.Printf("Authorized on account %s", bot.Self.UserName)
418
419	u := tgbotapi.NewUpdate(0)
420	u.Timeout = 60
421
422	err = bot.UpdatesChan(u)
423
424	for update := range bot.Updates {
425		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
426
427		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
428		msg.ReplyToMessageID = update.Message.MessageID
429
430		bot.Send(msg)
431	}
432}
433
434func ExampleNewWebhook() {
435	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
436	if err != nil {
437		log.Fatal(err)
438	}
439
440	bot.Debug = true
441
442	log.Printf("Authorized on account %s", bot.Self.UserName)
443
444	_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
445	if err != nil {
446		log.Fatal(err)
447	}
448
449	bot.ListenForWebhook("/" + bot.Token)
450	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
451
452	for update := range bot.Updates {
453		log.Printf("%+v\n", update)
454	}
455}