all repos — telegram-bot-api @ d40f7f97034c8473b3fabe6e3034512fa1d655c9

Golang bindings for the Telegram Bot API

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	msg.MimeType = "audio/mpeg"
188	msg.FileSize = 688
189	_, err := bot.Send(msg)
190
191	if err != nil {
192		t.Fail()
193	}
194}
195
196func TestSendWithExistingAudio(t *testing.T) {
197	bot, _ := getBot(t)
198
199	msg := tgbotapi.NewAudioShare(ChatID, ExistingAudioFileID)
200	msg.Title = "TEST"
201	msg.Duration = 10
202	msg.Performer = "TEST"
203
204	_, err := bot.Send(msg)
205
206	if err != nil {
207		t.Fail()
208	}
209}
210
211func TestSendWithNewVoice(t *testing.T) {
212	bot, _ := getBot(t)
213
214	msg := tgbotapi.NewVoiceUpload(ChatID, "tests/voice.ogg")
215	msg.Duration = 10
216	_, err := bot.Send(msg)
217
218	if err != nil {
219		t.Fail()
220	}
221}
222
223func TestSendWithExistingVoice(t *testing.T) {
224	bot, _ := getBot(t)
225
226	msg := tgbotapi.NewVoiceShare(ChatID, ExistingVoiceFileID)
227	msg.Duration = 10
228	_, err := bot.Send(msg)
229
230	if err != nil {
231		t.Fail()
232	}
233}
234
235func TestSendWithLocation(t *testing.T) {
236	bot, _ := getBot(t)
237
238	_, err := bot.Send(tgbotapi.NewLocation(ChatID, 40, 40))
239
240	if err != nil {
241		t.Fail()
242	}
243}
244
245func TestSendWithNewVideo(t *testing.T) {
246	bot, _ := getBot(t)
247
248	msg := tgbotapi.NewVideoUpload(ChatID, "tests/video.mp4")
249	msg.Duration = 10
250	msg.Caption = "TEST"
251
252	_, err := bot.Send(msg)
253
254	if err != nil {
255		t.Fail()
256	}
257}
258
259func TestSendWithExistingVideo(t *testing.T) {
260	bot, _ := getBot(t)
261
262	msg := tgbotapi.NewVideoShare(ChatID, ExistingVideoFileID)
263	msg.Duration = 10
264	msg.Caption = "TEST"
265
266	_, err := bot.Send(msg)
267
268	if err != nil {
269		t.Fail()
270	}
271}
272
273func TestSendWithNewSticker(t *testing.T) {
274	bot, _ := getBot(t)
275
276	msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
277
278	_, err := bot.Send(msg)
279
280	if err != nil {
281		t.Fail()
282	}
283}
284
285func TestSendWithExistingSticker(t *testing.T) {
286	bot, _ := getBot(t)
287
288	msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
289
290	_, err := bot.Send(msg)
291
292	if err != nil {
293		t.Fail()
294	}
295}
296
297func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
298	bot, _ := getBot(t)
299
300	msg := tgbotapi.NewStickerUpload(ChatID, "tests/image.jpg")
301	msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
302	_, err := bot.Send(msg)
303
304	if err != nil {
305		t.Fail()
306	}
307}
308
309func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
310	bot, _ := getBot(t)
311
312	msg := tgbotapi.NewStickerShare(ChatID, ExistingStickerFileID)
313	msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
314
315	_, err := bot.Send(msg)
316
317	if err != nil {
318
319		t.Fail()
320	}
321}
322
323func TestGetFile(t *testing.T) {
324	bot, _ := getBot(t)
325
326	file := tgbotapi.FileConfig{ExistingPhotoFileID}
327
328	_, err := bot.GetFile(file)
329
330	if err != nil {
331		t.Fail()
332	}
333}
334
335func TestSendChatConfig(t *testing.T) {
336	bot, _ := getBot(t)
337
338	_, err := bot.Send(tgbotapi.NewChatAction(ChatID, tgbotapi.ChatTyping))
339
340	if err != nil {
341		t.Fail()
342	}
343}
344
345func TestGetUserProfilePhotos(t *testing.T) {
346	bot, _ := getBot(t)
347
348	_, err := bot.GetUserProfilePhotos(tgbotapi.NewUserProfilePhotos(ChatID))
349	if err != nil {
350		t.Fail()
351	}
352}
353
354func TestListenForWebhook(t *testing.T) {
355	bot, _ := getBot(t)
356
357	_, handler := bot.ListenForWebhook("/")
358
359	req, _ := http.NewRequest("GET", "", strings.NewReader("{}"))
360	w := httptest.NewRecorder()
361
362	handler.ServeHTTP(w, req)
363	if w.Code != http.StatusOK {
364		t.Errorf("Home page didn't return %v", http.StatusOK)
365	}
366}
367
368func TestSetWebhookWithCert(t *testing.T) {
369	bot, _ := getBot(t)
370
371	bot.RemoveWebhook()
372
373	wh := tgbotapi.NewWebhookWithCert("https://example.com/tgbotapi-test/"+bot.Token, "tests/cert.pem")
374	_, err := bot.SetWebhook(wh)
375	if err != nil {
376		t.Fail()
377	}
378
379	bot.RemoveWebhook()
380}
381
382func TestSetWebhookWithoutCert(t *testing.T) {
383	bot, _ := getBot(t)
384
385	bot.RemoveWebhook()
386
387	wh := tgbotapi.NewWebhook("https://example.com/tgbotapi-test/" + bot.Token)
388	_, err := bot.SetWebhook(wh)
389	if err != nil {
390		t.Fail()
391	}
392
393	bot.RemoveWebhook()
394}
395
396func TestUpdatesChan(t *testing.T) {
397	bot, _ := getBot(t)
398
399	var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
400	ucfg.Timeout = 60
401	_, err := bot.GetUpdatesChan(ucfg)
402
403	if err != nil {
404		t.Fail()
405	}
406}
407
408func ExampleNewBotAPI() {
409	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
410	if err != nil {
411		log.Panic(err)
412	}
413
414	bot.Debug = true
415
416	log.Printf("Authorized on account %s", bot.Self.UserName)
417
418	u := tgbotapi.NewUpdate(0)
419	u.Timeout = 60
420
421	updates, err := bot.GetUpdatesChan(u)
422
423	for update := range updates {
424		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
425
426		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
427		msg.ReplyToMessageID = update.Message.MessageID
428
429		bot.Send(msg)
430	}
431}
432
433func ExampleNewWebhook() {
434	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
435	if err != nil {
436		log.Fatal(err)
437	}
438
439	bot.Debug = true
440
441	log.Printf("Authorized on account %s", bot.Self.UserName)
442
443	_, err = bot.SetWebhook(tgbotapi.NewWebhookWithCert("https://www.google.com:8443/"+bot.Token, "cert.pem"))
444	if err != nil {
445		log.Fatal(err)
446	}
447
448	updates, _ := bot.ListenForWebhook("/" + bot.Token)
449	go http.ListenAndServeTLS("0.0.0.0:8443", "cert.pem", "key.pem", nil)
450
451	for update := range updates {
452		log.Printf("%+v\n", update)
453	}
454}