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 ErrBadURL = "bad or empty url"
48)
49
50// Chattable is any config type that can be sent.
51type Chattable interface {
52 values() (url.Values, error)
53 method() string
54}
55
56// Fileable is any config type that can be sent that includes a file.
57type Fileable interface {
58 Chattable
59 params() (map[string]string, error)
60 name() string
61 getFile() interface{}
62 useExistingFile() bool
63}
64
65// BaseChat is base type for all chat config types.
66type BaseChat struct {
67 ChatID int64 // required
68 ChannelUsername string
69 ReplyToMessageID int
70 ReplyMarkup interface{}
71 DisableNotification bool
72}
73
74// values returns url.Values representation of BaseChat
75func (chat *BaseChat) values() (url.Values, error) {
76 v := url.Values{}
77 if chat.ChannelUsername != "" {
78 v.Add("chat_id", chat.ChannelUsername)
79 } else {
80 v.Add("chat_id", strconv.FormatInt(chat.ChatID, 10))
81 }
82
83 if chat.ReplyToMessageID != 0 {
84 v.Add("reply_to_message_id", strconv.Itoa(chat.ReplyToMessageID))
85 }
86
87 if chat.ReplyMarkup != nil {
88 data, err := json.Marshal(chat.ReplyMarkup)
89 if err != nil {
90 return v, err
91 }
92
93 v.Add("reply_markup", string(data))
94 }
95
96 v.Add("disable_notification", strconv.FormatBool(chat.DisableNotification))
97
98 return v, nil
99}
100
101// BaseFile is a base type for all file config types.
102type BaseFile struct {
103 BaseChat
104 File interface{}
105 FileID string
106 UseExisting bool
107 MimeType string
108 FileSize int
109}
110
111// params returns a map[string]string representation of BaseFile.
112func (file BaseFile) params() (map[string]string, error) {
113 params := make(map[string]string)
114
115 if file.ChannelUsername != "" {
116 params["chat_id"] = file.ChannelUsername
117 } else {
118 params["chat_id"] = strconv.FormatInt(file.ChatID, 10)
119 }
120
121 if file.ReplyToMessageID != 0 {
122 params["reply_to_message_id"] = strconv.Itoa(file.ReplyToMessageID)
123 }
124
125 if file.ReplyMarkup != nil {
126 data, err := json.Marshal(file.ReplyMarkup)
127 if err != nil {
128 return params, err
129 }
130
131 params["reply_markup"] = string(data)
132 }
133
134 if file.MimeType != "" {
135 params["mime_type"] = file.MimeType
136 }
137
138 if file.FileSize > 0 {
139 params["file_size"] = strconv.Itoa(file.FileSize)
140 }
141
142 params["disable_notification"] = strconv.FormatBool(file.DisableNotification)
143
144 return params, nil
145}
146
147// getFile returns the file.
148func (file BaseFile) getFile() interface{} {
149 return file.File
150}
151
152// useExistingFile returns if the BaseFile has already been uploaded.
153func (file BaseFile) useExistingFile() bool {
154 return file.UseExisting
155}
156
157// BaseEdit is base type of all chat edits.
158type BaseEdit struct {
159 ChatID int64
160 ChannelUsername string
161 MessageID int
162 InlineMessageID string
163 ReplyMarkup *InlineKeyboardMarkup
164}
165
166func (edit BaseEdit) values() (url.Values, error) {
167 v := url.Values{}
168
169 if edit.InlineMessageID == "" {
170 if edit.ChannelUsername != "" {
171 v.Add("chat_id", edit.ChannelUsername)
172 } else {
173 v.Add("chat_id", strconv.FormatInt(edit.ChatID, 10))
174 }
175 v.Add("message_id", strconv.Itoa(edit.MessageID))
176 } else {
177 v.Add("inline_message_id", edit.InlineMessageID)
178 }
179
180 if edit.ReplyMarkup != nil {
181 data, err := json.Marshal(edit.ReplyMarkup)
182 if err != nil {
183 return v, err
184 }
185 v.Add("reply_markup", string(data))
186 }
187
188 return v, nil
189}
190
191// MessageConfig contains information about a SendMessage request.
192type MessageConfig struct {
193 BaseChat
194 Text string
195 ParseMode string
196 DisableWebPagePreview bool
197}
198
199// values returns a url.Values representation of MessageConfig.
200func (config MessageConfig) values() (url.Values, error) {
201 v, err := config.BaseChat.values()
202 if err != nil {
203 return v, err
204 }
205 v.Add("text", config.Text)
206 v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview))
207 if config.ParseMode != "" {
208 v.Add("parse_mode", config.ParseMode)
209 }
210
211 return v, nil
212}
213
214// method returns Telegram API method name for sending Message.
215func (config MessageConfig) method() string {
216 return "sendMessage"
217}
218
219// ForwardConfig contains information about a ForwardMessage request.
220type ForwardConfig struct {
221 BaseChat
222 FromChatID int64 // required
223 FromChannelUsername string
224 MessageID int // required
225}
226
227// values returns a url.Values representation of ForwardConfig.
228func (config ForwardConfig) values() (url.Values, error) {
229 v, err := config.BaseChat.values()
230 if err != nil {
231 return v, err
232 }
233 v.Add("from_chat_id", strconv.FormatInt(config.FromChatID, 10))
234 v.Add("message_id", strconv.Itoa(config.MessageID))
235 return v, nil
236}
237
238// method returns Telegram API method name for sending Forward.
239func (config ForwardConfig) method() string {
240 return "forwardMessage"
241}
242
243// PhotoConfig contains information about a SendPhoto request.
244type PhotoConfig struct {
245 BaseFile
246 Caption string
247}
248
249// Params returns a map[string]string representation of PhotoConfig.
250func (config PhotoConfig) params() (map[string]string, error) {
251 params, _ := config.BaseFile.params()
252
253 if config.Caption != "" {
254 params["caption"] = config.Caption
255 }
256
257 return params, nil
258}
259
260// Values returns a url.Values representation of PhotoConfig.
261func (config PhotoConfig) values() (url.Values, error) {
262 v, err := config.BaseChat.values()
263 if err != nil {
264 return v, err
265 }
266
267 v.Add(config.name(), config.FileID)
268 if config.Caption != "" {
269 v.Add("caption", config.Caption)
270 }
271 return v, nil
272}
273
274// name returns the field name for the Photo.
275func (config PhotoConfig) name() string {
276 return "photo"
277}
278
279// method returns Telegram API method name for sending Photo.
280func (config PhotoConfig) method() string {
281 return "sendPhoto"
282}
283
284// AudioConfig contains information about a SendAudio request.
285type AudioConfig struct {
286 BaseFile
287 Caption string
288 Duration int
289 Performer string
290 Title string
291}
292
293// values returns a url.Values representation of AudioConfig.
294func (config AudioConfig) values() (url.Values, error) {
295 v, err := config.BaseChat.values()
296 if err != nil {
297 return v, err
298 }
299
300 v.Add(config.name(), config.FileID)
301 if config.Duration != 0 {
302 v.Add("duration", strconv.Itoa(config.Duration))
303 }
304
305 if config.Performer != "" {
306 v.Add("performer", config.Performer)
307 }
308 if config.Title != "" {
309 v.Add("title", config.Title)
310 }
311 if config.Caption != "" {
312 v.Add("caption", config.Caption)
313 }
314
315 return v, nil
316}
317
318// params returns a map[string]string representation of AudioConfig.
319func (config AudioConfig) params() (map[string]string, error) {
320 params, _ := config.BaseFile.params()
321
322 if config.Duration != 0 {
323 params["duration"] = strconv.Itoa(config.Duration)
324 }
325
326 if config.Performer != "" {
327 params["performer"] = config.Performer
328 }
329 if config.Title != "" {
330 params["title"] = config.Title
331 }
332 if config.Caption != "" {
333 params["caption"] = config.Caption
334 }
335
336 return params, nil
337}
338
339// name returns the field name for the Audio.
340func (config AudioConfig) name() string {
341 return "audio"
342}
343
344// method returns Telegram API method name for sending Audio.
345func (config AudioConfig) method() string {
346 return "sendAudio"
347}
348
349// DocumentConfig contains information about a SendDocument request.
350type DocumentConfig struct {
351 BaseFile
352 Caption string
353}
354
355// values returns a url.Values representation of DocumentConfig.
356func (config DocumentConfig) values() (url.Values, error) {
357 v, err := config.BaseChat.values()
358 if err != nil {
359 return v, err
360 }
361
362 v.Add(config.name(), config.FileID)
363 if config.Caption != "" {
364 v.Add("caption", config.Caption)
365 }
366
367 return v, nil
368}
369
370// params returns a map[string]string representation of DocumentConfig.
371func (config DocumentConfig) params() (map[string]string, error) {
372 params, _ := config.BaseFile.params()
373
374 if config.Caption != "" {
375 params["caption"] = config.Caption
376 }
377
378 return params, nil
379}
380
381// name returns the field name for the Document.
382func (config DocumentConfig) name() string {
383 return "document"
384}
385
386// method returns Telegram API method name for sending Document.
387func (config DocumentConfig) method() string {
388 return "sendDocument"
389}
390
391// StickerConfig contains information about a SendSticker request.
392type StickerConfig struct {
393 BaseFile
394}
395
396// values returns a url.Values representation of StickerConfig.
397func (config StickerConfig) values() (url.Values, error) {
398 v, err := config.BaseChat.values()
399 if err != nil {
400 return v, err
401 }
402
403 v.Add(config.name(), config.FileID)
404
405 return v, nil
406}
407
408// params returns a map[string]string representation of StickerConfig.
409func (config StickerConfig) params() (map[string]string, error) {
410 params, _ := config.BaseFile.params()
411
412 return params, nil
413}
414
415// name returns the field name for the Sticker.
416func (config StickerConfig) name() string {
417 return "sticker"
418}
419
420// method returns Telegram API method name for sending Sticker.
421func (config StickerConfig) method() string {
422 return "sendSticker"
423}
424
425// VideoConfig contains information about a SendVideo request.
426type VideoConfig struct {
427 BaseFile
428 Duration int
429 Caption string
430}
431
432// values returns a url.Values representation of VideoConfig.
433func (config VideoConfig) values() (url.Values, error) {
434 v, err := config.BaseChat.values()
435 if err != nil {
436 return v, err
437 }
438
439 v.Add(config.name(), config.FileID)
440 if config.Duration != 0 {
441 v.Add("duration", strconv.Itoa(config.Duration))
442 }
443 if config.Caption != "" {
444 v.Add("caption", config.Caption)
445 }
446
447 return v, nil
448}
449
450// params returns a map[string]string representation of VideoConfig.
451func (config VideoConfig) params() (map[string]string, error) {
452 params, _ := config.BaseFile.params()
453
454 return params, nil
455}
456
457// name returns the field name for the Video.
458func (config VideoConfig) name() string {
459 return "video"
460}
461
462// method returns Telegram API method name for sending Video.
463func (config VideoConfig) method() string {
464 return "sendVideo"
465}
466
467// VoiceConfig contains information about a SendVoice request.
468type VoiceConfig struct {
469 BaseFile
470 Caption string
471 Duration int
472}
473
474// values returns a url.Values representation of VoiceConfig.
475func (config VoiceConfig) values() (url.Values, error) {
476 v, err := config.BaseChat.values()
477 if err != nil {
478 return v, err
479 }
480
481 v.Add(config.name(), config.FileID)
482 if config.Duration != 0 {
483 v.Add("duration", strconv.Itoa(config.Duration))
484 }
485 if config.Caption != "" {
486 v.Add("caption", config.Caption)
487 }
488
489 return v, nil
490}
491
492// params returns a map[string]string representation of VoiceConfig.
493func (config VoiceConfig) params() (map[string]string, error) {
494 params, _ := config.BaseFile.params()
495
496 if config.Duration != 0 {
497 params["duration"] = strconv.Itoa(config.Duration)
498 }
499 if config.Caption != "" {
500 params["caption"] = config.Caption
501 }
502
503 return params, nil
504}
505
506// name returns the field name for the Voice.
507func (config VoiceConfig) name() string {
508 return "voice"
509}
510
511// method returns Telegram API method name for sending Voice.
512func (config VoiceConfig) method() string {
513 return "sendVoice"
514}
515
516// LocationConfig contains information about a SendLocation request.
517type LocationConfig struct {
518 BaseChat
519 Latitude float64 // required
520 Longitude float64 // required
521}
522
523// values returns a url.Values representation of LocationConfig.
524func (config LocationConfig) values() (url.Values, error) {
525 v, err := config.BaseChat.values()
526 if err != nil {
527 return v, err
528 }
529
530 v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
531 v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
532
533 return v, nil
534}
535
536// method returns Telegram API method name for sending Location.
537func (config LocationConfig) method() string {
538 return "sendLocation"
539}
540
541// VenueConfig contains information about a SendVenue request.
542type VenueConfig struct {
543 BaseChat
544 Latitude float64 // required
545 Longitude float64 // required
546 Title string // required
547 Address string // required
548 FoursquareID string
549}
550
551func (config VenueConfig) values() (url.Values, error) {
552 v, err := config.BaseChat.values()
553 if err != nil {
554 return v, err
555 }
556
557 v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
558 v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
559 v.Add("title", config.Title)
560 v.Add("address", config.Address)
561 if config.FoursquareID != "" {
562 v.Add("foursquare_id", config.FoursquareID)
563 }
564
565 return v, nil
566}
567
568func (config VenueConfig) method() string {
569 return "sendVenue"
570}
571
572// ContactConfig allows you to send a contact.
573type ContactConfig struct {
574 BaseChat
575 PhoneNumber string
576 FirstName string
577 LastName string
578}
579
580func (config ContactConfig) values() (url.Values, error) {
581 v, err := config.BaseChat.values()
582 if err != nil {
583 return v, err
584 }
585
586 v.Add("phone_number", config.PhoneNumber)
587 v.Add("first_name", config.FirstName)
588 v.Add("last_name", config.LastName)
589
590 return v, nil
591}
592
593func (config ContactConfig) method() string {
594 return "sendContact"
595}
596
597// GameConfig allows you to send a game.
598type GameConfig struct {
599 BaseChat
600 GameShortName string
601}
602
603func (config GameConfig) values() (url.Values, error) {
604 v, err := config.BaseChat.values()
605 if err != nil {
606 return v, err
607 }
608
609 v.Add("game_short_name", config.GameShortName)
610
611 return v, nil
612}
613
614func (config GameConfig) method() string {
615 return "sendGame"
616}
617
618// SetGameScoreConfig allows you to update the game score in a chat.
619type SetGameScoreConfig struct {
620 UserID int
621 Score int
622 Force bool
623 DisableEditMessage bool
624 ChatID int
625 ChannelUsername string
626 MessageID int
627 InlineMessageID string
628}
629
630func (config SetGameScoreConfig) values() (url.Values, error) {
631 v := url.Values{}
632
633 v.Add("user_id", strconv.Itoa(config.UserID))
634 v.Add("score", strconv.Itoa(config.Score))
635 if config.InlineMessageID == "" {
636 if config.ChannelUsername == "" {
637 v.Add("chat_id", strconv.Itoa(config.ChatID))
638 } else {
639 v.Add("chat_id", config.ChannelUsername)
640 }
641 v.Add("message_id", strconv.Itoa(config.MessageID))
642 } else {
643 v.Add("inline_message_id", config.InlineMessageID)
644 }
645 v.Add("disable_edit_message", strconv.FormatBool(config.DisableEditMessage))
646
647 return v, nil
648}
649
650func (config SetGameScoreConfig) method() string {
651 return "setGameScore"
652}
653
654// GetGameHighScoresConfig allows you to fetch the high scores for a game.
655type GetGameHighScoresConfig struct {
656 UserID int
657 ChatID int
658 ChannelUsername string
659 MessageID int
660 InlineMessageID string
661}
662
663func (config GetGameHighScoresConfig) values() (url.Values, error) {
664 v := url.Values{}
665
666 v.Add("user_id", strconv.Itoa(config.UserID))
667 if config.InlineMessageID == "" {
668 if config.ChannelUsername == "" {
669 v.Add("chat_id", strconv.Itoa(config.ChatID))
670 } else {
671 v.Add("chat_id", config.ChannelUsername)
672 }
673 v.Add("message_id", strconv.Itoa(config.MessageID))
674 } else {
675 v.Add("inline_message_id", config.InlineMessageID)
676 }
677
678 return v, nil
679}
680
681func (config GetGameHighScoresConfig) method() string {
682 return "getGameHighScores"
683}
684
685// ChatActionConfig contains information about a SendChatAction request.
686type ChatActionConfig struct {
687 BaseChat
688 Action string // required
689}
690
691// values returns a url.Values representation of ChatActionConfig.
692func (config ChatActionConfig) values() (url.Values, error) {
693 v, err := config.BaseChat.values()
694 if err != nil {
695 return v, err
696 }
697 v.Add("action", config.Action)
698 return v, nil
699}
700
701// method returns Telegram API method name for sending ChatAction.
702func (config ChatActionConfig) method() string {
703 return "sendChatAction"
704}
705
706// EditMessageTextConfig allows you to modify the text in a message.
707type EditMessageTextConfig struct {
708 BaseEdit
709 Text string
710 ParseMode string
711 DisableWebPagePreview bool
712}
713
714func (config EditMessageTextConfig) values() (url.Values, error) {
715 v, err := config.BaseEdit.values()
716 if err != nil {
717 return v, err
718 }
719
720 v.Add("text", config.Text)
721 v.Add("parse_mode", config.ParseMode)
722 v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview))
723
724 return v, nil
725}
726
727func (config EditMessageTextConfig) method() string {
728 return "editMessageText"
729}
730
731// EditMessageCaptionConfig allows you to modify the caption of a message.
732type EditMessageCaptionConfig struct {
733 BaseEdit
734 Caption string
735}
736
737func (config EditMessageCaptionConfig) values() (url.Values, error) {
738 v, _ := config.BaseEdit.values()
739
740 v.Add("caption", config.Caption)
741
742 return v, nil
743}
744
745func (config EditMessageCaptionConfig) method() string {
746 return "editMessageCaption"
747}
748
749// EditMessageReplyMarkupConfig allows you to modify the reply markup
750// of a message.
751type EditMessageReplyMarkupConfig struct {
752 BaseEdit
753}
754
755func (config EditMessageReplyMarkupConfig) values() (url.Values, error) {
756 return config.BaseEdit.values()
757}
758
759func (config EditMessageReplyMarkupConfig) method() string {
760 return "editMessageReplyMarkup"
761}
762
763// UserProfilePhotosConfig contains information about a
764// GetUserProfilePhotos request.
765type UserProfilePhotosConfig struct {
766 UserID int
767 Offset int
768 Limit int
769}
770
771// FileConfig has information about a file hosted on Telegram.
772type FileConfig struct {
773 FileID string
774}
775
776// UpdateConfig contains information about a GetUpdates request.
777type UpdateConfig struct {
778 Offset int
779 Limit int
780 Timeout int
781}
782
783// WebhookConfig contains information about a SetWebhook request.
784type WebhookConfig struct {
785 URL *url.URL
786 Certificate interface{}
787 MaxConnections int
788}
789
790// FileBytes contains information about a set of bytes to upload
791// as a File.
792type FileBytes struct {
793 Name string
794 Bytes []byte
795}
796
797// FileReader contains information about a reader to upload as a File.
798// If Size is -1, it will read the entire Reader into memory to
799// calculate a Size.
800type FileReader struct {
801 Name string
802 Reader io.Reader
803 Size int64
804}
805
806// InlineConfig contains information on making an InlineQuery response.
807type InlineConfig struct {
808 InlineQueryID string `json:"inline_query_id"`
809 Results []interface{} `json:"results"`
810 CacheTime int `json:"cache_time"`
811 IsPersonal bool `json:"is_personal"`
812 NextOffset string `json:"next_offset"`
813 SwitchPMText string `json:"switch_pm_text"`
814 SwitchPMParameter string `json:"switch_pm_parameter"`
815}
816
817// CallbackConfig contains information on making a CallbackQuery response.
818type CallbackConfig struct {
819 CallbackQueryID string `json:"callback_query_id"`
820 Text string `json:"text"`
821 ShowAlert bool `json:"show_alert"`
822 URL string `json:"url"`
823 CacheTime int `json:"cache_time"`
824}
825
826// ChatMemberConfig contains information about a user in a chat for use
827// with administrative functions such as kicking or unbanning a user.
828type ChatMemberConfig struct {
829 ChatID int64
830 SuperGroupUsername string
831 UserID int
832}
833
834// ChatConfig contains information about getting information on a chat.
835type ChatConfig struct {
836 ChatID int64
837 SuperGroupUsername string
838}
839
840// ChatConfigWithUser contains information about getting information on
841// a specific user within a chat.
842type ChatConfigWithUser struct {
843 ChatID int64
844 SuperGroupUsername string
845 UserID int
846}