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