forked from sbstjn/hanu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.go
297 lines (244 loc) · 7.6 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package hanu
import (
"context"
"fmt"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode"
"log"
"os"
)
// Bot is the main object
type Bot struct {
SocketClient *socketmode.Client
ID string
Commands []CommandInterface
ReplyOnly bool
CmdPrefix string
socketHandler *socketmode.SocketmodeHandler
unknownCmdHandler Handler
listenerEnabled bool
}
// New creates a new bot
func New(token string, appToken string) (*Bot, error) {
api := slack.New(
token,
slack.OptionDebug(false),
slack.OptionAppLevelToken(appToken),
)
socketClient := socketmode.New(
api,
socketmode.OptionDebug(false),
)
r, e := api.AuthTest()
if e != nil {
return nil, e
}
bot := &Bot{
SocketClient: socketClient,
ID: r.UserID,
socketHandler: socketmode.NewSocketmodeHandler(socketClient),
}
return bot, nil
}
// NewDebug New creates a new bot with Debug
func NewDebug(token string, appToken string) (*Bot, error) {
api := slack.New(
token,
slack.OptionDebug(true),
slack.OptionLog(log.New(os.Stdout, "api: ", log.Lshortfile|log.LstdFlags)),
slack.OptionAppLevelToken(appToken),
)
socketClient := socketmode.New(
api,
socketmode.OptionDebug(true),
socketmode.OptionLog(log.New(os.Stdout, "socketmode: ", log.Lshortfile|log.LstdFlags)),
)
r, e := api.AuthTest()
if e != nil {
return nil, e
}
bot := &Bot{
SocketClient: socketClient,
ID: r.UserID,
socketHandler: socketmode.NewSocketmodeHandler(socketClient),
}
return bot, nil
}
// SetCommandPrefix will set thing that must be prefixed to the command,
// there is no prefix by default but one could set it to "!" for instance
func (b *Bot) SetCommandPrefix(pfx string) *Bot {
b.CmdPrefix = pfx
return b
}
// SetReplyOnly will make the bot only respond to messages it is mentioned in
func (b *Bot) SetReplyOnly(ro bool) *Bot {
b.ReplyOnly = ro
return b
}
// Process incoming message
func (b *Bot) process(msg Message) {
// Strip @BotName from public message
msg.SetText(msg.StripMention(b.ID))
// Strip Slack's link markup
msg.SetText(msg.StripLinkMarkup())
// Only send auto-generated help command list if directly mentioned
if msg.IsRelevantFor(b.ID) && msg.IsHelpRequest() {
b.sendHelp(msg)
return
}
// if bot can only reply, ensure we were mentioned
if b.ReplyOnly && !msg.IsRelevantFor(b.ID) {
return
}
handled := b.searchCommand(msg)
if !handled && b.ReplyOnly {
if b.unknownCmdHandler != nil {
b.unknownCmdHandler(NewConversation(dummyMatch{}, msg, b))
}
}
}
// Search for a command matching the message
func (b *Bot) searchCommand(msg Message) bool {
var cmd CommandInterface
for i := 0; i < len(b.Commands); i++ {
cmd = b.Commands[i]
match, err := cmd.Get().Match(msg.Text())
if err == nil {
cmd.Handle(NewConversation(match, msg, b))
return true
}
}
return false
}
// Channel will return a channel that the bot can talk in
func (b *Bot) Channel(id string) Channel {
return Channel{b, id}
}
// Say will cause the bot to say something in the specified channel
func (b *Bot) Say(channel, msg string, a ...interface{}) {
b.send(Message{ChannelID: channel, Message: fmt.Sprintf(msg, a...)})
}
func (b *Bot) send(msg MessageInterface) {
_, _, err := b.SocketClient.PostMessage(
msg.Channel(),
slack.MsgOptionText(msg.Text(), false))
if err != nil {
fmt.Printf("failed posting message: %v", err)
}
}
// BuildHelpText will build the help text
func (b *Bot) BuildHelpText() string {
var cmd CommandInterface
help := "The available commands are:\n\n"
for i := 0; i < len(b.Commands); i++ {
cmd = b.Commands[i]
help = help + "`" + b.CmdPrefix + cmd.Get().Text() + "`"
if cmd.Description() != "" {
help = help + " *–* " + cmd.Description()
}
help = help + "\n"
}
return help
}
// sendHelp will send help to the channel and user in the given message
func (b *Bot) sendHelp(msg MessageInterface) {
help := b.BuildHelpText()
if !msg.IsDirectMessage() {
help = "<@" + msg.User() + ">: " + help
}
b.Say(msg.Channel(), help)
}
// Listen for message on socket
func (b *Bot) Listen(ctx context.Context) {
b.socketHandler.Handle(socketmode.EventTypeConnecting, middlewareConnecting)
b.socketHandler.Handle(socketmode.EventTypeConnectionError, middlewareConnectionError)
b.socketHandler.Handle(socketmode.EventTypeConnected, middlewareConnected)
// Handle a specific event from EventsAPI
b.socketHandler.HandleEvents(slackevents.AppMention, middlewareAppMentionEventWithBot(b))
b.socketHandler.HandleEvents(slackevents.Message, middlewareMessageEventWithBot(b))
b.listenerEnabled = true
b.socketHandler.RunEventLoopContext(ctx)
}
func middlewareAppMentionEventWithBot(b *Bot) socketmode.SocketmodeHandlerFunc {
return func(evt *socketmode.Event, client *socketmode.Client) {
middlewareAppMentionEvent(evt, client, b)
}
}
func middlewareAppMentionEvent(evt *socketmode.Event, client *socketmode.Client, b *Bot) {
eventsAPIEvent, ok := evt.Data.(slackevents.EventsAPIEvent)
if !ok {
client.Debugf("Ignored %+v\n", evt)
return
}
client.Ack(*evt.Request)
ev, ok := eventsAPIEvent.InnerEvent.Data.(*slackevents.AppMentionEvent)
if !ok {
client.Debugf("Ignored %+v\n", ev)
return
}
go b.process(NewMentionMessage(ev))
}
func middlewareMessageEventWithBot(b *Bot) socketmode.SocketmodeHandlerFunc {
return func(evt *socketmode.Event, client *socketmode.Client) {
middlewareMessageEvent(evt, client, b)
}
}
func middlewareMessageEvent(evt *socketmode.Event, client *socketmode.Client, b *Bot) {
eventsAPIEvent, ok := evt.Data.(slackevents.EventsAPIEvent)
if !ok {
client.Debugf("Ignored %+v\n", evt)
return
}
client.Ack(*evt.Request)
ev, ok := eventsAPIEvent.InnerEvent.Data.(*slackevents.MessageEvent)
if !ok {
client.Debugf("Ignored %+v\n", ev)
return
}
go b.process(NewMessage(ev))
}
func middlewareConnecting(evt *socketmode.Event, client *socketmode.Client) {
client.Debugf("Connecting to Slack with Socket Mode...")
}
func middlewareConnectionError(evt *socketmode.Event, client *socketmode.Client) {
client.Debugf("Connection failed. Retrying later...")
}
func middlewareConnected(evt *socketmode.Event, client *socketmode.Client) {
client.Debugf("Connected to Slack with Socket Mode.")
}
// Command adds a new command with custom handler
func (b *Bot) Command(cmd string, handler Handler) {
b.Commands = append(b.Commands, NewCommand(b.CmdPrefix+cmd, "", handler))
}
// UnknownCommand will be called when the user calls a command that is unknown,
// but it will only work when the bot is in reply only mode
func (b *Bot) UnknownCommand(h Handler) {
b.unknownCmdHandler = h
}
// Register registers a Command
func (b *Bot) Register(cmd CommandInterface) {
b.Commands = append(b.Commands, cmd)
}
func (b *Bot) RegisterSlashCommand(cmd string, handler func(evt *socketmode.Event, client *socketmode.Client)) {
if b.listenerEnabled {
log.Fatal("RegisterSlashCommand must be called before Listen")
}
b.socketHandler.HandleSlashCommand(cmd, handler)
}
func (b *Bot) RegisterInteraction(et slack.InteractionType, handler func(evt *socketmode.Event, client *socketmode.Client)) {
if b.listenerEnabled {
log.Fatal("RegisterSlashCommand must be called before Listen")
}
b.socketHandler.HandleInteraction(et, handler)
}
func (b *Bot) RegisterEventHandler(et slackevents.EventsAPIType, handler func(evt *socketmode.Event, client *socketmode.Client)) {
if b.listenerEnabled {
log.Fatal("RegisterSlashCommand must be called before Listen")
}
if et == slackevents.AppMention {
log.Fatal("AppMention event type is reserved for Bot")
return
}
b.socketHandler.HandleEvents(et, handler)
}