-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
61 lines (52 loc) · 1.47 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
var express = require('express'),
request = require('request'),
bodyParser = require('body-parser'),
async = require('async');
app = express();
app.use(bodyParser.json());
app.post('/', function(req, res){
async.waterfall([
function(callback) {
request.get('https://hacker-news.firebaseio.com/v0/topstories.json',
function(err, r, body) {
body = JSON.parse(body);
var random = Math.floor(Math.random() * (body.length - 1));
callback(null, body[random])
}
);
},
function(id, callback){
request.get('https://hacker-news.firebaseio.com/v0/item/' + id + '.json',
function(err, r, body) {
// Parse out the valuable information.
body = JSON.parse(body);
if(body.url == "")
body.url = "https://news.ycombinator.com/item?id=" + id;
var message = {
"text": "Check this out:",
"attachments": [
{
"fallback": "Something went wrong :(\nWe know you're bored... try the command again maybe?",
"title": body.title,
"title_link": body.url,
"text": body.text
}
]
}
res.send(message);
callback(null, 'done');
}
);
}
], function(err, result) {
if(err)
console.log(err);
else
console.log("Relieved boredom.");
});
});
var server = app.listen(process.env.PORT || 3000, function() {
var host = server.address().address;
var port = server.address().port;
console.log('Start slacking now at http://%s:%s', host, port);
});