-
Notifications
You must be signed in to change notification settings - Fork 0
/
webServer.js
116 lines (100 loc) · 2.87 KB
/
webServer.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
const env = process.env;
var isInProduction = process.env.NODE_ENV == 'production';
var Slack = require('node-slack');
var express = require('express');
var bodyParser = require('body-parser');
if (!isInProduction) {
var ngrok = require('ngrok');
}
var multer = require('multer'); // v1.0.5
var app = express();
var webServer = {
server: null,
init: function (slackCommandResponder, authHandler) {
this.initServer();
this.registerToRequests(slackCommandResponder, authHandler);
this.startServer();
if (!isInProduction) {
this.startNgrok();
}
},
initServer: function() {
app.set('port', (process.env.PORT || 5000));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
},
startServer: function() {
this.server = app.listen(app.get('port'), function () {
console.log('Node app is running on port', app.get('port'));
}.bind(this));
},
registerToRequests: function(slackCommandResponder, authHandler) {
var upload = multer(); // for parsing multipart/form-data
var slack = new Slack();
// Listen for post messages
app.post('/', upload.array(), function (req, res, next) {
console.log("request received");
if (isInProduction) {
if (req.body && req.body.user_name) {
req.body.user_name = "[sanitized]";
req.body.team_domain = "[sanitized]";
if (req.body.channel_name != "directmessage" &&
req.body.channel_name != "privategroup") {
req.body.channel_name = "[sanitized]";
}
}
console.log("request sanitized");
}
console.log(req.body);
if (req.body.ssl_check == "1")
{
console.log("ssl check");
res.sendStatus(200);
return;
}
slack.respond(req.body, function (hook) {
if (hook.token == "[INSERT HOOK TOKEN HERE]") {
slackCommandResponder(hook, function(result) {
res.json(result);
});
}
else {
res.sendStatus(403);
}
});
});
// Listen for get messages
app.get('/', function(req, res){
res.send('/Oops is up and running');
});
app.get('/auth', function(req, res) {
authHandler(req, function(err, result) {
if (err) {
console.log("OAuth failed");
res.redirect('http://www.galgreen.com/oops/');
return;
}
console.log("OAuth succeeded :)");
res.redirect('http://www.galgreen.com/oops/success.html');
});
});
app.get('/health', function(req, res){
res.sendStatus(200);
});
console.log("requests registered");
},
startNgrok: function() {
ngrok.connect(3000, function (err, url) {
if (!err) {
// use this url in the slash command configurations
console.log("ngrok url: " + url);
}
else {
console.log("ngrok error: " + err);
}
});
},
};
module.exports = {
init: webServer.init.bind(webServer),
};