Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

44 lines (34 sloc) 1.344 kb
# Returns the latest news headlines from Google
#
# news - Get the latest headlines
# news <topic> - Get the latest headlines for a specific topic
module.exports = (robot) ->
robot.respond /news(?: me| on)?\s?(.*)/, (msg) ->
query msg, (response, err) ->
return msg.send err if err
strings = []
topic = msg.match[1]
if (topic != "")
strings.push "Here's the latest news on \"#{topic}\":\n"
else
strings.push "Here's the latest news headlines:\n"
for story in response.responseData.results
strings.push story.titleNoFormatting.replace(/&#39;/g, "'").replace(/`/g, "'").replace(/&quot;/g, "\"")
strings.push story.unescapedUrl + "\n"
msg.send strings.join "\n"
query = (msg, cb) ->
if (msg.match[1] != "")
msg.http("https://ajax.googleapis.com/ajax/services/search/news?v=1.0&rsz=5")
.query(q: msg.match[1])
.get() (err, res, body) ->
complete cb, body, err
else
msg.http("https://ajax.googleapis.com/ajax/services/search/news?v=1.0&rsz=5&topic=h")
.get() (err, res, body) ->
complete cb, body, err
complete = (cb, body, err) ->
try
response = JSON.parse body
catch err
err = "Sorry, but I could not fetch the latest headlines."
cb(response, err)
Jump to Line
Something went wrong with that request. Please try again.