Skip to content

Commit 3fc45d8

Browse files
author
Carter Capocaccia
authored
News Bug Fixes (#82)
* adds news gathering by generic technology topic or specified topic * resolves bug with date strings and keyword argument going undefined
1 parent 2df97e0 commit 3fc45d8

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

helper/news.js

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1-
const getNews = (robot, topic = "technology") => {
1+
const getNews = (robot, topic) => {
22
let twoDaysAgo = new Date()
33
twoDaysAgo.setDate(twoDaysAgo.getDate() - 2)
4-
let today = new Date()
4+
twoDaysAgo = twoDaysAgo.toISOString();
5+
let today = new Date().toISOString()
56

67
return new Promise((resolve, reject) => {
78
let response = '';
89
//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+
robot.http(`http://newsapi.org/v2/top-headlines?q=${topic}&from=${twoDaysAgo}&to=${today}&pageSize=3&apiKey=835dc66684334edf8cd1d542023afc08`)
1011
.get()((err, resp, body) => {
1112
if (err) {
1213
reject(new Error(err))
1314
} else {
1415
const data = JSON.parse(body)
1516

16-
if (data.status !== 'ok') {
17+
if (data.status === 'error') {
18+
console.log(data)
1719
reject(new Error('Something went wrong getting the news!'))
1820
}
1921

2022
if(data.totalResults > 0) {
23+
2124
data.articles.forEach(article => {
2225
response += `\n Source: ${article.source.name} \n`
2326
response += ` Title: ${article.title} \n`
2427
response += ` Url: ${article.url}`
2528
})
29+
30+
} else {
31+
response += `No news found for the topic: ${topic}`
32+
2633
}
2734

2835
resolve(response)

scripts/news.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const getNews = require('../helper/news.js')
1414
module.exports = robot => {
1515
robot.respond(/(news)\s*(\w*)/i, (res) => {
1616
keyword = res.match[2].trim()
17+
keyword = keyword.length > 0 ? keyword : "technology"
1718
getNews(robot, keyword)
1819
.then((data) => {
1920
res.reply(JSON.stringify(data))

0 commit comments

Comments
 (0)