-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
446 lines (405 loc) · 15.8 KB
/
app.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
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
// 👀 Current status: going to add a dashboard.html with d3.j visualization of status data
const express = require('express');
const bodyParser = require('body-parser');
const Intercom = require('intercom-client');
const glitchup = require('glitchup');
const Datastore = require('nedb'); // setup a new database
const unescape = require('unescape');
const request = require('request');
const app = express();
const client = new Intercom.Client({ token: process.env.INTERCOM_TOKEN });
const db = new Datastore({ filename: '.data/datafile', autoload: true });
// Express middleware for parsing request/resonse bodies
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(`${__dirname}/public/`));
// prevent server from sleeping
glitchup();
// the 'model' for team status data record
class StatusRecord {
constructor(type, timestamp, data, total) {
this.type = type;
this.timestamp = timestamp;
this.data = data;
this.total = total;
}
}
// Sets a TTL for all rows to expire so that oldest rows are flushed out after 300000 seconds
db.ensureIndex({ fieldName: 'type', expireAfterSeconds: 300000 }, (err) => {
if (err) console.log('There\'s a problem with the database: ', err);
});
/* Manually remove all team data if you need to refresh your team list (i.e. after adding
// a new team)
function removeTeams() {
db.remove({ type: { $in: ['team'] }}, { multi: true }, function (err, numRemoved) {
console.log(numRemoved)
});
}
// removeTeams()
*/
// default conversations status data with some placeholder data
const defaultStatusRecords = [
new StatusRecord('status', 1439640522522, { test: '1' }, 1),
new StatusRecord('status', 1439640522521, { test: '2' }, 2),
];
// global response_url to use in failure callbacks
let responseUrl;
// Handles async requests back to slack after a command is handled and completed
const postToSlack = (message, aResponseUrl) => {
request.post({
url: aResponseUrl,
json: message,
}, (err) => {
if (err) {
failureCallback(err);
}
});
};
const failureCallback = (result) => {
const failureMessage = {
response_type: 'ephemeral',
text: `Sorry, something didn't work right: ${result}`,
};
console.log(`Handle rejected promise (${result})`);
postToSlack(failureMessage, responseUrl);
};
// Call intercom for all admins, which includes teams
function storeTeams() {
client.admins.list().then(
(firstPage) => {
const teams = firstPage.body.admins.filter(admin => admin.type === 'team');
return teams;
},
).then(
(teams) => {
teams.forEach((team) => {
db.insert(team, (insertErr, recordsAdded) => {
if (insertErr) console.log('There\'s a problem with the database: ', insertErr);
else if (recordsAdded) console.log('Team inserted in the database');
});
});
},
).catch(failureCallback);
}
// Initialize database with Teams. TODO: check if teams have changed and update
db.count({ type: 'team' }, (countErr, count) => {
console.log(`There are ${count} team rows in the database`);
if (countErr) console.log('There\'s a problem with the database: ', countErr);
else if (count <= 0) { // empty database so needs populating
// default users inserted in the database
storeTeams();
}
});
// Initialize database with Conversation stats
db.count({type: 'status'}, (countErr, count) => {
console.log(`There are ${count} status rows in the database`);
if (countErr) console.log('There\'s a problem with the database: ', countErr);
else if (count <= 0) { // empty database so needs populating
// default users inserted in the database
db.insert(defaultStatusRecords, (insertErr, recordsAdded) => {
if (insertErr) console.log('There\'s a problem with the database: ', insertErr);
else if (recordsAdded) console.log('Default statuses inserted in the database');
});
}
});
// Array of team name strings to monitor, if empty formatForSlack() will return all
let monitoredTeams = [
// Example:
// 'Priority',
// 'Partners',
// 'Paid customers',
];
// This reassignment should be intentionally ignored if you leave DISQUS_TOKEN blank
if (process.env.DISQUS_TOKEN === 'hellofromdisqus') {
monitoredTeams = [
'Commenters',
'DMCA',
'Publisher Success',
'Priority Support',
'Community Publishers',
'Payments',
'Direct Support',
'Delete & Access',
];
}
// Store status record
function storeStatus(statusRecord) {
db.insert(statusRecord, (insertErr, recordsAdded) => {
if (insertErr) console.log('There\'s a problem with the database: ', insertErr);
else if (recordsAdded) console.log('Status inserted in the database');
});
}
// Maps converstation data to simple counts for each team name
const mapConvoStats = (data) => {
// First, finding all teams in database
db.find({ type: 'team' }, (findErr, docsFound) => {
if (findErr) console.log('There\'s a problem with the database: ', findErr);
else if (docsFound) {
// Reduce all conversation data down to counts per team ID
const reducedData = data.reduce((acc, convo) => {
// make the assignedTo key either the assignee or 'nobody_admin'
// if no id exists (unassigned)
const assignedTo = convo.assignee.id || convo.assignee.type;
if (acc[assignedTo] === undefined) {
acc[assignedTo] = 1;
} else {
acc[assignedTo] += 1;
}
return acc;
}, {});
// Create a new pretty count object to store
const statusRecord = new StatusRecord('status', Date.now(), {}, 0);
// Format team/assignee names and if the assignee id is present in
// reduced convo counts set to that count, else set to 0 (no open cases)
docsFound.forEach((assignee) => {
statusRecord.data[unescape(assignee.name)] = reducedData[assignee.id]
? reducedData[assignee.id] : 0;
});
// Reduce all team count values for a total
statusRecord.total = Object.values(statusRecord.data).reduce((acc, count) => acc + count);
storeStatus(statusRecord);
}
});
};
// Get the most recent status record from database, used by slash command for quick responses
const getLastStatus = () => new Promise((resolve, reject) => {
db.findOne({ type: 'status' }).sort({ timestamp: -1 }).exec((findErr, docsFound) => {
findErr ? reject(`Problem with the database: ${findErr}`) : docsFound ? resolve(docsFound) : null
});
});
// Paginate through Intercom nextPage objects recursively
function getMorePages(page, conversationData) {
client.nextPage(page).then(
(nextPage) => {
const converationCollection = conversationData.concat(nextPage.body.conversations);
if (nextPage.body.pages.next) {
getMorePages(nextPage.body.pages, converationCollection);
} else {
mapConvoStats(converationCollection);
}
return converationCollection;
},
).catch(failureCallback);
}
// Call intercom for first page converations and paginate if more results exist
function listConversations() {
client.conversations.list({ state: 'open', per_page: 20 }).then(
(firstPage, conversationData = []) => {
const conversationDataSingle = conversationData.concat(firstPage.body.conversations);
if (firstPage.body.pages.next) {
getMorePages(firstPage.body.pages, conversationDataSingle);
} else {
mapConvoStats(conversationDataSingle);
}
return conversationDataSingle;
},
).catch(failureCallback);
}
// Callback to listConversations() every 10 min. to store new Intercom data
// listConversations();
setInterval(listConversations, 300000 );
// When given conversation ID, get and send all case, customer, and assigned user details to slack
const getConversationById = id => new Promise((resolve) => {
const conversationId = id.split('#part_id=')[0];
const partId = id.includes('#part_id=') ? id.split('#part_id=')[1].split(`${conversationId}-`)[1] : null;
client.conversations.find({ id: conversationId, display_as: 'plaintext' }).then(
(conversation) => {
const conversationWithPart = conversation;
if (partId) {
conversationWithPart.body.chosen_conversation_part = partId;
}
resolve(conversationWithPart.body);
},
).catch(failureCallback);
});
// Return help text to client with examples of proper usage
function help() {
const helpMessage = {
response_type: 'ephemeral',
text: 'Type `/support` for the status of all monitored teams. Add a conversation link `https://app.intercom.io/a/apps/x2byp8hg/inbox/inbox/1935680/conversations/18437669699` to return the message.',
};
return helpMessage;
}
// Return help text to client with examples of proper usage
function badCommand() {
const badCommandMessage = {
response_type: 'ephemeral',
text: 'Sorry bub, I\'m not quite following. Type `/support help` to see what I can understand.',
};
return badCommandMessage;
}
// return true if team name is in the monitored list, default is true if no teams to monitor
const isMonitoredTeam = (team) => {
return monitoredTeams.length && !monitoredTeams.includes(team) ? false : true;
}
// TODO: create a better message structure: monitored teams, on-fire, last touched
function formatForSlack(statusRecord) {
const attachments = [];
const teamConvoCounts = statusRecord.data;
let monitoredTeamsTotal = 0;
Object.keys(teamConvoCounts).map((objectKey) => {
const addAttachment = () => {
monitoredTeamsTotal += teamConvoCounts[objectKey];
attachments.push({
fallback: `${objectKey}: ${teamConvoCounts[objectKey]}`,
color: teamConvoCounts[objectKey] < 10 ? '#7fbd5a' : '#e76c35',
title: `${objectKey}: ${teamConvoCounts[objectKey]}`,
});
};
// Add the team stats to message only if actively monitoring
if (isMonitoredTeam(objectKey)) {
addAttachment();
}
});
const message = {
response_type: 'in_channel',
text: `${monitoredTeamsTotal} open coversations right now.`,
attachments,
};
return message;
}
// grab the first conversation part where the author is the customer
const getSpecificMessage = (body) => {
let customerIndicator = ['lead','user']
if (body.chosen_conversation_part) {
return body.conversation_parts.conversation_parts.filter(part => part.id === body.chosen_conversation_part)[0];
} else if (customerIndicator.includes(body.conversation_message.author.type)) {
return body.conversation_message
}
return body.conversation_parts.conversation_parts.filter(part => customerIndicator.includes(part.author.type))[0]
}
function formatSingleConvoForSlack(conversation) {
let specificMessage = getSpecificMessage(conversation)
let body = specificMessage.body
let subject = conversation.conversation_message.subject
let timestamp = specificMessage.created_at
let ratingMap = ['😠','🙁','😐','😃','🤩']
let rating = (conversation.conversation_rating.rating !== null) ? ratingMap[conversation.conversation_rating.rating - 1] : 'Not yet rated';
const attachments = [
{
fallback: `${body}`,
color: '#36a64f',
// 'author_name': `${conversation.user.id}`,
title: `${subject}`,
title_link: `'https://app.intercom.io/a/apps/x2byp8hg/inbox/inbox/conversation/${conversation.id}`,
text: `${body}`,
fields: [
{
title: 'Status',
value: `${conversation.state}`,
short: true,
},
{
title: 'Rating',
value: `${rating}`,
short: true,
},
],
footer: 'Slackercom',
footer_icon: 'https://cdn.glitch.com/project-avatar/e899f9c6-39d0-4acf-abe0-e0d88c21c524.png?1524523219471',
ts: timestamp,
},
];
const message = {
response_type: 'in_channel',
attachments,
};
return message;
}
app.get('/wakeup', (req, res) => {
console.log('Ok, I\'ll try to wake up');
res.status(200).send('Ok, I\'ll try to wake up');
});
app.get('/dashboard', (req, res) => {
const trustedKey = process.env.TRUSTED_KEY;
const requestKey = req.query.key;
console.log(requestKey, trustedKey);
if (trustedKey === requestKey) {
res.status(200).sendFile(__dirname + '/dashboard.html');
} else {
res.status(403).send('Your IP is not allowed to see this dashboard.');
}
});
// endpoint to get all status records in database
app.get('/getStatus', (req, res) => {
// First, finding all teams in database
db.find({ type: 'status' }, { $not: { team: 'convo' } }).sort({ timestamp: -1 }).limit(1440).exec((findErr, docsFound) => {
if (findErr) console.log('There\'s a problem with the database: ', findErr);
else if (docsFound) {
// check for both cases, headers are case-insensitive
const refererIndex = req.rawHeaders.indexOf('referer') < 0 ? req.rawHeaders.indexOf('Referer') : req.rawHeaders.indexOf('referer');
const referrer = req.rawHeaders[refererIndex + 1];
const trustedKey = process.env.TRUSTED_KEY;
const requestKey = referrer.split('=')[1];
if (trustedKey === requestKey) {
res.status(200).send(JSON.stringify(docsFound));
} else {
res.status(403).send('Your IP is not allowed to see this dashboard.');
}
}
});
});
/*
let testCommand = (responseUrlOverride) => {
getLastStatus().then((lastStat) => {
const lastStatFormatted = formatForSlack(lastStat);
postToSlack(lastStatFormatted, responseUrlOverride);
}).catch(failureCallback);
};
testCommand('https://hooks.slack.com/commands/T024PTBSY/554237657924/bh3MF2FP6TaQqClEPBDUhEZO');
*/
// Handler of post requests to server, checks request text to trigger different functions
app.post('/', (req, res) => {
responseUrl = req.body.response_url;
// Check the slack token so that this request is authenticated
if (req.body.token === process.env.SLACK_TOKEN) {
// Send an acknowledgement immediately, then finish async command and POST to `response_url`
res.send({ response_type: 'ephemeral', text: '🤔' });
// Different commands that can be attached to `/support`
let conversationId = req.body.text.split(/conversations\/|conversation\//)[1];
let isEmptyCommand = (req.body.text.length < 1);
let isHelp = (req.body.text === 'help');
// call the correct function for each command
if (isEmptyCommand) {
// get last status from database
getLastStatus().then((lastStat) => {
const lastStatFormatted = formatForSlack(lastStat);
postToSlack(lastStatFormatted, req.body.response_url);
}).catch(failureCallback);
// validates a full Intercom link exists in command text
} else if (conversationId) {
// search the conversation ID in Intercom API
getConversationById(conversationId).then((conversation) => {
postToSlack(formatSingleConvoForSlack(conversation), req.body.response_url);
}).catch(failureCallback);
// validates email
} else if (isHelp) {
postToSlack(help(res), req.body.response_url);
} else {
postToSlack(badCommand(), req.body.response_url);
}
// Request does not have token so it is not authenticated
} else {
res.send(
{
response_type: 'ephemeral',
text: 'Wow, such unauthorized. Make sure your secret Slack token is correctly set.',
},
);
}
});
/* Example of how to log the last 10 status
const logLastTenStatus = () => {
// First, finding all teams in database
db.find({ type: 'status' }).sort({ timestamp: -1 }).limit(10).exec(function (findErr, docsFound) {
if (findErr) console.log('There\'s a problem with the database: ', findErr);
else if (docsFound) {
console.log(docsFound)
}
});
};
logLastTenStatus()
*/
// Local debug listen for requests :)
const listener = app.listen(process.env.PORT || 53923, () => {
console.log(`Your app is listening on port ${listener.address().port}`);
});