-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (117 loc) · 3.14 KB
/
index.js
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
const CHANNEL = process.env.CHANNEL || 'editor-chat'
const SlackBot = require('slackbots')
const redis = require('redis')
const SerialNumber = require('redis-serial')
function createRedisClient() {
return redis.createClient({
host: process.env.REDIS || 'localhost'
})
}
exports.shouldDrop = function shouldDrop(text) {
return !!text.match(/\<@\S+\|\S+\>/)
}
exports.filterText = function filterText(text) {
return text.replace(/[\<\>]+/g, '')
}
function VizorSlackBot() {
var that = this
this._redisClient = createRedisClient()
this._redisPublisher = createRedisClient()
this._redisSubscriber = createRedisClient()
// relay messages on Global chat
this._redisSubscriber.subscribe('Global')
this._redisSubscriber.on('message', function(_ch, payload) {
var message = JSON.parse(payload)
if (message.from === 'server')
return;
that.postToSlack(message.username+': '+message.message)
})
this._serialNumber = new SerialNumber(this._redisClient)
this._bot = new SlackBot({
token: process.env.SLACKBOT_TOKEN,
name: 'Vizor'
})
this._bot.on('close', function() {
that.close()
process.exit(1)
})
this._bot.once('start', function() {
var userMap = {}
var channelId
that._bot.getUsers().then(function(users) {
users.members.forEach(function(user) {
userMap[user.id] = user.name
})
})
that._bot.getChannels().then(function(channels) {
channels.channels.map(function(channel) {
if (channel.name === CHANNEL)
channelId = channel.id
})
})
that._bot.on('message', function(data) {
switch(data.type) {
case 'message':
if (data.bot_id)
return;
if (data.channel !== channelId)
return;
if (exports.shouldDrop(data.text))
return;
var text = exports.filterText(data.text)
console.log('IN:', userMap[data.user], data)
that.postToVizor(userMap[data.user], text)
break;
case 'hello':
that.postToSlack('I\'ve been reloaded!')
break;
default:
if (data.type === 'user_typing')
return;
console.log('INFO', userMap[data.user], data.type, data)
break;
}
})
})
}
VizorSlackBot.prototype.close = function(err) {
if (err)
console.error(err)
this._bot.ws.close()
this._redisClient.end()
this._redisSubscriber.end()
this._redisPublisher.end()
}
VizorSlackBot.prototype.postToVizor = function(asUserName, text) {
var that = this
var channel = 'Global'
var action = {
actionType: 'uiChatMessageAdded',
channel: channel,
from: 'server',
username: asUserName,
message: text
}
this._serialNumber.next(channel)
.then(function(serial) {
var payload = {
id: serial,
date: Date.now(),
log: JSON.stringify(action)
}
that._redisPublisher.zadd(channel, serial, JSON.stringify(payload), function(err) {
if (err)
console.error('REDIS WRITE FAILED', err.stack)
var message = action
message.id = payload.id
message.date = payload.date
that._redisPublisher.publish(channel, JSON.stringify(message))
})
})
}
VizorSlackBot.prototype.postToSlack = function(text) {
console.log('OUT:', text)
return this._bot.postTo(CHANNEL, text)
.fail(this.close.bind(this))
}
var bot = new VizorSlackBot()