-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
556 lines (460 loc) · 13.5 KB
/
main.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
package karmabot
import (
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/kamaln7/karmabot/database"
"github.com/kamaln7/karmabot/munge"
"github.com/kamaln7/karmabot/ui"
"github.com/aybabtme/log"
"github.com/dustin/go-humanize"
"github.com/nlopes/slack"
)
var (
regexps = struct {
Motivate, GiveKarma, QueryKarma, Leaderboard, URL, SlackUser, Throwback *regexp.Regexp
}{
Motivate: karmaReg.GetMotivate(),
GiveKarma: karmaReg.GetGive(),
QueryKarma: karmaReg.GetQuery(),
Leaderboard: regexp.MustCompile(`^karma(?:bot)? (?:leaderboard|top|highscores) ?([0-9]+)?$`),
URL: regexp.MustCompile(`^karma(?:bot)? (?:url|web|link)?$`),
SlackUser: regexp.MustCompile(`^<@([A-Za-z0-9]+)>$`),
Throwback: karmaReg.GetThrowback(),
}
)
// Database is an abstraction around the database, mostly designed for use in tests.
type Database interface {
// InsertPoints persistently records that points have been given or deducted.
InsertPoints(points *database.Points) error
// GetUser returns information about a user, including their current number of points.
GetUser(name string) (*database.User, error)
// GetLeaderboard returns the top X users with the most points, in order.
GetLeaderboard(limit int) (database.Leaderboard, error)
// GetTotalPoints returns the total number of points transferred across all users.
GetTotalPoints() (int, error)
// GetThrowback returns a random karma operation on a specific user.
GetThrowback(user string) (*database.Throwback, error)
}
// ChatService is an abstraction around Slack, mostly designed for use in tests.
type ChatService interface {
// IncomingEventsChan returns a channel of real-time events.
IncomingEventsChan() chan slack.RTMEvent
// NewOutgoingMessage constructs a new OutgoingMessage using the provided text and channel.
NewOutgoingMessage(text string, channel string, options ...slack.RTMsgOption) *slack.OutgoingMessage
// SendMessage sends the provided *OutgoingMessage.
SendMessage(msg *slack.OutgoingMessage)
// OpenIMChannel opens a new direct-message channel with the specified user.
// It returns some status information, and the channel ID.
OpenIMChannel(user string) (bool, bool, string, error)
// GetUserInfo retrieves the complete user information for the specified username.
GetUserInfo(user string) (*slack.User, error)
// PostEphemeral sends an ephemeral message to a user in a channel.
PostEphemeral(channelID, userID string, options ...slack.MsgOption) (string, error)
}
// SlackChatService is an implementation of ChatService using github.com/nlopes/slack.
type SlackChatService struct {
slack.RTM
}
// IncomingEventsChan returns a channel of real-time messaging events.
func (s SlackChatService) IncomingEventsChan() chan slack.RTMEvent {
return s.IncomingEvents
}
// UserAliases is a map of alias -> main username
type UserAliases map[string]string
// ReactjiConfig contains the configuration for reactji-based votes
type ReactjiConfig struct {
Enabled bool
Upvote, Downvote StringList
}
// Config contains all the necessary configs for karmabot.
type Config struct {
Slack ChatService
Debug, Motivate, SelfKarma bool
MaxPoints, LeaderboardLimit int
Log *log.Log
UI ui.Provider
DB Database
UserBlacklist StringList
Aliases UserAliases
Reactji *ReactjiConfig
ReplyType string
}
// A Bot is an instance of karmabot.
type Bot struct {
Config *Config
}
// New returns a pointer to an new instance of karmabot.
func New(config *Config) *Bot {
return &Bot{
Config: config,
}
}
// Listen starts listening for Slack messages and calls the
// appropriate handlers.
func (b *Bot) Listen() {
for msg := range b.Config.Slack.IncomingEventsChan() {
switch ev := msg.Data.(type) {
case *slack.ReactionAddedEvent:
go b.handleReactionAddedEvent(msg.Data.(*slack.ReactionAddedEvent))
case *slack.ReactionRemovedEvent:
go b.handleReactionRemovedEvent(msg.Data.(*slack.ReactionRemovedEvent))
case *slack.MessageEvent:
go b.handleMessageEvent(msg.Data.(*slack.MessageEvent))
case *slack.ConnectedEvent:
b.Config.Log.Info("connected to slack")
if b.Config.Debug {
b.Config.Log.KV("info", ev.Info).Info("got slack info")
b.Config.Log.KV("connections", ev.ConnectionCount).Info("got connection count")
}
case *slack.RTMError:
b.Config.Log.Err(ev).Error("slack rtm error")
case *slack.InvalidAuthEvent:
b.Config.Log.Fatal("invalid slack token")
default:
b.Config.Log.KV("data", msg.Data).KV("event", reflect.TypeOf(msg.Data)).Info("unexpected slack api event")
}
}
}
func (b *Bot) getReplyThread(message *slack.MessageEvent) string {
var thread string
switch b.Config.ReplyType {
case "message":
thread = message.ThreadTimestamp
case "thread":
if message.ThreadTimestamp != "" {
thread = message.ThreadTimestamp
} else {
thread = message.Timestamp
}
}
return thread
}
// SendReply sends a reply to a message, either as a new message in the channel or a thread (configurable)
func (b *Bot) SendReply(reply string, message *slack.MessageEvent) {
switch b.Config.ReplyType {
case "ephemeral":
b.SendReplyEphemeral(reply, message)
default:
b.SendMessage(reply, message.Channel, b.getReplyThread(message))
}
}
// SendReplyEphemeral sends a reply to a message as an ephemeral message to the user
func (b *Bot) SendReplyEphemeral(reply string, message *slack.MessageEvent) {
b.SendMessageEphemeral(message.Channel, message.User, reply, message.ThreadTimestamp)
}
// SendMessageEphemeral sends an ephemeral message to a user
func (b *Bot) SendMessageEphemeral(reply, channel, user, thread string) {
b.Config.Slack.PostEphemeral(channel, user, slack.MsgOptionText(reply, false), slack.MsgOptionTS(thread))
}
// SendMessage sends a message to a Slack channel.
func (b *Bot) SendMessage(message, channel, thread string) {
msg := b.Config.Slack.NewOutgoingMessage(message, channel)
msg.ThreadTimestamp = thread
b.Config.Slack.SendMessage(msg)
}
// DMUser sends a message directly to a Slack user.
func (b *Bot) DMUser(message, user string) {
_, _, channel, err := b.Config.Slack.OpenIMChannel(user)
if err != nil {
b.Config.Log.Err(err).KV("user", user).Error("could not open IM channel with user")
return
}
b.SendMessage(message, channel, "")
}
func (b *Bot) handleError(err error, message *slack.MessageEvent) bool {
if err == nil {
return false
}
b.Config.Log.Err(err).Error("error")
if message != nil {
var text string
if b.Config.Debug {
text = err.Error()
} else {
text = "an error has occurred."
}
b.SendReply(text, message)
}
return true
}
func (b *Bot) handleReactionAddedEvent(ev *slack.ReactionAddedEvent) {
if !b.Config.Reactji.Enabled {
return
}
var (
points int
reason string
)
switch {
case b.Config.Reactji.Upvote.Contains(ev.Reaction):
points = +1
case b.Config.Reactji.Downvote.Contains(ev.Reaction):
points = -1
default:
return
}
reason = fmt.Sprintf("added a :%s: reactji", ev.Reaction)
b.handleReactionEvent(ev, reason, points)
}
func (b *Bot) handleReactionRemovedEvent(ev *slack.ReactionRemovedEvent) {
if !b.Config.Reactji.Enabled {
return
}
var (
points int
reason string
)
switch {
case b.Config.Reactji.Upvote.Contains(ev.Reaction):
points = -1
case b.Config.Reactji.Downvote.Contains(ev.Reaction):
points = +1
default:
return
}
reason = fmt.Sprintf("removed a :%s: reactji", ev.Reaction)
b.handleReactionEvent((*slack.ReactionAddedEvent)(ev), reason, points)
}
// at this point there is no difference between ReactionAddedEvent and ReactionRemovedEvent
func (b *Bot) handleReactionEvent(ev *slack.ReactionAddedEvent, reason string, points int) {
// look up usernames
from, err := b.getUserNameByID(ev.User)
if b.handleError(err, nil) {
return
}
to, err := b.getUserNameByID(ev.ItemUser)
if b.handleError(err, nil) {
return
}
// add the actor's username to the reason
reason = fmt.Sprintf("%s %s", from, reason)
from, to = strings.ToLower(from), strings.ToLower(to)
// insert points
record := &database.Points{
From: from,
To: to,
Points: points,
Reason: reason,
}
err = b.Config.DB.InsertPoints(record)
if b.handleError(err, nil) {
return
}
pointsMsg, err := b.getUserPointsMessage(to, reason, points)
if b.handleError(err, nil) {
return
}
// reply as ephemeral message
b.SendMessageEphemeral(pointsMsg, ev.Item.Channel, ev.User, "")
}
func (b *Bot) handleMessageEvent(ev *slack.MessageEvent) {
if ev.Type != "message" {
return
}
// convert motivates into karmabot syntax
if b.Config.Motivate {
if match := regexps.Motivate.FindStringSubmatch(ev.Text); len(match) > 0 {
ev.Text = match[1] + "++ for doing good work"
}
}
switch {
case regexps.URL.MatchString(ev.Text):
b.printURL(ev)
case regexps.GiveKarma.MatchString(ev.Text):
b.givePoints(ev)
case regexps.Leaderboard.MatchString(ev.Text):
b.printLeaderboard(ev)
case regexps.Throwback.MatchString(ev.Text):
b.getThrowback(ev)
case regexps.QueryKarma.MatchString(ev.Text):
b.queryKarma(ev)
}
}
func (b *Bot) printURL(ev *slack.MessageEvent) {
url, err := b.Config.UI.GetURL("/")
if b.handleError(err, ev) {
return
}
// ui is disabled
if url == "" {
return
}
b.SendReply(url, ev)
}
func (b *Bot) givePoints(ev *slack.MessageEvent) {
match := regexps.GiveKarma.FindStringSubmatch(ev.Text)
if len(match) == 0 {
return
}
// forgive me
if match[1] != "" {
// we matched the first alt expression
match = match[:4]
} else {
// we matched the second alt expression
match = append(match[:1], match[4:]...)
}
from, err := b.getUserNameByID(ev.User)
if b.handleError(err, ev) {
return
}
to, err := b.parseUser(match[1])
if b.handleError(err, ev) {
return
}
to = strings.ToLower(to)
if _, blacklisted := b.Config.UserBlacklist[to]; blacklisted {
b.Config.Log.KV("user", to).Info("user is blacklisted, ignoring karma command")
return
}
points := min(len(match[2])-1, b.Config.MaxPoints)
if match[2][0] == '-' {
points *= -1
}
reason := match[3]
if !b.Config.SelfKarma && from == to {
b.SendReply("Sorry, you are not allowed to do that.", ev)
return
}
record := &database.Points{
From: from,
To: to,
Points: points,
Reason: reason,
}
err = b.Config.DB.InsertPoints(record)
if b.handleError(err, ev) {
return
}
pointsMsg, err := b.getUserPointsMessage(to, reason, points)
if b.handleError(err, ev) {
return
}
b.SendReply(pointsMsg, ev)
}
func (b *Bot) getThrowback(ev *slack.MessageEvent) {
match := regexps.Throwback.FindStringSubmatch(ev.Text)
if len(match) == 0 {
return
}
var (
user string
err error
)
if match[1] != "" {
user, err = b.parseUser(match[1])
if b.handleError(err, ev) {
return
}
user = strings.ToLower(user)
} else {
user, err = b.getUserNameByID(ev.User)
if b.handleError(err, ev) {
return
}
}
throwback, err := b.Config.DB.GetThrowback(user)
if err == database.ErrNoSuchUser {
b.SendReply(fmt.Sprintf("could not find any karma operations for %s", user), ev)
return
}
if b.handleError(err, ev) {
return
}
date := humanize.Time(throwback.Timestamp)
if throwback.Reason != "" {
throwback.Reason = fmt.Sprintf(" for %s", throwback.Reason)
}
text := fmt.Sprintf("%s received %d points from %s %s%s", munge.Munge(throwback.To), throwback.Points.Points, munge.Munge(throwback.From), date, throwback.Reason)
b.SendReply(text, ev)
}
func (b *Bot) getUserPointsMessage(name, reason string, points int) (string, error) {
user, err := b.Config.DB.GetUser(name)
if err != nil {
return "", err
}
text := fmt.Sprintf("%s == %d (", name, user.Points)
if points > 0 {
text += "+"
}
text = fmt.Sprintf("%s%d", text, points)
if reason != "" {
text += fmt.Sprintf(" for %s", reason)
}
text += ")"
return text, nil
}
func (b *Bot) printLeaderboard(ev *slack.MessageEvent) {
match := regexps.Leaderboard.FindStringSubmatch(ev.Text)
if len(match) == 0 {
return
}
limit := b.Config.LeaderboardLimit
if match[1] != "" {
var err error
limit, err = strconv.Atoi(match[1])
if b.handleError(err, ev) {
return
}
}
text := fmt.Sprintf("*top %d leaderboard*\n", limit)
url, err := b.Config.UI.GetURL(fmt.Sprintf("/leaderboard/%d", limit))
if b.handleError(err, ev) {
return
}
if url != "" {
text = fmt.Sprintf("%s%s\n", text, url)
}
leaderboard, err := b.Config.DB.GetLeaderboard(limit)
if b.handleError(err, ev) {
return
}
for i, user := range leaderboard {
text += fmt.Sprintf("%d. %s == %d\n", i+1, munge.Munge(user.Name), user.Points)
}
b.SendReply(text, ev)
}
func (b *Bot) parseUser(user string) (string, error) {
if match := regexps.SlackUser.FindStringSubmatch(user); len(match) > 0 {
var err error
user, err = b.getUserNameByID(match[1])
if err != nil {
return "", err
}
}
// check if it is aliased
if alias, ok := b.Config.Aliases[user]; ok {
user = alias
}
return user, nil
}
func (b *Bot) getUserNameByID(id string) (string, error) {
userInfo, err := b.Config.Slack.GetUserInfo(id)
if err != nil {
return "", err
}
return userInfo.Name, nil
}
func (b *Bot) queryKarma(ev *slack.MessageEvent) {
match := regexps.QueryKarma.FindStringSubmatch(ev.Text)
if len(match) == 0 {
return
}
name, err := b.parseUser(match[1])
if b.handleError(err, ev) {
return
}
name = strings.ToLower(name)
user, err := b.Config.DB.GetUser(name)
switch {
case err == database.ErrNoSuchUser:
// override debug mode
b.SendReply(err.Error(), ev)
case b.handleError(err, ev):
default:
b.SendReply(fmt.Sprintf("%s == %d", user.Name, user.Points), ev)
}
}