forked from tshamz/brianwilliams
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathchainbot.js
401 lines (349 loc) · 12.5 KB
/
chainbot.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
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
// Config ===============================================
var config = require('dotenv').config();
var Q = require('q');
var moment = require('moment');
var Botkit = require('botkit');
var responses = require('./responses.js');
var moderatorWhiteList = process.env.MODERATOR_USERS.split(',');
var announcerWhiteList = process.env.ANNOUNCER_USERS.split(',');
var updateWhiteList = process.env.UPDATE_USERS.split(',');
var fullWhiteList = updateWhiteList.concat(announcerWhiteList,moderatorWhiteList); //meh
var readOnlyChannels = process.env.READ_ONLY_CHANNELS.split(',');
var listenChannels = process.env.LISTEN_CHANNELS.split(',');
var randomHappyEmojiReactions = process.env.HAPPY_REACTIONS.split(',');
var randomAngryEmojiReactions = process.env.ANGRY_REACTIONS.split(',');
// Init ===============================================
if (!process.env.BOT_TOKEN) {
console.log('Error: Specify token in environment');
process.exit(1);
}
var controller = Botkit.slackbot({
debug: true,
logLevel: 6
});
controller.configureSlackApp({
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
scopes: ['bot', 'chat:write:user', 'chat:write:bot', 'channels:read', 'channels:write', 'groups:write']
});
controller.setupWebserver(process.env.PORT, function(err, webserver) {
if (err) {
throw new Error(err);
}
controller.createWebhookEndpoints(controller.webserver);
controller.createHomepageEndpoint(controller.webserver);
controller.createOauthEndpoints(controller.webserver, function(err, req, res) {
if (err) {
res.status(500).send('ERROR: ' + err);
} else {
res.send('Great Success!');
}
});
});
var bot = controller.spawn({
token: process.env.BOT_TOKEN
});
bot.startRTM(function(err) {
if (err) {
console.log('Even if you fall on your face, you\'re still moving forward.');
throw new Error(err);
}
});
// USED TO GET THE MEGA ADMIN TOKEN
controller.on('create_user',function(bot, user) {
console.log('USED TO GET THE MEGA ADMIN TOKEN');
console.log('-----');
console.log('USER TOKEN:');
console.log(user.access_token);
console.log('-----');
});
var _bots = {};
var trackBot = function(bot) {
_bots[bot.config.token] = bot;
};
controller.on('create_bot',function(bot, config) {
if (_bots[bot.config.token]) {
// already online! do nothing.
} else {
bot.startRTM(function(err, bot, payload) {
if (err) {
console.log('Even if you fall on your face, you\'re still moving forward.');
throw new Error(err);
} else {
trackBot(bot);
}
});
}
});
controller.storage.teams.all(function(err, teams) {
if (err) {
throw new Error(err);
}
for (var t in teams) {
if (teams[t].bot) {
controller.spawn(teams[t]).startRTM(function(err, bot) {
if (err) {
console.log('Error connecting bot to Slack:',err);
} else {
trackBot(bot);
}
});
}
}
});
// Helper Functions ===============================================
var getRealNameFromId = function(bot, userId) {
var deferred = Q.defer();
var realName = '';
bot.api.users.info({user: userId}, function(err, response) {
realName = response.user.real_name.toLowerCase();
deferred.resolve(realName);
});
return deferred.promise;
};
var getSlackUserNameFromId = function(bot, userId) {
var deferred = Q.defer();
var slackUserName = '';
bot.api.users.info({user: userId}, function(err, response) {
slackUserName = response.user.name;
deferred.resolve(slackUserName);
});
return deferred.promise;
};
var isValidChannelName = function(bot, channelName) {
var deferred = Q.defer();
bot.api.channels.list({}, function(err, response) {
deferred.resolve(response.channels.some(function(channel) {
return channel.name === channelName;
}));
});
return deferred.promise;
};
var isValidUser = function(userID, whiteList) {
var deferred = Q.defer();
deferred.resolve(whiteList.some(function(validUserID) {
return validUserID === userID;
}));
return deferred.promise;
};
// Listeners ===============================================
controller.hears([/post to (\S+)\n([\s\S]*)/], 'direct_message', function(bot, message) {
var channelName = message.match[1];
var update = message.match[2];
var parsedMessages = update.split('\n\n\n').map(function(block) {
var messageParts = block.split('\n___\n');
return {
fallback: messageParts[1],
title: messageParts[0],
text: messageParts[1],
color: 'danger',
mrkdwn_in: ['fallback', 'text']
};
});
var validateChannel = isValidChannelName(bot, channelName);
//var validateName = getRealNameFromId(bot, message.user).then(isValidUser, announcerWhiteList);
var slackUserID = message.user;
var validateID = isValidUser(slackUserID, announcerWhiteList);
var slackUserName = getSlackUserNameFromId(bot, slackUserID);
var isValidated = Q.all([validateChannel, validateID, slackUserName]);
isValidated.spread(function(validChannel, validUser, slackUserID, slackUserName) {
if (!validUser) {
return bot.reply(message, 'Sorry, you\'re not authenticated to post.');
} else if (!validChannel) {
return bot.reply(message, 'Sorry, I can\'t post to the channel: ' + channelName + '.');
} else if (validUser && validChannel) {
var theDate = moment().format('dddd, MMMM Do YYYY');
bot.startConversation(message, function(err, convo) {
convo.say('*I\'m about to post the following:*');
convo.say({
slackUsername: 'ChainBot: Dev Team Announcer',
icon_url: 'https://toaster.chaincoin.org/img/icons/chainbot/ChainBot.png',
text: '*Posted by:* ' + slackUserID + ' *on* ' + theDate + '\n<!channel>',
attachments: parsedMessages
});
convo.ask(responses.confirm(bot, channelName, parsedMessages, theDate, slackUserID, slackUserName), [
responses.yes(bot, 'post', {channel:channelName, message:parsedMessages, date:theDate, id:slackUserID, user:slackUserName}),
responses.no(bot),
responses.default()
]);
});
}
});
});
controller.hears(['help'], ['direct_message'], function(bot, message) {
isValidUser(message.user, fullWhiteList)
.then(function(result) {
if (result) {
bot.reply(message, '*Syntax:*');
bot.reply(message, 'post to <channel name>');
bot.reply(message, '<message title>');
bot.reply(message, '___');
bot.reply(message, '<message body>');
}
});
});
/*controller.hears(['hey mister'], ['direct_message', 'mention', 'direct_mention'], function(bot, message) {
//getRealNameFromId(bot, message.user)
//.then(isValidUser)
isValidUser(message.user)
.then(function(result) {
bot.reply(message, 'Hello!');
if (result) {
bot.reply(message, 'Hey! You\'re pretty valid!');
}
});
});*/
/*
controller.hears([/delete (\S+) from (\S+)/], ['direct_message'], function(bot, message) {
//TODO: this function should check the moderator list before running
var channelOptions = {
token: bot.config.token,
channel: message.match[2],
};
var deleteOptions = {
token: process.env.MEGA_TOKEN,
ts: message.match[1],
channel: message.match[2],
as_user: true
};
bot.api.channels.info(channelOptions, function (err, response) {
if (!response.ok) {
console.log(response);
bot.reply(message, 'incorrect channel id');
} else if (message.match[1][9] !== '.') {
bot.reply(message, 'incorrect time stamp');
} else {
bot.startConversation(message, function(err, convo) {
convo.say('*I\'m about to delete:*');
convo.say(`https://bva.slack.com/archives/${response.channel.name}/p${message.match[1].replace('.', '')}`);
convo.ask(responses.confirm(bot, deleteOptions), [
responses.yes(bot, 'delete', deleteOptions),
responses.no(bot),
responses.default()
]);
});
}
});
});*/
/*controller.on('direct_message, mention, direct_mention', function(bot, message) {
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: 'jesus',
}, function(err) {
if (err) {
console.log(err);
} else {
bot.reply(message, 'go with christ brah.');
}
});
});*/
controller.hears(['update'], ['ambient'], function(bot, message) {
if (listenChannels.indexOf(message.channel) !== -1) { //only be useful in specified channels
getSlackUserNameFromId(bot, message.user).then(function(userName) {
bot.startConversation(message, function(err, convo) {
convo.say('To hear about the latest Chaincoin updates, just @mention me in your question.');
});
});
}
});
controller.hears(['update'], ['direct_mention', 'mention'], function(bot, message) {
if (listenChannels.indexOf(message.channel) !== -1) { //only be useful in specified channels
getSlackUserNameFromId(bot, message.user).then(function(userName) {
bot.startConversation(message, function(err, convo) {
convo.say('Thanks for asking about the latest Chaincoin updates *' + userName + '*.');
convo.say('Unfortunately I\'ve only been programmed to spit out this message for now, but soon the various dev team members will be able to post their updates to me and I\'ll remember them so I can repeat them back to people like you who ask for updates!');
convo.say('Have a nice day!');
});
});
}
});
controller.hears(['thank'], ['direct_mention', 'mention'], function(bot, message) {
if (listenChannels.indexOf(message.channel) !== -1) { //only be useful in specified channels
getSlackUserNameFromId(bot, message.user).then(function(userName) {
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: 'chaincoin',
}, function(err) {
if (err) {
console.log(err);
} else {
bot.reply(message, 'You\'re welcome, *' + userName + '*.');
}
});
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: 'thumbsup',
}, function(err) {
if (err) {
console.log(err);
}
});
});
}
});
controller.hears(['hi', 'hello', 'greeting'], ['direct_mention', 'mention'], function(bot, message) {
if (listenChannels.indexOf(message.channel) !== -1) { //only be useful in specified channels
getSlackUserNameFromId(bot, message.user).then(function(userName) {
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: 'wave',
}, function(err) {
if (err) {
console.log(err);
} else {
bot.reply(message, 'Hello *' + userName + '*! :sunglasses:');
}
});
});
}
});
controller.hears(['chainbot'], ['ambient'], function(bot, message) {
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: 'chaincoin',
});
});
//keep readOnlyChannels clear by deleting all messages
controller.hears([/[\s\S]*/], ['direct_message', 'direct_mention', 'mention', 'ambient', 'user_channel_join'], function(bot, message) {
//can also add 'channel_leave' to remove messages when users leave channel
if (readOnlyChannels.indexOf(message.channel) !== -1) {
getRealNameFromId(bot, message.user).then(function(realName) {
var options = {
token: process.env.MEGA_TOKEN,
ts: message.ts,
channel: message.channel,
as_user: true
};
console.log(options);
console.log('%s said: "%s"', realName, message.text);
console.log('Attempting to delete the message.' );
// this whole block looks pretty ripe for some abstraction and recursion (tsham)
bot.api.chat.delete(options, function(err, response) {
if (!response.ok) {
console.log('Unable to delete due to error: ' + err);
console.log('Trying one more time in 2 seconds');
setTimeout(function() {
bot.api.chat.delete(options, function(err, response) {
if (!response.ok) {
console.log('Unable to delete after a second attempt due to error: ' + err);
}
});
}, 2000);
} else {
console.log('Message successfully deleted!');
}
});
});
}
});
controller.on('rtm_open', function(bot) {
console.log('** The RTM api just connected: ' + bot.identity.name);
});
controller.on('rtm_close', function() {
console.log('** The RTM api just closed');
});