-
Notifications
You must be signed in to change notification settings - Fork 7
/
web.js
143 lines (124 loc) · 4.43 KB
/
web.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// web.js
var _ = require('lodash');
var express = require("express");
var bodyParser = require('body-parser');
var logfmt = require("logfmt");
var request = require('request');
var PairDb = require('./lib/pair_db.js');
var debug = require('debug')('pair');
var db;
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use(logfmt.requestLogger());
var help = 'Usage:\t `/pair [yes|ok|no <your project here>]` or `/pair` alone to see who is free.';
function validToken(token) {
if (token == process.env.SLACK_TOKEN) {
debug('Slack token verified');
return true;
} else{
debug('Slack token does not match stored token, if present.');
return false;
}
}
function notifyChannel(text) {
payload = {
"channel": process.env.SLACK_PAIR_CHANNEL,
"username": "pair",
"text": text,
"icon_url": "http://s8.postimg.org/kmlmmglid/noun_19161_cc.png" // thx @ainsleywagon, https://thenounproject.com/term/pair/19161/
};
request.post({
uri: process.env.SLACK_WEBHOOK_URL,
body: JSON.stringify(payload),
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
debug(body);
} else if (error) {
console.error("Error posting to channel: " + error);
}
}
);
}
app.post('/', function(req, res) {
var hasArgs = req.body.text.length > 0;
var args = req.body.text.toLowerCase().split(' '),
command = req.body.command,
username = req.body.user_name,
token = req.body.token,
acceptable = ["yes", "ok", "no"];
var user, status, notification;
var pairList;
debug('args');
debug(args);
if (!validToken(token)) {
res.send('Invalid Slack token. Ensure that the correct Slack integration token is set as the SLACK_TOKEN env var.');
} else {
if (hasArgs) {
if (args[0] === 'help') {
// send help
res.send(
{
"response_type": "ephemeral",
"text": help + "\n" +
"Full documentation <https://github.com/techieshark/slack-pair|on Github>.",
});
} else if (_.contains(acceptable, args[0])) {
var comment = args.slice(1).join(' ');
status = args[0];
// get pairList from DB
pairList = db.getPairList();
pairList.fetch(function (err, pairList) {
pairList.update(username, status, comment, function (err, data) {
user = data.user; // users = data.users
// People want confirmation that we got their status, so show it and everyone else's:
notification = 'Your pairing status was set to: ' + status + '\n';
notification += pairList.toString();
res.send(notification);
// notify pairing channel if environment vars are provided and status is yes/ok
if (process.env.SLACK_WEBHOOK_URL && process.env.SLACK_PAIR_CHANNEL &&
(status === 'yes' || status === 'ok')) {
notifyChannel(user.username + " says '" + user.status + "' to pairing" + (user.comment ? " (" + user.comment + ")" : "" ) + "! Go pair!");
}
});
});
} else {
res.send('Close but no cigar. What is this command, "' + args[0] + '", that you speak of?\n' + help);
}
}
else {
// get pairList from DB
pairList = db.getPairList();
pairList.fetch(function (err, pairList) {
if(err) throw err;
status = pairList.toString();
debug(status);
res.send(status);
});
}
}
});
app.get('/keepalive', function (req, res) {
debug('pong');
res.send(Date.now()+'');
});
function keepalive() {
request(process.env.PAIRBOT_URL + '/keepalive');
}
db = PairDb.build();
db.connect(function (err) {
if (err) throw err;
// Start the application after the database connection is ready
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
console.log("Listening on " + port);
console.log("PAIR URL: " + process.env.PAIRBOT_URL);
console.log("SLACK_TOKEN: " + process.env.SLACK_TOKEN);
console.log("SLACK_WEBHOOK_URL: " + process.env.SLACK_WEBHOOK_URL);
console.log("SLACK_PAIR_CHANNEL: " + process.env.SLACK_PAIR_CHANNEL);
console.log("MONGO_URL: " + process.env.MONGO_URL);
console.log("DB_PROVIDER: " + process.env.DB_PROVIDER);
setInterval(keepalive, 60e3);
});
});