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