-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
76 lines (68 loc) · 2.27 KB
/
routes.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
module.exports = function(app) {
var boganisms = require('./boganisms'),
slack = require('./slack');
// the main page
app.get("/", function (request, response) {
response.render('index', {
title: boganisms.one(),
projectName: process.env.PROJECT_NAME,
});
});
// the examples on the main page
app.get("/quotes", function (request, response) {
response.send(boganisms.heaps(3));
});
// "Add to Slack" action
app.get('/auth', function (request, response) {
response.redirect('https://slack.com/oauth/authorize?scope=chat:write:bot,commands&client_id=' + process.env.SLACK_CLIENT_ID);
});
// https://api.slack.com/docs/oauth
app.get('/auth/grant', function (request, response) {
// get the code, turn it into a token
var code = request.query.code;
slack.oauth(code).then(function(body) {
/* {
ok: true,
access_token: for posting without being asked,
scope: 'identify,commands,chat:write:bot',
user_id: user id,
team_name: team name,
team_id: team id
} */
// TODO: stash the creds somewhere permanent so we can use the token later
// TODO: a confirmation page!?
response.redirect('/');
}).catch(function(error) {
console.log(error);
response.send(error);
});
});
app.get('/bogan', function (request, response) {
response.send(boganisms.one());
});
// https://api.slack.com/slash-commands
app.post('/bogan', function (request, response) {
/* {
token: for verification,
team_id: team id,
team_domain: <domain>.slack.com,
channel_id: channel id,
channel_name: e.g., general,
user_id: user id,
user_name: no @,
command: '/bogan',
text: whatever they typed after bogan,
response_url: to post more in the channel
} */
if (request.body.token == process.env.SLACK_TOKEN) {
slack.respond(boganisms.one(), request.body.response_url) // follow up in the channel for all to see
.then(function(body) {
response.send(''); // Empty 200 prevents the "only you can see this" response
}).catch(function(error) {
response.status(500).send(error);
});
} else {
response.status(400).send('nope');
}
});
};