File tree 2 files changed +57
-0
lines changed
2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 ( / ( n e w s ) \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
+
You can’t perform that action at this time.
0 commit comments