-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhandlers.go
More file actions
226 lines (190 loc) · 5.04 KB
/
Copy pathhandlers.go
File metadata and controls
226 lines (190 loc) · 5.04 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package gotdbot
import (
"sort"
"strings"
)
// Handler is the interface for all update handlers.
type Handler interface {
// CheckUpdate checks if the update should be handled by this handler.
CheckUpdate(client *Client, update TlObject) bool
// HandleUpdate handles the update.
HandleUpdate(client *Client, update TlObject) error
}
// MatchingOptions contains options for matching updates.
type MatchingOptions struct {
AllowOutgoing bool
AllowIncoming bool
AllowEdited bool
AllowChannel bool
AllowGroup bool
AllowPrivate bool
}
// DefaultMatchingOptions returns the default matching options.
func DefaultMatchingOptions() MatchingOptions {
return MatchingOptions{
AllowIncoming: true,
AllowEdited: true,
AllowChannel: true,
AllowGroup: true,
AllowPrivate: true,
}
}
// checkMessageOptions checks if the message matches the matching options.
func (o MatchingOptions) check(msg *Message) bool {
if msg == nil {
return false
}
if msg.IsOutgoing && !o.AllowOutgoing {
return false
}
if !msg.IsOutgoing && !o.AllowIncoming {
return false
}
if msg.EditDate > 0 && !o.AllowEdited {
return false
}
if msg.IsChannel() && !o.AllowChannel {
return false
}
if msg.IsGroup() && !o.AllowGroup {
return false
}
if msg.IsPrivate() && !o.AllowPrivate {
return false
}
return true
}
// CommandHandler handles bot commands.
type CommandHandler struct {
Options MatchingOptions
Triggers []rune
Command string
Handler func(client *Client, message *Message) error
Filter func(msg *Message) bool
}
// NewCommandHandler creates a new CommandHandler.
func NewCommandHandler(command string, handler func(client *Client, message *Message) error) *CommandHandler {
return &CommandHandler{
Options: DefaultMatchingOptions(),
Triggers: []rune{'/'},
Command: strings.ToLower(command),
Handler: handler,
}
}
// CheckUpdate checks if the update is a command that matches this handler.
func (h *CommandHandler) CheckUpdate(client *Client, update TlObject) bool {
msg, msgOk := getMessageFromUpdate(update)
if !msgOk || !h.Options.check(msg) {
return false
}
u, ok := update.(*UpdateNewMessage)
if !ok || u.Message == nil || u.Message.Content == nil {
return false
}
msg = u.Message
if h.Filter != nil && !h.Filter(msg) {
return false
}
text := msg.GetText()
if text == "" {
return false
}
for _, trigger := range h.Triggers {
prefix := string(trigger)
if !strings.HasPrefix(text, prefix) {
continue
}
parts := strings.Fields(text)
if len(parts) == 0 {
continue
}
cmdPart := parts[0][len(prefix):]
var (
cmdName string
mentionedBot string
)
if i := strings.Index(cmdPart, "@"); i != -1 {
cmdName = cmdPart[:i]
mentionedBot = cmdPart[i+1:]
} else {
cmdName = cmdPart
}
if strings.ToLower(cmdName) != h.Command {
continue
}
if mentionedBot != "" {
if client.Me == nil {
return false
}
botUsername := ""
if client.Me.Usernames != nil && len(client.Me.Usernames.ActiveUsernames) > 0 {
botUsername = strings.ToLower(client.Me.Usernames.ActiveUsernames[0])
}
if botUsername == "" {
return false
}
if strings.ToLower(mentionedBot) != botUsername {
return false
}
}
return true
}
return false
}
// HandleUpdate handles the command update.
func (h *CommandHandler) HandleUpdate(client *Client, update TlObject) error {
u := update.(*UpdateNewMessage)
return h.Handler(client, u.Message)
}
func getMessageFromUpdate(update TlObject) (*Message, bool) {
switch u := update.(type) {
case *UpdateNewMessage:
return u.Message, true
case *UpdateNewBusinessMessage:
if u.Message != nil {
return u.Message.Message, true
}
case *UpdateBusinessMessageEdited:
if u.Message != nil {
return u.Message.Message, true
}
}
return nil, false
}
// AddHandler adds a handler with the default group (0).
func (c *Client) AddHandler(handler Handler) {
c.AddHandlerGroup(handler, 0)
}
// AddHandlerGroup adds a handler with a specific group.
func (c *Client) AddHandlerGroup(handler Handler, group int) {
c.hMu.Lock()
defer c.hMu.Unlock()
oldData := c.handlers.Load()
newMap := make(map[int][]Handler, len(oldData.handlers))
for k, v := range oldData.handlers {
newMap[k] = v
}
handlers := make([]Handler, len(newMap[group]))
copy(handlers, newMap[group])
handlers = append(handlers, handler)
newMap[group] = handlers
groups := make([]int, 0, len(newMap))
for k := range newMap {
groups = append(groups, k)
}
sort.Ints(groups)
c.handlers.Store(&handlersData{
handlers: newMap,
groups: groups,
})
}
// OnCommand registers a new command handler with the default group (0).
func (c *Client) OnCommand(command string, handler func(client *Client, message *Message) error) *CommandHandler {
return c.OnCommandGroup(command, handler, 0)
}
// OnCommandGroup registers a new command handler with a specific group.
func (c *Client) OnCommandGroup(command string, handler func(client *Client, message *Message) error, group int) *CommandHandler {
ch := NewCommandHandler(command, handler)
c.AddHandlerGroup(ch, group)
return ch
}