This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (84 loc) · 3.46 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
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
const express = require('express');
const fs = require('fs');
const SlackBot = require('./SlackBot');
const bodyParser = require('body-parser');
const dotenv = require('dotenv').config();
const app = express();
const port = process.env.PORT
var thisParser = new SlackBot(process.env.SLACK_TOKEN, process.env.SLACK_OAUTH, process.env.SLACK_ADMIN);
//ideally, our globals would be on something that is thread-safe and not in-memory. TODO: fix!
if(fs.existsSync('globals_'+process.env.BRANCH+'.json')) {
process.globals = JSON.parse(fs.readFileSync('globals_'+process.env.BRANCH+'.json'));
process.globals.slackbot = thisParser;
console.log("loaded globals from file!", Object.keys(process.globals.userInfo).length);
}
if(!process.globals) {
process.globals = {};
if(process.env.BRANCH == "master") {
process.globals.privilegedList = ['U0146Q88V2N'];
process.globals.privilegedChannels = [''];
process.globals.welcomeChannel = '';
process.globals.staffChannel = 'G015H8570E4';
process.globals.userInfo = {};
process.globals.teamChannels = {};
process.globals.preCheckin_list = [];
process.globals.pendingInvites = {};
process.globals.nodropin = [];
process.globals.lfgList = [];
} else if(process.env.BRANCH == "development") {
process.globals.privilegedList = ['U013W1ST95H'];
process.globals.privilegedChannels = [];
process.globals.welcomeChannel = '';
process.globals.staffChannel = 'G015H8570E4';
process.globals.userInfo = {};
process.globals.teamChannels = {};
process.globals.preCheckin_list = ["[email protected]", "[email protected]"];
process.globals.pendingInvites = {};
process.globals.nodropin = [];
process.globals.lfgList = [];
}
process.globals.slackbot = thisParser;
}
//Regularly save our globals to file
setInterval(function() {
//save globals to file ...
console.log("Doing save ...");
var data = JSON.stringify(process.globals, null, 2);
fs.writeFile('globals_'+process.env.BRANCH+'.json', data, (err) => {
if(err) {
console.log("A save error occurred! Saving an emergency backup ...", err);
fs.writeFileSync('globals_bak.json', data);
console.log("Emergency backup saved!");
}
console.log("Performed save ... len:"+data.length);
});
}, 60 * 1000);
//When we exit our script, make sure we perform a save.
function exitHandler(options, exitCode) {
if (options.cleanup) {
console.log("Going down, saving ...");
fs.writeFileSync('globals_'+process.env.BRANCH+'.json', JSON.stringify(process.globals, null, 2)); //HAS to be syncronous
}
if (exitCode || exitCode === 0) {
console.log("Going down "+exitCode);
}
if (options.exit) process.exit();
}
process.on('exit', exitHandler.bind(null,{cleanup:true}));
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
process.on('SIGUSR1', exitHandler.bind(null, {exit:true}));
process.on('SIGUSR2', exitHandler.bind(null, {exit:true}));
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
// Handle for Slack slash commands -- these are URL Encoded
app.use('/handle', bodyParser.urlencoded({ extended: true, verify: (req, res, buf) => {
req.rawBody = buf;
} }));
// Event for Slack reported events -- these are JSON encoded.
app.use('/event', bodyParser.json({ verify: (req, res, buf) => {
req.rawBody = buf;
} }));
app.use('/handle', thisParser.parse);
app.use('/event', thisParser.eventParse);
//what index??? we don't exist.
app.get('/', (req, res) => res.sendStatus(404))
app.listen(port, () => console.log(`Listening at ${port}`))