-
Notifications
You must be signed in to change notification settings - Fork 7
/
hnbot.js
45 lines (36 loc) · 1.09 KB
/
hnbot.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
var request = require('request');
var async = require('async');
var https = require('https');
var WEBHOOK_URL = process.env.SLACK_WEBHOOK_URL;
module.exports = function (req, res, next) {
request('http://node-hnapi.herokuapp.com/news', function(err, response, items) {
if (err || response.statusCode != 200) {
return res.status(500).end();
}
items = JSON.parse(items);
var botPayload = 'Current Hacker News homepage:\n'
var index = 0;
async.forEach(items, function(item, cb) {
botPayload += '<%url%|%rank%. %title%>\n'
.replace('%url%', item.url)
.replace('%rank%', index + 1)
.replace('%title%', item.title);
index++;
cb();
}, function(err) {
var payload = {
text: botPayload,
username: 'HackerNews',
channel: req.query.channel_id
};
request.post({
url: WEBHOOK_URL,
body: JSON.stringify(payload)
}, function(err, resp, data) {
if (err) {
return res.status(500).end();
}
});
});
});
}