configs.go (view raw)
1package tgbotapi
2
3import (
4 "io"
5 "net/url"
6 "strconv"
7)
8
9// Telegram constants
10const (
11 // APIEndpoint is the endpoint for all API methods,
12 // 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 ChatRecordVideoNote = "record_video_note"
29 ChatUploadVideoNote = "upload_video_note"
30)
31
32// API errors
33const (
34 // ErrAPIForbidden happens when a token is bad
35 ErrAPIForbidden = "forbidden"
36)
37
38// Constant values for ParseMode in MessageConfig
39const (
40 ModeMarkdown = "Markdown"
41 ModeMarkdownV2 = "MarkdownV2"
42 ModeHTML = "HTML"
43)
44
45// Library errors
46const (
47 // ErrBadFileType happens when you pass an unknown type
48 ErrBadFileType = "bad file type"
49 ErrBadURL = "bad or empty url"
50)
51
52// Chattable is any config type that can be sent.
53type Chattable interface {
54 params() (Params, error)
55 method() string
56}
57
58// RequestFile represents a file associated with a request. May involve
59// uploading a file, or passing an existing ID.
60type RequestFile struct {
61 // The multipart upload field name.
62 Name string
63 // The file to upload.
64 File interface{}
65}
66
67// Fileable is any config type that can be sent that includes a file.
68type Fileable interface {
69 Chattable
70 files() []RequestFile
71}
72
73// BaseChat is base type for all chat config types.
74type BaseChat struct {
75 ChatID int64 // required
76 ChannelUsername string
77 ReplyToMessageID int
78 ReplyMarkup interface{}
79 DisableNotification bool
80}
81
82func (chat *BaseChat) params() (Params, error) {
83 params := make(Params)
84
85 params.AddFirstValid("chat_id", chat.ChatID, chat.ChannelUsername)
86 params.AddNonZero("reply_to_message_id", chat.ReplyToMessageID)
87 params.AddBool("disable_notification", chat.DisableNotification)
88
89 err := params.AddInterface("reply_markup", chat.ReplyMarkup)
90
91 return params, err
92}
93
94// BaseFile is a base type for all file config types.
95type BaseFile struct {
96 BaseChat
97 Files []RequestFile
98 MimeType string
99 FileSize int
100}
101
102// AddFile specifies a file for a Telegram request.
103func (file *BaseFile) AddFile(name string, f interface{}) {
104 if file.Files == nil {
105 file.Files = make([]RequestFile, 0, 1)
106 }
107
108 file.Files = append(file.Files, RequestFile{
109 Name: name,
110 File: f,
111 })
112}
113
114func (file BaseFile) params() (Params, error) {
115 params, err := file.BaseChat.params()
116
117 params.AddNonEmpty("mime_type", file.MimeType)
118 params.AddNonZero("file_size", file.FileSize)
119
120 return params, err
121}
122
123func (file BaseFile) files() []RequestFile {
124 return file.Files
125}
126
127// BaseEdit is base type of all chat edits.
128type BaseEdit struct {
129 ChatID int64
130 ChannelUsername string
131 MessageID int
132 InlineMessageID string
133 ReplyMarkup *InlineKeyboardMarkup
134}
135
136func (edit BaseEdit) params() (Params, error) {
137 params := make(Params)
138
139 if edit.InlineMessageID != "" {
140 params["inline_message_id"] = edit.InlineMessageID
141 } else {
142 params.AddFirstValid("chat_id", edit.ChatID, edit.ChannelUsername)
143 params.AddNonZero("message_id", edit.MessageID)
144 }
145
146 err := params.AddInterface("reply_markup", edit.ReplyMarkup)
147
148 return params, err
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
159func (config MessageConfig) params() (Params, error) {
160 params, err := config.BaseChat.params()
161 if err != nil {
162 return params, err
163 }
164
165 params.AddNonEmpty("text", config.Text)
166 params.AddBool("disable_web_page_preview", config.DisableWebPagePreview)
167 params.AddNonEmpty("parse_mode", config.ParseMode)
168
169 return params, nil
170}
171
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 int64 // required
180 FromChannelUsername string
181 MessageID int // required
182}
183
184func (config ForwardConfig) params() (Params, error) {
185 params, err := config.BaseChat.params()
186 if err != nil {
187 return params, err
188 }
189
190 params.AddNonZero64("from_chat_id", config.FromChatID)
191 params.AddNonZero("message_id", config.MessageID)
192
193 return params, nil
194}
195
196func (config ForwardConfig) method() string {
197 return "forwardMessage"
198}
199
200// PhotoConfig contains information about a SendPhoto request.
201type PhotoConfig struct {
202 BaseFile
203 Caption string
204 ParseMode string
205}
206
207func (config PhotoConfig) params() (Params, error) {
208 params, err := config.BaseFile.params()
209
210 params.AddNonEmpty("caption", config.Caption)
211 params.AddNonEmpty("parse_mode", config.ParseMode)
212
213 return params, err
214}
215
216func (config PhotoConfig) name() string {
217 return "photo"
218}
219
220func (config PhotoConfig) method() string {
221 return "sendPhoto"
222}
223
224// AudioConfig contains information about a SendAudio request.
225type AudioConfig struct {
226 BaseFile
227 Caption string
228 ParseMode string
229 Duration int
230 Performer string
231 Title string
232}
233
234func (config AudioConfig) params() (Params, error) {
235 params, err := config.BaseChat.params()
236 if err != nil {
237 return params, err
238 }
239
240 params.AddNonZero("duration", config.Duration)
241 params.AddNonEmpty("performer", config.Performer)
242 params.AddNonEmpty("title", config.Title)
243 params.AddNonEmpty("caption", config.Caption)
244 params.AddNonEmpty("parse_mode", config.ParseMode)
245
246 return params, nil
247}
248
249func (config AudioConfig) name() string {
250 return "audio"
251}
252
253func (config AudioConfig) method() string {
254 return "sendAudio"
255}
256
257// DocumentConfig contains information about a SendDocument request.
258type DocumentConfig struct {
259 BaseFile
260 Caption string
261 ParseMode string
262}
263
264func (config DocumentConfig) params() (Params, error) {
265 params, err := config.BaseFile.params()
266
267 params.AddNonEmpty("caption", config.Caption)
268 params.AddNonEmpty("parse_mode", config.ParseMode)
269
270 return params, err
271}
272
273func (config DocumentConfig) name() string {
274 return "document"
275}
276
277func (config DocumentConfig) method() string {
278 return "sendDocument"
279}
280
281// StickerConfig contains information about a SendSticker request.
282type StickerConfig struct {
283 BaseFile
284}
285
286func (config StickerConfig) params() (Params, error) {
287 return config.BaseChat.params()
288}
289
290func (config StickerConfig) name() string {
291 return "sticker"
292}
293
294func (config StickerConfig) method() string {
295 return "sendSticker"
296}
297
298// VideoConfig contains information about a SendVideo request.
299type VideoConfig struct {
300 BaseFile
301 Duration int
302 Caption string
303 ParseMode string
304 SupportsStreaming bool
305}
306
307func (config VideoConfig) params() (Params, error) {
308 params, err := config.BaseChat.params()
309
310 params.AddNonZero("duration", config.Duration)
311 params.AddNonEmpty("caption", config.Caption)
312 params.AddNonEmpty("parse_mode", config.ParseMode)
313 params.AddBool("supports_streaming", config.SupportsStreaming)
314
315 return params, err
316}
317
318func (config VideoConfig) name() string {
319 return "video"
320}
321
322func (config VideoConfig) method() string {
323 return "sendVideo"
324}
325
326// AnimationConfig contains information about a SendAnimation request.
327type AnimationConfig struct {
328 BaseFile
329 Duration int
330 Caption string
331 ParseMode string
332}
333
334func (config AnimationConfig) params() (Params, error) {
335 params, err := config.BaseChat.params()
336
337 params.AddNonZero("duration", config.Duration)
338 params.AddNonEmpty("caption", config.Caption)
339 params.AddNonEmpty("parse_mode", config.ParseMode)
340
341 return params, err
342}
343
344func (config AnimationConfig) name() string {
345 return "animation"
346}
347
348func (config AnimationConfig) method() string {
349 return "sendAnimation"
350}
351
352// VideoNoteConfig contains information about a SendVideoNote request.
353type VideoNoteConfig struct {
354 BaseFile
355 Duration int
356 Length int
357}
358
359func (config VideoNoteConfig) params() (Params, error) {
360 params, err := config.BaseChat.params()
361
362 params.AddNonZero("duration", config.Duration)
363 params.AddNonZero("length", config.Length)
364
365 return params, err
366}
367
368func (config VideoNoteConfig) name() string {
369 return "video_note"
370}
371
372func (config VideoNoteConfig) method() string {
373 return "sendVideoNote"
374}
375
376// VoiceConfig contains information about a SendVoice request.
377type VoiceConfig struct {
378 BaseFile
379 Caption string
380 ParseMode string
381 Duration int
382}
383
384func (config VoiceConfig) params() (Params, error) {
385 params, err := config.BaseChat.params()
386
387 params.AddNonZero("duration", config.Duration)
388 params.AddNonEmpty("caption", config.Caption)
389 params.AddNonEmpty("parse_mode", config.ParseMode)
390
391 return params, err
392}
393
394func (config VoiceConfig) name() string {
395 return "voice"
396}
397
398func (config VoiceConfig) method() string {
399 return "sendVoice"
400}
401
402// LocationConfig contains information about a SendLocation request.
403type LocationConfig struct {
404 BaseChat
405 Latitude float64 // required
406 Longitude float64 // required
407 LivePeriod int // optional
408}
409
410func (config LocationConfig) params() (Params, error) {
411 params, err := config.BaseChat.params()
412
413 params.AddNonZeroFloat("latitude", config.Latitude)
414 params.AddNonZeroFloat("longitude", config.Longitude)
415 params.AddNonZero("live_period", config.LivePeriod)
416
417 return params, err
418}
419
420func (config LocationConfig) method() string {
421 return "sendLocation"
422}
423
424// EditMessageLiveLocationConfig allows you to update a live location.
425type EditMessageLiveLocationConfig struct {
426 BaseEdit
427 Latitude float64 // required
428 Longitude float64 // required
429}
430
431func (config EditMessageLiveLocationConfig) params() (Params, error) {
432 params, err := config.BaseEdit.params()
433
434 params.AddNonZeroFloat("latitude", config.Latitude)
435 params.AddNonZeroFloat("longitude", config.Longitude)
436
437 return params, err
438}
439
440func (config EditMessageLiveLocationConfig) method() string {
441 return "editMessageLiveLocation"
442}
443
444// StopMessageLiveLocationConfig stops updating a live location.
445type StopMessageLiveLocationConfig struct {
446 BaseEdit
447}
448
449func (config StopMessageLiveLocationConfig) params() (Params, error) {
450 return config.BaseEdit.params()
451}
452
453func (config StopMessageLiveLocationConfig) method() string {
454 return "stopMessageLiveLocation"
455}
456
457// VenueConfig contains information about a SendVenue request.
458type VenueConfig struct {
459 BaseChat
460 Latitude float64 // required
461 Longitude float64 // required
462 Title string // required
463 Address string // required
464 FoursquareID string
465}
466
467func (config VenueConfig) params() (Params, error) {
468 params, err := config.BaseChat.params()
469
470 params.AddNonZeroFloat("latitude", config.Latitude)
471 params.AddNonZeroFloat("longitude", config.Longitude)
472 params["title"] = config.Title
473 params["address"] = config.Address
474 params.AddNonEmpty("foursquare_id", config.FoursquareID)
475
476 return params, err
477}
478
479func (config VenueConfig) method() string {
480 return "sendVenue"
481}
482
483// ContactConfig allows you to send a contact.
484type ContactConfig struct {
485 BaseChat
486 PhoneNumber string
487 FirstName string
488 LastName string
489 VCard string
490}
491
492func (config ContactConfig) params() (Params, error) {
493 params, err := config.BaseChat.params()
494
495 params["phone_number"] = config.PhoneNumber
496 params["first_name"] = config.FirstName
497
498 params.AddNonEmpty("last_name", config.LastName)
499 params.AddNonEmpty("vcard", config.VCard)
500
501 return params, err
502}
503
504func (config ContactConfig) method() string {
505 return "sendContact"
506}
507
508// SendPollConfig allows you to send a poll.
509type SendPollConfig struct {
510 BaseChat
511 Question string
512 Options []string
513 IsAnonymous bool
514 Type string
515 AllowsMultipleAnswers bool
516 CorrectOptionID int64
517 Explanation string
518 ExplanationParseMode string
519 OpenPeriod int
520 CloseDate int
521 IsClosed bool
522}
523
524func (config SendPollConfig) params() (Params, error) {
525 params, err := config.BaseChat.params()
526 if err != nil {
527 return params, err
528 }
529
530 params["question"] = config.Question
531 err = params.AddInterface("options", config.Options)
532 params["is_anonymous"] = strconv.FormatBool(config.IsAnonymous)
533 params.AddNonEmpty("type", config.Type)
534 params["allows_multiple_answers"] = strconv.FormatBool(config.AllowsMultipleAnswers)
535 params["correct_option_id"] = strconv.FormatInt(config.CorrectOptionID, 10)
536 params.AddBool("is_closed", config.IsClosed)
537 params.AddNonEmpty("explanation", config.Explanation)
538 params.AddNonEmpty("explanation_parse_mode", config.ExplanationParseMode)
539 params.AddNonZero("open_period", config.OpenPeriod)
540 params.AddNonZero("close_date", config.CloseDate)
541
542 return params, err
543}
544
545func (SendPollConfig) method() string {
546 return "sendPoll"
547}
548
549// GameConfig allows you to send a game.
550type GameConfig struct {
551 BaseChat
552 GameShortName string
553}
554
555func (config GameConfig) params() (Params, error) {
556 params, err := config.BaseChat.params()
557
558 params["game_short_name"] = config.GameShortName
559
560 return params, err
561}
562
563func (config GameConfig) method() string {
564 return "sendGame"
565}
566
567// SetGameScoreConfig allows you to update the game score in a chat.
568type SetGameScoreConfig struct {
569 UserID int
570 Score int
571 Force bool
572 DisableEditMessage bool
573 ChatID int64
574 ChannelUsername string
575 MessageID int
576 InlineMessageID string
577}
578
579func (config SetGameScoreConfig) params() (Params, error) {
580 params := make(Params)
581
582 params.AddNonZero("user_id", config.UserID)
583 params.AddNonZero("scrore", config.Score)
584 params.AddBool("disable_edit_message", config.DisableEditMessage)
585
586 if config.InlineMessageID != "" {
587 params["inline_message_id"] = config.InlineMessageID
588 } else {
589 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
590 params.AddNonZero("message_id", config.MessageID)
591 }
592
593 return params, nil
594}
595
596func (config SetGameScoreConfig) method() string {
597 return "setGameScore"
598}
599
600// GetGameHighScoresConfig allows you to fetch the high scores for a game.
601type GetGameHighScoresConfig struct {
602 UserID int
603 ChatID int
604 ChannelUsername string
605 MessageID int
606 InlineMessageID string
607}
608
609func (config GetGameHighScoresConfig) params() (Params, error) {
610 params := make(Params)
611
612 params.AddNonZero("user_id", config.UserID)
613
614 if config.InlineMessageID != "" {
615 params["inline_message_id"] = config.InlineMessageID
616 } else {
617 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
618 params.AddNonZero("message_id", config.MessageID)
619 }
620
621 return params, nil
622}
623
624func (config GetGameHighScoresConfig) method() string {
625 return "getGameHighScores"
626}
627
628// ChatActionConfig contains information about a SendChatAction request.
629type ChatActionConfig struct {
630 BaseChat
631 Action string // required
632}
633
634func (config ChatActionConfig) params() (Params, error) {
635 params, err := config.BaseChat.params()
636
637 params["action"] = config.Action
638
639 return params, err
640}
641
642func (config ChatActionConfig) method() string {
643 return "sendChatAction"
644}
645
646// EditMessageTextConfig allows you to modify the text in a message.
647type EditMessageTextConfig struct {
648 BaseEdit
649 Text string
650 ParseMode string
651 DisableWebPagePreview bool
652}
653
654func (config EditMessageTextConfig) params() (Params, error) {
655 params, err := config.BaseEdit.params()
656
657 params["text"] = config.Text
658 params.AddNonEmpty("parse_mode", config.ParseMode)
659 params.AddBool("disable_web_page_preview", config.DisableWebPagePreview)
660
661 return params, err
662}
663
664func (config EditMessageTextConfig) method() string {
665 return "editMessageText"
666}
667
668// EditMessageCaptionConfig allows you to modify the caption of a message.
669type EditMessageCaptionConfig struct {
670 BaseEdit
671 Caption string
672 ParseMode string
673}
674
675func (config EditMessageCaptionConfig) params() (Params, error) {
676 params, err := config.BaseEdit.params()
677
678 params["caption"] = config.Caption
679 params.AddNonEmpty("parse_mode", config.ParseMode)
680
681 return params, err
682}
683
684func (config EditMessageCaptionConfig) method() string {
685 return "editMessageCaption"
686}
687
688// EditMessageMediaConfig allows you to make an editMessageMedia request.
689type EditMessageMediaConfig struct {
690 BaseEdit
691
692 Media interface{}
693}
694
695func (config EditMessageMediaConfig) files() []RequestFile {
696 return []RequestFile{
697 {
698 Name: "media",
699 File: config.Media,
700 },
701 }
702}
703
704func (EditMessageMediaConfig) method() string {
705 return "editMessageMedia"
706}
707
708func (config EditMessageMediaConfig) params() (Params, error) {
709 return config.BaseEdit.params()
710}
711
712// EditMessageReplyMarkupConfig allows you to modify the reply markup
713// of a message.
714type EditMessageReplyMarkupConfig struct {
715 BaseEdit
716}
717
718func (config EditMessageReplyMarkupConfig) params() (Params, error) {
719 return config.BaseEdit.params()
720}
721
722func (config EditMessageReplyMarkupConfig) method() string {
723 return "editMessageReplyMarkup"
724}
725
726// StopPollConfig allows you to stop a poll sent by the bot.
727type StopPollConfig struct {
728 BaseEdit
729}
730
731func (config StopPollConfig) params() (Params, error) {
732 return config.BaseEdit.params()
733}
734
735func (StopPollConfig) method() string {
736 return "stopPoll"
737}
738
739// UserProfilePhotosConfig contains information about a
740// GetUserProfilePhotos request.
741type UserProfilePhotosConfig struct {
742 UserID int
743 Offset int
744 Limit int
745}
746
747func (UserProfilePhotosConfig) method() string {
748 return "getUserProfilePhotos"
749}
750
751func (config UserProfilePhotosConfig) params() (Params, error) {
752 params := make(Params)
753
754 params.AddNonZero("user_id", config.UserID)
755 params.AddNonZero("offset", config.Offset)
756 params.AddNonZero("limit", config.Limit)
757
758 return params, nil
759}
760
761// FileConfig has information about a file hosted on Telegram.
762type FileConfig struct {
763 FileID string
764}
765
766func (FileConfig) method() string {
767 return "getFile"
768}
769
770func (config FileConfig) params() (Params, error) {
771 params := make(Params)
772
773 params["file_id"] = config.FileID
774
775 return params, nil
776}
777
778// UpdateConfig contains information about a GetUpdates request.
779type UpdateConfig struct {
780 Offset int
781 Limit int
782 Timeout int
783}
784
785func (UpdateConfig) method() string {
786 return "getUpdates"
787}
788
789func (config UpdateConfig) params() (Params, error) {
790 params := make(Params)
791
792 params.AddNonZero("offset", config.Offset)
793 params.AddNonZero("limit", config.Limit)
794 params.AddNonZero("timeout", config.Timeout)
795
796 return params, nil
797}
798
799// WebhookConfig contains information about a SetWebhook request.
800type WebhookConfig struct {
801 URL *url.URL
802 Certificate interface{}
803 MaxConnections int
804 AllowedUpdates []string
805}
806
807func (config WebhookConfig) method() string {
808 return "setWebhook"
809}
810
811func (config WebhookConfig) params() (Params, error) {
812 params := make(Params)
813
814 if config.URL != nil {
815 params["url"] = config.URL.String()
816 }
817
818 params.AddNonZero("max_connections", config.MaxConnections)
819 params.AddInterface("allowed_updates", config.AllowedUpdates)
820
821 return params, nil
822}
823
824func (config WebhookConfig) name() string {
825 return "certificate"
826}
827
828// RemoveWebhookConfig is a helper to remove a webhook.
829type RemoveWebhookConfig struct {
830}
831
832func (config RemoveWebhookConfig) method() string {
833 return "setWebhook"
834}
835
836func (config RemoveWebhookConfig) params() (Params, error) {
837 return nil, nil
838}
839
840// FileBytes contains information about a set of bytes to upload
841// as a File.
842type FileBytes struct {
843 Name string
844 Bytes []byte
845}
846
847// FileReader contains information about a reader to upload as a File.
848// If Size is -1, it will read the entire Reader into memory to
849// calculate a Size.
850type FileReader struct {
851 Name string
852 Reader io.Reader
853 Size int64
854}
855
856// FileURL is a URL to use as a file for a request.
857type FileURL string
858
859// FileID is an ID of a file already uploaded to Telegram.
860type FileID string
861
862// InlineConfig contains information on making an InlineQuery response.
863type InlineConfig struct {
864 InlineQueryID string `json:"inline_query_id"`
865 Results []interface{} `json:"results"`
866 CacheTime int `json:"cache_time"`
867 IsPersonal bool `json:"is_personal"`
868 NextOffset string `json:"next_offset"`
869 SwitchPMText string `json:"switch_pm_text"`
870 SwitchPMParameter string `json:"switch_pm_parameter"`
871}
872
873func (config InlineConfig) method() string {
874 return "answerInlineQuery"
875}
876
877func (config InlineConfig) params() (Params, error) {
878 params := make(Params)
879
880 params["inline_query_id"] = config.InlineQueryID
881 params.AddNonZero("cache_time", config.CacheTime)
882 params.AddBool("is_personal", config.IsPersonal)
883 params.AddNonEmpty("next_offset", config.NextOffset)
884 params.AddNonEmpty("switch_pm_text", config.SwitchPMText)
885 params.AddNonEmpty("switch_pm_parameter", config.SwitchPMParameter)
886
887 if err := params.AddInterface("results", config.Results); err != nil {
888 return params, err
889 }
890
891 return params, nil
892}
893
894// CallbackConfig contains information on making a CallbackQuery response.
895type CallbackConfig struct {
896 CallbackQueryID string `json:"callback_query_id"`
897 Text string `json:"text"`
898 ShowAlert bool `json:"show_alert"`
899 URL string `json:"url"`
900 CacheTime int `json:"cache_time"`
901}
902
903func (config CallbackConfig) method() string {
904 return "answerCallbackQuery"
905}
906
907func (config CallbackConfig) params() (Params, error) {
908 params := make(Params)
909
910 params["callback_query_id"] = config.CallbackQueryID
911 params.AddNonEmpty("text", config.Text)
912 params.AddBool("show_alert", config.ShowAlert)
913 params.AddNonEmpty("url", config.URL)
914 params.AddNonZero("cache_time", config.CacheTime)
915
916 return params, nil
917}
918
919// ChatMemberConfig contains information about a user in a chat for use
920// with administrative functions such as kicking or unbanning a user.
921type ChatMemberConfig struct {
922 ChatID int64
923 SuperGroupUsername string
924 ChannelUsername string
925 UserID int
926}
927
928// UnbanChatMemberConfig allows you to unban a user.
929type UnbanChatMemberConfig struct {
930 ChatMemberConfig
931}
932
933func (config UnbanChatMemberConfig) method() string {
934 return "unbanChatMember"
935}
936
937func (config UnbanChatMemberConfig) params() (Params, error) {
938 params := make(Params)
939
940 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
941 params.AddNonZero("user_id", config.UserID)
942
943 return params, nil
944}
945
946// KickChatMemberConfig contains extra fields to kick user
947type KickChatMemberConfig struct {
948 ChatMemberConfig
949 UntilDate int64
950}
951
952func (config KickChatMemberConfig) method() string {
953 return "kickChatMember"
954}
955
956func (config KickChatMemberConfig) params() (Params, error) {
957 params := make(Params)
958
959 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
960 params.AddNonZero("user_id", config.UserID)
961 params.AddNonZero64("until_date", config.UntilDate)
962
963 return params, nil
964}
965
966// RestrictChatMemberConfig contains fields to restrict members of chat
967type RestrictChatMemberConfig struct {
968 ChatMemberConfig
969 UntilDate int64
970 Permissions *ChatPermissions
971}
972
973func (config RestrictChatMemberConfig) method() string {
974 return "restrictChatMember"
975}
976
977func (config RestrictChatMemberConfig) params() (Params, error) {
978 params := make(Params)
979
980 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
981 params.AddNonZero("user_id", config.UserID)
982
983 if err := params.AddInterface("permissions", config.Permissions); err != nil {
984 return params, err
985 }
986 params.AddNonZero64("until_date", config.UntilDate)
987
988 return params, nil
989}
990
991// PromoteChatMemberConfig contains fields to promote members of chat
992type PromoteChatMemberConfig struct {
993 ChatMemberConfig
994 CanChangeInfo bool
995 CanPostMessages bool
996 CanEditMessages bool
997 CanDeleteMessages bool
998 CanInviteUsers bool
999 CanRestrictMembers bool
1000 CanPinMessages bool
1001 CanPromoteMembers bool
1002}
1003
1004func (config PromoteChatMemberConfig) method() string {
1005 return "promoteChatMember"
1006}
1007
1008func (config PromoteChatMemberConfig) params() (Params, error) {
1009 params := make(Params)
1010
1011 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
1012 params.AddNonZero("user_id", config.UserID)
1013
1014 params.AddBool("can_change_info", config.CanChangeInfo)
1015 params.AddBool("can_post_messages", config.CanPostMessages)
1016 params.AddBool("can_edit_messages", config.CanEditMessages)
1017 params.AddBool("can_delete_messages", config.CanDeleteMessages)
1018 params.AddBool("can_invite_users", config.CanInviteUsers)
1019 params.AddBool("can_restrict_members", config.CanRestrictMembers)
1020 params.AddBool("can_pin_messages", config.CanPinMessages)
1021 params.AddBool("can_promote_members", config.CanPromoteMembers)
1022
1023 return params, nil
1024}
1025
1026// SetChatAdministratorCustomTitle sets the title of an administrative user
1027// promoted by the bot for a chat.
1028type SetChatAdministratorCustomTitle struct {
1029 ChatMemberConfig
1030 CustomTitle string
1031}
1032
1033func (SetChatAdministratorCustomTitle) method() string {
1034 return "setChatAdministratorCustomTitle"
1035}
1036
1037func (config SetChatAdministratorCustomTitle) params() (Params, error) {
1038 params := make(Params)
1039
1040 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
1041 params.AddNonZero("user_id", config.UserID)
1042 params.AddNonEmpty("custom_title", config.CustomTitle)
1043
1044 return params, nil
1045}
1046
1047// ChatConfig contains information about getting information on a chat.
1048type ChatConfig struct {
1049 ChatID int64
1050 SuperGroupUsername string
1051}
1052
1053func (config ChatConfig) params() (Params, error) {
1054 params := make(Params)
1055
1056 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
1057
1058 return params, nil
1059}
1060
1061// ChatInfoConfig contains information about getting chat information.
1062type ChatInfoConfig struct {
1063 ChatConfig
1064}
1065
1066func (ChatInfoConfig) method() string {
1067 return "getChat"
1068}
1069
1070// ChatMemberCountConfig contains information about getting the number of users in a chat.
1071type ChatMemberCountConfig struct {
1072 ChatConfig
1073}
1074
1075func (ChatMemberCountConfig) method() string {
1076 return "getChatMembersCount"
1077}
1078
1079// ChatAdministratorsConfig contains information about getting chat administrators.
1080type ChatAdministratorsConfig struct {
1081 ChatConfig
1082}
1083
1084func (ChatAdministratorsConfig) method() string {
1085 return "getChatAdministrators"
1086}
1087
1088// SetChatPermissionsConfig allows you to set default permissions for the
1089// members in a group. The bot must be an administrator and have rights to
1090// restrict members.
1091type SetChatPermissionsConfig struct {
1092 ChatConfig
1093 Permissions *ChatPermissions
1094}
1095
1096func (SetChatPermissionsConfig) method() string {
1097 return "setChatPermissions"
1098}
1099
1100func (config SetChatPermissionsConfig) params() (Params, error) {
1101 params := make(Params)
1102
1103 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
1104 params.AddInterface("permissions", config.Permissions)
1105
1106 return params, nil
1107}
1108
1109// ChatInviteLinkConfig contains information about getting a chat link.
1110//
1111// Note that generating a new link will revoke any previous links.
1112type ChatInviteLinkConfig struct {
1113 ChatConfig
1114}
1115
1116func (ChatInviteLinkConfig) method() string {
1117 return "exportChatInviteLink"
1118}
1119
1120func (config ChatInviteLinkConfig) params() (Params, error) {
1121 params := make(Params)
1122
1123 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
1124
1125 return params, nil
1126}
1127
1128// LeaveChatConfig allows you to leave a chat.
1129type LeaveChatConfig struct {
1130 ChatID int64
1131 ChannelUsername string
1132}
1133
1134func (config LeaveChatConfig) method() string {
1135 return "leaveChat"
1136}
1137
1138func (config LeaveChatConfig) params() (Params, error) {
1139 params := make(Params)
1140
1141 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1142
1143 return params, nil
1144}
1145
1146// ChatConfigWithUser contains information about a chat and a user.
1147type ChatConfigWithUser struct {
1148 ChatID int64
1149 SuperGroupUsername string
1150 UserID int
1151}
1152
1153func (config ChatConfigWithUser) params() (Params, error) {
1154 params := make(Params)
1155
1156 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
1157 params.AddNonZero("user_id", config.UserID)
1158
1159 return params, nil
1160}
1161
1162// GetChatMemberConfig is information about getting a specific member in a chat.
1163type GetChatMemberConfig struct {
1164 ChatConfigWithUser
1165}
1166
1167func (GetChatMemberConfig) method() string {
1168 return "getChatMember"
1169}
1170
1171// InvoiceConfig contains information for sendInvoice request.
1172type InvoiceConfig struct {
1173 BaseChat
1174 Title string // required
1175 Description string // required
1176 Payload string // required
1177 ProviderToken string // required
1178 StartParameter string // required
1179 Currency string // required
1180 Prices []LabeledPrice // required
1181 ProviderData string
1182 PhotoURL string
1183 PhotoSize int
1184 PhotoWidth int
1185 PhotoHeight int
1186 NeedName bool
1187 NeedPhoneNumber bool
1188 NeedEmail bool
1189 NeedShippingAddress bool
1190 SendPhoneNumberToProvider bool
1191 SendEmailToProvider bool
1192 IsFlexible bool
1193}
1194
1195func (config InvoiceConfig) params() (Params, error) {
1196 params, err := config.BaseChat.params()
1197 if err != nil {
1198 return params, err
1199 }
1200
1201 params["title"] = config.Title
1202 params["description"] = config.Description
1203 params["payload"] = config.Payload
1204 params["provider_token"] = config.ProviderToken
1205 params["start_parameter"] = config.StartParameter
1206 params["currency"] = config.Currency
1207
1208 if err = params.AddInterface("prices", config.Prices); err != nil {
1209 return params, err
1210 }
1211
1212 params.AddNonEmpty("provider_data", config.ProviderData)
1213 params.AddNonEmpty("photo_url", config.PhotoURL)
1214 params.AddNonZero("photo_size", config.PhotoSize)
1215 params.AddNonZero("photo_width", config.PhotoWidth)
1216 params.AddNonZero("photo_height", config.PhotoHeight)
1217 params.AddBool("need_name", config.NeedName)
1218 params.AddBool("need_phone_number", config.NeedPhoneNumber)
1219 params.AddBool("need_email", config.NeedEmail)
1220 params.AddBool("need_shipping_address", config.NeedShippingAddress)
1221 params.AddBool("is_flexible", config.IsFlexible)
1222 params.AddBool("send_phone_number_to_provider", config.SendPhoneNumberToProvider)
1223 params.AddBool("send_email_to_provider", config.SendEmailToProvider)
1224
1225 return params, nil
1226}
1227
1228func (config InvoiceConfig) method() string {
1229 return "sendInvoice"
1230}
1231
1232// ShippingConfig contains information for answerShippingQuery request.
1233type ShippingConfig struct {
1234 ShippingQueryID string // required
1235 OK bool // required
1236 ShippingOptions []ShippingOption
1237 ErrorMessage string
1238}
1239
1240// PreCheckoutConfig conatins information for answerPreCheckoutQuery request.
1241type PreCheckoutConfig struct {
1242 PreCheckoutQueryID string // required
1243 OK bool // required
1244 ErrorMessage string
1245}
1246
1247// DeleteMessageConfig contains information of a message in a chat to delete.
1248type DeleteMessageConfig struct {
1249 ChannelUsername string
1250 ChatID int64
1251 MessageID int
1252}
1253
1254func (config DeleteMessageConfig) method() string {
1255 return "deleteMessage"
1256}
1257
1258func (config DeleteMessageConfig) params() (Params, error) {
1259 params := make(Params)
1260
1261 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1262 params.AddNonZero("message_id", config.MessageID)
1263
1264 return params, nil
1265}
1266
1267// PinChatMessageConfig contains information of a message in a chat to pin.
1268type PinChatMessageConfig struct {
1269 ChatID int64
1270 ChannelUsername string
1271 MessageID int
1272 DisableNotification bool
1273}
1274
1275func (config PinChatMessageConfig) method() string {
1276 return "pinChatMessage"
1277}
1278
1279func (config PinChatMessageConfig) params() (Params, error) {
1280 params := make(Params)
1281
1282 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1283 params.AddNonZero("message_id", config.MessageID)
1284 params.AddBool("disable_notification", config.DisableNotification)
1285
1286 return params, nil
1287}
1288
1289// UnpinChatMessageConfig contains information of chat to unpin.
1290type UnpinChatMessageConfig struct {
1291 ChatID int64
1292 ChannelUsername string
1293}
1294
1295func (config UnpinChatMessageConfig) method() string {
1296 return "unpinChatMessage"
1297}
1298
1299func (config UnpinChatMessageConfig) params() (Params, error) {
1300 params := make(Params)
1301
1302 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1303
1304 return params, nil
1305}
1306
1307// SetChatPhotoConfig allows you to set a group, supergroup, or channel's photo.
1308type SetChatPhotoConfig struct {
1309 BaseFile
1310}
1311
1312func (config SetChatPhotoConfig) method() string {
1313 return "setChatPhoto"
1314}
1315
1316func (config SetChatPhotoConfig) name() string {
1317 return "photo"
1318}
1319
1320// DeleteChatPhotoConfig allows you to delete a group, supergroup, or channel's photo.
1321type DeleteChatPhotoConfig struct {
1322 ChatID int64
1323 ChannelUsername string
1324}
1325
1326func (config DeleteChatPhotoConfig) method() string {
1327 return "deleteChatPhoto"
1328}
1329
1330func (config DeleteChatPhotoConfig) params() (Params, error) {
1331 params := make(Params)
1332
1333 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1334
1335 return params, nil
1336}
1337
1338// SetChatTitleConfig allows you to set the title of something other than a private chat.
1339type SetChatTitleConfig struct {
1340 ChatID int64
1341 ChannelUsername string
1342
1343 Title string
1344}
1345
1346func (config SetChatTitleConfig) method() string {
1347 return "setChatTitle"
1348}
1349
1350func (config SetChatTitleConfig) params() (Params, error) {
1351 params := make(Params)
1352
1353 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1354 params["title"] = config.Title
1355
1356 return params, nil
1357}
1358
1359// SetChatDescriptionConfig allows you to set the description of a supergroup or channel.
1360type SetChatDescriptionConfig struct {
1361 ChatID int64
1362 ChannelUsername string
1363
1364 Description string
1365}
1366
1367func (config SetChatDescriptionConfig) method() string {
1368 return "setChatDescription"
1369}
1370
1371func (config SetChatDescriptionConfig) params() (Params, error) {
1372 params := make(Params)
1373
1374 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1375 params["description"] = config.Description
1376
1377 return params, nil
1378}
1379
1380// GetStickerSetConfig allows you to get the stickers in a set.
1381type GetStickerSetConfig struct {
1382 Name string
1383}
1384
1385func (config GetStickerSetConfig) method() string {
1386 return "getStickerSet"
1387}
1388
1389func (config GetStickerSetConfig) params() (Params, error) {
1390 params := make(Params)
1391
1392 params["name"] = config.Name
1393
1394 return params, nil
1395}
1396
1397// UploadStickerConfig allows you to upload a sticker for use in a set later.
1398type UploadStickerConfig struct {
1399 UserID int64
1400 PNGSticker interface{}
1401}
1402
1403func (config UploadStickerConfig) method() string {
1404 return "uploadStickerFile"
1405}
1406
1407func (config UploadStickerConfig) params() (Params, error) {
1408 params := make(Params)
1409
1410 params.AddNonZero64("user_id", config.UserID)
1411
1412 return params, nil
1413}
1414
1415func (config UploadStickerConfig) files() []RequestFile {
1416 return []RequestFile{
1417 {
1418 Name: "png_sticker",
1419 File: config.PNGSticker,
1420 },
1421 }
1422}
1423
1424// NewStickerSetConfig allows creating a new sticker set.
1425//
1426// You must set either PNGSticker or TGSSticker.
1427type NewStickerSetConfig struct {
1428 UserID int64
1429 Name string
1430 Title string
1431 PNGSticker interface{}
1432 TGSSticker interface{}
1433 Emojis string
1434 ContainsMasks bool
1435 MaskPosition *MaskPosition
1436}
1437
1438func (config NewStickerSetConfig) method() string {
1439 return "createNewStickerSet"
1440}
1441
1442func (config NewStickerSetConfig) params() (Params, error) {
1443 params := make(Params)
1444
1445 params.AddNonZero64("user_id", config.UserID)
1446 params["name"] = config.Name
1447 params["title"] = config.Title
1448
1449 params["emojis"] = config.Emojis
1450
1451 params.AddBool("contains_masks", config.ContainsMasks)
1452
1453 err := params.AddInterface("mask_position", config.MaskPosition)
1454
1455 return params, err
1456}
1457
1458func (config NewStickerSetConfig) files() []RequestFile {
1459 if config.PNGSticker != nil {
1460 return []RequestFile{{
1461 Name: "png_sticker",
1462 File: config.PNGSticker,
1463 }}
1464 }
1465
1466 return []RequestFile{{
1467 Name: "tgs_sticker",
1468 File: config.TGSSticker,
1469 }}
1470}
1471
1472// AddStickerConfig allows you to add a sticker to a set.
1473type AddStickerConfig struct {
1474 UserID int64
1475 Name string
1476 PNGSticker interface{}
1477 TGSSticker interface{}
1478 Emojis string
1479 MaskPosition *MaskPosition
1480}
1481
1482func (config AddStickerConfig) method() string {
1483 return "addStickerToSet"
1484}
1485
1486func (config AddStickerConfig) params() (Params, error) {
1487 params := make(Params)
1488
1489 params.AddNonZero64("user_id", config.UserID)
1490 params["name"] = config.Name
1491 params["emojis"] = config.Emojis
1492
1493 err := params.AddInterface("mask_position", config.MaskPosition)
1494
1495 return params, err
1496}
1497
1498func (config AddStickerConfig) files() []RequestFile {
1499 if config.PNGSticker != nil {
1500 return []RequestFile{{
1501 Name: "png_sticker",
1502 File: config.PNGSticker,
1503 }}
1504 }
1505
1506 return []RequestFile{{
1507 Name: "tgs_sticker",
1508 File: config.TGSSticker,
1509 }}
1510
1511}
1512
1513// SetStickerPositionConfig allows you to change the position of a sticker in a set.
1514type SetStickerPositionConfig struct {
1515 Sticker string
1516 Position int
1517}
1518
1519func (config SetStickerPositionConfig) method() string {
1520 return "setStickerPositionInSet"
1521}
1522
1523func (config SetStickerPositionConfig) params() (Params, error) {
1524 params := make(Params)
1525
1526 params["sticker"] = config.Sticker
1527 params.AddNonZero("position", config.Position)
1528
1529 return params, nil
1530}
1531
1532// DeleteStickerConfig allows you to delete a sticker from a set.
1533type DeleteStickerConfig struct {
1534 Sticker string
1535}
1536
1537func (config DeleteStickerConfig) method() string {
1538 return "deleteStickerFromSet"
1539}
1540
1541func (config DeleteStickerConfig) params() (Params, error) {
1542 params := make(Params)
1543
1544 params["sticker"] = config.Sticker
1545
1546 return params, nil
1547}
1548
1549// SetStickerSetThumbConfig allows you to set the thumbnail for a sticker set.
1550type SetStickerSetThumbConfig struct {
1551 Name string
1552 UserID int
1553 Thumb interface{}
1554}
1555
1556func (config SetStickerSetThumbConfig) method() string {
1557 return "setStickerSetThumb"
1558}
1559
1560func (config SetStickerSetThumbConfig) params() (Params, error) {
1561 params := make(Params)
1562
1563 params["name"] = config.Name
1564 params.AddNonZero("user_id", config.UserID)
1565
1566 if thumb, ok := config.Thumb.(string); ok {
1567 params["thumb"] = thumb
1568 }
1569
1570 return params, nil
1571}
1572
1573func (config SetStickerSetThumbConfig) name() string {
1574 return "thumb"
1575}
1576
1577// SetChatStickerSetConfig allows you to set the sticker set for a supergroup.
1578type SetChatStickerSetConfig struct {
1579 ChatID int64
1580 SuperGroupUsername string
1581
1582 StickerSetName string
1583}
1584
1585func (config SetChatStickerSetConfig) method() string {
1586 return "setChatStickerSet"
1587}
1588
1589func (config SetChatStickerSetConfig) params() (Params, error) {
1590 params := make(Params)
1591
1592 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
1593 params["sticker_set_name"] = config.StickerSetName
1594
1595 return params, nil
1596}
1597
1598// DeleteChatStickerSetConfig allows you to remove a supergroup's sticker set.
1599type DeleteChatStickerSetConfig struct {
1600 ChatID int64
1601 SuperGroupUsername string
1602}
1603
1604func (config DeleteChatStickerSetConfig) method() string {
1605 return "deleteChatStickerSet"
1606}
1607
1608func (config DeleteChatStickerSetConfig) params() (Params, error) {
1609 params := make(Params)
1610
1611 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
1612
1613 return params, nil
1614}
1615
1616// MediaGroupConfig allows you to send a group of media.
1617//
1618// Media consist of InputMedia items (InputMediaPhoto, InputMediaVideo).
1619//
1620// Due to additional processing required, this config is not Chattable or
1621// Fileable. It must be uploaded with SendMediaGroup.
1622type MediaGroupConfig struct {
1623 ChatID int64
1624 ChannelUsername string
1625
1626 Media []interface{}
1627 DisableNotification bool
1628 ReplyToMessageID int
1629}
1630
1631func (config MediaGroupConfig) method() string {
1632 return "sendMediaGroup"
1633}
1634
1635func (config MediaGroupConfig) params() (Params, error) {
1636 params := make(Params)
1637
1638 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1639 params.AddBool("disable_notification", config.DisableNotification)
1640 params.AddNonZero("reply_to_message_id", config.ReplyToMessageID)
1641
1642 return params, nil
1643}
1644
1645// DiceConfig allows you to send a random dice roll to Telegram.
1646//
1647// Emoji may be one of the following: 🎲 (1-6), 🎯 (1-6), 🏀 (1-5).
1648type DiceConfig struct {
1649 BaseChat
1650
1651 Emoji string
1652}
1653
1654func (config DiceConfig) method() string {
1655 return "sendDice"
1656}
1657
1658func (config DiceConfig) params() (Params, error) {
1659 params, err := config.BaseChat.params()
1660 if err != nil {
1661 return params, err
1662 }
1663
1664 params.AddNonEmpty("emoji", config.Emoji)
1665
1666 return params, err
1667}
1668
1669// GetMyCommandsConfig gets a list of the currently registered commands.
1670type GetMyCommandsConfig struct{}
1671
1672func (config GetMyCommandsConfig) method() string {
1673 return "getMyCommands"
1674}
1675
1676func (config GetMyCommandsConfig) params() (Params, error) {
1677 return make(Params), nil
1678}
1679
1680// SetMyCommandsConfig sets a list of commands the bot understands.
1681type SetMyCommandsConfig struct {
1682 commands []BotCommand
1683}
1684
1685func (config SetMyCommandsConfig) method() string {
1686 return "setMyCommands"
1687}
1688
1689func (config SetMyCommandsConfig) params() (Params, error) {
1690 params := make(Params)
1691
1692 err := params.AddInterface("commands", config.commands)
1693
1694 return params, err
1695}