-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
144 lines (126 loc) · 3.19 KB
/
server.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
package main
import (
"database/sql"
"fmt"
"os"
"regexp"
"time"
"github.com/benbariteau/slack"
"github.com/benbariteau/slack/rtm"
_ "github.com/mattn/go-sqlite3"
)
func Serve(slackTokens []string) error {
commands := []cmd{
rawGetKarmaCmd,
getKarmaCmd,
helpCmd, // must be after getKarma since its regex matches anything that getKarma's does
mutateKarmaCmd, // must be after getKarma because getKarma could be given an identifier that matches
singleMessageKarmaMutate,
mentionedCmd,
leaderboardCmd,
}
db, err := sql.Open("sqlite3", "./dabopobo.db")
if err != nil {
return err
}
s := sqliteBackend{
db: db,
}
for _, slackToken := range slackTokens {
go rtmHandle(slackToken, commands, s)
}
for {
time.Sleep(5 * time.Minute)
}
return nil
}
func rtmHandle(token string, commands []cmd, m model) error {
for {
fmt.Println("rtm start")
conn, err := rtm.Dial(token)
if err != nil {
fmt.Println(err)
continue
}
defer conn.Close()
fmt.Println("rtm connected")
rtmHelper(conn, commands, m)
fmt.Println("attempting rtm reconnect")
}
return nil
}
func rtmHelper(conn *rtm.Conn, commands []cmd, m model) {
events := eventsChannel(conn)
ticker := time.Tick(10 * time.Minute)
eventHandledRecently := true
for {
select {
case event := <-events:
handleEvent(event, conn, commands, m)
eventHandledRecently = true
case <-ticker:
if eventHandledRecently {
eventHandledRecently = false
} else {
return
}
}
}
}
func handleEvent(event rtm.Event, conn *rtm.Conn, commands []cmd, m model) {
fmt.Printf("handling %v event\n", event.Type())
switch event.(type) {
case rtm.Message:
e := event.(rtm.Message)
fmt.Println("handling message", e)
handleMessage(e, conn, commands, m)
}
}
func postprocessUser(userString string) string {
// string leading "@"
userString = userString[1:]
// wrap in parens so that it gets treated as a single unit for karma purposes, even if it has spaces
return "(" + userString + ")"
}
func postprocessLink(unescapedString string, escapeType rtm.EscapeType) string {
if escapeType != rtm.UserEscape {
return unescapedString
}
return postprocessUser(unescapedString)
}
func handleMessage(message rtm.Message, conn *rtm.Conn, commands []cmd, m model) {
// ignore bot messages
if message.Subtype() == "bot_message" || message.Subtype() == "slackbot_response" {
fmt.Println("Skipping bot message", message)
return
}
for _, command := range commands {
r := regexp.MustCompile(command.regex)
unescapedText := conn.UnescapeMessagePostprocess(message.Text(), postprocessLink)
matches := r.FindAllStringSubmatch(unescapedText, -1)
if matches == nil {
continue
}
fmt.Printf("matched with %v\n", command)
user, _ := slack.NewAPI(conn.Token).UsersInfo(message.User())
text, err := command.handler(m, matches, user.Profile.DisplayName, message.Channel())
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
if text != "" {
conn.SendMessage(text, message.Channel())
}
break
}
}
func eventsChannel(conn *rtm.Conn) <-chan rtm.Event {
events := make(chan rtm.Event)
go func() {
for {
event := conn.NextEvent()
fmt.Println(event.Type(), event)
events <- event
}
}()
return events
}