-
Notifications
You must be signed in to change notification settings - Fork 5
/
slackbot.go
208 lines (179 loc) · 5.5 KB
/
slackbot.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
package slackbot
import (
"net/http"
"strings"
log "github.com/sirupsen/logrus"
"github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents"
"github.com/tarkalabs/slackbot/commander"
"github.com/tarkalabs/slackbot/handler"
"github.com/tarkalabs/slackbot/interactor"
"github.com/tarkalabs/slackbot/message"
"github.com/tarkalabs/slackbot/submitter"
"github.com/tarkalabs/slackbot/utils"
)
type EventMatcher func(*slackevents.MessageEvent) bool
type SlackConfig struct {
Port string
BotID string
APIToken string
VerificationToken string
EventMatcher
}
type SlackConfigs func(*SlackConfig)
func WithEventMatcher(eventMatcher EventMatcher) SlackConfigs {
return func(o *SlackConfig) {
o.EventMatcher = eventMatcher
}
}
func WithAllEventMatcher() SlackConfigs {
return func(o *SlackConfig) {
o.EventMatcher = func(d *slackevents.MessageEvent) bool {
return true
}
}
}
func (config SlackConfig) GetVerificationToken() slackevents.TokenComparator {
return slackevents.TokenComparator{
VerificationToken: config.VerificationToken,
}
}
type SlackBot struct {
config SlackConfig
SlackClient *slack.Client
slashCmdChan chan *slack.SlashCommand
eventChan chan *slackevents.MessageEvent
actionChan chan *slackevents.MessageAction
submissionChan chan *slack.DialogCallback
outgoingMessages chan *message.Message
Commander commander.Commander
Interactor interactor.Interactor
Submitter submitter.Submitter
}
func New(config SlackConfig, opts ...SlackConfigs) (*SlackBot, error) {
for _, o := range opts {
o(&config)
}
slackClient := slack.New(config.APIToken, slack.OptionDebug(true))
slackBot := &SlackBot{
config: config,
SlackClient: slackClient,
}
slackBot.slashCmdChan = make(chan *slack.SlashCommand, 50)
slackBot.eventChan = make(chan *slackevents.MessageEvent, 50)
slackBot.actionChan = make(chan *slackevents.MessageAction, 50)
slackBot.submissionChan = make(chan *slack.DialogCallback, 50)
slackBot.outgoingMessages = make(chan *message.Message, 50)
slackBot.Commander.Add(commander.NewCommand(
"help",
"Provides help for available commands",
"Examples:\n`help` will show help for all available commands\n`help <command>` will show help for specific command",
commander.WithPrefixMatcher(),
commander.WithHandler(func(data *slackevents.MessageEvent) error {
cmd := strings.TrimSpace(data.Text[len("help"):])
slackBot.SendHelpMessage(
data.Channel,
message.HelpMessage(),
cmd,
)
return nil
}),
))
return slackBot, nil
}
func (slackBot *SlackBot) Listen() {
http.Handle(
"/slashcmd",
handler.NewSlashCmdHandler(handler.SlashCmdHandlerConfig{
SlashCmdChan: slackBot.slashCmdChan,
VerificationToken: slackBot.config.GetVerificationToken(),
}),
)
http.Handle(
"/event",
handler.NewEventHandler(handler.EventHandlerConfig{
EventChan: slackBot.eventChan,
VerificationToken: slackBot.config.GetVerificationToken(),
}),
)
http.Handle(
"/interaction",
handler.NewInteractionHandler(handler.InteractionHandlerConfig{
ActionChan: slackBot.actionChan,
SubmissionChan: slackBot.submissionChan,
VerificationToken: slackBot.config.GetVerificationToken(),
}),
)
go slackBot.handleSlashCmds()
go slackBot.handleEvents()
go slackBot.handleActions()
go slackBot.handleSubmissions()
go slackBot.handleOutgoingMessages()
slackBot.listenAndServe()
}
func (slackBot SlackBot) listenAndServe() {
log.Info("Listening on port ", slackBot.config.Port)
err := http.ListenAndServe(slackBot.config.Port, nil)
if err != nil {
log.Fatal("Error starting slack events listener: ", err)
}
}
func (slackBot SlackBot) handleSlashCmds() {
for d := range slackBot.slashCmdChan {
cmd, err := slackBot.Commander.Match(d.Command)
if err != nil {
slackBot.SendHelpMessage(d.ChannelID, message.BotDidNotUnderstandMessage(), "")
}
cmd.Handle(utils.CmdToMessageEvent(d))
if err != nil {
slackBot.SendHelpMessage(d.ChannelID, err.Error(), "")
}
}
}
func (slackBot SlackBot) handleEvents() {
for d := range slackBot.eventChan {
if slackBot.config.EventMatcher(d) {
err := slackBot.Commander.Handle(d)
if err != nil {
slackBot.SendHelpMessage(d.Channel, message.BotDidNotUnderstandMessage(), "")
}
}
}
}
func (slackBot SlackBot) handleActions() {
for d := range slackBot.actionChan {
err := slackBot.Interactor.Handle(d)
if err != nil {
slackBot.SendHelpMessage(d.User.ID, err.Error(), "")
}
}
}
func (slackBot SlackBot) handleSubmissions() {
for d := range slackBot.submissionChan {
err := slackBot.Submitter.Handle(d)
if err != nil {
slackBot.SendHelpMessage(d.User.ID, err.Error(), "")
}
}
}
func (slackBot SlackBot) handleOutgoingMessages() {
for m := range slackBot.outgoingMessages {
slackBot.SlackClient.PostMessage(m.Channel, m.Options...)
}
}
func (slackBot SlackBot) GetUser(userID string) (*slack.User, error) {
return slackBot.SlackClient.GetUserInfo(userID)
}
func (slackBot SlackBot) SendMessage(channel string, options ...slack.MsgOption) {
withBotID := append(options, slack.MsgOptionUser(slackBot.config.BotID), slack.MsgOptionAsUser(true))
slackBot.outgoingMessages <- message.New(channel, withBotID...)
}
func (slackBot SlackBot) SendSimpleMessage(channel, msg string) {
slackBot.SendMessage(channel, slack.MsgOptionText(msg, false))
}
func (slackBot SlackBot) SendHelpMessage(channel, msg, command string) {
slackBot.SendMessage(
channel,
slackBot.Commander.HelpMessageOptions(msg, command)...,
)
}