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,
13 // with formatting for Sprintf.
14 APIEndpoint = "https://api.telegram.org/bot%s/%s"
15 // FileEndpoint is the endpoint for downloading a file from Telegram.
16 FileEndpoint = "https://api.telegram.org/file/bot%s/%s"
17)
18
19// Constant values for ChatActions
20const (
21 ChatTyping = "typing"
22 ChatUploadPhoto = "upload_photo"
23 ChatRecordVideo = "record_video"
24 ChatUploadVideo = "upload_video"
25 ChatRecordAudio = "record_audio"
26 ChatUploadAudio = "upload_audio"
27 ChatUploadDocument = "upload_document"
28 ChatFindLocation = "find_location"
29)
30
31// API errors
32const (
33 // APIForbidden happens when a token is bad
34 APIForbidden = "forbidden"
35)
36
37// Constant values for ParseMode in MessageConfig
38const (
39 ModeMarkdown = "Markdown"
40)
41
42// Library errors
43const (
44 // ErrBadFileType happens when you pass an unknown type
45 ErrBadFileType = "bad file type"
46)
47
48// Chattable is any config type that can be sent.
49type Chattable interface {
50 values() (url.Values, error)
51 method() string
52}
53
54// Fileable is any config type that can be sent that includes a file.
55type Fileable interface {
56 Chattable
57 params() (map[string]string, error)
58 name() string
59 getFile() interface{}
60 useExistingFile() bool
61}
62
63// BaseChat is base type for all chat config types.
64type BaseChat struct {
65 ChatID int // required
66 ChannelUsername string
67 ReplyToMessageID int
68 ReplyMarkup interface{}
69}
70
71// values returns url.Values representation of BaseChat
72func (chat *BaseChat) values() (url.Values, error) {
73 v := url.Values{}
74 if chat.ChannelUsername != "" {
75 v.Add("chat_id", chat.ChannelUsername)
76 } else {
77 v.Add("chat_id", strconv.Itoa(chat.ChatID))
78 }
79
80 if chat.ReplyToMessageID != 0 {
81 v.Add("reply_to_message_id", strconv.Itoa(chat.ReplyToMessageID))
82 }
83
84 if chat.ReplyMarkup != nil {
85 data, err := json.Marshal(chat.ReplyMarkup)
86 if err != nil {
87 return v, err
88 }
89
90 v.Add("reply_markup", string(data))
91 }
92
93 return v, nil
94}
95
96// BaseFile is a base type for all file config types.
97type BaseFile struct {
98 BaseChat
99 File interface{}
100 FileID string
101 UseExisting bool
102 MimeType string
103 FileSize int
104}
105
106// params returns a map[string]string representation of BaseFile.
107func (file BaseFile) params() (map[string]string, error) {
108 params := make(map[string]string)
109
110 if file.ChannelUsername != "" {
111 params["chat_id"] = file.ChannelUsername
112 } else {
113 params["chat_id"] = strconv.Itoa(file.ChatID)
114 }
115
116 if file.ReplyToMessageID != 0 {
117 params["reply_to_message_id"] = strconv.Itoa(file.ReplyToMessageID)
118 }
119
120 if file.ReplyMarkup != nil {
121 data, err := json.Marshal(file.ReplyMarkup)
122 if err != nil {
123 return params, err
124 }
125
126 params["reply_markup"] = string(data)
127 }
128
129 if file.MimeType != "" {
130 params["mime_type"] = file.MimeType
131 }
132
133 if file.FileSize > 0 {
134 params["file_size"] = strconv.Itoa(file.FileSize)
135 }
136
137 return params, nil
138}
139
140// getFile returns the file.
141func (file BaseFile) getFile() interface{} {
142 return file.File
143}
144
145// useExistingFile returns if the BaseFile has already been uploaded.
146func (file BaseFile) useExistingFile() bool {
147 return file.UseExisting
148}
149
150// MessageConfig contains information about a SendMessage request.
151type MessageConfig struct {
152 BaseChat
153 Text string
154 ParseMode string
155 DisableWebPagePreview bool
156}
157
158// values returns a url.Values representation of MessageConfig.
159func (config MessageConfig) values() (url.Values, error) {
160 v, _ := config.BaseChat.values()
161 v.Add("text", config.Text)
162 v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview))
163 if config.ParseMode != "" {
164 v.Add("parse_mode", config.ParseMode)
165 }
166
167 return v, nil
168}
169
170// method returns Telegram API method name for sending Message.
171func (config MessageConfig) method() string {
172 return "sendMessage"
173}
174
175// ForwardConfig contains information about a ForwardMessage request.
176type ForwardConfig struct {
177 BaseChat
178 FromChatID int // required
179 FromChannelUsername string
180 MessageID int // required
181}
182
183// values returns a url.Values representation of ForwardConfig.
184func (config ForwardConfig) values() (url.Values, error) {
185 v, _ := config.BaseChat.values()
186 v.Add("from_chat_id", strconv.Itoa(config.FromChatID))
187 v.Add("message_id", strconv.Itoa(config.MessageID))
188 return v, nil
189}
190
191// method returns Telegram API method name for sending Forward.
192func (config ForwardConfig) method() string {
193 return "forwardMessage"
194}
195
196// PhotoConfig contains information about a SendPhoto request.
197type PhotoConfig struct {
198 BaseFile
199 Caption string
200}
201
202// Params returns a map[string]string representation of PhotoConfig.
203func (config PhotoConfig) params() (map[string]string, error) {
204 params, _ := config.BaseFile.params()
205
206 if config.Caption != "" {
207 params["caption"] = config.Caption
208 }
209
210 return params, nil
211}
212
213// Values returns a url.Values representation of PhotoConfig.
214func (config PhotoConfig) values() (url.Values, error) {
215 v, _ := config.BaseChat.values()
216
217 v.Add(config.name(), config.FileID)
218 if config.Caption != "" {
219 v.Add("caption", config.Caption)
220 }
221 return v, nil
222}
223
224// name returns the field name for the Photo.
225func (config PhotoConfig) name() string {
226 return "photo"
227}
228
229// method returns Telegram API method name for sending Photo.
230func (config PhotoConfig) method() string {
231 return "sendPhoto"
232}
233
234// AudioConfig contains information about a SendAudio request.
235type AudioConfig struct {
236 BaseFile
237 Duration int
238 Performer string
239 Title string
240}
241
242// values returns a url.Values representation of AudioConfig.
243func (config AudioConfig) values() (url.Values, error) {
244 v, _ := config.BaseChat.values()
245
246 v.Add(config.name(), config.FileID)
247 if config.Duration != 0 {
248 v.Add("duration", strconv.Itoa(config.Duration))
249 }
250
251 if config.Performer != "" {
252 v.Add("performer", config.Performer)
253 }
254 if config.Title != "" {
255 v.Add("title", config.Title)
256 }
257
258 return v, nil
259}
260
261// params returns a map[string]string representation of AudioConfig.
262func (config AudioConfig) params() (map[string]string, error) {
263 params, _ := config.BaseFile.params()
264
265 if config.Duration != 0 {
266 params["duration"] = strconv.Itoa(config.Duration)
267 }
268
269 if config.Performer != "" {
270 params["performer"] = config.Performer
271 }
272 if config.Title != "" {
273 params["title"] = config.Title
274 }
275
276 return params, nil
277}
278
279// name returns the field name for the Audio.
280func (config AudioConfig) name() string {
281 return "audio"
282}
283
284// method returns Telegram API method name for sending Audio.
285func (config AudioConfig) method() string {
286 return "sendAudio"
287}
288
289// DocumentConfig contains information about a SendDocument request.
290type DocumentConfig struct {
291 BaseFile
292}
293
294// values returns a url.Values representation of DocumentConfig.
295func (config DocumentConfig) values() (url.Values, error) {
296 v, _ := config.BaseChat.values()
297
298 v.Add(config.name(), config.FileID)
299
300 return v, nil
301}
302
303// params returns a map[string]string representation of DocumentConfig.
304func (config DocumentConfig) params() (map[string]string, error) {
305 params, _ := config.BaseFile.params()
306
307 return params, nil
308}
309
310// name returns the field name for the Document.
311func (config DocumentConfig) name() string {
312 return "document"
313}
314
315// method returns Telegram API method name for sending Document.
316func (config DocumentConfig) method() string {
317 return "sendDocument"
318}
319
320// StickerConfig contains information about a SendSticker request.
321type StickerConfig struct {
322 BaseFile
323}
324
325// values returns a url.Values representation of StickerConfig.
326func (config StickerConfig) values() (url.Values, error) {
327 v, _ := config.BaseChat.values()
328
329 v.Add(config.name(), config.FileID)
330
331 return v, nil
332}
333
334// params returns a map[string]string representation of StickerConfig.
335func (config StickerConfig) params() (map[string]string, error) {
336 params, _ := config.BaseFile.params()
337
338 return params, nil
339}
340
341// name returns the field name for the Sticker.
342func (config StickerConfig) name() string {
343 return "sticker"
344}
345
346// method returns Telegram API method name for sending Sticker.
347func (config StickerConfig) method() string {
348 return "sendSticker"
349}
350
351// VideoConfig contains information about a SendVideo request.
352type VideoConfig struct {
353 BaseFile
354 Duration int
355 Caption string
356}
357
358// values returns a url.Values representation of VideoConfig.
359func (config VideoConfig) values() (url.Values, error) {
360 v, _ := config.BaseChat.values()
361
362 v.Add(config.name(), config.FileID)
363 if config.Duration != 0 {
364 v.Add("duration", strconv.Itoa(config.Duration))
365 }
366 if config.Caption != "" {
367 v.Add("caption", config.Caption)
368 }
369
370 return v, nil
371}
372
373// params returns a map[string]string representation of VideoConfig.
374func (config VideoConfig) params() (map[string]string, error) {
375 params, _ := config.BaseFile.params()
376
377 return params, nil
378}
379
380// name returns the field name for the Video.
381func (config VideoConfig) name() string {
382 return "video"
383}
384
385// method returns Telegram API method name for sending Video.
386func (config VideoConfig) method() string {
387 return "sendVideo"
388}
389
390// VoiceConfig contains information about a SendVoice request.
391type VoiceConfig struct {
392 BaseFile
393 Duration int
394}
395
396// values returns a url.Values representation of VoiceConfig.
397func (config VoiceConfig) values() (url.Values, error) {
398 v, _ := config.BaseChat.values()
399
400 v.Add(config.name(), config.FileID)
401 if config.Duration != 0 {
402 v.Add("duration", strconv.Itoa(config.Duration))
403 }
404
405 return v, nil
406}
407
408// params returns a map[string]string representation of VoiceConfig.
409func (config VoiceConfig) params() (map[string]string, error) {
410 params, _ := config.BaseFile.params()
411
412 if config.Duration != 0 {
413 params["duration"] = strconv.Itoa(config.Duration)
414 }
415
416 return params, nil
417}
418
419// name returns the field name for the Voice.
420func (config VoiceConfig) name() string {
421 return "voice"
422}
423
424// method returns Telegram API method name for sending Voice.
425func (config VoiceConfig) method() string {
426 return "sendVoice"
427}
428
429// LocationConfig contains information about a SendLocation request.
430type LocationConfig struct {
431 BaseChat
432 Latitude float64 // required
433 Longitude float64 // required
434}
435
436// values returns a url.Values representation of LocationConfig.
437func (config LocationConfig) values() (url.Values, error) {
438 v, _ := config.BaseChat.values()
439
440 v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
441 v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
442
443 return v, nil
444}
445
446// method returns Telegram API method name for sending Location.
447func (config LocationConfig) method() string {
448 return "sendLocation"
449}
450
451// ChatActionConfig contains information about a SendChatAction request.
452type ChatActionConfig struct {
453 BaseChat
454 Action string // required
455}
456
457// values returns a url.Values representation of ChatActionConfig.
458func (config ChatActionConfig) values() (url.Values, error) {
459 v, _ := config.BaseChat.values()
460 v.Add("action", config.Action)
461 return v, nil
462}
463
464// method returns Telegram API method name for sending ChatAction.
465func (config ChatActionConfig) method() string {
466 return "sendChatAction"
467}
468
469// UserProfilePhotosConfig contains information about a
470// GetUserProfilePhotos request.
471type UserProfilePhotosConfig struct {
472 UserID int
473 Offset int
474 Limit int
475}
476
477// FileConfig has information about a file hosted on Telegram.
478type FileConfig struct {
479 FileID string
480}
481
482// UpdateConfig contains information about a GetUpdates request.
483type UpdateConfig struct {
484 Offset int
485 Limit int
486 Timeout int
487}
488
489// WebhookConfig contains information about a SetWebhook request.
490type WebhookConfig struct {
491 URL *url.URL
492 Certificate interface{}
493}
494
495// FileBytes contains information about a set of bytes to upload
496// as a File.
497type FileBytes struct {
498 Name string
499 Bytes []byte
500}
501
502// FileReader contains information about a reader to upload as a File.
503// If Size is -1, it will read the entire Reader into memory to
504// calculate a Size.
505type FileReader struct {
506 Name string
507 Reader io.Reader
508 Size int64
509}
510
511// InlineConfig contains information on making an InlineQuery response.
512type InlineConfig struct {
513 InlineQueryID string `json:"inline_query_id"`
514 Results []InlineQueryResult `json:"results"`
515 CacheTime int `json:"cache_time"`
516 IsPersonal bool `json:"is_personal"`
517 NextOffset string `json:"next_offset"`
518}