This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapp.js
365 lines (308 loc) · 11 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
const util = require('util');
const fs = require('fs');
const markdown = require('markdown').markdown;
const request = require('request');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const Cloudant = require('cloudant');
const dbName = process.env.CLOUDANT_DB || 'slack-poll';
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// env vars
if (!process.env.VERIFY_TOKEN) console.error('WARNING: missing env var VERIFY_TOKEN, will not validate requests');
if (!process.env.CLIENT_ID || !process.env.CLIENT_SECRET) console.error('WARNING: misisng env vars CLIENT_ID and CLIENT_SECRET for slack-oauth authentication');
if (!process.env.APP_ID) {}
let polls = {
_id: 'polls'
};
function debug() {
if (process.env.NODE_ENV !== 'development') return;
console.log.apply(console.log, arguments);
}
function getStatsMessage(pollId) {
let poll = polls[pollId];
if (!poll) return 'This channel does not have an active poll.';
let output = "Current stats:\n";
for(let idx in poll.opts) {
output += " " + poll.opts[idx] + " - " + Object.keys(poll.votes[idx]).length + " vote(s).\n";
}
return output;
}
let icons = [':one:',':two:',':three:',':four:',':five:',':six:',':seven:',':eight:',':nine:'];
function createVoteMessage(poll) {
let text = poll.title+'\n\n';
for(let i=0; i<poll.opts.length; i++) {
text += icons[i] + ' ' + poll.opts[i];
let votes = Object.keys(poll.votes[i]).length;
if (votes > 0) {
text += '\t`' + votes + '`';
}
text += '\n';
}
return text;
}
function createPoll(pollId, match, res) {
if (typeof match === 'string') {
match = match.replace(/(\u201C|\u201D)/g, '"');
match = match.match(/[^"\s]+|(?:"[^"]+")/g);
}
let poll = polls[pollId] = {
title: match[0],
opts: [],
votes: []
};
for(let i=1; i<match.length; i++) {
poll.opts[i-1] = match[i];
poll.votes[i-1] = {};
}
let attachments = [{
"callback_id": pollId,
"text": "Click button to vote",
"fallback": "Your slack client does not support voting",
"actions": []
}];
let actions = attachments[0].actions;
// Create vote options.
for(let i=0; i<poll.opts.length; i++) {
actions.push({
"name": "vote-option",
"text": i+1,
"type": "button",
"style": "primary",
"value": i
});
if (i==4) {
let attachment = {
"callback_id": pollId,
"fallback": "Your slack client does not support voting",
"actions": []
};
attachments.push(attachment);
actions = attachment.actions;
}
}
actions.push({
"name": "poll-action",
"type": "button",
"text": "Close",
"style": "danger",
"value": "close",
});
if (!process.env.SLACK_ENTERPRISE) actions[actions.length-1].confirm = {
"title": "Close Poll?",
"text": "Vote buttons will be removed, are you sure?",
"ok_text": "Yes",
"dismiss_text": "No"
};
// Display in channel.
let output = {
"response_type": "in_channel",
"replace_original": false,
"text": createVoteMessage(poll),
"attachments": attachments
};
poll.message = output;
debug('create-poll', JSON.stringify(output));
res.status(200).send(output);
}
app.post('/slack/command', (req, res) => {
let payload = req.body;
if (process.env.VERIFY_TOKEN && process.env.VERIFY_TOKEN !== payload.token) return res.status(403);
let pollId = payload.team_id + ':' + payload.channel_id;
debug('pollId', pollId);
if (!payload.text) payload.text = '';
if (payload.text.match(/^\s*debug\s*$/)) {
debug(polls);
return res.status(200).send("Sent debug contents to console");
}
if (payload.text.match(/^\s*delete\s*$/)) {
let poll = polls[pollId];
if (!poll) return res.status(200).send('This channel does not have an active poll.');
delete polls[pollId];
return res.status(200).send('Active poll deleted.');
}
// Fix Mac smart quotes.
payload.text = payload.text.replace(/(\u201C|\u201D)/g, '"');
let match = payload.text.match(/[^"\s]+|(?:"[^"]+")/g);
if (!match) {
return res.status(200).send(util.format('Usage:\n %s "Would you like to play a game?" "Chess" "Falken\'s Maze" "Thermonuclear War"', payload.command));
}
if (match.length < 2) return res.status(200).send('You must provide some options to vote!');
if (match.length > 10) return res.status(200).send('You entered ' + (match.length-1) + ' options. I only allow 9 options at most.');
// Remove double quotes.
for(let i=0; i<match.length; i++) {
match[i] = match[i].replace(/^"(.*)"$/, '$1');
}
if (polls[pollId]) {
let message = {
"text": "There is an active poll in this channel. Locate it and click its *Close* button, or click the button below to delete active poll.",
"attachments": [{
"callback_id": pollId,
"fallback": "Your slack client does not support voting",
"actions": [{
"name": "poll-action",
"type": "button",
"style": "danger",
"text": "Delete",
"value": "delete"
}]
}]
};
// slack enterprise seems do not work with confirmation dialog.
if (!process.env.SLACK_ENTERPRISE) {
message.attachments[0].actions[0].confirm = {
"title": "Delete Poll?",
"text": "Vote buttons of current active poll will stop working. Are you sure?",
"ok_text": "Yes",
"dismiss_text": "No"
};
}
return res.status(200).send(message);
}
createPoll(pollId, match, res);
});
app.post('/slack/action', (req, res) => {
let payload = req.body.payload;
if (!payload) return res.status(400);
try {
payload = JSON.parse(payload);
} catch (ex) {
return res.status(400);
}
if (process.env.VERIFY_TOKEN && process.env.VERIFY_TOKEN !== payload.token) return res.status(403);
let poll = polls[payload.callback_id];
if (!poll) return res.status(200).send({
"response_type": "ephemeral",
"replace_original": false,
"text": 'This poll has already been closed.'
});
let action = payload.actions && payload.actions[0];
if (!action) return res.status(200).send({
"response_type": "ephemeral",
"replace_original": false,
"text": 'ERROR: Missing action field in received payload.'
});
if (action.name === 'poll-action') {
if (action.value === 'close') {
let ts = Math.round(Date.now()/1000);
delete polls[payload.callback_id];
payload.original_message.attachments = [{
"text": '<!date^'+ts+'^Poll closed at {date_num} {time_secs}|sometime> by <@'+payload.user.id+'|'+payload.user.name+'>.'
}];
payload.original_message.text = createVoteMessage(poll);
return res.status(200).send(payload.original_message);
}
if (action.value === 'delete') {
delete polls[payload.callback_id];
return res.status(200).send('Active poll deleted. You may now start a new poll.');
}
}
if (action.name !== 'vote-option') return res.status(200).send(`ERROR: unknown action \`${action.name}\``);
// Check if user already voted.
for(let idx=0; idx<poll.votes.length; idx++) {
if (poll.votes[idx][payload.user.id]) {
// If user clicked on same vote option.
if (idx === payload.actions[0].value) return res.status(200).send({
"response_type": "ephemeral",
"replace_original": false,
"text": "You already voted voted for `" + poll.opts[idx] + "`"
});
// If user clicked different vote option, update.
delete poll.votes[idx][payload.user.id];
poll.votes[payload.actions[0].value][payload.user.id] = true;
payload.original_message.text = createVoteMessage(poll);
return res.status(200).send(payload.original_message);
}
}
poll.votes[payload.actions[0].value][payload.user.id] = true;
payload.original_message.text = createVoteMessage(poll);
return res.status(200).send(payload.original_message);
});
// slack-oauth
let html;
app.get('/', (req, res) => {
if (!html) {
html = '<html lang="en"><head>';
if (process.env.APP_ID) html += util.format('<meta name="slack-app-id" content="%s">',process.env.APP_ID);
html += '<title>slack-poll-app</title></head><body>';
if (process.env.CLIENT_ID) {
html += util.format('<p><a href="https://slack.com/oauth/authorize?client_id=%s&scope=commands"><img alt="Add to Slack" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/[email protected] 2x" /></a></p>', process.env.CLIENT_ID);
}
let content = fs.readFileSync('README.md', {encoding:'utf8'});
html += markdown.toHTML(content);
html += '</body></html>';
}
res.status(200).send(html);
});
app.get('/slack/auth/redirect', (req, res) => {
if (!process.env.CLIENT_ID || !process.env.CLIENT_SECRET) {
console.error('ERROR: misisng env vars CLIENT_ID and CLIENT_SECRET for slack-oauth authentication');
return res.status(500).send('ERROR: misisng env vars CLIENT_ID and CLIENT_SECRET for slack-oauth authentication');
}
var options = {
uri: 'https://slack.com/api/oauth.access?code='
+req.query.code+
'&client_id='+process.env.CLIENT_ID+
'&client_secret='+process.env.CLIENT_SECRET,
method: 'GET'
}
request(options, (err, response, body) => {
if (err) {
console.error('slack oauth request error:', err);
return res.send(err);
}
let jsonResp = JSON.parse(body);
if (!jsonResp.ok){
console.error(jsonResp);
res.send("Error encountered: \n"+jsonResp).status(200).end();
} else {
console.log(jsonResp);
res.send("Success!")
}
})
});
let listener = app.listen(process.env.PORT || 3000, () => console.log("\n slack-poll-app service listening on port %d in %s mode", listener.address().port, app.get('env')));
let savedData = null;
function ready(cloudant) {
let db = cloudant.db.use(dbName);
db.get('polls', (err, body) => {
if (err) return console.error(err);
polls = body;
savedData = JSON.stringify(polls);
console.log('loaded polls from cloudant.');
});
setInterval(() => {
let data = JSON.stringify(polls);
if (savedData == data) return;
db.insert(polls, (err, body) => {
if (err) return console.error(err);
if (!body.ok) return console.error(err);
console.log('saved polls to cloudant.', body.rev);
polls._rev = body.rev;
savedData = JSON.stringify(polls);
});
}, 30000);
}
function connected(err, cloudant) {
if (err) return console.error(err);
cloudant.db.get(dbName, (err, body) => {
if (err) {
console.error(err);
if ( err.error == 'not_found') {
cloudant.db.create(dbName, (err, body) => {
if (err) return console.error(err);
ready(cloudant);
});
}
} else {
ready(cloudant);
}
});
}
try {
if (process.env.CLOUDANT_URL) cloudant = Cloudant(process.env.CLOUDANT_URL, connected);
else if (process.env.VCAP_SERVICES) cloudant = Cloudant({ vcapServices: JSON.parse(process.env.VCAP_SERVICES) }, connected);
} catch (ex) {
console.error(ex);
}