bot_test.go (view raw)
1package tgbotapi_test
2
3import (
4 "github.com/syfaro/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.Error() != tgbotapi.APIForbidden {
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 u.Limit = 10000
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}