-
Notifications
You must be signed in to change notification settings - Fork 16
/
bot.go
228 lines (197 loc) · 4.42 KB
/
bot.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package slackbot
import (
"bytes"
"fmt"
"log"
"os"
"sort"
"strings"
"text/tabwriter"
)
type BotCommandRunner interface {
RunCommand(args []string, channel string) error
}
type BotBackend interface {
SendMessage(text string, channel string)
Listen(BotCommandRunner)
}
// Bot describes a generic bot
type Bot struct {
backend BotBackend
help string
name string
label string
config Config
commands map[string]Command
defaultCommand Command
}
func NewBot(config Config, name, label string, backend BotBackend) *Bot {
return &Bot{
backend: backend,
config: config,
commands: make(map[string]Command),
name: name,
label: label,
}
}
func (b *Bot) Name() string {
return b.name
}
func (b *Bot) Config() Config {
return b.config
}
func (b *Bot) AddCommand(trigger string, command Command) {
b.commands[trigger] = command
}
func (b *Bot) triggers() []string {
triggers := make([]string, 0, len(b.commands))
for trigger := range b.commands {
triggers = append(triggers, trigger)
}
sort.Strings(triggers)
return triggers
}
// HelpMessage is the default help message for the bot
func (b *Bot) HelpMessage() string {
w := new(tabwriter.Writer)
buf := new(bytes.Buffer)
w.Init(buf, 8, 8, 8, ' ', 0)
fmt.Fprintln(w, "Command\tDescription")
for _, trigger := range b.triggers() {
command := b.commands[trigger]
fmt.Fprintf(w, "%s\t%s\n", trigger, command.Description())
}
_ = w.Flush()
return BlockQuote(buf.String())
}
func (b *Bot) SetHelp(help string) {
b.help = help
}
func (b *Bot) Label() string {
return b.label
}
func (b *Bot) SetDefault(command Command) {
b.defaultCommand = command
}
// RunCommand runs a command
func (b *Bot) RunCommand(args []string, channel string) error {
if len(args) == 0 || args[0] == "help" {
b.sendHelpMessage(channel)
return nil
}
command, ok := b.commands[args[0]]
if !ok {
if b.defaultCommand != nil {
command = b.defaultCommand
} else {
return fmt.Errorf("Unrecognized command: %q", args)
}
}
if args[0] != "resume" && args[0] != "config" && b.Config().Paused() {
b.backend.SendMessage("I can't do that, I'm paused.", channel)
return nil
}
go b.run(args, command, channel)
return nil
}
func (b *Bot) run(args []string, command Command, channel string) {
out, err := command.Run(channel, args)
if err != nil {
log.Printf("Error %s running: %#v; %s\n", err, command, out)
b.backend.SendMessage(fmt.Sprintf("Oops, there was an error in %q:\n%s", strings.Join(args, " "),
BlockQuote(out)), channel)
return
}
log.Printf("Output: %s\n", out)
if command.ShowResult() {
b.backend.SendMessage(out, channel)
}
}
func (b *Bot) sendHelpMessage(channel string) {
help := b.help
if help == "" {
help = b.HelpMessage()
}
b.backend.SendMessage(help, channel)
}
func (b *Bot) SendMessage(text string, channel string) {
b.backend.SendMessage(text, channel)
}
func (b *Bot) Listen() {
b.backend.Listen(b)
}
// NewTestBot returns a bot for testing
func NewTestBot() (*Bot, error) {
backend := &SlackBotBackend{}
return NewBot(NewConfig(true, false), "testbot", "", backend), nil
}
// BlockQuote returns the string block-quoted
func BlockQuote(s string) string {
if !strings.HasSuffix(s, "\n") {
s += "\n"
}
return "```\n" + s + "```"
}
// GetTokenFromEnv returns slack token from the environment
func GetTokenFromEnv() string {
token := os.Getenv("SLACK_TOKEN")
if token == "" {
log.Fatal("SLACK_TOKEN is not set")
}
return token
}
func isSpace(r rune) bool {
switch r {
case ' ', '\t', '\r', '\n':
return true
}
return false
}
func parseInput(s string) []string {
buf := ""
args := []string{}
var escaped, doubleQuoted, singleQuoted bool
for _, r := range s {
if escaped {
buf += string(r)
escaped = false
continue
}
if r == '\\' {
if singleQuoted {
buf += string(r)
} else {
escaped = true
}
continue
}
if isSpace(r) {
if singleQuoted || doubleQuoted {
buf += string(r)
} else if buf != "" {
args = append(args, buf)
buf = ""
}
continue
}
switch r {
case '"':
if !singleQuoted {
doubleQuoted = !doubleQuoted
continue
}
case '\'':
if !doubleQuoted {
singleQuoted = !singleQuoted
continue
}
}
buf += string(r)
}
if buf != "" {
args = append(args, buf)
}
return args
}