-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
106 lines (92 loc) · 3.23 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
'use strict';
const express = require('express');
const logger = require('morgan');
const _ = require('lodash');
const crypto = require('crypto');
const contentful = require('./contenful');
const getFact = require('./facts');
const profileFallback = '//fink.no/images/profileFallback.jpg';
function factObject(extra) {
return Object.assign({
text: 'Did you know?',
attachments: [{ text: getFact(), color: 'db2316' }],
}, extra);
}
const getEmplyees = role => contentful
.fetchContent('finksEmployees')
.then(res => res[0].fields.finkEmployee.map(empl => empl.fields))
.then(employees => employees.filter(empl => !!role ? empl.position.includes(role) : true));
const getEmplyeesBlocks = role => getEmplyees(role)
.then(employees => employees.map(empl => ({
type: "section",
text: {
type: "mrkdwn",
text: `*${empl.name}* (${empl.email})\n${empl.keyWords}`
},
accessory: {
type: "image",
image_url: `https:${empl.largeProfilePicture
&& empl.largeProfilePicture.fields.file.url
|| profileFallback}`,
alt_text: `${empl.name}`
}
})));
const verifySignature = (req, res, next) => {
const fail = () => res.status(401).end();
const signature = req.headers['x-slack-signature'];
const timestamp = req.headers['x-slack-request-timestamp'];
if (!signature || !timestamp) {
return fail();
}
const hmac = crypto.createHmac('sha256', process.env.SLACK_SIGNING_SECRET);
const [version, hash] = signature.split('=');
const sig_base = `${version}:${timestamp}:${req.rawBody}`;
hmac.update(sig_base);
const myHash = hmac.digest('hex');
return myHash === hash
? next()
: fail();
};
const app = express();
app.use(express.urlencoded({ extended: true, verify: (req, res, buf) => { req.rawBody = buf } }));
app.use(logger('dev'));
app.use(verifySignature)
app.post('/slack', async (req, res, next) => {
const [cmd, arg] = req.body.text.split(" ");
switch (cmd) {
case 'dev':
return getEmplyeesBlocks("Utvikler")
.then(employees => res.send({ blocks: employees }))
.catch(error => res.send(`error: ${error}`));
case 'design':
return getEmplyeesBlocks("Designer")
.then(employees => res.send({ blocks: employees }))
.catch(error => res.send(`error: ${error}`));
case 'group':
case 'gruppe':
return getEmplyees()
.then(employees => employees.map(empl => empl.name))
.then(names => _.shuffle(names))
.then(randomNames => _.chunk(randomNames, arg || 4))
.then(groups => groups.map((group, i) => ({
type: "section",
text: {
type: "mrkdwn",
text: `*Gruppe ${i}:*\n${group.join("\n")}`
}
})))
.then(result => res.send({ blocks: result }))
.catch(error => res.send(`error: ${error}`));
case 'fact':
case 'fakta':
return res.json(factObject({ response_type: 'in_channel' }));
default:
console.warn(`Unrecognized command ${cmd}`);
return res.send('Try `/fink fact`');
}
});
const server = app.listen(process.env.PORT || 8888, function () {
const host = server.address().address;
const port = server.address().port;
console.log('Listening at http://%s:%s', host, port);
});