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 TestSendWithNewPhoto(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
82
83
84func TestSendWithExistingPhoto(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.NewPhotoShare(76918703, "AgADAgADxKcxG4cBswqt13DnHOgbmBxDhCoABC0h01_AL4SKe20BAAEC")
92 _, err = bot.Send(msg)
93
94 if err != nil {
95 t.Fail()
96 }
97}
98
99func TestSendWithNewDocument(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.NewDocumentUpload(76918703, "tests/image.jpg")
107 _, err = bot.Send(msg)
108
109 if err != nil {
110 t.Fail()
111 }
112}
113
114func TestSendWithExistingDocument(t *testing.T) {
115 bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
116
117 if err != nil {
118 t.Fail()
119 }
120
121 msg := tgbotapi.NewDocumentShare(76918703, "BQADAgADBwADhwGzCjWgiUU4T8VNAg")
122 _, err = bot.Send(msg)
123
124 if err != nil {
125 t.Fail()
126 }
127}
128
129func TestGetFile(t *testing.T) {
130 bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
131
132 if err != nil {
133 t.Fail()
134 }
135
136 file := tgbotapi.FileConfig{"BQADAgADBwADhwGzCjWgiUU4T8VNAg"}
137
138 _, err = bot.GetFile(file)
139
140 if err != nil {
141 t.Fail()
142 }
143}
144
145func TestSendChatConfig(t *testing.T) {
146 bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
147
148 if err != nil {
149 t.Fail()
150 }
151
152 err = bot.SendChatAction(tgbotapi.NewChatAction(76918703, tgbotapi.ChatTyping))
153
154 if err != nil {
155 t.Fail()
156 }
157}
158
159func TestGetUserProfilePhotos(t *testing.T) {
160 bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
161
162 if err != nil {
163 t.Fail()
164 }
165
166 _, err = bot.GetUserProfilePhotos(tgbotapi.NewUserProfilePhotos(76918703))
167 if err != nil {
168 t.Fail()
169 }
170}
171
172func TestUpdatesChan(t *testing.T) {
173 bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_API_TOKEN"))
174
175 if err != nil {
176 t.Fail()
177 }
178
179 var ucfg tgbotapi.UpdateConfig = tgbotapi.NewUpdate(0)
180 ucfg.Timeout = 60
181 err = bot.UpdatesChan(ucfg)
182
183 if err != nil {
184 t.Fail()
185 }
186}
187
188func ExampleNewBotAPI() {
189 bot, err := tgbotapi.NewBotAPI("MyAwesomeBotToken")
190 if err != nil {
191 log.Panic(err)
192 }
193
194 bot.Debug = true
195
196 log.Printf("Authorized on account %s", bot.Self.UserName)
197
198 u := tgbotapi.NewUpdate(0)
199 u.Timeout = 60
200
201 err = bot.UpdatesChan(u)
202
203 for update := range bot.Updates {
204 log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
205
206 msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
207 msg.ReplyToMessageID = update.Message.MessageID
208
209 bot.Send(msg)
210 }
211}