-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathhandlers.go
175 lines (145 loc) · 3.49 KB
/
handlers.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
172
173
174
175
package bot
import (
"regexp"
"strings"
"github.com/go-telegram/bot/models"
)
type HandlerType int
const (
HandlerTypeMessageText HandlerType = iota
HandlerTypeCallbackQueryData
HandlerTypeCallbackQueryGameShortName
HandlerTypePhotoCaption
)
type MatchType int
const (
MatchTypeExact MatchType = iota
MatchTypePrefix
MatchTypeContains
MatchTypeCommand
MatchTypeCommandStartOnly
matchTypeRegexp
matchTypeFunc
)
type handler struct {
id string
handlerType HandlerType
matchType MatchType
handler HandlerFunc
pattern string
re *regexp.Regexp
matchFunc MatchFunc
}
func (h handler) match(update *models.Update) bool {
if h.matchType == matchTypeFunc {
return h.matchFunc(update)
}
var data string
var entities []models.MessageEntity
switch h.handlerType {
case HandlerTypeMessageText:
if update.Message == nil {
return false
}
data = update.Message.Text
entities = update.Message.Entities
case HandlerTypeCallbackQueryData:
if update.CallbackQuery == nil {
return false
}
data = update.CallbackQuery.Data
case HandlerTypeCallbackQueryGameShortName:
if update.CallbackQuery == nil {
return false
}
data = update.CallbackQuery.GameShortName
case HandlerTypePhotoCaption:
if update.Message == nil {
return false
}
data = update.Message.Caption
entities = update.Message.CaptionEntities
}
if h.matchType == MatchTypeExact {
return data == h.pattern
}
if h.matchType == MatchTypePrefix {
return strings.HasPrefix(data, h.pattern)
}
if h.matchType == MatchTypeContains {
return strings.Contains(data, h.pattern)
}
if h.matchType == MatchTypeCommand {
for _, e := range entities {
if e.Type == models.MessageEntityTypeBotCommand {
if data[e.Offset+1:e.Offset+e.Length] == h.pattern {
return true
}
}
}
}
if h.matchType == MatchTypeCommandStartOnly {
for _, e := range entities {
if e.Type == models.MessageEntityTypeBotCommand {
if e.Offset == 0 && data[e.Offset+1:e.Offset+e.Length] == h.pattern {
return true
}
}
}
}
if h.matchType == matchTypeRegexp {
return h.re.Match([]byte(data))
}
return false
}
func (b *Bot) RegisterHandlerMatchFunc(matchFunc MatchFunc, f HandlerFunc, m ...Middleware) string {
b.handlersMx.Lock()
defer b.handlersMx.Unlock()
id := RandomString(16)
h := handler{
id: id,
matchType: matchTypeFunc,
matchFunc: matchFunc,
handler: applyMiddlewares(f, m...),
}
b.handlers = append(b.handlers, h)
return id
}
func (b *Bot) RegisterHandlerRegexp(handlerType HandlerType, re *regexp.Regexp, f HandlerFunc, m ...Middleware) string {
b.handlersMx.Lock()
defer b.handlersMx.Unlock()
id := RandomString(16)
h := handler{
id: id,
handlerType: handlerType,
matchType: matchTypeRegexp,
re: re,
handler: applyMiddlewares(f, m...),
}
b.handlers = append(b.handlers, h)
return id
}
func (b *Bot) RegisterHandler(handlerType HandlerType, pattern string, matchType MatchType, f HandlerFunc, m ...Middleware) string {
b.handlersMx.Lock()
defer b.handlersMx.Unlock()
id := RandomString(16)
h := handler{
id: id,
handlerType: handlerType,
matchType: matchType,
pattern: pattern,
handler: applyMiddlewares(f, m...),
}
b.handlers = append(b.handlers, h)
return id
}
func (b *Bot) UnregisterHandler(id string) {
b.handlersMx.Lock()
defer b.handlersMx.Unlock()
for i, h := range b.handlers {
if h.id == id {
b.handlers = append(b.handlers[:i], b.handlers[i+1:]...)
return
}
}
}