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 v, err := config.BaseChat.params()
144 if err != nil {
145 return v, err
146 }
147
148 v.AddNonEmpty("text", config.Text)
149 v.AddBool("disable_web_page_preview", config.DisableWebPagePreview)
150 v.AddNonEmpty("parse_mode", config.ParseMode)
151
152 return v, 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 v, err := config.BaseChat.params()
169 if err != nil {
170 return v, err
171 }
172
173 v.AddNonZero64("from_chat_id", config.FromChatID)
174 v.AddNonZero("message_id", config.MessageID)
175
176 return v, 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 v, err := config.BaseChat.params()
220 if err != nil {
221 return v, err
222 }
223
224 v.AddNonEmpty(config.name(), config.FileID)
225 v.AddNonZero("duration", config.Duration)
226 v.AddNonEmpty("performer", config.Performer)
227 v.AddNonEmpty("title", config.Title)
228 v.AddNonEmpty("caption", config.Caption)
229 v.AddNonEmpty("parse_mode", config.ParseMode)
230
231 return v, 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 v, err := config.BaseChat.params()
274
275 v.AddNonEmpty(config.name(), config.FileID)
276
277 return v, 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}
295
296func (config VideoConfig) params() (Params, error) {
297 v, err := config.BaseChat.params()
298
299 v.AddNonEmpty(config.name(), config.FileID)
300 v.AddNonZero("duration", config.Duration)
301 v.AddNonEmpty("caption", config.Caption)
302 v.AddNonEmpty("parse_mode", config.ParseMode)
303
304 return v, err
305}
306
307func (config VideoConfig) name() string {
308 return "video"
309}
310
311func (config VideoConfig) method() string {
312 return "sendVideo"
313}
314
315// AnimationConfig contains information about a SendAnimation request.
316type AnimationConfig struct {
317 BaseFile
318 Duration int
319 Caption string
320 ParseMode string
321}
322
323func (config AnimationConfig) params() (Params, error) {
324 v, err := config.BaseChat.params()
325
326 v.AddNonEmpty(config.name(), config.FileID)
327 v.AddNonZero("duration", config.Duration)
328 v.AddNonEmpty("caption", config.Caption)
329 v.AddNonEmpty("parse_mode", config.ParseMode)
330
331 return v, err
332}
333
334func (config AnimationConfig) name() string {
335 return "animation"
336}
337
338func (config AnimationConfig) method() string {
339 return "sendAnimation"
340}
341
342// VideoNoteConfig contains information about a SendVideoNote request.
343type VideoNoteConfig struct {
344 BaseFile
345 Duration int
346 Length int
347}
348
349func (config VideoNoteConfig) params() (Params, error) {
350 v, err := config.BaseChat.params()
351
352 v.AddNonEmpty(config.name(), config.FileID)
353 v.AddNonZero("duration", config.Duration)
354 v.AddNonZero("length", config.Length)
355
356 return v, err
357}
358
359func (config VideoNoteConfig) name() string {
360 return "video_note"
361}
362
363func (config VideoNoteConfig) method() string {
364 return "sendVideoNote"
365}
366
367// VoiceConfig contains information about a SendVoice request.
368type VoiceConfig struct {
369 BaseFile
370 Caption string
371 ParseMode string
372 Duration int
373}
374
375func (config VoiceConfig) params() (Params, error) {
376 v, err := config.BaseChat.params()
377
378 v.AddNonEmpty(config.name(), config.FileID)
379 v.AddNonZero("duration", config.Duration)
380 v.AddNonEmpty("caption", config.Caption)
381 v.AddNonEmpty("parse_mode", config.ParseMode)
382
383 return v, err
384}
385
386func (config VoiceConfig) name() string {
387 return "voice"
388}
389
390func (config VoiceConfig) method() string {
391 return "sendVoice"
392}
393
394// LocationConfig contains information about a SendLocation request.
395type LocationConfig struct {
396 BaseChat
397 Latitude float64 // required
398 Longitude float64 // required
399 LivePeriod int // optional
400}
401
402func (config LocationConfig) params() (Params, error) {
403 v, err := config.BaseChat.params()
404
405 v.AddNonZeroFloat("latitude", config.Latitude)
406 v.AddNonZeroFloat("longitude", config.Longitude)
407 v.AddNonZero("live_period", config.LivePeriod)
408
409 return v, err
410}
411
412func (config LocationConfig) method() string {
413 return "sendLocation"
414}
415
416// EditMessageLiveLocationConfig allows you to update a live location.
417type EditMessageLiveLocationConfig struct {
418 BaseEdit
419 Latitude float64 // required
420 Longitude float64 // required
421}
422
423func (config EditMessageLiveLocationConfig) params() (Params, error) {
424 v, err := config.BaseEdit.params()
425
426 v.AddNonZeroFloat("latitude", config.Latitude)
427 v.AddNonZeroFloat("longitude", config.Longitude)
428
429 return v, err
430}
431
432func (config EditMessageLiveLocationConfig) method() string {
433 return "editMessageLiveLocation"
434}
435
436// StopMessageLiveLocationConfig stops updating a live location.
437type StopMessageLiveLocationConfig struct {
438 BaseEdit
439}
440
441func (config StopMessageLiveLocationConfig) params() (Params, error) {
442 return config.BaseEdit.params()
443}
444
445func (config StopMessageLiveLocationConfig) method() string {
446 return "stopMessageLiveLocation"
447}
448
449// VenueConfig contains information about a SendVenue request.
450type VenueConfig struct {
451 BaseChat
452 Latitude float64 // required
453 Longitude float64 // required
454 Title string // required
455 Address string // required
456 FoursquareID string
457}
458
459func (config VenueConfig) params() (Params, error) {
460 v, err := config.BaseChat.params()
461
462 v.AddNonZeroFloat("latitude", config.Latitude)
463 v.AddNonZeroFloat("longitude", config.Longitude)
464 v["title"] = config.Title
465 v["address"] = config.Address
466 v.AddNonEmpty("foursquare_id", config.FoursquareID)
467
468 return v, err
469}
470
471func (config VenueConfig) method() string {
472 return "sendVenue"
473}
474
475// ContactConfig allows you to send a contact.
476type ContactConfig struct {
477 BaseChat
478 PhoneNumber string
479 FirstName string
480 LastName string
481}
482
483func (config ContactConfig) params() (Params, error) {
484 v, err := config.BaseChat.params()
485
486 v["phone_number"] = config.PhoneNumber
487 v["first_name"] = config.FirstName
488 v["last_name"] = config.LastName
489
490 return v, err
491}
492
493func (config ContactConfig) method() string {
494 return "sendContact"
495}
496
497// GameConfig allows you to send a game.
498type GameConfig struct {
499 BaseChat
500 GameShortName string
501}
502
503func (config GameConfig) params() (Params, error) {
504 v, err := config.BaseChat.params()
505
506 v["game_short_name"] = config.GameShortName
507
508 return v, err
509}
510
511func (config GameConfig) method() string {
512 return "sendGame"
513}
514
515// SetGameScoreConfig allows you to update the game score in a chat.
516type SetGameScoreConfig struct {
517 UserID int
518 Score int
519 Force bool
520 DisableEditMessage bool
521 ChatID int64
522 ChannelUsername string
523 MessageID int
524 InlineMessageID string
525}
526
527func (config SetGameScoreConfig) params() (Params, error) {
528 params := make(Params)
529
530 params.AddNonZero("user_id", config.UserID)
531 params.AddNonZero("scrore", config.Score)
532 params.AddBool("disable_edit_message", config.DisableEditMessage)
533
534 if config.InlineMessageID != "" {
535 params["inline_message_id"] = config.InlineMessageID
536 } else {
537 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
538 params.AddNonZero("message_id", config.MessageID)
539 }
540
541 return params, nil
542}
543
544func (config SetGameScoreConfig) method() string {
545 return "setGameScore"
546}
547
548// GetGameHighScoresConfig allows you to fetch the high scores for a game.
549type GetGameHighScoresConfig struct {
550 UserID int
551 ChatID int
552 ChannelUsername string
553 MessageID int
554 InlineMessageID string
555}
556
557func (config GetGameHighScoresConfig) params() (Params, error) {
558 params := make(Params)
559
560 params.AddNonZero("user_id", config.UserID)
561
562 if config.InlineMessageID != "" {
563 params["inline_message_id"] = config.InlineMessageID
564 } else {
565 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
566 params.AddNonZero("message_id", config.MessageID)
567 }
568
569 return params, nil
570}
571
572func (config GetGameHighScoresConfig) method() string {
573 return "getGameHighScores"
574}
575
576// ChatActionConfig contains information about a SendChatAction request.
577type ChatActionConfig struct {
578 BaseChat
579 Action string // required
580}
581
582func (config ChatActionConfig) params() (Params, error) {
583 v, err := config.BaseChat.params()
584
585 v["action"] = config.Action
586
587 return v, err
588}
589
590func (config ChatActionConfig) method() string {
591 return "sendChatAction"
592}
593
594// EditMessageTextConfig allows you to modify the text in a message.
595type EditMessageTextConfig struct {
596 BaseEdit
597 Text string
598 ParseMode string
599 DisableWebPagePreview bool
600}
601
602func (config EditMessageTextConfig) params() (Params, error) {
603 v, err := config.BaseEdit.params()
604
605 v["text"] = config.Text
606 v.AddNonEmpty("parse_mode", config.ParseMode)
607 v.AddBool("disable_web_page_preview", config.DisableWebPagePreview)
608
609 return v, err
610}
611
612func (config EditMessageTextConfig) method() string {
613 return "editMessageText"
614}
615
616// EditMessageCaptionConfig allows you to modify the caption of a message.
617type EditMessageCaptionConfig struct {
618 BaseEdit
619 Caption string
620 ParseMode string
621}
622
623func (config EditMessageCaptionConfig) params() (Params, error) {
624 v, err := config.BaseEdit.params()
625
626 v["caption"] = config.Caption
627 v.AddNonEmpty("parse_mode", config.ParseMode)
628
629 return v, err
630}
631
632func (config EditMessageCaptionConfig) method() string {
633 return "editMessageCaption"
634}
635
636// EditMessageReplyMarkupConfig allows you to modify the reply markup
637// of a message.
638type EditMessageReplyMarkupConfig struct {
639 BaseEdit
640}
641
642func (config EditMessageReplyMarkupConfig) params() (Params, error) {
643 return config.BaseEdit.params()
644}
645
646func (config EditMessageReplyMarkupConfig) method() string {
647 return "editMessageReplyMarkup"
648}
649
650// UserProfilePhotosConfig contains information about a
651// GetUserProfilePhotos request.
652type UserProfilePhotosConfig struct {
653 UserID int
654 Offset int
655 Limit int
656}
657
658func (UserProfilePhotosConfig) method() string {
659 return "getUserProfilePhotos"
660}
661
662func (config UserProfilePhotosConfig) params() (Params, error) {
663 params := make(Params)
664
665 params.AddNonZero("user_id", config.UserID)
666 params.AddNonZero("offset", config.Offset)
667 params.AddNonZero("limit", config.Limit)
668
669 return params, nil
670}
671
672// FileConfig has information about a file hosted on Telegram.
673type FileConfig struct {
674 FileID string
675}
676
677// UpdateConfig contains information about a GetUpdates request.
678type UpdateConfig struct {
679 Offset int
680 Limit int
681 Timeout int
682}
683
684func (UpdateConfig) method() string {
685 return "getUpdates"
686}
687
688func (config UpdateConfig) params() (Params, error) {
689 params := make(Params)
690
691 params.AddNonZero("offset", config.Offset)
692 params.AddNonZero("limit", config.Limit)
693 params.AddNonZero("timeout", config.Timeout)
694
695 return params, nil
696}
697
698// WebhookConfig contains information about a SetWebhook request.
699type WebhookConfig struct {
700 URL *url.URL
701 Certificate interface{}
702 MaxConnections int
703}
704
705func (config WebhookConfig) method() string {
706 return "setWebhook"
707}
708
709func (config WebhookConfig) params() (Params, error) {
710 params := make(Params)
711
712 if config.URL != nil {
713 params["url"] = config.URL.String()
714 }
715
716 params.AddNonZero("max_connections", config.MaxConnections)
717
718 return params, nil
719}
720
721func (config WebhookConfig) name() string {
722 return "certificate"
723}
724
725func (config WebhookConfig) getFile() interface{} {
726 return config.Certificate
727}
728
729func (config WebhookConfig) useExistingFile() bool {
730 return config.URL != nil
731}
732
733// RemoveWebhookConfig is a helper to remove a webhook.
734type RemoveWebhookConfig struct {
735}
736
737func (config RemoveWebhookConfig) method() string {
738 return "setWebhook"
739}
740
741func (config RemoveWebhookConfig) params() (Params, error) {
742 return nil, nil
743}
744
745// FileBytes contains information about a set of bytes to upload
746// as a File.
747type FileBytes struct {
748 Name string
749 Bytes []byte
750}
751
752// FileReader contains information about a reader to upload as a File.
753// If Size is -1, it will read the entire Reader into memory to
754// calculate a Size.
755type FileReader struct {
756 Name string
757 Reader io.Reader
758 Size int64
759}
760
761// InlineConfig contains information on making an InlineQuery response.
762type InlineConfig struct {
763 InlineQueryID string `json:"inline_query_id"`
764 Results []interface{} `json:"results"`
765 CacheTime int `json:"cache_time"`
766 IsPersonal bool `json:"is_personal"`
767 NextOffset string `json:"next_offset"`
768 SwitchPMText string `json:"switch_pm_text"`
769 SwitchPMParameter string `json:"switch_pm_parameter"`
770}
771
772func (config InlineConfig) method() string {
773 return "answerInlineQuery"
774}
775
776func (config InlineConfig) params() (Params, error) {
777 params := make(Params)
778
779 params["inline_query_id"] = config.InlineQueryID
780 params.AddNonZero("cache_time", config.CacheTime)
781 params.AddBool("is_personal", config.IsPersonal)
782 params.AddNonEmpty("next_offset", config.NextOffset)
783 params.AddNonEmpty("switch_pm_text", config.SwitchPMText)
784 params.AddNonEmpty("switch_pm_parameter", config.SwitchPMParameter)
785
786 if err := params.AddInterface("results", config.Results); err != nil {
787 return params, err
788 }
789
790 return params, nil
791}
792
793// CallbackConfig contains information on making a CallbackQuery response.
794type CallbackConfig struct {
795 CallbackQueryID string `json:"callback_query_id"`
796 Text string `json:"text"`
797 ShowAlert bool `json:"show_alert"`
798 URL string `json:"url"`
799 CacheTime int `json:"cache_time"`
800}
801
802func (config CallbackConfig) method() string {
803 return "answerCallbackQuery"
804}
805
806func (config CallbackConfig) params() (Params, error) {
807 params := make(Params)
808
809 params["callback_query_id"] = config.CallbackQueryID
810 params.AddNonEmpty("text", config.Text)
811 params.AddBool("show_alert", config.ShowAlert)
812 params.AddNonEmpty("url", config.URL)
813 params.AddNonZero("cache_time", config.CacheTime)
814
815 return params, nil
816}
817
818// ChatMemberConfig contains information about a user in a chat for use
819// with administrative functions such as kicking or unbanning a user.
820type ChatMemberConfig struct {
821 ChatID int64
822 SuperGroupUsername string
823 ChannelUsername string
824 UserID int
825}
826
827// UnbanChatMemberConfig allows you to unban a user.
828type UnbanChatMemberConfig struct {
829 ChatMemberConfig
830}
831
832func (config UnbanChatMemberConfig) method() string {
833 return "unbanChatMember"
834}
835
836func (config UnbanChatMemberConfig) params() (Params, error) {
837 params := make(Params)
838
839 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
840 params.AddNonZero("user_id", config.UserID)
841
842 return params, nil
843}
844
845// KickChatMemberConfig contains extra fields to kick user
846type KickChatMemberConfig struct {
847 ChatMemberConfig
848 UntilDate int64
849}
850
851func (config KickChatMemberConfig) method() string {
852 return "kickChatMember"
853}
854
855func (config KickChatMemberConfig) params() (Params, error) {
856 params := make(Params)
857
858 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
859 params.AddNonZero("user_id", config.UserID)
860 params.AddNonZero64("until_date", config.UntilDate)
861
862 return params, nil
863}
864
865// RestrictChatMemberConfig contains fields to restrict members of chat
866type RestrictChatMemberConfig struct {
867 ChatMemberConfig
868 UntilDate int64
869 CanSendMessages *bool
870 CanSendMediaMessages *bool
871 CanSendOtherMessages *bool
872 CanAddWebPagePreviews *bool
873}
874
875func (config RestrictChatMemberConfig) method() string {
876 return "restrictChatMember"
877}
878
879func (config RestrictChatMemberConfig) params() (Params, error) {
880 params := make(Params)
881
882 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
883 params.AddNonZero("user_id", config.UserID)
884
885 params.AddNonNilBool("can_send_messages", config.CanSendMessages)
886 params.AddNonNilBool("can_send_media_messages", config.CanSendMediaMessages)
887 params.AddNonNilBool("can_send_other_messages", config.CanSendOtherMessages)
888 params.AddNonNilBool("can_add_web_page_previews", config.CanAddWebPagePreviews)
889 params.AddNonZero64("until_date", config.UntilDate)
890
891 return params, nil
892}
893
894// PromoteChatMemberConfig contains fields to promote members of chat
895type PromoteChatMemberConfig struct {
896 ChatMemberConfig
897 CanChangeInfo *bool
898 CanPostMessages *bool
899 CanEditMessages *bool
900 CanDeleteMessages *bool
901 CanInviteUsers *bool
902 CanRestrictMembers *bool
903 CanPinMessages *bool
904 CanPromoteMembers *bool
905}
906
907func (config PromoteChatMemberConfig) method() string {
908 return "promoteChatMember"
909}
910
911func (config PromoteChatMemberConfig) params() (Params, error) {
912 params := make(Params)
913
914 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername, config.ChannelUsername)
915 params.AddNonZero("user_id", config.UserID)
916
917 params.AddNonNilBool("can_change_info", config.CanChangeInfo)
918 params.AddNonNilBool("can_post_messages", config.CanPostMessages)
919 params.AddNonNilBool("can_edit_messages", config.CanEditMessages)
920 params.AddNonNilBool("can_delete_messages", config.CanDeleteMessages)
921 params.AddNonNilBool("can_invite_users", config.CanInviteUsers)
922 params.AddNonNilBool("can_restrict_members", config.CanRestrictMembers)
923 params.AddNonNilBool("can_pin_messages", config.CanPinMessages)
924 params.AddNonNilBool("can_promote_members", config.CanPromoteMembers)
925
926 return params, nil
927}
928
929// ChatConfig contains information about getting information on a chat.
930type ChatConfig struct {
931 ChatID int64
932 SuperGroupUsername string
933}
934
935// LeaveChatConfig allows you to leave a chat.
936type LeaveChatConfig struct {
937 ChatID int64
938 ChannelUsername string
939}
940
941func (config LeaveChatConfig) method() string {
942 return "leaveChat"
943}
944
945func (config LeaveChatConfig) params() (Params, error) {
946 params := make(Params)
947
948 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
949
950 return params, nil
951}
952
953// ChatConfigWithUser contains information about getting information on
954// a specific user within a chat.
955type ChatConfigWithUser struct {
956 ChatID int64
957 SuperGroupUsername string
958 UserID int
959}
960
961// InvoiceConfig contains information for sendInvoice request.
962type InvoiceConfig struct {
963 BaseChat
964 Title string // required
965 Description string // required
966 Payload string // required
967 ProviderToken string // required
968 StartParameter string // required
969 Currency string // required
970 Prices *[]LabeledPrice // required
971 ProviderData string
972 PhotoURL string
973 PhotoSize int
974 PhotoWidth int
975 PhotoHeight int
976 NeedName bool
977 NeedPhoneNumber bool
978 NeedEmail bool
979 NeedShippingAddress bool
980 IsFlexible bool
981}
982
983func (config InvoiceConfig) params() (Params, error) {
984 v, err := config.BaseChat.params()
985 if err != nil {
986 return v, err
987 }
988
989 v["title"] = config.Title
990 v["description"] = config.Description
991 v["payload"] = config.Payload
992 v["provider_token"] = config.ProviderToken
993 v["start_parameter"] = config.StartParameter
994 v["currency"] = config.Currency
995
996 if err = v.AddInterface("prices", config.Prices); err != nil {
997 return v, err
998 }
999
1000 v.AddNonEmpty("provider_data", config.ProviderData)
1001 v.AddNonEmpty("photo_url", config.PhotoURL)
1002 v.AddNonZero("photo_size", config.PhotoSize)
1003 v.AddNonZero("photo_width", config.PhotoWidth)
1004 v.AddNonZero("photo_height", config.PhotoHeight)
1005 v.AddBool("need_name", config.NeedName)
1006 v.AddBool("need_phone_number", config.NeedPhoneNumber)
1007 v.AddBool("need_email", config.NeedEmail)
1008 v.AddBool("need_shipping_address", config.NeedShippingAddress)
1009 v.AddBool("is_flexible", config.IsFlexible)
1010
1011 return v, nil
1012}
1013
1014func (config InvoiceConfig) method() string {
1015 return "sendInvoice"
1016}
1017
1018// ShippingConfig contains information for answerShippingQuery request.
1019type ShippingConfig struct {
1020 ShippingQueryID string // required
1021 OK bool // required
1022 ShippingOptions *[]ShippingOption
1023 ErrorMessage string
1024}
1025
1026// PreCheckoutConfig conatins information for answerPreCheckoutQuery request.
1027type PreCheckoutConfig struct {
1028 PreCheckoutQueryID string // required
1029 OK bool // required
1030 ErrorMessage string
1031}
1032
1033// DeleteMessageConfig contains information of a message in a chat to delete.
1034type DeleteMessageConfig struct {
1035 ChatID int64
1036 MessageID int
1037}
1038
1039func (config DeleteMessageConfig) method() string {
1040 return "deleteMessage"
1041}
1042
1043func (config DeleteMessageConfig) params() (Params, error) {
1044 params := make(Params)
1045
1046 params.AddNonZero64("chat_id", config.ChatID)
1047 params.AddNonZero("message_id", config.MessageID)
1048
1049 return params, nil
1050}
1051
1052// PinChatMessageConfig contains information of a message in a chat to pin.
1053type PinChatMessageConfig struct {
1054 ChatID int64
1055 ChannelUsername string
1056 MessageID int
1057 DisableNotification bool
1058}
1059
1060func (config PinChatMessageConfig) method() string {
1061 return "pinChatMessage"
1062}
1063
1064func (config PinChatMessageConfig) params() (Params, error) {
1065 params := make(Params)
1066
1067 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1068 params.AddNonZero("message_id", config.MessageID)
1069 params.AddBool("disable_notification", config.DisableNotification)
1070
1071 return params, nil
1072}
1073
1074// UnpinChatMessageConfig contains information of chat to unpin.
1075type UnpinChatMessageConfig struct {
1076 ChatID int64
1077 ChannelUsername string
1078}
1079
1080func (config UnpinChatMessageConfig) method() string {
1081 return "unpinChatMessage"
1082}
1083
1084func (config UnpinChatMessageConfig) params() (Params, error) {
1085 params := make(Params)
1086
1087 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1088
1089 return params, nil
1090}
1091
1092// SetChatPhotoConfig allows you to set a group, supergroup, or channel's photo.
1093type SetChatPhotoConfig struct {
1094 BaseFile
1095}
1096
1097func (config SetChatPhotoConfig) method() string {
1098 return "setChatPhoto"
1099}
1100
1101func (config SetChatPhotoConfig) name() string {
1102 return "photo"
1103}
1104
1105func (config SetChatPhotoConfig) getFile() interface{} {
1106 return config.File
1107}
1108
1109func (config SetChatPhotoConfig) useExistingFile() bool {
1110 return config.UseExisting
1111}
1112
1113// DeleteChatPhotoConfig allows you to delete a group, supergroup, or channel's photo.
1114type DeleteChatPhotoConfig struct {
1115 ChatID int64
1116 ChannelUsername string
1117}
1118
1119func (config DeleteChatPhotoConfig) method() string {
1120 return "deleteChatPhoto"
1121}
1122
1123func (config DeleteChatPhotoConfig) 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// SetChatTitleConfig allows you to set the title of something other than a private chat.
1132type SetChatTitleConfig struct {
1133 ChatID int64
1134 ChannelUsername string
1135
1136 Title string
1137}
1138
1139func (config SetChatTitleConfig) method() string {
1140 return "setChatTitle"
1141}
1142
1143func (config SetChatTitleConfig) params() (Params, error) {
1144 params := make(Params)
1145
1146 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1147 params["title"] = config.Title
1148
1149 return params, nil
1150}
1151
1152// SetChatDescriptionConfig allows you to set the description of a supergroup or channel.
1153type SetChatDescriptionConfig struct {
1154 ChatID int64
1155 ChannelUsername string
1156
1157 Description string
1158}
1159
1160func (config SetChatDescriptionConfig) method() string {
1161 return "setChatDescription"
1162}
1163
1164func (config SetChatDescriptionConfig) params() (Params, error) {
1165 params := make(Params)
1166
1167 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1168 params["description"] = config.Description
1169
1170 return params, nil
1171}
1172
1173// GetStickerSetConfig allows you to get the stickers in a set.
1174type GetStickerSetConfig struct {
1175 Name string
1176}
1177
1178func (config GetStickerSetConfig) method() string {
1179 return "getStickerSet"
1180}
1181
1182func (config GetStickerSetConfig) params() (Params, error) {
1183 params := make(Params)
1184
1185 params["name"] = config.Name
1186
1187 return params, nil
1188}
1189
1190// UploadStickerConfig allows you to upload a sticker for use in a set later.
1191type UploadStickerConfig struct {
1192 UserID int64
1193 PNGSticker interface{}
1194}
1195
1196func (config UploadStickerConfig) method() string {
1197 return "uploadStickerFile"
1198}
1199
1200func (config UploadStickerConfig) params() (Params, error) {
1201 params := make(Params)
1202
1203 params.AddNonZero64("user_id", config.UserID)
1204
1205 return params, nil
1206}
1207
1208func (config UploadStickerConfig) name() string {
1209 return "png_sticker"
1210}
1211
1212func (config UploadStickerConfig) getFile() interface{} {
1213 return config.PNGSticker
1214}
1215
1216func (config UploadStickerConfig) useExistingFile() bool {
1217 return false
1218}
1219
1220// NewStickerSetConfig allows creating a new sticker set.
1221type NewStickerSetConfig struct {
1222 UserID int64
1223 Name string
1224 Title string
1225 PNGSticker interface{}
1226 Emojis string
1227 ContainsMasks bool
1228 MaskPosition *MaskPosition
1229}
1230
1231func (config NewStickerSetConfig) method() string {
1232 return "createNewStickerSet"
1233}
1234
1235func (config NewStickerSetConfig) params() (Params, error) {
1236 params := make(Params)
1237
1238 params.AddNonZero64("user_id", config.UserID)
1239 params["name"] = config.Name
1240 params["title"] = config.Title
1241
1242 if sticker, ok := config.PNGSticker.(string); ok {
1243 params[config.name()] = sticker
1244 }
1245
1246 params["emojis"] = config.Emojis
1247
1248 params.AddBool("contains_masks", config.ContainsMasks)
1249
1250 err := params.AddInterface("mask_position", config.MaskPosition)
1251
1252 return params, err
1253}
1254
1255func (config NewStickerSetConfig) getFile() interface{} {
1256 return config.PNGSticker
1257}
1258
1259func (config NewStickerSetConfig) name() string {
1260 return "png_sticker"
1261}
1262
1263func (config NewStickerSetConfig) useExistingFile() bool {
1264 _, ok := config.PNGSticker.(string)
1265
1266 return ok
1267}
1268
1269// AddStickerConfig allows you to add a sticker to a set.
1270type AddStickerConfig struct {
1271 UserID int64
1272 Name string
1273 PNGSticker interface{}
1274 Emojis string
1275 MaskPosition *MaskPosition
1276}
1277
1278func (config AddStickerConfig) method() string {
1279 return "addStickerToSet"
1280}
1281
1282func (config AddStickerConfig) params() (Params, error) {
1283 params := make(Params)
1284
1285 params.AddNonZero64("user_id", config.UserID)
1286 params["name"] = config.Name
1287 params["emojis"] = config.Emojis
1288
1289 if sticker, ok := config.PNGSticker.(string); ok {
1290 params[config.name()] = sticker
1291 }
1292
1293 err := params.AddInterface("mask_position", config.MaskPosition)
1294
1295 return params, err
1296}
1297
1298func (config AddStickerConfig) name() string {
1299 return "png_sticker"
1300}
1301
1302func (config AddStickerConfig) getFile() interface{} {
1303 return config.PNGSticker
1304}
1305
1306func (config AddStickerConfig) useExistingFile() bool {
1307 return false
1308}
1309
1310// SetStickerPositionConfig allows you to change the position of a sticker in a set.
1311type SetStickerPositionConfig struct {
1312 Sticker string
1313 Position int
1314}
1315
1316func (config SetStickerPositionConfig) method() string {
1317 return "setStickerPositionInSet"
1318}
1319
1320func (config SetStickerPositionConfig) params() (Params, error) {
1321 params := make(Params)
1322
1323 params["sticker"] = config.Sticker
1324 params.AddNonZero("position", config.Position)
1325
1326 return params, nil
1327}
1328
1329// DeleteStickerConfig allows you to delete a sticker from a set.
1330type DeleteStickerConfig struct {
1331 Sticker string
1332}
1333
1334func (config DeleteStickerConfig) method() string {
1335 return "deleteStickerFromSet"
1336}
1337
1338func (config DeleteStickerConfig) params() (Params, error) {
1339 params := make(Params)
1340
1341 params["sticker"] = config.Sticker
1342
1343 return params, nil
1344}
1345
1346// SetChatStickerSetConfig allows you to set the sticker set for a supergroup.
1347type SetChatStickerSetConfig struct {
1348 ChatID int64
1349 SuperGroupUsername string
1350
1351 StickerSetName string
1352}
1353
1354func (config SetChatStickerSetConfig) method() string {
1355 return "setChatStickerSet"
1356}
1357
1358func (config SetChatStickerSetConfig) params() (Params, error) {
1359 params := make(Params)
1360
1361 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
1362 params["sticker_set_name"] = config.StickerSetName
1363
1364 return params, nil
1365}
1366
1367// DeleteChatStickerSetConfig allows you to remove a supergroup's sticker set.
1368type DeleteChatStickerSetConfig struct {
1369 ChatID int64
1370 SuperGroupUsername string
1371}
1372
1373func (config DeleteChatStickerSetConfig) method() string {
1374 return "deleteChatStickerSet"
1375}
1376
1377func (config DeleteChatStickerSetConfig) params() (Params, error) {
1378 params := make(Params)
1379
1380 params.AddFirstValid("chat_id", config.ChatID, config.SuperGroupUsername)
1381
1382 return params, nil
1383}
1384
1385// MediaGroupConfig allows you to send a group of media.
1386//
1387// Media consist of InputMedia items (InputMediaPhoto, InputMediaVideo).
1388type MediaGroupConfig struct {
1389 ChatID int64
1390 ChannelUsername string
1391
1392 Media []interface{}
1393 DisableNotification bool
1394 ReplyToMessageID int
1395}
1396
1397func (config MediaGroupConfig) method() string {
1398 return "sendMediaGroup"
1399}
1400
1401func (config MediaGroupConfig) params() (Params, error) {
1402 params := make(Params)
1403
1404 params.AddFirstValid("chat_id", config.ChatID, config.ChannelUsername)
1405 if err := params.AddInterface("media", config.Media); err != nil {
1406 return params, nil
1407 }
1408 params.AddBool("disable_notification", config.DisableNotification)
1409 params.AddNonZero("reply_to_message_id", config.ReplyToMessageID)
1410
1411 return params, nil
1412}