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 _, err = bot.Send(msg)
61
62 if err != nil {
63 t.Fail()
64 }
65}
66
67func TestSendWithPhoto(t *testing.T) {
68 bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
69
70 if err != nil {
71 t.Fail()
72 }
73
74 msg := tgbotapi.NewPhotoUpload(76918703, "tests/image.jpg")
75 _, err = bot.Send(msg)
76
77 if err != nil {
78 t.Fail()
79 }
80}
81
82func ExampleNewBotAPI() {
83 bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
84 if err != nil {
85 log.Panic(err)
86 }
87
88 bot.Debug = true
89
90 log.Printf("Authorized on account %s", bot.Self.UserName)
91
92 u := tgbotapi.NewUpdate(0)
93 u.Timeout = 60
94
95 err = bot.UpdatesChan(u)
96
97 for update := range bot.Updates {
98 log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
99
100 msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
101 msg.ReplyToMessageID = update.Message.MessageID
102
103 bot.Send(msg)
104 }
105}