Last active
July 7, 2017 11:55
-
-
Save jonathan-nwosu/5e45dd70e07843b24da27a02ec2c040a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const Restify = require('restify'); | |
const server = Restify.createServer({ | |
name: "NewsBot" | |
}); | |
const request = require('request'); | |
const PORT = process.env.PORT || 3000; | |
server.use(Restify.bodyParser()); | |
server.use(Restify.jsonp()); | |
const getNews = (newsSource, cb) => { | |
var http = require('http'); | |
var url = `http://newsapi.org/v1/articles?source=bloomberg&apiKey=6e6688c27d54463b8b0b9e250d6c20cd`; | |
http.get(url, function(res){ | |
var body = ''; | |
res.on('data', function(chunk){ | |
body += chunk; | |
}); | |
res.on('end', function(){ | |
//body = body.replace(/\\/g,''); | |
var quote = JSON.parse(body); | |
var string = quote.articles; | |
cb(null, `TOP NEWS: 1) ` + string[0].title); | |
}); | |
}); | |
} | |
//POST route handler | |
server.post('/', (req, res, next) => {let{status, result} = req.body; | |
if(status.code === 200 && result.action === 'getnews'){ | |
const { | |
newsSource | |
} = result.parameters; | |
getNews(newsSource, (error, result) => { | |
console.log(result.action); | |
if(!error && result){ | |
res.json({speech: result, displayText: result, source: "newsbot" | |
}); | |
}}); | |
} | |
return next(); | |
}); | |
server.listen(PORT, () => console.log(`NewsBot running on ${PORT}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment