all repos — telegram-bot-api @ c67df954072c3347a29182a1fbcf8b69b096a5f2

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	"log"
  6	"os"
  7	"testing"
  8)
  9
 10func TestMain(m *testing.M) {
 11	botToken := os.Getenv("TELEGRAM_API_TOKEN")
 12
 13	if botToken == "" {
 14		log.Panic("You must provide a TELEGRAM_API_TOKEN env variable to test!")
 15	}
 16
 17	os.Exit(m.Run())
 18}
 19
 20func TestNewBotAPI_notoken(t *testing.T) {
 21	_, err := tgbotapi.NewBotAPI("")
 22
 23	if err == nil {
 24		t.Fail()
 25	}
 26}
 27
 28func TestNewBotAPI_token(t *testing.T) {
 29	_, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
 30
 31	if err != nil {
 32		t.Fail()
 33	}
 34}
 35
 36func TestGetUpdates(t *testing.T) {
 37	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
 38
 39	if err != nil {
 40		t.Fail()
 41	}
 42
 43	u := tgbotapi.NewUpdate(0)
 44
 45	_, err = bot.GetUpdates(u)
 46
 47	if err != nil {
 48		t.Fail()
 49	}
 50}
 51
 52func TestSendWithMessage(t *testing.T) {
 53	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
 54
 55	if err != nil {
 56		t.Fail()
 57	}
 58
 59	msg := tgbotapi.NewMessage(76918703, "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, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
 70
 71	if err != nil {
 72		t.Fail()
 73	}
 74
 75	msg := tgbotapi.NewMessage(76918703, "A test message from the test library in telegram-bot-api")
 76	msg.ReplyToMessageID = 480
 77	_, err = bot.Send(msg)
 78
 79	if err != nil {
 80		t.Fail()
 81	}
 82}
 83
 84func TestSendWithMessageForward(t *testing.T) {
 85	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
 86
 87	if err != nil {
 88		t.Fail()
 89	}
 90
 91	msg := tgbotapi.NewForward(76918703, 76918703, 480)
 92	_, err = bot.Send(msg)
 93
 94	if err != nil {
 95		t.Fail()
 96	}
 97}
 98
 99func TestSendWithNewPhoto(t *testing.T) {
100	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
101
102	if err != nil {
103		t.Fail()
104	}
105
106	msg := tgbotapi.NewPhotoUpload(76918703, "tests/image.jpg")
107	msg.Caption = "Test"
108	_, err = bot.Send(msg)
109
110	if err != nil {
111		t.Fail()
112	}
113}
114
115func TestSendWithNewPhotoReply(t *testing.T) {
116	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
117
118	if err != nil {
119		t.Fail()
120	}
121
122	msg := tgbotapi.NewPhotoUpload(76918703, "tests/image.jpg")
123	msg.ReplyToMessageID = 480
124
125	_, err = bot.Send(msg)
126
127	if err != nil {
128		t.Fail()
129	}
130}
131
132func TestSendWithExistingPhoto(t *testing.T) {
133	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
134
135	if err != nil {
136		t.Fail()
137	}
138
139	msg := tgbotapi.NewPhotoShare(76918703, "AgADAgADxKcxG4cBswqt13DnHOgbmBxDhCoABC0h01_AL4SKe20BAAEC")
140	msg.Caption = "Test"
141	_, err = bot.Send(msg)
142
143	if err != nil {
144		t.Fail()
145	}
146}
147
148func TestSendWithNewDocument(t *testing.T) {
149	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
150
151	if err != nil {
152		t.Fail()
153	}
154
155	msg := tgbotapi.NewDocumentUpload(76918703, "tests/image.jpg")
156	_, err = bot.Send(msg)
157
158	if err != nil {
159		t.Fail()
160	}
161}
162
163func TestSendWithExistingDocument(t *testing.T) {
164	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
165
166	if err != nil {
167		t.Fail()
168	}
169
170	msg := tgbotapi.NewDocumentShare(76918703, "BQADAgADBwADhwGzCjWgiUU4T8VNAg")
171	_, err = bot.Send(msg)
172
173	if err != nil {
174		t.Fail()
175	}
176}
177
178func TestSendWithNewAudio(t *testing.T) {
179	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
180
181	if err != nil {
182		t.Fail()
183	}
184
185	msg := tgbotapi.NewAudioUpload(76918703, "tests/audio.mp3")
186	msg.Title = "TEST"
187	msg.Duration = 10
188	msg.Performer = "TEST"
189	_, err = bot.Send(msg)
190
191	if err != nil {
192		t.Fail()
193	}
194}
195
196func TestSendWithExistingAudio(t *testing.T) {
197	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
198
199	if err != nil {
200		t.Fail()
201	}
202
203	msg := tgbotapi.NewAudioShare(76918703, "BQADAgADMwADhwGzCkYFlCTpxiP6Ag")
204	msg.Title = "TEST"
205	msg.Duration = 10
206	msg.Performer = "TEST"
207
208	_, err = bot.Send(msg)
209
210	if err != nil {
211		t.Fail()
212	}
213}
214
215func TestSendWithNewVoice(t *testing.T) {
216	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
217
218	if err != nil {
219		t.Fail()
220	}
221
222	msg := tgbotapi.NewVoiceUpload(76918703, "tests/voice.ogg")
223	msg.Duration = 10
224	_, err = bot.Send(msg)
225
226	if err != nil {
227		t.Fail()
228	}
229}
230
231func TestSendWithExistingVoice(t *testing.T) {
232	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
233
234	if err != nil {
235		t.Fail()
236	}
237
238	msg := tgbotapi.NewVoiceShare(76918703, "AwADAgADIgADhwGzCigyMW_GUtWIAg")
239	msg.Duration = 10
240	_, err = bot.Send(msg)
241
242	if err != nil {
243		t.Fail()
244	}
245}
246
247func TestSendWithLocation(t *testing.T) {
248	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
249
250	if err != nil {
251		t.Fail()
252	}
253
254	_, err = bot.Send(tgbotapi.NewLocation(76918703, 40, 40))
255
256	if err != nil {
257		t.Fail()
258	}
259}
260
261func TestSendWithNewVideo(t *testing.T) {
262	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
263
264	if err != nil {
265		t.Fail()
266	}
267
268	msg := tgbotapi.NewVideoUpload(76918703, "tests/video.mp4")
269	msg.Duration = 10
270	msg.Caption = "TEST"
271
272	_, err = bot.Send(msg)
273
274	if err != nil {
275
276		t.Fail()
277	}
278}
279
280func TestSendWithExistingVideo(t *testing.T) {
281	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
282
283	if err != nil {
284		t.Fail()
285	}
286
287	msg := tgbotapi.NewVideoShare(76918703, "BAADAgADRgADhwGzCopBeKJ7rv9SAg")
288	msg.Duration = 10
289	msg.Caption = "TEST"
290
291	_, err = bot.Send(msg)
292
293	if err != nil {
294
295		t.Fail()
296	}
297}
298
299func TestSendWithNewSticker(t *testing.T) {
300	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
301
302	if err != nil {
303		t.Fail()
304	}
305
306	msg := tgbotapi.NewStickerUpload(76918703, "tests/image.jpg")
307
308	_, err = bot.Send(msg)
309
310	if err != nil {
311
312		t.Fail()
313	}
314}
315
316func TestSendWithExistingSticker(t *testing.T) {
317	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
318
319	if err != nil {
320		t.Fail()
321	}
322
323	msg := tgbotapi.NewStickerShare(76918703, "BQADAgADUwADhwGzCmwtOypNFlrRAg")
324
325	_, err = bot.Send(msg)
326
327	if err != nil {
328
329		t.Fail()
330	}
331}
332
333func TestSendWithNewStickerAndKeyboardHide(t *testing.T) {
334	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
335
336	if err != nil {
337		t.Fail()
338	}
339
340	msg := tgbotapi.NewStickerUpload(76918703, "tests/image.jpg")
341	msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
342	_, err = bot.Send(msg)
343
344	if err != nil {
345
346		t.Fail()
347	}
348}
349
350func TestSendWithExistingStickerAndKeyboardHide(t *testing.T) {
351	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
352
353	if err != nil {
354		t.Fail()
355	}
356
357	msg := tgbotapi.NewStickerShare(76918703, "BQADAgADUwADhwGzCmwtOypNFlrRAg")
358	msg.ReplyMarkup = tgbotapi.ReplyKeyboardHide{true, false}
359
360	_, err = bot.Send(msg)
361
362	if err != nil {
363
364		t.Fail()
365	}
366}
367
368func TestGetFile(t *testing.T) {
369	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
370
371	if err != nil {
372		t.Fail()
373	}
374
375	file := tgbotapi.FileConfig{"BQADAgADBwADhwGzCjWgiUU4T8VNAg"}
376
377	_, err = bot.GetFile(file)
378
379	if err != nil {
380		t.Fail()
381	}
382}
383
384func TestSendChatConfig(t *testing.T) {
385	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
386
387	if err != nil {
388		t.Fail()
389	}
390
391	err = bot.SendChatAction(tgbotapi.NewChatAction(76918703, tgbotapi.ChatTyping))
392
393	if err != nil {
394		t.Fail()
395	}
396}
397
398func TestGetUserProfilePhotos(t *testing.T) {
399	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
400
401	if err != nil {
402		t.Fail()
403	}
404
405	_, err = bot.GetUserProfilePhotos(tgbotapi.NewUserProfilePhotos(76918703))
406	if err != nil {
407		t.Fail()
408	}
409}
410
411func TestUpdatesChan(t *testing.T) {
412	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
413
414	if err != nil {
415		t.Fail()
416	}
417
418	var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
419	ucfg.Timeout = 60
420	err = bot.UpdatesChan(ucfg)
421
422	if err != nil {
423		t.Fail()
424	}
425}
426
427func ExampleNewBotAPI() {
428	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
429	if err != nil {
430		log.Panic(err)
431	}
432
433	bot.Debug = true
434
435	log.Printf("Authorized on account %s", bot.Self.UserName)
436
437	u := tgbotapi.NewUpdate(0)
438	u.Timeout = 60
439
440	err = bot.UpdatesChan(u)
441
442	for update := range bot.Updates {
443		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
444
445		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
446		msg.ReplyToMessageID = update.Message.MessageID
447
448		bot.Send(msg)
449	}
450}