This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (54 loc) · 1.64 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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var bodyParser = require('body-parser');
var _ = require('lodash');
var https = require('https');
var port = process.env.PORT || 3000;
var repeat = function(str, count) {
var output = str;
for (var ctr = 0; ctr < count - 1; ctr++) {
output += str;
}
return output;
};
// Gotta go fast!
var catchPhrase = function(excitement) {
var howFast;
// Range-checks, yo
excitement = excitement && Math.max(Math.min(excitement, 100), 1);
howFast = repeat('a', excitement || Math.floor(Math.random() * 10, 10));
return _.template('Gotta go f<%= howFast %>st!')({ howFast: howFast });
};
var displaySlackMessage = function(message) {
// For Slack
var slackData = { text: message };
var slackOptions = {
hostname: 'hooks.slack.com',
port: 443,
path: process.env.SLACK_ENDPOINT,
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
};
var slackReq = https.request(slackOptions);
slackReq.write(JSON.stringify(slackData));
slackReq.end();
};
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/', function(req, res) {
res.send('Ready!');
});
app.post('/say', function(req, res) {
displaySlackMessage(req.body.text || catchPhrase());
res.send(req.body.text);
});
app.post('/', function(req, res) {
var sanicSaysWut = catchPhrase(parseInt(req.body.text, 10));
displaySlackMessage(sanicSaysWut);
res.send(sanicSaysWut);
});
http.listen(port, function() {
console.log('Ready on port ' + port + '!');
});