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