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//Chattable represents any event in chat(MessageConfig, PhotoConfig, ChatActionConfig and others)
42type Chattable interface {
43 Values() (url.Values, error)
44 Method() string
45}
46
47//Fileable represents any file event(PhotoConfig, DocumentConfig, AudioConfig, VoiceConfig, VideoConfig, StickerConfig)
48type Fileable interface {
49 Chattable
50 Params() (map[string]string, error)
51 Name() string
52 GetFile() interface{}
53 UseExistingFile() bool
54}
55
56// BaseChat is base struct for all chat event(Message, Photo and so on)
57type BaseChat struct {
58 ChatID int
59 ChannelUsername string
60 ReplyToMessageID int
61 ReplyMarkup interface{}
62}
63
64// Values returns url.Values representation of BaseChat
65func (chat *BaseChat) Values() (url.Values, error) {
66 v := url.Values{}
67 if chat.ChannelUsername != "" {
68 v.Add("chat_id", chat.ChannelUsername)
69 } else {
70 v.Add("chat_id", strconv.Itoa(chat.ChatID))
71 }
72
73 if chat.ReplyToMessageID != 0 {
74 v.Add("reply_to_message_id", strconv.Itoa(chat.ReplyToMessageID))
75 }
76
77 if chat.ReplyMarkup != nil {
78 data, err := json.Marshal(chat.ReplyMarkup)
79 if err != nil {
80 return v, err
81 }
82
83 v.Add("reply_markup", string(data))
84 }
85
86 return v, nil
87}
88
89// BaseFile is base struct for all file events(PhotoConfig, DocumentConfig, AudioConfig, VoiceConfig, VideoConfig, StickerConfig)
90type BaseFile struct {
91 BaseChat
92 FilePath string
93 File interface{}
94 FileID string
95 UseExisting bool
96 MimeType string
97 FileSize int
98}
99
100// Params returns 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 len(file.MimeType) > 0 {
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 abstract representation of File inside BaseFile
135func (file BaseFile) GetFile() interface{} {
136 var result interface{}
137 if file.FilePath == "" {
138 result = file.File
139 } else {
140 result = file.FilePath
141 }
142
143 return result
144}
145
146// UseExistingFile returns true if BaseFile contains already uploaded file by FileID
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 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
180 FromChannelUsername string
181 MessageID int
182}
183
184// Values returns 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 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 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 return field name for uploading file
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 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 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 return field name for uploading file
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 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 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 return field name for uploading file
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 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 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 return field name for uploading file
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 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 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 return field name for uploading file
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 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 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 return field name for uploading file
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
434 Longitude float64
435}
436
437// Values returns 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
456}
457
458// Values returns 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 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 as a File.
496type FileBytes struct {
497 Name string
498 Bytes []byte
499}
500
501// FileReader contains information about a reader to upload as a File.
502// If Size is -1, it will read the entire Reader into memory to calculate a Size.
503type FileReader struct {
504 Name string
505 Reader io.Reader
506 Size int64
507}