-
Notifications
You must be signed in to change notification settings - Fork 8
/
server.js
66 lines (56 loc) · 2.07 KB
/
server.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
const express = require('express');
const axios = require('axios');
const bodyParser = require('body-parser');
const flatten = require('flat');
const path = require('path');
const sslRedirect = require('heroku-ssl-redirect').default;
const app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(express.json({
type: [
'application/json',
'text/plain'
]
}));
app.use(sslRedirect());
// Redirect http calls to https
// comment this out if you aren't using SSL
// app.use(function(req, res, next) {
// if (!req.secure && process.env.NODE_ENV === 'production') {
// // request was via http, so redirect to https
// res.redirect('https://' + req.headers.host + req.url);
// } else {
// // request was via https or on dev instance, so do no special handling
// next();
// }
// });
app.use(express.static('public'))
app.post(['/workflows/*', '/triggers/*'], (req, res) => {
res.send('ok');
const payload = req.body;
// Slack's new Workflow builder restricts variable keys to
// "a combination of letters, numbers, hyphens, and underscores."
//
// Maintain backwards compatibility with legacy workflow builder
// where a '.' is supported to delimit flattened payloads
const delimiter = req.path.includes('/triggers/') ? '_' : '.';
const flatPayload = flatten(payload, { delimiter: delimiter });
// workflow builder requires values to be strings
// iterate over every value and convert it to string
Object.keys(flatPayload).forEach((key) => {
flatPayload[key] = '' + flatPayload[key];
})
axios.post(`https://hooks.slack.com${req.path}`, flatPayload)
})
app.get('/', (req, res) => {
// this route should ask you to post your slack webhook urls and give you the webhook to supply to github
// (Essentially changes hooks.slack.com to our servers path)
res.sendFile(path.join(__dirname+'/public/index.html'));
console.log(req);
})
app.listen(port, () => {
console.log(`Server running at ${port}`);
})