Skip to content

Commit 480e827

Browse files
authored
adds news gathering by generic technology topic or specified topic (#80)
1 parent 6d77648 commit 480e827

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

Diff for: helper/news.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const getNews = (robot, topic = "technology") => {
2+
let twoDaysAgo = new Date()
3+
twoDaysAgo.setDate(twoDaysAgo.getDate() - 2)
4+
let today = new Date()
5+
6+
return new Promise((resolve, reject) => {
7+
let response = '';
8+
//this is a free API and a public API key.
9+
robot.http(`http://newsapi.org/v2/top-headlines?q=${topic}&from=${twoDaysAgo}&to=${today}&apiKey=835dc66684334edf8cd1d542023afc08`)
10+
.get()((err, resp, body) => {
11+
if (err) {
12+
reject(new Error(err))
13+
} else {
14+
const data = JSON.parse(body)
15+
16+
if (data.status !== 'ok') {
17+
reject(new Error('Something went wrong getting the news!'))
18+
}
19+
20+
if(data.totalResults > 0) {
21+
data.articles.forEach(article => {
22+
response += `\n Source: ${article.source.name} \n`
23+
response += ` Title: ${article.title} \n`
24+
response += ` Url: ${article.url}`
25+
})
26+
}
27+
28+
resolve(response)
29+
}
30+
})
31+
})
32+
}
33+
34+
module.exports = getNews

Diff for: scripts/news.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Description:
2+
// A bot to gather news based on a topic
3+
//
4+
// Author:
5+
// Capocaccia
6+
//
7+
// Commands:
8+
// hubot news
9+
// hubot news topic
10+
11+
12+
const getNews = require('../helper/news.js')
13+
14+
module.exports = robot => {
15+
robot.respond(/(news)\s*(\w*)/i, (res) => {
16+
keyword = res.match[2].trim()
17+
getNews(robot, keyword)
18+
.then((data) => {
19+
res.reply(JSON.stringify(data))
20+
})
21+
})
22+
};
23+

0 commit comments

Comments
 (0)