-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
81 lines (64 loc) · 1.99 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
/*
app.js
the entry point for the do.bot node app.
*/
require('dotenv').config();
process.env.TZ = 'America/New_York';
const express = require('express'),
bodyParser = require('body-parser'),
request_module = require('request'),
utils = require('./utils'),
glob = require('glob'),
path = require('path'),
slack = require('./models/slack').slack_client;
const app = express(),
port = process.env.port || 9911;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
/**
* add some common utilities to the request object
*/
app.use(function(request, response, next){
request.slack = slack;
request.external = request_module;
request.messageChannel = utils.messageChannel;
request.messageUser = utils.messageUser;
request.nextColor = utils.nextColor;
//add this field to the response header so we can handle requests from the site
response.set({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': ['POST', 'GET'],
'Access-Control-Allow-Headers': '*'
});
next();
});
/**
* verify every reqeust aginst the token sent from the slack server
*/
app.use(function(request, response, next){
let token = process.env.SLACK_VERIFICATION_TOKEN,
request_token = request.body.token;
if('payload' in request.body){
let payload = JSON.parse(request.body.payload);
request_token = payload.token;
}
if(!token || token != request_token){
console.error(`Verification tokens do not match`);
return response.send(400);
}
next();
});
// load all of the apps
glob.sync('./apps/*/app.js').forEach(function(file){
console.info(`Loading app: ${file}`);
require(path.resolve(file));
});
app.get('/', function(req, res){
res.send('do.bot');
});
app.listen(port, function(){
console.log('do.bot is running on port ' + port);
});
require('./routes')(app);