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