-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslackbot.go
502 lines (411 loc) · 11.3 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
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
package goslackbot
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
"sync/atomic"
"time"
"golang.org/x/net/websocket"
)
type SlackBot struct {
ID string
rtmToken string
wsURL string
users map[string]SlackUser
channels map[string]SlackChannel
groups map[string]SlackChannel
ims map[string]SlackChannel // direct messages or im in slack lingo
mpims map[string]SlackChannel
teams map[string]SlackTeam
ws *websocket.Conn
OutgoingMessages chan SlackMessage
IncomingMessages map[string]chan SlackMessage
IncomingFunctions map[string]func(SlackMessage)
Conversations map[string]SlackConversation
ReactionCallbacks map[string]func(SlackMessage)
}
// type SlackReactionCallback func(channel, timestamp string)
type SlackAPIReactionAdd struct {
Token string `json:"token"`
Name string `json:"name"`
Channel string `json:"channel"`
TimeStamp string `json:"timestamp"`
}
type SlackPostMessageResponse struct {
Ok bool `json:"ok"`
Channel string `json:"channel"`
TimeStamp string `json:"ts"`
}
type SlackConversation struct {
Messages []SlackMessage
Ongoing bool
State string
Started time.Time
}
type ConversationMap map[string]SlackConversation
var counter uint64
func NewSlackBot(token string) (*SlackBot, error) {
url := fmt.Sprintf("https://slack.com/api/rtm.start?mpim_aware=1&token=%s", token)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
err = fmt.Errorf("API request failed with code %d", resp.StatusCode)
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Printf("Error Creating Bot: %s \n", err)
return nil, err
}
var respObj SlackRTMResponse
err = json.Unmarshal(body, &respObj)
if err != nil {
return nil, err
}
if !respObj.Ok {
err = fmt.Errorf("Slack error: %s", respObj.Error)
return nil, err
}
bot := SlackBot{}
bot.SetURL(respObj.Url)
bot.SetID((respObj.Self.Id))
bot.channels = make(map[string]SlackChannel)
for _, i := range respObj.Channels {
bot.channels[i.Name] = i
fmt.Printf("Channel: %s %s\n", i.ID, i.Name)
}
bot.teams = make(map[string]SlackTeam)
for _, i := range respObj.Teams {
bot.teams[i.Name] = i
log.Printf("Team: %s %s\n", i.ID, i.Name)
}
bot.users = make(map[string]SlackUser)
for _, u := range respObj.Users {
bot.users[u.Name] = u
// fmt.Printf("User: %s\t%s\n", u.ID, u.Name)
}
bot.mpims = make(map[string]SlackChannel)
for _, mpim := range respObj.MPIMs {
bot.channels[mpim.Name] = mpim
// fmt.Printf("MPIM: %s\t%s\n", mpim.ID, mpim.Name)
}
bot.groups = make(map[string]SlackChannel)
for _, group := range respObj.Groups {
bot.channels[group.ID] = group
// fmt.Printf("Group: %s\t%s\n", group.ID, group.Name)
}
bot.ims = make(map[string]SlackChannel)
for _, im := range respObj.IMs {
bot.ims[im.ID] = im
}
bot.OutgoingMessages = make(chan SlackMessage)
bot.IncomingMessages = make(map[string]chan SlackMessage, 0)
bot.rtmToken = token
bot.ReactionCallbacks = make(map[string]func(SlackMessage))
return &bot, nil
}
func (s *SlackBot) ReConnect() *websocket.Conn {
for {
url := fmt.Sprintf("https://slack.com/api/rtm.start?mpim_aware=1&token=%s", s.rtmToken)
resp, err := http.Get(url)
if err != nil {
time.Sleep(time.Minute * 1)
continue
}
if resp.StatusCode != 200 {
err = fmt.Errorf("API request failed with code %d", resp.StatusCode)
time.Sleep(time.Minute * 1)
continue
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Printf("Error Reconnecting Bot: %s \n", err)
time.Sleep(time.Minute * 1)
continue
}
var respObj SlackRTMResponse
err = json.Unmarshal(body, &respObj)
if err != nil {
log.Printf("Error Unmarshaling RTM Response : %s \n", err)
time.Sleep(time.Minute * 1)
continue
}
if !respObj.Ok {
log.Printf("Slack error: %s", respObj.Error)
time.Sleep(time.Minute * 1)
continue
}
s.SetURL(respObj.Url)
s.SetID((respObj.Self.Id))
s.channels = make(map[string]SlackChannel)
for _, i := range respObj.Channels {
s.channels[i.Name] = i
fmt.Printf("Channel: %s %s\n", i.ID, i.Name)
}
s.teams = make(map[string]SlackTeam)
for _, i := range respObj.Teams {
s.teams[i.Name] = i
log.Printf("Team: %s %s\n", i.ID, i.Name)
}
s.users = make(map[string]SlackUser)
for _, u := range respObj.Users {
s.users[u.Name] = u
// fmt.Printf("User: %s\t%s\n", u.ID, u.Name)
}
s.mpims = make(map[string]SlackChannel)
for _, mpim := range respObj.MPIMs {
s.channels[mpim.Name] = mpim
// fmt.Printf("MPIM: %s\t%s\n", mpim.ID, mpim.Name)
}
s.groups = make(map[string]SlackChannel)
for _, group := range respObj.Groups {
s.channels[group.ID] = group
// fmt.Printf("Group: %s\t%s\n", group.ID, group.Name)
}
s.ims = make(map[string]SlackChannel)
for _, im := range respObj.IMs {
s.ims[im.ID] = im
}
ws, err := websocket.Dial(s.wsURL, "", "https://api.slack.com/")
if err == nil {
s.ws = ws
return ws
}
time.Sleep(time.Minute * 1)
}
}
func (s *SlackBot) RemoveReactionCallback(channel, ts string) {
key := strings.Join([]string{channel, ts}, "+")
s.ReactionCallbacks[key] = nil
}
func (s *SlackBot) AddReactionCallback(channel, ts string, callback func(SlackMessage)) {
key := strings.Join([]string{channel, ts}, "+")
s.ReactionCallbacks[key] = callback
}
func (s *SlackBot) TriggerReactionCallback(m SlackMessage) error {
key := strings.Join([]string{m.Channel, m.TimeStamp}, "+")
if callback, ok := s.ReactionCallbacks[key]; ok {
callback(m)
}
return nil
}
func (s *SlackBot) FetchReactionCallback(channel, timestamp string) func(m SlackMessage) {
key := strings.Join([]string{channel, timestamp}, "+")
if callback, ok := s.ReactionCallbacks[key]; ok {
return callback
}
return func(m SlackMessage) {
log.Println("DO NOTHING")
}
}
func (s *SlackBot) GetUser(id string) SlackUser {
return s.users[id]
}
func (s *SlackBot) GetChannel(id string) SlackChannel {
if strings.HasPrefix(id, "G") {
return s.groups[id]
} else {
if strings.HasPrefix(id, "D") {
return s.ims[id]
}
return s.channels[id]
}
}
func (s *SlackBot) GetChannelByName(name string) SlackChannel {
if strings.HasPrefix(name, "G") {
return s.groups[name]
} else {
if strings.HasPrefix(name, "D") {
return s.ims[name]
}
return s.channels[name]
}
}
func (s *SlackBot) RegisterIncomingChannel(name string, incoming chan SlackMessage) error {
s.IncomingMessages[name] = incoming
return nil
}
func (s *SlackBot) RegisterIncomingFunction(name string, runme func(SlackMessage)) {
log.Printf("Registering Incoming Function %s", name)
c := make(chan SlackMessage)
s.RegisterIncomingChannel(name, c)
go func() {
for {
m := <-c
if m.Type != "" && m.Type != "error" && m.Type != "pong" {
runme(m)
}
}
}()
}
func getMessage(ws *websocket.Conn) (m SlackMessage, shouldReconnect bool, err error) {
// err = websocket.JSON.Receive(ws, &m)
var message string
var retry time.Duration = 1
// socket/network errors
errors := []string{"EOF", "timed out", "network is down"}
for {
if err = websocket.Message.Receive(ws, &message); err != nil {
log.Printf("Failed to receive websocket message %s \n", err)
for _, e := range errors {
if strings.HasSuffix(err.Error(), e) {
shouldReconnect = true
return // network/socket error, need to tear down the socket and start over.
}
}
// transient error, exponential retry for n times and then just give up
time.Sleep(time.Second * retry)
retry = retry * 2
if retry > 30 {
// still having issues with the network, an error we are not accounting for yet, just give up and reconstruct connection
shouldReconnect = true
return
}
} else {
break
}
}
shouldReconnect = false // we may get further errors with the unmarshaling but that should not cause us to reconnect.
err = json.Unmarshal([]byte(message), &m)
// log.Printf("RAW %s\n", message)
if m.Channel == "" && m.Item.Channel != "" {
m.Channel = m.Item.Channel
m.TimeStamp = m.Item.TimeStamp
}
return
}
func (s *SlackBot) PostMessage(channel, text string) (*SlackPostMessageResponse, error) {
v := url.Values{}
v.Set("token", s.rtmToken)
v.Set("channel", channel)
v.Set("text", text)
req, err := http.NewRequest("GET", "https://slack.com/api/chat.postMessage?"+v.Encode(), nil)
req.Header.Add("Content-type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
response := SlackPostMessageResponse{}
err = json.Unmarshal(responseBody, &response)
if err != nil {
return nil, err
}
return &response, nil
}
func (s *SlackBot) AddReaction(channel, timestamp, reaction string) error {
v := url.Values{}
v.Set("token", s.rtmToken)
v.Set("name", reaction)
v.Set("channel", channel)
v.Set("timestamp", timestamp)
req, err := http.NewRequest("GET", "https://slack.com/api/reactions.add?"+v.Encode(), nil)
req.Header.Add("X-Conversation-ID", "0xf00f6")
req.Header.Add("Content-type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return nil
}
func (s *SlackBot) SendMessage(channel, message string) error {
m := SlackMessage{
Text: message,
Channel: channel,
Type: "message",
}
s.OutgoingMessages <- m
return nil
}
func (s *SlackBot) Ping() error {
m := SlackMessage{
Text: "ping",
Type: "ping",
Channel: "",
}
// log.Printf("Queueing Ping message %s \n", time.Now())
s.OutgoingMessages <- m
return nil
}
func (s *SlackBot) SetID(id string) error {
s.ID = id
return nil
}
func (s *SlackBot) SetURL(url string) error {
s.wsURL = url
return nil
}
func (s *SlackBot) Connect() error {
ws, err := websocket.Dial(s.wsURL, "", "https://api.slack.com/")
if err != nil {
return err
}
s.ws = ws
go func() {
for {
m := <-s.OutgoingMessages
channel := s.GetChannel(m.Channel)
if m.Channel != "" {
m.Id = atomic.AddUint64(&channel.LastMessageID, 1)
}
// if m.Type == "ping" {
// log.Printf("pinging... %s \n", time.Now().String())
// }
var retry time.Duration = 1
for {
if err := websocket.JSON.Send(s.ws, m); err != nil {
log.Printf("Error %v sending message %#v on websocket\n", err, m)
if m.Type == "ping" {
// throw away the ping if we are having network issues. wait on the socket to be reconstructed elsewhere.
retry = 1
break
}
time.Sleep(retry * time.Second)
if retry < 60 {
retry = retry * 2 // exponential retry
}
} else {
retry = 1
break
}
}
}
}()
go func() {
for {
m, shouldReconnect, err := getMessage(ws)
if err != nil && shouldReconnect {
time.Sleep(time.Second * 1)
log.Println("Reattempting WS reconstruction...")
ws = s.ReConnect()
} else {
// send the incoming message to all registered listeners.
for _, c := range s.IncomingMessages {
c <- m
}
}
}
}()
go func() {
for {
// log.Printf("Ping process starting...\n")
s.Ping()
time.Sleep(time.Second * 30)
}
}()
return nil
}