-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
65 lines (54 loc) · 1.83 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
var express = require("express");
var request = require('request');
var logfmt = require("logfmt");
var app = express();
app.use(logfmt.requestLogger());
app.use(express.bodyParser());
app.post('/:id/:secret', function(req, res) {
var token = null;
if (process.env.slack_token === process.env.slack_token + '') {
// Token is specified and is string
token = process.env.slack_token;
}
else {
res.send('Failed: no token specified in environment variables');
return;
}
if (req.params.id === req.params.id + '') {
// File id is specified and is string
var fileId = req.params.id;
var secret = req.params.secret;
if (process.env.slack_secret === process.env.slack_secret + '') {
// If secret is specified in environment variables, validate it
var isSecretValid = process.env.slack_secret === secret;
if (!isSecretValid) {
return;
}
}
var url = 'https://slack.com/api/files.info?token=' + token + '&file=' + fileId;
request.get({ url: url, json: true },
function (error, response, result) {
if (!error && response.statusCode == 200) {
var content = result.content;
content = content + '\n' + req.body.text;
var postUrl = 'https://slack.com/api/files.edit';
request.post({ url: postUrl, json: true, form: {token: token, file: fileId, content: content} },
function (error, response, result) {
if (!error && response.statusCode == 200) {
res.send('Thank you. Brady will comply.');
}
else {
res.send('Error: ' + error);
}
});
}
});
}
else {
res.send('Failed: no file id in URL');
}
});
var port = process.env.PORT || 5000;
app.listen(port, function() {
console.log("Listening on " + port);
});