-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
171 lines (143 loc) · 4.09 KB
/
command.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package tgbot
import (
"fmt"
)
const (
ScopeTypeDefault = "default"
ScopeTypeAllPrivateChats = "all_private_chats"
ScopeTypeAllGroupChats = "all_group_chats"
ScopeTypeAllChatAdministrators = "all_chat_administrators"
ScopeTypeChat = "chat"
ScopeTypeChatAdministrators = "chat_administrators"
ScopeTypeChatMember = "chat_member"
)
var noScope = &commandScope{}
// CommandScope is command scope for telegram.
type CommandScope interface {
Type() string
ChatID() int64
UserID() int64
LanguageCode() string
}
// commandScope represent a telegram command scope.
type commandScope struct {
typ string
chatID int64
userID int64
languageCode string
}
func (c commandScope) Type() string {
return c.typ
}
func (c commandScope) ChatID() int64 {
return c.chatID
}
func (c commandScope) UserID() int64 {
return c.userID
}
func (c commandScope) LanguageCode() string {
return c.languageCode
}
// Command is telegram command.
type Command struct {
Name string
Description string
Handler Handler
hide bool // hide the command on telegram commands menu.
scopes []CommandScope
}
type CommandOption func(cmd *Command)
func WithHide(v bool) CommandOption {
return func(cmd *Command) {
cmd.hide = v
}
}
func WithScopes(scopes ...CommandScope) CommandOption {
return func(cmd *Command) {
cmd.scopes = make([]CommandScope, 0, len(scopes))
scopeSet := make(map[CommandScope]struct{}, len(scopes))
for _, scope := range scopes {
if _, ok := scopeSet[scope]; ok {
continue
}
scopeSet[scope] = struct{}{}
cmd.scopes = append(cmd.scopes, scope)
}
}
}
func NewCommand(name, desc string, handler Handler, opts ...CommandOption) *Command {
cmd := &Command{
Name: name,
Description: desc,
Handler: handler,
}
for _, opt := range opts {
opt(cmd)
}
return cmd
}
func (c Command) String() string {
return fmt.Sprintf("/%s - %s", c.Name, c.Description)
}
func (c *Command) Hide() bool {
return c.hide
}
func (c *Command) Scopes() []CommandScope {
return c.scopes
}
func CommandScopeNoScope() CommandScope {
return noScope
}
func lang(lc ...string) string {
if len(lc) > 0 {
return lc[0]
}
return ""
}
// CommandScopeDefault represents the default scope of bot commands.
func CommandScopeDefault(lc ...string) CommandScope {
return commandScope{typ: ScopeTypeDefault, languageCode: lang(lc...)}
}
// CommandScopeAllPrivateChats represents the scope of bot commands,
// covering all private chats.
func CommandScopeAllPrivateChats(lc ...string) CommandScope {
return commandScope{typ: ScopeTypeAllPrivateChats, languageCode: lang(lc...)}
}
// CommandScopeAllGroupChats represents the scope of bot commands,
// covering all group and supergroup chats.
func CommandScopeAllGroupChats(lc ...string) CommandScope {
return commandScope{typ: ScopeTypeAllGroupChats, languageCode: lang(lc...)}
}
// CommandScopeAllChatAdministrators represents the scope of bot commands,
// covering all group and supergroup chat administrators.
func CommandScopeAllChatAdministrators(lc ...string) CommandScope {
return commandScope{typ: ScopeTypeAllChatAdministrators, languageCode: lang(lc...)}
}
// CommandScopeChat represents the scope of bot commands, covering a
// specific chat.
func CommandScopeChat(chatID int64, lc ...string) CommandScope {
return commandScope{
typ: ScopeTypeChat,
chatID: chatID,
languageCode: lang(lc...),
}
}
// CommandScopeChatAdministrators represents the scope of bot commands,
// covering all administrators of a specific group or supergroup chat.
func CommandScopeChatAdministrators(chatID int64, lc ...string) CommandScope {
return commandScope{
typ: ScopeTypeChatAdministrators,
chatID: chatID,
languageCode: lang(lc...),
}
}
// CommandScopeChatMember represents the scope of bot commands, covering a
// specific member of a group or supergroup chat.
func CommandScopeChatMember(chatID, userID int64, lc ...string) CommandScope {
return commandScope{
typ: ScopeTypeChatMember,
chatID: chatID,
userID: userID,
languageCode: lang(lc...),
}
}