forked from adampointer/go-slackbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.go
172 lines (145 loc) · 3.7 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
package slackbot
import (
"regexp"
"golang.org/x/net/context"
)
type Route struct {
handler Handler
err error
matchers []Matcher
subrouter Router
preprocessor Preprocessor
botUserID string
}
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.preprocessor != nil {
ctx = r.preprocessor(ctx)
}
for _, m := range r.matchers {
var matched bool
matched, ctx = m.Match(ctx)
if !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
}
// Hear adds a matcher for the message text
func (r *Route) Hear(regex string) *Route {
r.err = 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) *Route {
if r.err == nil {
r.handler = handler
}
return r
}
func (r *Route) MessageHandler(fn MessageHandler) *Route {
return r.Handler(func(ctx context.Context) {
bot := BotFromContext(ctx)
msg := MessageFromContext(ctx)
fn(ctx, bot, msg)
})
}
func (r *Route) Preprocess(fn Preprocessor) *Route {
if r.err == nil {
r.preprocessor = fn
}
return r
}
func (r *Route) Subrouter() Router {
if r.err == nil {
r.subrouter = &SimpleRouter{}
}
return r.subrouter
}
// addMatcher adds a matcher to the route.
func (r *Route) AddMatcher(m Matcher) *Route {
if r.err == nil {
r.matchers = append(r.matchers, m)
}
return r
}
// ============================================================================
// Regex Type Matcher
// ============================================================================
type RegexpMatcher struct {
regex string
botUserID string
}
func (rm *RegexpMatcher) Match(ctx context.Context) (bool, context.Context) {
msg := MessageFromContext(ctx)
// A message be receded by a direct mention. For simplicity sake, strip out any potention direct mentions first
text := StripDirectMention(msg.Text)
// now consider stripped text against regular expression
matched := regexp.MustCompile(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) error {
if r.err != nil {
return r.err
}
r.AddMatcher(&RegexpMatcher{regex: regex})
return nil
}
// ============================================================================
// 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) error {
if r.err != nil {
return r.err
}
r.AddMatcher(&TypesMatcher{types: types, botUserID: ""})
return nil
}