all repos — telegram-bot-api @ d8ba9c84c817ae4f7eccb12c98fc764cf8c966f8

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		log.Println(err.Error())
25		t.Fail()
26	}
27}
28
29func TestNewBotAPI_token(t *testing.T) {
30	_, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
31
32	if err != nil {
33		t.Fail()
34	}
35}
36
37func TestGetUpdates(t *testing.T) {
38	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
39
40	if err != nil {
41		t.Fail()
42	}
43
44	u := tgbotapi.NewUpdate(0)
45
46	_, err = bot.GetUpdates(u)
47
48	if err != nil {
49		t.Fail()
50	}
51}
52
53func TestSendMessage(t *testing.T) {
54	bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
55
56	if err != nil {
57		t.Fail()
58	}
59
60	msg := tgbotapi.NewMessage(36529758, "A test message from the test library in telegram-bot-api")
61	bot.SendMessage(msg)
62}
63
64func ExampleNewBotAPI() {
65	bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
66	if err != nil {
67		log.Panic(err)
68	}
69
70	bot.Debug = true
71
72	log.Printf("Authorized on account %s", bot.Self.UserName)
73
74	u := tgbotapi.NewUpdate(0)
75	u.Timeout = 60
76
77	err = bot.UpdatesChan(u)
78
79	for update := range bot.Updates {
80		log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
81
82		msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
83		msg.ReplyToMessageID = update.Message.MessageID
84
85		bot.SendMessage(msg)
86	}
87}