File tree 2 files changed +12
-4
lines changed
2 files changed +12
-4
lines changed Original file line number Diff line number Diff line change 1
- const getNews = ( robot , topic = "technology" ) => {
1
+ const getNews = ( robot , topic ) => {
2
2
let twoDaysAgo = new Date ( )
3
3
twoDaysAgo . setDate ( twoDaysAgo . getDate ( ) - 2 )
4
- let today = new Date ( )
4
+ twoDaysAgo = twoDaysAgo . toISOString ( ) ;
5
+ let today = new Date ( ) . toISOString ( )
5
6
6
7
return new Promise ( ( resolve , reject ) => {
7
8
let response = '' ;
8
9
//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` )
10
11
. get ( ) ( ( err , resp , body ) => {
11
12
if ( err ) {
12
13
reject ( new Error ( err ) )
13
14
} else {
14
15
const data = JSON . parse ( body )
15
16
16
- if ( data . status !== 'ok' ) {
17
+ if ( data . status === 'error' ) {
18
+ console . log ( data )
17
19
reject ( new Error ( 'Something went wrong getting the news!' ) )
18
20
}
19
21
20
22
if ( data . totalResults > 0 ) {
23
+
21
24
data . articles . forEach ( article => {
22
25
response += `\n Source: ${ article . source . name } \n`
23
26
response += ` Title: ${ article . title } \n`
24
27
response += ` Url: ${ article . url } `
25
28
} )
29
+
30
+ } else {
31
+ response += `No news found for the topic: ${ topic } `
32
+
26
33
}
27
34
28
35
resolve ( response )
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ const getNews = require('../helper/news.js')
14
14
module . exports = robot => {
15
15
robot . respond ( / ( n e w s ) \s * ( \w * ) / i, ( res ) => {
16
16
keyword = res . match [ 2 ] . trim ( )
17
+ keyword = keyword . length > 0 ? keyword : "technology"
17
18
getNews ( robot , keyword )
18
19
. then ( ( data ) => {
19
20
res . reply ( JSON . stringify ( data ) )
You can’t perform that action at this time.
0 commit comments