-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (52 loc) · 1.6 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
'use strict';
const config = require('./config.json');
const flip = require('flip')
const request = require('request')
function verifyWebhook(body) {
if (!body || body.token !== config.SLACK_TOKEN) {
const error = new Error('Invalid credentials');
error.code = 401;
throw error;
}
}
function makePayload(body) {
const channel = body.channel_name === 'directmessage' ?
body.channel_id : '#' + body.channel_name;
const text = body.text ? flip(body.text) : '┻━┻';
var payload = {
channel: channel,
text: '(╯°□°)╯︵ ' + text,
icon_emoji: ':rage1:'
};
if (config.NO_RAGE) {
delete payload.icon_emoji;
}
return payload;
}
exports.tableFlip = function tableFlip(req, res) {
return Promise.resolve().then(() => {
if (req.method !== 'POST') {
const error = new Error('Only POST requests are accepted');
error.code = 405;
throw error;
}
verifyWebhook(req.body);
return makePayload(req.body);
}).then((response) => {
request.post({
url: config.SLACK_WEBHOOK,
form: { payload: JSON.stringify(response) }
}, function (err, resp, body) {
if (err) {
const error = new Error('{ success: false, error: err.message }');
error.code(500);
throw error;
}
})
res.end();
}).catch((err) => {
console.error(err);
res.status(err.code || 500).send(err);
return Promise.reject(err);
});
};