Implement missing parameters of commands-related methods

pull/452/head
zry98 3 years ago
parent 9a08d98f4e
commit dbe29aa9b4
No known key found for this signature in database
GPG Key ID: 2152B61B6EC3C2DD

@ -1500,9 +1500,10 @@ func (b *Bot) ChatMemberOf(chat, user Recipient) (*ChatMember, error) {
return resp.Result, nil
}
// Commands returns the current list of the bot's commands.
func (b *Bot) Commands() ([]Command, error) {
data, err := b.Raw("getMyCommands", nil)
// Commands returns the current list of the bot's commands for the given scope and user language.
func (b *Bot) Commands(opts ...interface{}) ([]Command, error) {
params := extractBotCommandsParams(opts...)
data, err := b.Raw("getMyCommands", params)
if err != nil {
return nil, err
}
@ -1517,15 +1518,27 @@ func (b *Bot) Commands() ([]Command, error) {
}
// SetCommands changes the list of the bot's commands.
func (b *Bot) SetCommands(cmds []Command) error {
data, _ := json.Marshal(cmds)
func (b *Bot) SetCommands(opts ...interface{}) error {
params := extractBotCommandsParams(opts...)
_, err := b.Raw("setMyCommands", params)
return err
}
params := map[string]string{
"commands": string(data),
// DeleteCommands deletes the list of the bot's commands for the given scope and user language.
func (b *Bot) DeleteCommands(opts ...interface{}) ([]Command, error) {
params := extractBotCommandsParams(opts...)
data, err := b.Raw("deleteMyCommands", params)
if err != nil {
return nil, err
}
_, err := b.Raw("setMyCommands", params)
return err
var resp struct {
Result []Command
}
if err := json.Unmarshal(data, &resp); err != nil {
return nil, wrapError(err)
}
return resp.Result, nil
}
// Logout logs out from the cloud Bot API server before launching the bot locally.

@ -541,5 +541,19 @@ func TestBot(t *testing.T) {
cmds, err := b.Commands()
require.NoError(t, err)
assert.Equal(t, orig, cmds)
orig2 := []Command{{
Text: "test_2",
Description: "test command 2",
}}
require.NoError(t, b.SetCommands(orig2, BotCommandScope{Type: BotCommandScopeChat, ChatID: chatID}, "en"))
cmds, err = b.Commands()
require.NoError(t, err)
assert.Equal(t, orig, cmds)
cmds, err = b.Commands(BotCommandScope{Type: BotCommandScopeChat, ChatID: chatID}, "en")
require.NoError(t, err)
assert.Equal(t, orig2, cmds)
})
}

@ -324,3 +324,28 @@ func (b Btn) Reply() *ReplyButton {
Poll: b.Poll,
}
}
// BotCommandParams controls parameters for commands-related methods (setMyCommands, deleteMyCommands and getMyCommands).
type BotCommandParams struct {
Commands []Command `json:"commands,omitempty"`
Scope *BotCommandScope `json:"scope,omitempty"`
LanguageCode string `json:"language_code,omitempty"`
}
// BotCommandScope object represents a scope to which bot commands are applied.
type BotCommandScope struct {
Type string `json:"type"`
ChatID int64 `json:"chat_id,omitempty"`
UserID int64 `json:"user_id,omitempty"`
}
const (
// BotCommandScope types
BotCommandScopeDefault = "default"
BotCommandScopeAllPrivateChats = "all_private_chats"
BotCommandScopeAllGroupChats = "all_group_chats"
BotCommandScopeAllChatAdministrators = "all_chat_administrators"
BotCommandScopeChat = "chat"
BotCommandScopeChatAdministrators = "chat_administrators"
BotScopeChatMember = "chat_member"
)

@ -274,3 +274,18 @@ func intsToStrs(ns []int) (s []string) {
}
return
}
// extractBotCommandsParams extracts parameters for commands-related methods from the given options.
func extractBotCommandsParams(opts ...interface{}) (params BotCommandParams) {
for _, opt := range opts {
switch value := opt.(type) {
case []Command:
params.Commands = value
case string:
params.LanguageCode = value
case BotCommandScope:
params.Scope = &value
}
}
return
}

Loading…
Cancel
Save