-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.js
79 lines (64 loc) · 2.76 KB
/
app.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
var express = require('express');
var bodyParser = require('body-parser');
var http = require('http');
var app = express();
var port = process.env.PORT || 3000;
// vars this app uses to know who has chatted recently.
var prevUserName = '';
var userName = 'Beyonce';
// Need this to get user's ip address correctly.
app.enable('trust proxy');
// body parser middleware
app.use(bodyParser.urlencoded({ extended: true }));
// kanye west will interrupt all post requests to this app.
app.post('*', function immaLetYouFinish (req, res) {
var data = req.body;
// if the token doesn't match, the request is not coming from Slack. 401 = unauthorized
if (process.env.OUTGOING_WEBHOOK_TOKEN && data.token !== process.env.OUTGOING_WEBHOOK_TOKEN) {
return res.status(401).end();
}
// if slackbot is chatting, return. if we allow him through, we can get an infinite loop.
// slack can ban this app for spamming the chatroom.
if (data.user_name === 'slackbot') {
return res.status(200).end();
}
// if this user is the same one as the last one, don't change prevUserName. Otherwise, userName is stored in prevUserName.
prevUserName = req.body.user_name === userName ? prevUserName : userName;
// userName is the user that is currently chatting.
userName = req.body.user_name;
// there's a 0.5% chance of interrupting the conversation.
var probability = Math.random();
if (probability <= 0.005) {
// kanye west's response
var botResponse = {
icon_url: 'http://i.imgur.com/GSEfJzI.jpg',
username: 'Kanye West',
text: 'Yo, @' + userName + ', I\'m really happy for you, Imma let you finish, but @' + prevUserName + ' had one of the best comments of all time! One of the best comments of all time!'
};
// reply to the channel.
console.log('Hmmmm, I feel like interrupting ' + userName + '['+ probability +']');
res.json(botResponse);
}
else {
// otherwise reply to the server and don't keep it hanging. too many timeouts and slack disables your integration.
console.log('Yeah, yeah. Whatever. ['+probability+']');
return res.status(200).end();
}
});
// kanye also replies to everyone else doing a get request.
app.get('*', function(req, res) {
var ip = req.ip;
res.status(200).send('Yo ' + ip + ', I’m really happy for you, Imma let you finish, but Beyonce had one of the best videos of all time...one of the best videos of all time!');
});
// error handler
app.use(function (err, req, res) {
console.error(err.stack);
res.status(400).send(err.message);
});
app.listen(port, function () {
console.log('Kanye West is listening on port ' + port);
});
// Keep app alive on heroku
setInterval(function(){
http.get('http://imma-let-you-finish.herokuapp.com', console.log.bind(null, 'Keeping Kanye Alive.'));
}, 60 * 1000);