all repos — telegram-bot-api @ a192540b8c20d382fe2c29616650ef449dd5a6d6

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 TestGetFile(t *testing.T) {
262	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
263
264	if err != nil {
265		t.Fail()
266	}
267
268	file := tgbotapi.FileConfig{"BQADAgADBwADhwGzCjWgiUU4T8VNAg"}
269
270	_, err = bot.GetFile(file)
271
272	if err != nil {
273		t.Fail()
274	}
275}
276
277func TestSendChatConfig(t *testing.T) {
278	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
279
280	if err != nil {
281		t.Fail()
282	}
283
284	err = bot.SendChatAction(tgbotapi.NewChatAction(76918703, tgbotapi.ChatTyping))
285
286	if err != nil {
287		t.Fail()
288	}
289}
290
291func TestGetUserProfilePhotos(t *testing.T) {
292	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
293
294	if err != nil {
295		t.Fail()
296	}
297
298	_, err = bot.GetUserProfilePhotos(tgbotapi.NewUserProfilePhotos(76918703))
299	if err != nil {
300		t.Fail()
301	}
302}
303
304func TestUpdatesChan(t *testing.T) {
305	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
306
307	if err != nil {
308		t.Fail()
309	}
310
311	var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
312	ucfg.Timeout = 60
313	err = bot.UpdatesChan(ucfg)
314
315	if err != nil {
316		t.Fail()
317	}
318}
319
320func ExampleNewBotAPI() {
321	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
322	if err != nil {
323		log.Panic(err)
324	}
325
326	bot.Debug = true
327
328	log.Printf("Authorized on account %s", bot.Self.UserName)
329
330	u := tgbotapi.NewUpdate(0)
331	u.Timeout = 60
332
333	err = bot.UpdatesChan(u)
334
335	for update := range bot.Updates {
336		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
337
338		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
339		msg.ReplyToMessageID = update.Message.MessageID
340
341		bot.Send(msg)
342	}
343}