-
Notifications
You must be signed in to change notification settings - Fork 2
/
processor.go
254 lines (211 loc) · 6.63 KB
/
processor.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
package slack
import (
"encoding/json"
"fmt"
"log"
"regexp"
"strings"
)
const (
slackEventTypeMessage = "message"
maxMessageSize = 4000
maxMessageLines = 25
)
// Processor type processes inbound events from Slack
type Processor struct {
// Connection to Slack
con *Connection
// Slack user information relating to the bot account
self User
// a sequence number to uniquely identify sent messages and correlate with acks from Slack
sequence int
// map of event handler functions to handle types of Slack event
eventHandlers map[string]func(*Processor, map[string]interface{}, []byte)
// map of users who are members of the Slack group
users map[string]User
}
// event type represents an event sent to Slack e.g. messages
type event struct {
Id int `json:"id"`
Type string `json:"type"`
Channel string `json:"channel"`
Text string `json:"text"`
}
// user_change event type represents a user change event from Slack
type userChangeEvent struct {
Type string `json:"type"`
UpdatedUser User `json:"user"`
}
// send Event to Slack
func (p *Processor) sendEvent(eventType string, channel string, text string) error {
p.sequence++
response := &event{Id: p.sequence, Type: eventType, Channel: channel, Text: text}
responseJson, err := json.Marshal(response)
if err != nil {
return err
}
p.con.Write(responseJson)
return nil
}
// Write the message on the specified channel to Slack
func (p *Processor) Write(channel string, text string) error {
for len(text) > 0 {
lines := strings.Count(text, "\n")
if len(text) <= maxMessageSize && lines <= maxMessageLines {
if err := p.sendEvent(slackEventTypeMessage, channel, text); err != nil {
return err
}
text = ""
} else {
// split message at a convenient place
var breakIndex int
maxSizeChunk := text
if len(text) > maxMessageSize {
maxSizeChunk := text[:maxMessageSize]
lines = strings.Count(maxSizeChunk, "\n")
}
if lines > maxMessageLines {
var index int
for n := 0; index < len(maxSizeChunk) && n < maxMessageLines; n++ {
p := strings.Index(maxSizeChunk[index:], "\n")
if p == -1 {
break
}
index += p + 1
}
breakIndex = index
} else if lastLineBreak := strings.LastIndex(maxSizeChunk, "\n"); lastLineBreak > -1 {
breakIndex = lastLineBreak
} else if lastWordBreak := strings.LastIndexAny(maxSizeChunk, "\n\t .,/\\-(){}[]|=+*&"); lastWordBreak > -1 {
breakIndex = lastWordBreak
} else {
breakIndex = maxMessageSize
}
if err := p.sendEvent(slackEventTypeMessage, channel, text[:breakIndex]); err != nil {
return err
}
if breakIndex != maxMessageSize && lines <= maxMessageLines {
breakIndex++
}
text = text[breakIndex:]
}
}
return nil
}
// Start processing events from Slack
func (p *Processor) Start() {
for {
msg := p.con.Read()
log.Printf("%s", msg)
var data map[string]interface{}
err := json.Unmarshal(msg, &data)
if err != nil {
fmt.Printf("%T\n%s\n%#v\n", err, err, err)
switch v := err.(type) {
case *json.SyntaxError:
fmt.Println(string(msg[v.Offset-40 : v.Offset]))
}
log.Printf("%s", msg)
continue
}
// if reply_to attribute is present the event is an ack' for a sent message
_, isReply := data["reply_to"]
subtype, ok := data["subtype"]
var isMessageChangedEvent bool
if ok {
isMessageChangedEvent = (subtype.(string) == "message_changed" || subtype.(string) == "message_deleted")
}
if !isReply && !isMessageChangedEvent {
handler, ok := p.eventHandlers[data["type"].(string)]
if ok {
handler(p, data, msg)
}
}
}
}
// updateUser updates or adds (if not already existing) the specifed user
func (p *Processor) updateUser(user User) {
p.users[user.Id] = user
}
// onConnected is a callback for when the client connects (or reconnects) to Slack.
func (p *Processor) onConnected(con *Connection) {
p.self = con.config.Self
log.Printf("Connected to Slack as %s", p.self.Name)
p.users = make(map[string]User)
for _, user := range con.config.Users {
p.updateUser(user)
}
}
// type for callbacks to receive messages from Slack
type messageProcessor func(*Message)
// Starts processing events on the connection from Slack and passes any messages to the hear callback and only
// messages addressed to the bot to the respond callback
func EventProcessor(con *Connection, respond messageProcessor, hear messageProcessor) {
p := Processor{
con: con,
self: con.config.Self,
eventHandlers: map[string]func(*Processor, map[string]interface{}, []byte){
slackEventTypeMessage: func(p *Processor, event map[string]interface{}, rawEvent []byte) {
filterMessage(p, event, respond, hear)
},
"user_change": func(p *Processor, event map[string]interface{}, rawEvent []byte) {
var userEvent userChangeEvent
err := json.Unmarshal(rawEvent, &userEvent)
if err != nil {
fmt.Printf("%T\n%s\n%#v\n", err, err, err)
switch v := err.(type) {
case *json.SyntaxError:
fmt.Println(string(rawEvent[v.Offset-40 : v.Offset]))
}
log.Printf("%s", rawEvent)
}
p.updateUser(userEvent.UpdatedUser)
},
"hello": func(p *Processor, event map[string]interface{}, rawEvent []byte) {
p.onConnected(con)
},
"error": func(p *Processor, event map[string]interface{}, rawEvent []byte) {
log.Printf("Error received from Slack: %s", rawEvent)
},
},
users: make(map[string]User),
}
p.Start()
}
// Invoke one of the specified callbacks for the message if appropriate
func filterMessage(p *Processor, data map[string]interface{}, respond messageProcessor, hear messageProcessor) {
var userFullName string
var userId string
user, ok := data["user"]
if ok {
userId = user.(string)
user, exists := p.users[userId]
if exists {
userFullName = user.RealName
}
}
// process messages directed at Talbot
r, _ := regexp.Compile("^(<@" + p.self.Id + ">|@?" + p.self.Name + "):? (.+)")
text, ok := data["text"]
if !ok || text == nil {
return
}
matches := r.FindStringSubmatch(text.(string))
if len(matches) == 3 {
if respond != nil {
m := &Message{eventStream: p, responseStrategy: reply, Text: matches[2], From: userFullName, fromId: userId, channel: data["channel"].(string)}
respond(m)
}
} else if data["channel"].(string)[0] == 'D' {
if respond != nil {
// process direct messages
m := &Message{eventStream: p, responseStrategy: send, Text: text.(string), From: userFullName, fromId: userId, channel: data["channel"].(string)}
respond(m)
}
} else {
if hear != nil {
m := &Message{eventStream: p, responseStrategy: send, Text: text.(string), From: userFullName, fromId: userId, channel: data["channel"].(string)}
hear(m)
}
}
}