forked from adampointer/go-slackbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroute.go
187 lines (156 loc) · 4.05 KB
/
route.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
176
177
178
179
180
181
182
183
184
185
186
187
package slackbot
import (
"context"
"regexp"
)
type Route struct {
handler Handler
err error
matchers []Matcher
subrouter Router
preprocessor Preprocessor
botUserID string
talkToSelf bool // if set, the bot can reply to its own messages
}
func (r *Route) setBotID(botID string) {
r.botUserID = botID
for _, matcher := range r.matchers {
matcher.SetBotID(botID)
}
}
// RouteMatch stores information about a matched route.
type RouteMatch struct {
Route *Route
Handler Handler
}
func (r *Route) Match(ctx context.Context, match *RouteMatch) (bool, context.Context) {
if r.err != nil {
return false, ctx
}
if r.handler == nil {
return false, ctx
}
if ev := MessageFromContext(ctx); ev != nil && !r.talkToSelf && r.botUserID == ev.User {
return false, ctx
}
if r.preprocessor != nil {
ctx = r.preprocessor(ctx)
}
for _, m := range r.matchers {
var matched bool
if matched, ctx = m.Match(ctx); !matched {
return false, ctx
}
}
// if this route contains a subrouter, invoke the subrouter match
if r.subrouter != nil {
return r.subrouter.Match(ctx, match)
}
match.Route = r
match.Handler = r.handler
return true, ctx
}
func (r *Route) TalkToSelf() *Route {
r.talkToSelf = true
return r
}
func (r *Route) NoTalkToSelf() *Route {
r.talkToSelf = false
return r
}
// Hear adds a matcher for the message text
func (r *Route) Hear(regex string) *Route {
r.addRegexpMatcher(regex)
return r
}
func (r *Route) Messages(types ...MessageType) *Route {
r.addTypesMatcher(types...)
return r
}
// Handler sets a handler for the route.
func (r *Route) Handler(handler Handler) error {
if r.err != nil {
return r.err
}
r.handler = handler
return nil
}
func (r *Route) MessageHandler(fn MessageHandler) error {
return r.Handler(func(ctx context.Context) {
bot := BotFromContext(ctx)
msg := MessageFromContext(ctx)
fn(ctx, bot, msg)
})
}
func (r *Route) Preprocess(fn Preprocessor) *Route {
r.preprocessor = fn
return r
}
func (r *Route) Subrouter() Router {
r.subrouter = &SimpleRouter{err: r.err}
return r.subrouter
}
// addMatcher adds a matcher to the route.
func (r *Route) AddMatcher(m Matcher) *Route {
r.matchers = append(r.matchers, m)
return r
}
func (r *Route) Err() error {
return r.err
}
// ============================================================================
// Regex Type Matcher
// ============================================================================
type RegexpMatcher struct {
regex *regexp.Regexp
botUserID string
}
func (rm *RegexpMatcher) Match(ctx context.Context) (bool, context.Context) {
msg := MessageFromContext(ctx)
// A message may be received via a direct mention. For simplicity sake, strip out any potention direct mentions first
text := StripDirectMention(msg.Text)
// Now match the stripped text against the regular expression
matched := rm.regex.MatchString(text)
return matched, ctx
}
func (rm *RegexpMatcher) SetBotID(botID string) {
rm.botUserID = botID
}
// addRegexpMatcher adds a host or path matcher and builder to a route.
func (r *Route) addRegexpMatcher(regex string) {
re, err := regexp.Compile(regex)
if err != nil {
r.err = err
}
r.AddMatcher(&RegexpMatcher{regex: re})
}
// ============================================================================
// Message Type Matcher
// ============================================================================
type TypesMatcher struct {
types []MessageType
botUserID string
}
func (tm *TypesMatcher) Match(ctx context.Context) (bool, context.Context) {
msg := MessageFromContext(ctx)
for _, t := range tm.types {
switch t {
case DirectMessage:
if IsDirectMessage(msg) {
return true, ctx
}
case DirectMention:
if IsDirectMention(msg, tm.botUserID) {
return true, ctx
}
}
}
return false, ctx
}
func (tm *TypesMatcher) SetBotID(botID string) {
tm.botUserID = botID
}
// addRegexpMatcher adds a host or path matcher and builder to a route.
func (r *Route) addTypesMatcher(types ...MessageType) {
r.AddMatcher(&TypesMatcher{types: types, botUserID: r.botUserID})
}