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
45 _, err = bot.GetUpdates(u)
46
47 if err != nil {
48 t.Fail()
49 }
50}
51
52func TestSendMessage(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(36529758, "A test message from the test library in telegram-bot-api")
60 bot.SendMessage(msg)
61}
62
63func ExampleNewBotAPI() {
64 bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
65 if err != nil {
66 log.Panic(err)
67 }
68
69 bot.Debug = true
70
71 log.Printf("Authorized on account %s", bot.Self.UserName)
72
73 u := tgbotapi.NewUpdate(0)
74 u.Timeout = 60
75
76 err = bot.UpdatesChan(u)
77
78 for update := range bot.Updates {
79 log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
80
81 msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
82 msg.ReplyToMessageID = update.Message.MessageID
83
84 bot.SendMessage(msg)
85 }
86}