forked from brianpgerson/callie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
56 lines (49 loc) · 1.58 KB
/
router.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
const express = require('express'),
path = require('path'),
Bot = require('./lib/models/bot'),
SlackNode = require('slack-node');
slack = new SlackNode();
function handleSignup (req, res, countdownBot) {
let code = req.query.code;
slack.api('oauth.access', {
client_id: process.env.SLACK_CLIENT,
client_secret: process.env.SLACK_SECRET,
code: code
}, function (err, response) {
if (!!err || !response.bot) {
console.error(err);
res.status(500).sendFile(path.join(__dirname + '/public/error.html'));
} else {
const botAccessToken = response.bot.bot_access_token;
const botId = response.bot.bot_user_id;
const teamId = response.team_id;
const teamName = response.team_name;
Bot.findOne({teamId: teamId}).then(function (bot) {
if (bot) {
console.error('bot already exists for team', teamId);
res.sendFile(path.join(__dirname + '/public/oops.html'));
} else {
const bot = new Bot({
botAccessToken: botAccessToken,
userId: botId,
teamId: teamId,
teamName: teamName
});
bot.save().then(bot => {
countdownBot.onSignupSuccess(botAccessToken, response);
res.sendFile(path.join(__dirname + '/public/success.html'));
});
}
});
}
});
}
module.exports = function(app, db, countdownBot) {
// Initializing route groups
app.use("/", express.static(__dirname + '/public/'));
app.get('/success', (req, res) => handleSignup(req, res, countdownBot));
// Handle any other bad routes
app.use('/*', (req, res) => {
res.status(500).sendFile(path.join(__dirname + '/public/error.html'));
});
};