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