mirror of
https://github.com/tucnak/telebot
synced 2024-11-05 06:00:58 +00:00
Merge pull request #452 from zry98/v3
Implement missing parameters of commands-related methods
This commit is contained in:
commit
2aedc24dce
33
bot.go
33
bot.go
@ -1440,9 +1440,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 := extractCommandsParams(opts...)
|
||||
data, err := b.Raw("getMyCommands", params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -1457,17 +1458,29 @@ 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)
|
||||
|
||||
params := map[string]string{
|
||||
"commands": string(data),
|
||||
}
|
||||
|
||||
func (b *Bot) SetCommands(opts ...interface{}) error {
|
||||
params := extractCommandsParams(opts...)
|
||||
_, err := b.Raw("setMyCommands", params)
|
||||
return err
|
||||
}
|
||||
|
||||
// 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 := extractCommandsParams(opts...)
|
||||
data, err := b.Raw("deleteMyCommands", params)
|
||||
if err != nil {
|
||||
return nil, 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.
|
||||
func (b *Bot) Logout() (bool, error) {
|
||||
data, err := b.Raw("logOut", nil)
|
||||
|
14
bot_test.go
14
bot_test.go
@ -582,6 +582,20 @@ 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, CommandScope{Type: CommandScopeChat, ChatID: chatID}, "en"))
|
||||
|
||||
cmds, err = b.Commands()
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, orig, cmds)
|
||||
|
||||
cmds, err = b.Commands(CommandScope{Type: CommandScopeChat, ChatID: chatID}, "en")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, orig2, cmds)
|
||||
})
|
||||
|
||||
t.Run("CreateInviteLink", func(t *testing.T) {
|
||||
|
25
options.go
25
options.go
@ -319,3 +319,28 @@ func (b Btn) Reply() *ReplyButton {
|
||||
Poll: b.Poll,
|
||||
}
|
||||
}
|
||||
|
||||
// CommandParams controls parameters for commands-related methods (setMyCommands, deleteMyCommands and getMyCommands).
|
||||
type CommandParams struct {
|
||||
Commands []Command `json:"commands,omitempty"`
|
||||
Scope *CommandScope `json:"scope,omitempty"`
|
||||
LanguageCode string `json:"language_code,omitempty"`
|
||||
}
|
||||
|
||||
// CommandScope object represents a scope to which bot commands are applied.
|
||||
type CommandScope struct {
|
||||
Type string `json:"type"`
|
||||
ChatID int64 `json:"chat_id,omitempty"`
|
||||
UserID int64 `json:"user_id,omitempty"`
|
||||
}
|
||||
|
||||
// CommandScope types
|
||||
const (
|
||||
CommandScopeDefault = "default"
|
||||
CommandScopeAllPrivateChats = "all_private_chats"
|
||||
CommandScopeAllGroupChats = "all_group_chats"
|
||||
CommandScopeAllChatAdmin = "all_chat_administrators"
|
||||
CommandScopeChat = "chat"
|
||||
CommandScopeChatAdmin = "chat_administrators"
|
||||
CommandScopeChatMember = "chat_member"
|
||||
)
|
||||
|
15
util.go
15
util.go
@ -270,3 +270,18 @@ func intsToStrs(ns []int) (s []string) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// extractCommandsParams extracts parameters for commands-related methods from the given options.
|
||||
func extractCommandsParams(opts ...interface{}) (params CommandParams) {
|
||||
for _, opt := range opts {
|
||||
switch value := opt.(type) {
|
||||
case []Command:
|
||||
params.Commands = value
|
||||
case string:
|
||||
params.LanguageCode = value
|
||||
case CommandScope:
|
||||
params.Scope = &value
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user