Skip to content

Commit 000cb2e

Browse files
authored
Merge pull request #460 from go-telegram-bot-api/bot-api-5.3
Updates for Bot API 5.3
2 parents fb1de2f + 1198abd commit 000cb2e

File tree

6 files changed

+176
-7
lines changed

6 files changed

+176
-7
lines changed

bot.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -668,8 +668,11 @@ func (bot *BotAPI) StopPoll(config StopPollConfig) (Poll, error) {
668668

669669
// GetMyCommands gets the currently registered commands.
670670
func (bot *BotAPI) GetMyCommands() ([]BotCommand, error) {
671-
config := GetMyCommandsConfig{}
671+
return bot.GetMyCommandsWithConfig(GetMyCommandsConfig{})
672+
}
672673

674+
// GetMyCommandsWithConfig gets the currently registered commands with a config.
675+
func (bot *BotAPI) GetMyCommandsWithConfig(config GetMyCommandsConfig) ([]BotCommand, error) {
673676
resp, err := bot.Request(config)
674677
if err != nil {
675678
return nil, err

bot_test.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ func TestSendDice(t *testing.T) {
953953
}
954954
}
955955

956-
func TestSetCommands(t *testing.T) {
956+
func TestCommands(t *testing.T) {
957957
bot, _ := getBot(t)
958958

959959
setCommands := NewSetMyCommands(BotCommand{
@@ -977,6 +977,28 @@ func TestSetCommands(t *testing.T) {
977977
if commands[0].Command != "test" || commands[0].Description != "a test command" {
978978
t.Error("Commands were incorrectly set")
979979
}
980+
981+
setCommands = NewSetMyCommandsWithScope(NewBotCommandScopeAllPrivateChats(), BotCommand{
982+
Command: "private",
983+
Description: "a private command",
984+
})
985+
986+
if _, err := bot.Request(setCommands); err != nil {
987+
t.Error("Unable to set commands")
988+
}
989+
990+
commands, err = bot.GetMyCommandsWithConfig(NewGetMyCommandsWithScope(NewBotCommandScopeAllPrivateChats()))
991+
if err != nil {
992+
t.Error("Unable to get commands")
993+
}
994+
995+
if len(commands) != 1 {
996+
t.Error("Incorrect number of commands returned")
997+
}
998+
999+
if commands[0].Command != "private" || commands[0].Description != "a private command" {
1000+
t.Error("Commands were incorrectly set")
1001+
}
9801002
}
9811003

9821004
func TestEditMessageMedia(t *testing.T) {

configs.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2062,19 +2062,29 @@ func (config DiceConfig) params() (Params, error) {
20622062
}
20632063

20642064
// GetMyCommandsConfig gets a list of the currently registered commands.
2065-
type GetMyCommandsConfig struct{}
2065+
type GetMyCommandsConfig struct {
2066+
Scope *BotCommandScope
2067+
LanguageCode string
2068+
}
20662069

20672070
func (config GetMyCommandsConfig) method() string {
20682071
return "getMyCommands"
20692072
}
20702073

20712074
func (config GetMyCommandsConfig) params() (Params, error) {
2072-
return nil, nil
2075+
params := make(Params)
2076+
2077+
err := params.AddInterface("scope", config.Scope)
2078+
params.AddNonEmpty("language_code", config.LanguageCode)
2079+
2080+
return params, err
20732081
}
20742082

20752083
// SetMyCommandsConfig sets a list of commands the bot understands.
20762084
type SetMyCommandsConfig struct {
2077-
commands []BotCommand
2085+
Commands []BotCommand
2086+
Scope *BotCommandScope
2087+
LanguageCode string
20782088
}
20792089

20802090
func (config SetMyCommandsConfig) method() string {
@@ -2084,7 +2094,29 @@ func (config SetMyCommandsConfig) method() string {
20842094
func (config SetMyCommandsConfig) params() (Params, error) {
20852095
params := make(Params)
20862096

2087-
err := params.AddInterface("commands", config.commands)
2097+
if err := params.AddInterface("commands", config.Commands); err != nil {
2098+
return params, err
2099+
}
2100+
err := params.AddInterface("scope", config.Scope)
2101+
params.AddNonEmpty("language_code", config.LanguageCode)
2102+
2103+
return params, err
2104+
}
2105+
2106+
type DeleteMyCommandsConfig struct {
2107+
Scope *BotCommandScope
2108+
LanguageCode string
2109+
}
2110+
2111+
func (config DeleteMyCommandsConfig) method() string {
2112+
return "deleteMyCommands"
2113+
}
2114+
2115+
func (config DeleteMyCommandsConfig) params() (Params, error) {
2116+
params := make(Params)
2117+
2118+
err := params.AddInterface("scope", config.Scope)
2119+
params.AddNonEmpty("language_code", config.LanguageCode)
20882120

20892121
return params, err
20902122
}

helpers.go

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,98 @@ func NewDiceWithEmoji(chatID int64, emoji string) DiceConfig {
837837
}
838838
}
839839

840+
// NewBotCommandScopeDefault represents the default scope of bot commands.
841+
func NewBotCommandScopeDefault() BotCommandScope {
842+
return BotCommandScope{Type: "default"}
843+
}
844+
845+
// NewBotCommandScopeAllPrivateChats represents the scope of bot commands,
846+
// covering all private chats.
847+
func NewBotCommandScopeAllPrivateChats() BotCommandScope {
848+
return BotCommandScope{Type: "all_private_chats"}
849+
}
850+
851+
// NewBotCommandScopeAllGroupChats represents the scope of bot commands,
852+
// covering all group and supergroup chats.
853+
func NewBotCommandScopeAllGroupChats() BotCommandScope {
854+
return BotCommandScope{Type: "all_group_chats"}
855+
}
856+
857+
// NewBotCommandScopeAllChatAdministrators represents the scope of bot commands,
858+
// covering all group and supergroup chat administrators.
859+
func NewBotCommandScopeAllChatAdministrators() BotCommandScope {
860+
return BotCommandScope{Type: "all_chat_administrators"}
861+
}
862+
863+
// NewBotCommandScopeChat represents the scope of bot commands, covering a
864+
// specific chat.
865+
func NewBotCommandScopeChat(chatID int64) BotCommandScope {
866+
return BotCommandScope{
867+
Type: "chat",
868+
ChatID: chatID,
869+
}
870+
}
871+
872+
// NewBotCommandScopeChatAdministrators represents the scope of bot commands,
873+
// covering all administrators of a specific group or supergroup chat.
874+
func NewBotCommandScopeChatAdministrators(chatID int64) BotCommandScope {
875+
return BotCommandScope{
876+
Type: "chat_administrators",
877+
ChatID: chatID,
878+
}
879+
}
880+
881+
// NewBotCommandScopeChatMember represents the scope of bot commands, covering a
882+
// specific member of a group or supergroup chat.
883+
func NewBotCommandScopeChatMember(chatID, userID int64) BotCommandScope {
884+
return BotCommandScope{
885+
Type: "chat_member",
886+
ChatID: chatID,
887+
UserID: userID,
888+
}
889+
}
890+
891+
// NewGetMyCommandsWithScope allows you to set the registered commands for a
892+
// given scope.
893+
func NewGetMyCommandsWithScope(scope BotCommandScope) GetMyCommandsConfig {
894+
return GetMyCommandsConfig{Scope: &scope}
895+
}
896+
897+
// NewGetMyCommandsWithScopeAndLanguage allows you to set the registered
898+
// commands for a given scope and language code.
899+
func NewGetMyCommandsWithScopeAndLanguage(scope BotCommandScope, languageCode string) GetMyCommandsConfig {
900+
return GetMyCommandsConfig{Scope: &scope, LanguageCode: languageCode}
901+
}
902+
840903
// NewSetMyCommands allows you to set the registered commands.
841904
func NewSetMyCommands(commands ...BotCommand) SetMyCommandsConfig {
842-
return SetMyCommandsConfig{commands: commands}
905+
return SetMyCommandsConfig{Commands: commands}
906+
}
907+
908+
// NewSetMyCommands allows you to set the registered commands for a given scope.
909+
func NewSetMyCommandsWithScope(scope BotCommandScope, commands ...BotCommand) SetMyCommandsConfig {
910+
return SetMyCommandsConfig{Commands: commands, Scope: &scope}
911+
}
912+
913+
// NewSetMyCommands allows you to set the registered commands for a given scope
914+
// and language code.
915+
func NewSetMyCommandsWithScopeAndLanguage(scope BotCommandScope, languageCode string, commands ...BotCommand) SetMyCommandsConfig {
916+
return SetMyCommandsConfig{Commands: commands, Scope: &scope, LanguageCode: languageCode}
917+
}
918+
919+
// NewDeleteMyCommands allows you to delete the registered commands.
920+
func NewDeleteMyCommands() DeleteMyCommandsConfig {
921+
return DeleteMyCommandsConfig{}
922+
}
923+
924+
// NewDeleteMyCommands allows you to delete the registered commands for a given
925+
// scope.
926+
func NewDeleteMyCommandsWithScope(scope BotCommandScope) DeleteMyCommandsConfig {
927+
return DeleteMyCommandsConfig{Scope: &scope}
928+
}
929+
930+
// NewDeleteMyCommands allows you to delete the registered commands for a given
931+
// scope and language code.
932+
func NewDeleteMyCommandsWithScopeAndLanguage(scope BotCommandScope, languageCode string) DeleteMyCommandsConfig {
933+
return DeleteMyCommandsConfig{Scope: &scope, LanguageCode: languageCode}
843934
}

types.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,11 @@ type ReplyKeyboardMarkup struct {
11581158
//
11591159
// optional
11601160
OneTimeKeyboard bool `json:"one_time_keyboard,omitempty"`
1161+
// InputFieldPlaceholder is the placeholder to be shown in the input field when
1162+
// the keyboard is active; 1-64 characters.
1163+
//
1164+
// optional
1165+
InputFieldPlaceholder string `json:"input_field_placeholder,omitempty"`
11611166
// Selective use this parameter if you want to show the keyboard to specific users only.
11621167
// Targets:
11631168
// 1) users that are @mentioned in the text of the Message object;
@@ -1375,6 +1380,11 @@ type ForceReply struct {
13751380
// ForceReply shows reply interface to the user,
13761381
// as if they manually selected the bot's message and tapped 'Reply'.
13771382
ForceReply bool `json:"force_reply"`
1383+
// InputFieldPlaceholder is the placeholder to be shown in the input field when
1384+
// the reply is active; 1-64 characters.
1385+
//
1386+
// optional
1387+
InputFieldPlaceholder string `json:"input_field_placeholder,omitempty"`
13781388
// Selective use this parameter if you want to force reply from specific users only.
13791389
// Targets:
13801390
// 1) users that are @mentioned in the text of the Message object;
@@ -1643,6 +1653,16 @@ type BotCommand struct {
16431653
Description string `json:"description"`
16441654
}
16451655

1656+
// BotCommandScope represents the scope to which bot commands are applied.
1657+
//
1658+
// It contains the fields for all types of scopes, different types only support
1659+
// specific (or no) fields.
1660+
type BotCommandScope struct {
1661+
Type string `json:"type"`
1662+
ChatID int64 `json:"chat_id,omitempty"`
1663+
UserID int64 `json:"user_id,omitempty"`
1664+
}
1665+
16461666
// ResponseParameters are various errors that can be returned in APIResponse.
16471667
type ResponseParameters struct {
16481668
// The group has been migrated to a supergroup with the specified identifier.

types_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ var (
293293
_ Chattable = DeleteChatPhotoConfig{}
294294
_ Chattable = DeleteChatStickerSetConfig{}
295295
_ Chattable = DeleteMessageConfig{}
296+
_ Chattable = DeleteMyCommandsConfig{}
296297
_ Chattable = DeleteWebhookConfig{}
297298
_ Chattable = DocumentConfig{}
298299
_ Chattable = EditChatInviteLinkConfig{}

0 commit comments

Comments
 (0)