configs.go (view raw)
1package tgbotapi
2
3import (
4 "encoding/json"
5 "io"
6 "net/url"
7 "strconv"
8)
9
10// Telegram constants
11const (
12 // APIEndpoint is the endpoint for all API methods, with formatting for Sprintf
13 APIEndpoint = "https://api.telegram.org/bot%s/%s"
14 // FileEndpoint is the endpoint for downloading a file from Telegram
15 FileEndpoint = "https://api.telegram.org/file/bot%s/%s"
16)
17
18// Constant values for ChatActions
19const (
20 ChatTyping = "typing"
21 ChatUploadPhoto = "upload_photo"
22 ChatRecordVideo = "record_video"
23 ChatUploadVideo = "upload_video"
24 ChatRecordAudio = "record_audio"
25 ChatUploadAudio = "upload_audio"
26 ChatUploadDocument = "upload_document"
27 ChatFindLocation = "find_location"
28)
29
30// API errors
31const (
32 // APIForbidden happens when a token is bad
33 APIForbidden = "forbidden"
34)
35
36// Constant values for ParseMode in MessageConfig
37const (
38 ModeMarkdown = "Markdown"
39)
40
41// Base struct for all chat event(Message, Photo and so on)
42type Chattable struct {
43 ChatID int
44 ChannelUsername string
45}
46
47type Fileable struct {
48 FilePath string
49 File interface{}
50 FileID string
51}
52
53func (chattable *Chattable) Values() (url.Values, error) {
54 v := url.Values{}
55 if chattable.ChannelUsername != "" {
56 v.Add("chat_id", chattable.ChannelUsername)
57 } else {
58 v.Add("chat_id", strconv.Itoa(chattable.ChatID))
59 }
60 return v, nil
61}
62
63// MessageConfig contains information about a SendMessage request.
64type MessageConfig struct {
65 Chattable
66 Text string
67 ParseMode string
68 DisableWebPagePreview bool
69 ReplyToMessageID int
70 ReplyMarkup interface{}
71}
72
73func (config *MessageConfig) Values() (url.Values, error) {
74 v, _ := config.Chattable.Values()
75 v.Add("text", config.Text)
76 v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview))
77 if config.ParseMode != "" {
78 v.Add("parse_mode", config.ParseMode)
79 }
80 if config.ReplyToMessageID != 0 {
81 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
82 }
83 if config.ReplyMarkup != nil {
84 data, err := json.Marshal(config.ReplyMarkup)
85 if err != nil {
86 return v, err
87 }
88
89 v.Add("reply_markup", string(data))
90 }
91
92 return v, nil
93}
94
95// ForwardConfig contains information about a ForwardMessage request.
96type ForwardConfig struct {
97 Chattable
98 FromChatID int
99 FromChannelUsername string
100 MessageID int
101}
102
103func (config *ForwardConfig) Values() (url.Values, error) {
104 v, _ := config.Chattable.Values()
105
106 if config.FromChannelUsername != "" {
107 v.Add("chat_id", config.FromChannelUsername)
108 } else {
109 v.Add("chat_id", strconv.Itoa(config.FromChatID))
110 }
111 v.Add("message_id", strconv.Itoa(config.MessageID))
112
113 return v, nil
114}
115
116// PhotoConfig contains information about a SendPhoto request.
117type PhotoConfig struct {
118 Chattable
119 Fileable
120 Caption string
121 ReplyToMessageID int
122 ReplyMarkup interface{}
123 UseExistingPhoto bool
124}
125
126func (config *PhotoConfig) Values() (url.Values, error) {
127 v, _ := config.Chattable.Values()
128
129 v.Add("photo", config.FileID)
130 if config.Caption != "" {
131 v.Add("caption", config.Caption)
132 }
133 if config.ReplyToMessageID != 0 {
134 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
135 }
136 if config.ReplyMarkup != nil {
137 data, err := json.Marshal(config.ReplyMarkup)
138 if err != nil {
139 return v, err
140 }
141
142 v.Add("reply_markup", string(data))
143 }
144
145 return v, nil
146}
147
148// AudioConfig contains information about a SendAudio request.
149type AudioConfig struct {
150 Chattable
151 Fileable
152 Duration int
153 Performer string
154 Title string
155 ReplyToMessageID int
156 ReplyMarkup interface{}
157 UseExistingAudio bool
158}
159
160func (config *AudioConfig) Values() (url.Values, error) {
161 v, _ := config.Chattable.Values()
162
163 v.Add("audio", config.FileID)
164 if config.ReplyToMessageID != 0 {
165 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
166 }
167 if config.Duration != 0 {
168 v.Add("duration", strconv.Itoa(config.Duration))
169 }
170 if config.ReplyMarkup != nil {
171 data, err := json.Marshal(config.ReplyMarkup)
172 if err != nil {
173 return v, err
174 }
175
176 v.Add("reply_markup", string(data))
177 }
178 if config.Performer != "" {
179 v.Add("performer", config.Performer)
180 }
181 if config.Title != "" {
182 v.Add("title", config.Title)
183 }
184
185 return v, nil
186}
187
188// DocumentConfig contains information about a SendDocument request.
189type DocumentConfig struct {
190 Chattable
191 Fileable
192 ReplyToMessageID int
193 ReplyMarkup interface{}
194 UseExistingDocument bool
195}
196
197func (config *DocumentConfig) Values() (url.Values, error) {
198 v, _ := config.Chattable.Values()
199
200 v.Add("document", config.FileID)
201 if config.ReplyToMessageID != 0 {
202 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
203 }
204 if config.ReplyMarkup != nil {
205 data, err := json.Marshal(config.ReplyMarkup)
206 if err != nil {
207 return v, err
208 }
209
210 v.Add("reply_markup", string(data))
211 }
212
213 return v, nil
214}
215
216// StickerConfig contains information about a SendSticker request.
217type StickerConfig struct {
218 Chattable
219 Fileable
220 ReplyToMessageID int
221 ReplyMarkup interface{}
222 UseExistingSticker bool
223}
224
225func (config *StickerConfig) Values() (url.Values, error) {
226 v, _ := config.Chattable.Values()
227
228 v.Add("sticker", config.FileID)
229 if config.ReplyToMessageID != 0 {
230 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
231 }
232 if config.ReplyMarkup != nil {
233 data, err := json.Marshal(config.ReplyMarkup)
234 if err != nil {
235 return v, err
236 }
237
238 v.Add("reply_markup", string(data))
239 }
240
241 return v, nil
242}
243
244// VideoConfig contains information about a SendVideo request.
245type VideoConfig struct {
246 Chattable
247 Fileable
248 Duration int
249 Caption string
250 ReplyToMessageID int
251 ReplyMarkup interface{}
252 UseExistingVideo bool
253}
254
255func (config *VideoConfig) Values() (url.Values, error) {
256 v, _ := config.Chattable.Values()
257
258 v.Add("video", config.FileID)
259 if config.ReplyToMessageID != 0 {
260 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
261 }
262 if config.Duration != 0 {
263 v.Add("duration", strconv.Itoa(config.Duration))
264 }
265 if config.Caption != "" {
266 v.Add("caption", config.Caption)
267 }
268 if config.ReplyMarkup != nil {
269 data, err := json.Marshal(config.ReplyMarkup)
270 if err != nil {
271 return v, err
272 }
273
274 v.Add("reply_markup", string(data))
275 }
276
277 return v, nil
278}
279
280// VoiceConfig contains information about a SendVoice request.
281type VoiceConfig struct {
282 Chattable
283 Fileable
284 Duration int
285 ReplyToMessageID int
286 ReplyMarkup interface{}
287 UseExistingVoice bool
288}
289
290func (config *VoiceConfig) Values() (url.Values, error) {
291 v, _ := config.Chattable.Values()
292
293 v.Add("voice", config.FileID)
294 if config.ReplyToMessageID != 0 {
295 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
296 }
297 if config.Duration != 0 {
298 v.Add("duration", strconv.Itoa(config.Duration))
299 }
300 if config.ReplyMarkup != nil {
301 data, err := json.Marshal(config.ReplyMarkup)
302 if err != nil {
303 return v, err
304 }
305
306 v.Add("reply_markup", string(data))
307 }
308
309 return v, nil
310}
311
312// LocationConfig contains information about a SendLocation request.
313type LocationConfig struct {
314 Chattable
315 Latitude float64
316 Longitude float64
317 ReplyToMessageID int
318 ReplyMarkup interface{}
319}
320
321func (config *LocationConfig) Values() (url.Values, error) {
322 v, _ := config.Chattable.Values()
323
324 v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
325 v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
326
327 if config.ReplyToMessageID != 0 {
328 v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
329 }
330 if config.ReplyMarkup != nil {
331 data, err := json.Marshal(config.ReplyMarkup)
332 if err != nil {
333 return v, err
334 }
335
336 v.Add("reply_markup", string(data))
337 }
338
339 return v, nil
340}
341
342// ChatActionConfig contains information about a SendChatAction request.
343type ChatActionConfig struct {
344 Chattable
345 Action string
346}
347
348func (config *ChatActionConfig) Values() (url.Values, error) {
349 v, _ := config.Chattable.Values()
350 v.Add("action", config.Action)
351 return v, nil
352}
353
354// UserProfilePhotosConfig contains information about a GetUserProfilePhotos request.
355type UserProfilePhotosConfig struct {
356 UserID int
357 Offset int
358 Limit int
359}
360
361// FileConfig has information about a file hosted on Telegram
362type FileConfig struct {
363 FileID string
364}
365
366// UpdateConfig contains information about a GetUpdates request.
367type UpdateConfig struct {
368 Offset int
369 Limit int
370 Timeout int
371}
372
373// WebhookConfig contains information about a SetWebhook request.
374type WebhookConfig struct {
375 Clear bool
376 URL *url.URL
377 Certificate interface{}
378}
379
380// FileBytes contains information about a set of bytes to upload as a File.
381type FileBytes struct {
382 Name string
383 Bytes []byte
384}
385
386// FileReader contains information about a reader to upload as a File.
387// If Size is -1, it will read the entire Reader into memory to calculate a Size.
388type FileReader struct {
389 Name string
390 Reader io.Reader
391 Size int64
392}