forked from tucnak/telebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
85 lines (73 loc) · 2.64 KB
/
commands.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package telebot
import "encoding/json"
// Command represents a bot command.
type Command struct {
// Text is a text of the command, 1-32 characters.
// Can contain only lowercase English letters, digits and underscores.
Text string `json:"command"`
// Description of the command, 3-256 characters.
Description string `json:"description"`
}
// 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"`
}
type CommandScopeType = string
const (
CommandScopeDefault CommandScopeType = "default"
CommandScopeAllPrivateChats CommandScopeType = "all_private_chats"
CommandScopeAllGroupChats CommandScopeType = "all_group_chats"
CommandScopeAllChatAdmin CommandScopeType = "all_chat_administrators"
CommandScopeChat CommandScopeType = "chat"
CommandScopeChatAdmin CommandScopeType = "chat_administrators"
CommandScopeChatMember CommandScopeType = "chat_member"
)
// CommandScope object represents a scope to which bot commands are applied.
type CommandScope struct {
Type CommandScopeType `json:"type"`
ChatID int64 `json:"chat_id,omitempty"`
UserID int64 `json:"user_id,omitempty"`
}
// 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
}
var resp struct {
Result []Command
}
if err := json.Unmarshal(data, &resp); err != nil {
return nil, wrapError(err)
}
return resp.Result, nil
}
// SetCommands changes the list of the bot's commands.
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{}) error {
params := extractCommandsParams(opts...)
_, err := b.Raw("deleteMyCommands", params)
return err
}
// 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
}