-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple.go
45 lines (38 loc) · 1.06 KB
/
simple.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
package skit
import (
"context"
"fmt"
"text/template"
)
// SimpleHandler responds with a simple message if the input message matches one
// of the regular expressions. The message (argument tplStr) can be a golang text
// template. Named captures from the regex that matches the input message will be
// used as data for rendering the message template.
func SimpleHandler(tplStr string, exps ...string) (Handler, error) {
rexps, err := ParseExprs(exps)
if err != nil {
return nil, err
}
tpl, err := template.New("simple").Parse(tplStr)
if err != nil {
return nil, err
}
handler := HandlerFunc(func(ctx context.Context, sk *Skit, ev *MessageEvent) bool {
for _, rexp := range rexps {
matches := CaptureAll(rexp, ev.Text)
if matches == nil {
continue
}
matches["event"] = *ev
msg, err := Render(*tpl, matches)
if err != nil {
sk.SendText(ctx, fmt.Sprintf(":face_with_symbols: Something is not right: %v", err), ev.Channel)
return true
}
sk.SendText(ctx, msg, ev.Channel)
return true
}
return false
})
return handler, nil
}