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 Explanation string
514 ExplanationParseMode string
515 OpenPeriod int
516 CloseDate int
517 IsClosed bool
518}
519
520func (config SendPollConfig) params() (Params, error) {
521 params, err := config.BaseChat.params()
522 if err != nil {
523 return params, err
524 }
525
526 params["question"] = config.Question
527 err = params.AddInterface("options", config.Options)
528 params["is_anonymous"] = strconv.FormatBool(config.IsAnonymous)
529 params.AddNonEmpty("type", config.Type)
530 params["allows_multiple_answers"] = strconv.FormatBool(config.AllowsMultipleAnswers)
531 params["correct_option_id"] = strconv.FormatInt(config.CorrectOptionID, 10)
532 params.AddBool("is_closed", config.IsClosed)
533 params.AddNonEmpty("explanation", config.Explanation)
534 params.AddNonEmpty("explanation_parse_mode", config.ExplanationParseMode)
535 params.AddNonZero("open_period", config.OpenPeriod)
536 params.AddNonZero("close_date", config.CloseDate)
537
538 return params, err
539}
540
541func (SendPollConfig) method() string {
542 return "sendPoll"
543}
544
545// GameConfig allows you to send a game.
546type GameConfig struct {
547 BaseChat
548 GameShortName string
549}
550
551func (config GameConfig) params() (Params, error) {
552 params, err := config.BaseChat.params()
553
554 params["game_short_name"] = config.GameShortName
555
556 return params, err
557}
558
559func (config GameConfig) method() string {
560 return "sendGame"
561}
562
563// SetGameScoreConfig allows you to update the game score in a chat.
564type SetGameScoreConfig struct {
565 UserID int
566 Score int
567 Force bool
568 DisableEditMessage bool
569 ChatID int64
570 ChannelUsername string
571 MessageID int
572 InlineMessageID string
573}
574
575func (config SetGameScoreConfig) params() (Params, error) {
576 params := make(Params)
577
578 params.AddNonZero("user_id", config.UserID)
579 params.AddNonZero("scrore", config.Score)
580 params.AddBool("disable_edit_message", config.DisableEditMessage)
581
582 if config.InlineMessageID != "" {
583 params["inline_message_id"] = config.InlineMessageID
584 } else {
585 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
586 params.AddNonZero("message_id", config.MessageID)
587 }
588
589 return params, nil
590}
591
592func (config SetGameScoreConfig) method() string {
593 return "setGameScore"
594}
595
596// GetGameHighScoresConfig allows you to fetch the high scores for a game.
597type GetGameHighScoresConfig struct {
598 UserID int
599 ChatID int
600 ChannelUsername string
601 MessageID int
602 InlineMessageID string
603}
604
605func (config GetGameHighScoresConfig) params() (Params, error) {
606 params := make(Params)
607
608 params.AddNonZero("user_id", config.UserID)
609
610 if config.InlineMessageID != "" {
611 params["inline_message_id"] = config.InlineMessageID
612 } else {
613 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
614 params.AddNonZero("message_id", config.MessageID)
615 }
616
617 return params, nil
618}
619
620func (config GetGameHighScoresConfig) method() string {
621 return "getGameHighScores"
622}
623
624// ChatActionConfig contains information about a SendChatAction request.
625type ChatActionConfig struct {
626 BaseChat
627 Action string // required
628}
629
630func (config ChatActionConfig) params() (Params, error) {
631 params, err := config.BaseChat.params()
632
633 params["action"] = config.Action
634
635 return params, err
636}
637
638func (config ChatActionConfig) method() string {
639 return "sendChatAction"
640}
641
642// EditMessageTextConfig allows you to modify the text in a message.
643type EditMessageTextConfig struct {
644 BaseEdit
645 Text string
646 ParseMode string
647 DisableWebPagePreview bool
648}
649
650func (config EditMessageTextConfig) params() (Params, error) {
651 params, err := config.BaseEdit.params()
652
653 params["text"] = config.Text
654 params.AddNonEmpty("parse_mode", config.ParseMode)
655 params.AddBool("disable_web_page_preview", config.DisableWebPagePreview)
656
657 return params, err
658}
659
660func (config EditMessageTextConfig) method() string {
661 return "editMessageText"
662}
663
664// EditMessageCaptionConfig allows you to modify the caption of a message.
665type EditMessageCaptionConfig struct {
666 BaseEdit
667 Caption string
668 ParseMode string
669}
670
671func (config EditMessageCaptionConfig) params() (Params, error) {
672 params, err := config.BaseEdit.params()
673
674 params["caption"] = config.Caption
675 params.AddNonEmpty("parse_mode", config.ParseMode)
676
677 return params, err
678}
679
680func (config EditMessageCaptionConfig) method() string {
681 return "editMessageCaption"
682}
683
684// EditMessageMediaConfig contains information about editing a message's media.
685type EditMessageMediaConfig struct {
686 BaseEdit
687
688 Media interface{}
689}
690
691func (EditMessageMediaConfig) method() string {
692 return "editMessageMedia"
693}
694
695func (config EditMessageMediaConfig) params() (Params, error) {
696 params, err := config.BaseEdit.params()
697
698 params.AddInterface("media", config.Media)
699
700 return params, err
701}
702
703// EditMessageReplyMarkupConfig allows you to modify the reply markup
704// of a message.
705type EditMessageReplyMarkupConfig struct {
706 BaseEdit
707}
708
709func (config EditMessageReplyMarkupConfig) params() (Params, error) {
710 return config.BaseEdit.params()
711}
712
713func (config EditMessageReplyMarkupConfig) method() string {
714 return "editMessageReplyMarkup"
715}
716
717// StopPollConfig allows you to stop a poll sent by the bot.
718type StopPollConfig struct {
719 BaseEdit
720}
721
722func (config StopPollConfig) params() (Params, error) {
723 return config.BaseEdit.params()
724}
725
726func (StopPollConfig) method() string {
727 return "stopPoll"
728}
729
730// UserProfilePhotosConfig contains information about a
731// GetUserProfilePhotos request.
732type UserProfilePhotosConfig struct {
733 UserID int
734 Offset int
735 Limit int
736}
737
738func (UserProfilePhotosConfig) method() string {
739 return "getUserProfilePhotos"
740}
741
742func (config UserProfilePhotosConfig) params() (Params, error) {
743 params := make(Params)
744
745 params.AddNonZero("user_id", config.UserID)
746 params.AddNonZero("offset", config.Offset)
747 params.AddNonZero("limit", config.Limit)
748
749 return params, nil
750}
751
752// FileConfig has information about a file hosted on Telegram.
753type FileConfig struct {
754 FileID string
755}
756
757func (FileConfig) method() string {
758 return "getFile"
759}
760
761func (config FileConfig) params() (Params, error) {
762 params := make(Params)
763
764 params["file_id"] = config.FileID
765
766 return params, nil
767}
768
769// UpdateConfig contains information about a GetUpdates request.
770type UpdateConfig struct {
771 Offset int
772 Limit int
773 Timeout int
774}
775
776func (UpdateConfig) method() string {
777 return "getUpdates"
778}
779
780func (config UpdateConfig) params() (Params, error) {
781 params := make(Params)
782
783 params.AddNonZero("offset", config.Offset)
784 params.AddNonZero("limit", config.Limit)
785 params.AddNonZero("timeout", config.Timeout)
786
787 return params, nil
788}
789
790// WebhookConfig contains information about a SetWebhook request.
791type WebhookConfig struct {
792 URL *url.URL
793 Certificate interface{}
794 MaxConnections int
795 AllowedUpdates []string
796}
797
798func (config WebhookConfig) method() string {
799 return "setWebhook"
800}
801
802func (config WebhookConfig) params() (Params, error) {
803 params := make(Params)
804
805 if config.URL != nil {
806 params["url"] = config.URL.String()
807 }
808
809 params.AddNonZero("max_connections", config.MaxConnections)
810 params.AddInterface("allowed_updates", config.AllowedUpdates)
811
812 return params, nil
813}
814
815func (config WebhookConfig) name() string {
816 return "certificate"
817}
818
819func (config WebhookConfig) getFile() interface{} {
820 return config.Certificate
821}
822
823func (config WebhookConfig) useExistingFile() bool {
824 return config.URL != nil
825}
826
827// RemoveWebhookConfig is a helper to remove a webhook.
828type RemoveWebhookConfig struct {
829}
830
831func (config RemoveWebhookConfig) method() string {
832 return "setWebhook"
833}
834
835func (config RemoveWebhookConfig) params() (Params, error) {
836 return nil, nil
837}
838
839// FileBytes contains information about a set of bytes to upload
840// as a File.
841type FileBytes struct {
842 Name string
843 Bytes []byte
844}
845
846// FileReader contains information about a reader to upload as a File.
847// If Size is -1, it will read the entire Reader into memory to
848// calculate a Size.
849type FileReader struct {
850 Name string
851 Reader io.Reader
852 Size int64
853}
854
855// InlineConfig contains information on making an InlineQuery response.
856type InlineConfig struct {
857 InlineQueryID string `json:"inline_query_id"`
858 Results []interface{} `json:"results"`
859 CacheTime int `json:"cache_time"`
860 IsPersonal bool `json:"is_personal"`
861 NextOffset string `json:"next_offset"`
862 SwitchPMText string `json:"switch_pm_text"`
863 SwitchPMParameter string `json:"switch_pm_parameter"`
864}
865
866func (config InlineConfig) method() string {
867 return "answerInlineQuery"
868}
869
870func (config InlineConfig) params() (Params, error) {
871 params := make(Params)
872
873 params["inline_query_id"] = config.InlineQueryID
874 params.AddNonZero("cache_time", config.CacheTime)
875 params.AddBool("is_personal", config.IsPersonal)
876 params.AddNonEmpty("next_offset", config.NextOffset)
877 params.AddNonEmpty("switch_pm_text", config.SwitchPMText)
878 params.AddNonEmpty("switch_pm_parameter", config.SwitchPMParameter)
879
880 if err := params.AddInterface("results", config.Results); err != nil {
881 return params, err
882 }
883
884 return params, nil
885}
886
887// CallbackConfig contains information on making a CallbackQuery response.
888type CallbackConfig struct {
889 CallbackQueryID string `json:"callback_query_id"`
890 Text string `json:"text"`
891 ShowAlert bool `json:"show_alert"`
892 URL string `json:"url"`
893 CacheTime int `json:"cache_time"`
894}
895
896func (config CallbackConfig) method() string {
897 return "answerCallbackQuery"
898}
899
900func (config CallbackConfig) params() (Params, error) {
901 params := make(Params)
902
903 params["callback_query_id"] = config.CallbackQueryID
904 params.AddNonEmpty("text", config.Text)
905 params.AddBool("show_alert", config.ShowAlert)
906 params.AddNonEmpty("url", config.URL)
907 params.AddNonZero("cache_time", config.CacheTime)
908
909 return params, nil
910}
911
912// ChatMemberConfig contains information about a user in a chat for use
913// with administrative functions such as kicking or unbanning a user.
914type ChatMemberConfig struct {
915 ChatID int64
916 SuperGroupUsername string
917 ChannelUsername string
918 UserID int
919}
920
921// UnbanChatMemberConfig allows you to unban a user.
922type UnbanChatMemberConfig struct {
923 ChatMemberConfig
924}
925
926func (config UnbanChatMemberConfig) method() string {
927 return "unbanChatMember"
928}
929
930func (config UnbanChatMemberConfig) params() (Params, error) {
931 params := make(Params)
932
933 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
934 params.AddNonZero("user_id", config.UserID)
935
936 return params, nil
937}
938
939// KickChatMemberConfig contains extra fields to kick user
940type KickChatMemberConfig struct {
941 ChatMemberConfig
942 UntilDate int64
943}
944
945func (config KickChatMemberConfig) method() string {
946 return "kickChatMember"
947}
948
949func (config KickChatMemberConfig) params() (Params, error) {
950 params := make(Params)
951
952 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
953 params.AddNonZero("user_id", config.UserID)
954 params.AddNonZero64("until_date", config.UntilDate)
955
956 return params, nil
957}
958
959// RestrictChatMemberConfig contains fields to restrict members of chat
960type RestrictChatMemberConfig struct {
961 ChatMemberConfig
962 UntilDate int64
963 Permissions *ChatPermissions
964}
965
966func (config RestrictChatMemberConfig) method() string {
967 return "restrictChatMember"
968}
969
970func (config RestrictChatMemberConfig) params() (Params, error) {
971 params := make(Params)
972
973 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
974 params.AddNonZero("user_id", config.UserID)
975
976 if err := params.AddInterface("permissions", config.Permissions); err != nil {
977 return params, err
978 }
979 params.AddNonZero64("until_date", config.UntilDate)
980
981 return params, nil
982}
983
984// PromoteChatMemberConfig contains fields to promote members of chat
985type PromoteChatMemberConfig struct {
986 ChatMemberConfig
987 CanChangeInfo bool
988 CanPostMessages bool
989 CanEditMessages bool
990 CanDeleteMessages bool
991 CanInviteUsers bool
992 CanRestrictMembers bool
993 CanPinMessages bool
994 CanPromoteMembers bool
995}
996
997func (config PromoteChatMemberConfig) method() string {
998 return "promoteChatMember"
999}
1000
1001func (config PromoteChatMemberConfig) params() (Params, error) {
1002 params := make(Params)
1003
1004 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
1005 params.AddNonZero("user_id", config.UserID)
1006
1007 params.AddBool("can_change_info", config.CanChangeInfo)
1008 params.AddBool("can_post_messages", config.CanPostMessages)
1009 params.AddBool("can_edit_messages", config.CanEditMessages)
1010 params.AddBool("can_delete_messages", config.CanDeleteMessages)
1011 params.AddBool("can_invite_users", config.CanInviteUsers)
1012 params.AddBool("can_restrict_members", config.CanRestrictMembers)
1013 params.AddBool("can_pin_messages", config.CanPinMessages)
1014 params.AddBool("can_promote_members", config.CanPromoteMembers)
1015
1016 return params, nil
1017}
1018
1019// SetChatAdministratorCustomTitle sets the title of an administrative user
1020// promoted by the bot for a chat.
1021type SetChatAdministratorCustomTitle struct {
1022 ChatMemberConfig
1023 CustomTitle string
1024}
1025
1026func (SetChatAdministratorCustomTitle) method() string {
1027 return "setChatAdministratorCustomTitle"
1028}
1029
1030func (config SetChatAdministratorCustomTitle) params() (Params, error) {
1031 params := make(Params)
1032
1033 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
1034 params.AddNonZero("user_id", config.UserID)
1035 params.AddNonEmpty("custom_title", config.CustomTitle)
1036
1037 return params, nil
1038}
1039
1040// ChatConfig contains information about getting information on a chat.
1041type ChatConfig struct {
1042 ChatID int64
1043 SuperGroupUsername string
1044}
1045
1046func (config ChatConfig) params() (Params, error) {
1047 params := make(Params)
1048
1049 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
1050
1051 return params, nil
1052}
1053
1054// ChatInfoConfig contains information about getting chat information.
1055type ChatInfoConfig struct {
1056 ChatConfig
1057}
1058
1059func (ChatInfoConfig) method() string {
1060 return "getChat"
1061}
1062
1063// ChatMemberCountConfig contains information about getting the number of users in a chat.
1064type ChatMemberCountConfig struct {
1065 ChatConfig
1066}
1067
1068func (ChatMemberCountConfig) method() string {
1069 return "getChatMembersCount"
1070}
1071
1072// ChatAdministratorsConfig contains information about getting chat administrators.
1073type ChatAdministratorsConfig struct {
1074 ChatConfig
1075}
1076
1077func (ChatAdministratorsConfig) method() string {
1078 return "getChatAdministrators"
1079}
1080
1081// SetChatPermissionsConfig allows you to set default permissions for the
1082// members in a group. The bot must be an administrator and have rights to
1083// restrict members.
1084type SetChatPermissionsConfig struct {
1085 ChatConfig
1086 Permissions *ChatPermissions
1087}
1088
1089func (SetChatPermissionsConfig) method() string {
1090 return "setChatPermissions"
1091}
1092
1093func (config SetChatPermissionsConfig) params() (Params, error) {
1094 params := make(Params)
1095
1096 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
1097 params.AddInterface("permissions", config.Permissions)
1098
1099 return params, nil
1100}
1101
1102// ChatInviteLinkConfig contains information about getting a chat link.
1103//
1104// Note that generating a new link will revoke any previous links.
1105type ChatInviteLinkConfig struct {
1106 ChatConfig
1107}
1108
1109func (ChatInviteLinkConfig) method() string {
1110 return "exportChatInviteLink"
1111}
1112
1113func (config ChatInviteLinkConfig) params() (Params, error) {
1114 params := make(Params)
1115
1116 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
1117
1118 return params, nil
1119}
1120
1121// LeaveChatConfig allows you to leave a chat.
1122type LeaveChatConfig struct {
1123 ChatID int64
1124 ChannelUsername string
1125}
1126
1127func (config LeaveChatConfig) method() string {
1128 return "leaveChat"
1129}
1130
1131func (config LeaveChatConfig) params() (Params, error) {
1132 params := make(Params)
1133
1134 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1135
1136 return params, nil
1137}
1138
1139// ChatConfigWithUser contains information about a chat and a user.
1140type ChatConfigWithUser struct {
1141 ChatID int64
1142 SuperGroupUsername string
1143 UserID int
1144}
1145
1146func (config ChatConfigWithUser) params() (Params, error) {
1147 params := make(Params)
1148
1149 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
1150 params.AddNonZero("user_id", config.UserID)
1151
1152 return params, nil
1153}
1154
1155// GetChatMemberConfig is information about getting a specific member in a chat.
1156type GetChatMemberConfig struct {
1157 ChatConfigWithUser
1158}
1159
1160func (GetChatMemberConfig) method() string {
1161 return "getChatMember"
1162}
1163
1164// InvoiceConfig contains information for sendInvoice request.
1165type InvoiceConfig struct {
1166 BaseChat
1167 Title string // required
1168 Description string // required
1169 Payload string // required
1170 ProviderToken string // required
1171 StartParameter string // required
1172 Currency string // required
1173 Prices []LabeledPrice // required
1174 ProviderData string
1175 PhotoURL string
1176 PhotoSize int
1177 PhotoWidth int
1178 PhotoHeight int
1179 NeedName bool
1180 NeedPhoneNumber bool
1181 NeedEmail bool
1182 NeedShippingAddress bool
1183 SendPhoneNumberToProvider bool
1184 SendEmailToProvider bool
1185 IsFlexible bool
1186}
1187
1188func (config InvoiceConfig) params() (Params, error) {
1189 params, err := config.BaseChat.params()
1190 if err != nil {
1191 return params, err
1192 }
1193
1194 params["title"] = config.Title
1195 params["description"] = config.Description
1196 params["payload"] = config.Payload
1197 params["provider_token"] = config.ProviderToken
1198 params["start_parameter"] = config.StartParameter
1199 params["currency"] = config.Currency
1200
1201 if err = params.AddInterface("prices", config.Prices); err != nil {
1202 return params, err
1203 }
1204
1205 params.AddNonEmpty("provider_data", config.ProviderData)
1206 params.AddNonEmpty("photo_url", config.PhotoURL)
1207 params.AddNonZero("photo_size", config.PhotoSize)
1208 params.AddNonZero("photo_width", config.PhotoWidth)
1209 params.AddNonZero("photo_height", config.PhotoHeight)
1210 params.AddBool("need_name", config.NeedName)
1211 params.AddBool("need_phone_number", config.NeedPhoneNumber)
1212 params.AddBool("need_email", config.NeedEmail)
1213 params.AddBool("need_shipping_address", config.NeedShippingAddress)
1214 params.AddBool("is_flexible", config.IsFlexible)
1215 params.AddBool("send_phone_number_to_provider", config.SendPhoneNumberToProvider)
1216 params.AddBool("send_email_to_provider", config.SendEmailToProvider)
1217
1218 return params, nil
1219}
1220
1221func (config InvoiceConfig) method() string {
1222 return "sendInvoice"
1223}
1224
1225// ShippingConfig contains information for answerShippingQuery request.
1226type ShippingConfig struct {
1227 ShippingQueryID string // required
1228 OK bool // required
1229 ShippingOptions []ShippingOption
1230 ErrorMessage string
1231}
1232
1233// PreCheckoutConfig conatins information for answerPreCheckoutQuery request.
1234type PreCheckoutConfig struct {
1235 PreCheckoutQueryID string // required
1236 OK bool // required
1237 ErrorMessage string
1238}
1239
1240// DeleteMessageConfig contains information of a message in a chat to delete.
1241type DeleteMessageConfig struct {
1242 ChatID int64
1243 MessageID int
1244}
1245
1246func (config DeleteMessageConfig) method() string {
1247 return "deleteMessage"
1248}
1249
1250func (config DeleteMessageConfig) params() (Params, error) {
1251 params := make(Params)
1252
1253 params.AddNonZero64("chat_id", config.ChatID)
1254 params.AddNonZero("message_id", config.MessageID)
1255
1256 return params, nil
1257}
1258
1259// PinChatMessageConfig contains information of a message in a chat to pin.
1260type PinChatMessageConfig struct {
1261 ChatID int64
1262 ChannelUsername string
1263 MessageID int
1264 DisableNotification bool
1265}
1266
1267func (config PinChatMessageConfig) method() string {
1268 return "pinChatMessage"
1269}
1270
1271func (config PinChatMessageConfig) params() (Params, error) {
1272 params := make(Params)
1273
1274 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1275 params.AddNonZero("message_id", config.MessageID)
1276 params.AddBool("disable_notification", config.DisableNotification)
1277
1278 return params, nil
1279}
1280
1281// UnpinChatMessageConfig contains information of chat to unpin.
1282type UnpinChatMessageConfig struct {
1283 ChatID int64
1284 ChannelUsername string
1285}
1286
1287func (config UnpinChatMessageConfig) method() string {
1288 return "unpinChatMessage"
1289}
1290
1291func (config UnpinChatMessageConfig) params() (Params, error) {
1292 params := make(Params)
1293
1294 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1295
1296 return params, nil
1297}
1298
1299// SetChatPhotoConfig allows you to set a group, supergroup, or channel's photo.
1300type SetChatPhotoConfig struct {
1301 BaseFile
1302}
1303
1304func (config SetChatPhotoConfig) method() string {
1305 return "setChatPhoto"
1306}
1307
1308func (config SetChatPhotoConfig) name() string {
1309 return "photo"
1310}
1311
1312func (config SetChatPhotoConfig) getFile() interface{} {
1313 return config.File
1314}
1315
1316func (config SetChatPhotoConfig) useExistingFile() bool {
1317 return config.UseExisting
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) name() string {
1416 return "png_sticker"
1417}
1418
1419func (config UploadStickerConfig) getFile() interface{} {
1420 return config.PNGSticker
1421}
1422
1423func (config UploadStickerConfig) useExistingFile() bool {
1424 return false
1425}
1426
1427// NewStickerSetConfig allows creating a new sticker set.
1428//
1429// You must set either PNGSticker or TGSSticker.
1430type NewStickerSetConfig struct {
1431 UserID int64
1432 Name string
1433 Title string
1434 PNGSticker interface{}
1435 TGSSticker interface{}
1436 Emojis string
1437 ContainsMasks bool
1438 MaskPosition *MaskPosition
1439}
1440
1441func (config NewStickerSetConfig) method() string {
1442 return "createNewStickerSet"
1443}
1444
1445func (config NewStickerSetConfig) params() (Params, error) {
1446 params := make(Params)
1447
1448 params.AddNonZero64("user_id", config.UserID)
1449 params["name"] = config.Name
1450 params["title"] = config.Title
1451
1452 if sticker, ok := config.PNGSticker.(string); ok {
1453 params[config.name()] = sticker
1454 } else if sticker, ok := config.TGSSticker.(string); ok {
1455 params[config.name()] = sticker
1456 }
1457
1458 params["emojis"] = config.Emojis
1459
1460 params.AddBool("contains_masks", config.ContainsMasks)
1461
1462 err := params.AddInterface("mask_position", config.MaskPosition)
1463
1464 return params, err
1465}
1466
1467func (config NewStickerSetConfig) getFile() interface{} {
1468 return config.PNGSticker
1469}
1470
1471func (config NewStickerSetConfig) name() string {
1472 return "png_sticker"
1473}
1474
1475func (config NewStickerSetConfig) useExistingFile() bool {
1476 if config.PNGSticker != nil {
1477 _, ok := config.PNGSticker.(string)
1478 return ok
1479 }
1480
1481 if config.TGSSticker != nil {
1482 _, ok := config.TGSSticker.(string)
1483 return ok
1484 }
1485
1486 panic("NewStickerSetConfig had nil PNGSticker and TGSSticker")
1487}
1488
1489// AddStickerConfig allows you to add a sticker to a set.
1490type AddStickerConfig struct {
1491 UserID int64
1492 Name string
1493 PNGSticker interface{}
1494 TGSSticker interface{}
1495 Emojis string
1496 MaskPosition *MaskPosition
1497}
1498
1499func (config AddStickerConfig) method() string {
1500 return "addStickerToSet"
1501}
1502
1503func (config AddStickerConfig) params() (Params, error) {
1504 params := make(Params)
1505
1506 params.AddNonZero64("user_id", config.UserID)
1507 params["name"] = config.Name
1508 params["emojis"] = config.Emojis
1509
1510 if sticker, ok := config.PNGSticker.(string); ok {
1511 params[config.name()] = sticker
1512 } else if sticker, ok := config.TGSSticker.(string); ok {
1513 params[config.name()] = sticker
1514 }
1515
1516 err := params.AddInterface("mask_position", config.MaskPosition)
1517
1518 return params, err
1519}
1520
1521func (config AddStickerConfig) name() string {
1522 return "png_sticker"
1523}
1524
1525func (config AddStickerConfig) getFile() interface{} {
1526 return config.PNGSticker
1527}
1528
1529func (config AddStickerConfig) useExistingFile() bool {
1530 return false
1531}
1532
1533// SetStickerPositionConfig allows you to change the position of a sticker in a set.
1534type SetStickerPositionConfig struct {
1535 Sticker string
1536 Position int
1537}
1538
1539func (config SetStickerPositionConfig) method() string {
1540 return "setStickerPositionInSet"
1541}
1542
1543func (config SetStickerPositionConfig) params() (Params, error) {
1544 params := make(Params)
1545
1546 params["sticker"] = config.Sticker
1547 params.AddNonZero("position", config.Position)
1548
1549 return params, nil
1550}
1551
1552// DeleteStickerConfig allows you to delete a sticker from a set.
1553type DeleteStickerConfig struct {
1554 Sticker string
1555}
1556
1557func (config DeleteStickerConfig) method() string {
1558 return "deleteStickerFromSet"
1559}
1560
1561func (config DeleteStickerConfig) params() (Params, error) {
1562 params := make(Params)
1563
1564 params["sticker"] = config.Sticker
1565
1566 return params, nil
1567}
1568
1569// SetStickerSetThumbConfig allows you to set the thumbnail for a sticker set.
1570type SetStickerSetThumbConfig struct {
1571 Name string
1572 UserID int
1573 Thumb interface{}
1574}
1575
1576func (config SetStickerSetThumbConfig) method() string {
1577 return "setStickerSetThumb"
1578}
1579
1580func (config SetStickerSetThumbConfig) params() (Params, error) {
1581 params := make(Params)
1582
1583 params["name"] = config.Name
1584 params.AddNonZero("user_id", config.UserID)
1585
1586 if thumb, ok := config.Thumb.(string); ok {
1587 params["thumb"] = thumb
1588 }
1589
1590 return params, nil
1591}
1592
1593func (config SetStickerSetThumbConfig) name() string {
1594 return "thumb"
1595}
1596
1597func (config SetStickerSetThumbConfig) getFile() interface{} {
1598 return config.Thumb
1599}
1600
1601func (config SetStickerSetThumbConfig) useExistingFile() bool {
1602 _, ok := config.Thumb.(string)
1603 return ok
1604}
1605
1606// SetChatStickerSetConfig allows you to set the sticker set for a supergroup.
1607type SetChatStickerSetConfig struct {
1608 ChatID int64
1609 SuperGroupUsername string
1610
1611 StickerSetName string
1612}
1613
1614func (config SetChatStickerSetConfig) method() string {
1615 return "setChatStickerSet"
1616}
1617
1618func (config SetChatStickerSetConfig) params() (Params, error) {
1619 params := make(Params)
1620
1621 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
1622 params["sticker_set_name"] = config.StickerSetName
1623
1624 return params, nil
1625}
1626
1627// DeleteChatStickerSetConfig allows you to remove a supergroup's sticker set.
1628type DeleteChatStickerSetConfig struct {
1629 ChatID int64
1630 SuperGroupUsername string
1631}
1632
1633func (config DeleteChatStickerSetConfig) method() string {
1634 return "deleteChatStickerSet"
1635}
1636
1637func (config DeleteChatStickerSetConfig) params() (Params, error) {
1638 params := make(Params)
1639
1640 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
1641
1642 return params, nil
1643}
1644
1645// MediaGroupConfig allows you to send a group of media.
1646//
1647// Media consist of InputMedia items (InputMediaPhoto, InputMediaVideo).
1648type MediaGroupConfig struct {
1649 ChatID int64
1650 ChannelUsername string
1651
1652 Media []interface{}
1653 DisableNotification bool
1654 ReplyToMessageID int
1655}
1656
1657func (config MediaGroupConfig) method() string {
1658 return "sendMediaGroup"
1659}
1660
1661func (config MediaGroupConfig) params() (Params, error) {
1662 params := make(Params)
1663
1664 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1665 if err := params.AddInterface("media", config.Media); err != nil {
1666 return params, nil
1667 }
1668 params.AddBool("disable_notification", config.DisableNotification)
1669 params.AddNonZero("reply_to_message_id", config.ReplyToMessageID)
1670
1671 return params, nil
1672}
1673
1674// DiceConfig allows you to send a random dice roll to Telegram.
1675type DiceConfig struct {
1676 BaseChat
1677
1678 Emoji string
1679}
1680
1681func (config DiceConfig) method() string {
1682 return "sendDice"
1683}
1684
1685func (config DiceConfig) params() (Params, error) {
1686 params, err := config.BaseChat.params()
1687 if err != nil {
1688 return params, err
1689 }
1690
1691 params.AddNonEmpty("emoji", config.Emoji)
1692
1693 return params, err
1694}
1695
1696// GetMyCommandsConfig gets a list of the currently registered commands.
1697type GetMyCommandsConfig struct{}
1698
1699func (config GetMyCommandsConfig) method() string {
1700 return "getMyCommands"
1701}
1702
1703func (config GetMyCommandsConfig) params() (Params, error) {
1704 return make(Params), nil
1705}
1706
1707// SetMyCommandsConfig sets a list of commands the bot understands.
1708type SetMyCommandsConfig struct {
1709 commands []BotCommand
1710}
1711
1712func (config SetMyCommandsConfig) method() string {
1713 return "setMyCommands"
1714}
1715
1716func (config SetMyCommandsConfig) params() (Params, error) {
1717 params := make(Params)
1718
1719 err := params.AddInterface("commands", config.commands)
1720
1721 return params, err
1722}