/
ape.go
122 lines (105 loc) · 2.59 KB
/
ape.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
package ape
import (
"bytes"
"fmt"
"regexp"
"text/template"
"github.com/mix3/guiniol"
"golang.org/x/net/context"
)
type actionFunc func(*Event) error
type action struct {
help *template.Template
fn actionFunc
}
type Connection struct {
connection *guiniol.Connection
actionMap map[string]action
}
func New(token string) *Connection {
conn := &Connection{
connection: guiniol.NewConnection(token),
actionMap: map[string]action{},
}
conn.AddAction("help", "this message", func(e *Event) error {
args := e.Command().Args()
if 0 < len(args) {
var doc bytes.Buffer
if v, ok := conn.actionMap[args[0]]; ok {
if err := v.help.Execute(&doc, e); err != nil {
return err
}
m := "```\n"
m += fmt.Sprintf("%s --- %s\n", args[0], doc.String())
m += "```"
e.Reply(m)
} else {
e.Reply(fmt.Sprintf("command not found: %s", args[0]))
}
return nil
}
m := "command list\n```"
for k, v := range conn.actionMap {
var doc bytes.Buffer
if err := v.help.Execute(&doc, e); err != nil {
return err
}
m += fmt.Sprintf(" %s --- %s\n", k, doc.String())
}
m += "```"
e.Reply(m)
return nil
})
return conn
}
func (c *Connection) AddAction(command, help string, fn actionFunc) {
tmpl := template.Must(template.New(command).Parse(help))
c.actionMap[command] = action{
help: tmpl,
fn: fn,
}
}
var pattern1 = regexp.MustCompile(`^([^:]+):\s+(.+)`) // bot_name: *****
var pattern2 = regexp.MustCompile(`^<@(\w+)>:?\s+(.+)`) // @bot_name *****
func matcher(e *guiniol.EventCtx) (string, string) {
t := e.MessageEvent().Text
if m := pattern2.FindStringSubmatch(t); len(m) == 3 {
return m[1], m[2]
}
if m := pattern1.FindStringSubmatch(t); len(m) == 3 {
return m[1], m[2]
}
return "", ""
}
func (c *Connection) Loop() {
c.connection.RegisterCb(recoverCb(c.actionCb))
c.connection.Loop()
}
func (c *Connection) actionCb(ctx context.Context, eventCtx *guiniol.EventCtx) {
if eventCtx.MessageEvent().Subtype == "bot_message" {
return
}
name, m := matcher(eventCtx)
if name != eventCtx.UserName() && name != eventCtx.UserId() {
return
}
e := newEvent(ctx, eventCtx, m)
if action, ok := c.actionMap[e.Command().Name()]; ok {
if err := action.fn(e); err != nil {
e.Reply(err.Error())
}
} else {
e.Reply("???")
}
}
func recoverCb(cb guiniol.CallbackFunc) guiniol.Callback {
return guiniol.CallbackFunc(func(ctx context.Context, eventCtx *guiniol.EventCtx) {
defer func() {
if err := recover(); err != nil {
e := newEvent(ctx, eventCtx, "")
e.Reply(fmt.Sprintf("%v", err))
}
}()
cb.Next(ctx, eventCtx)
})
}