Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 54 lines (45 sloc) 1.474 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
# Description:
# Returns the latest news headlines from Google
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot news - Get the latest headlines
# hubot news <topic> - Get the latest headlines for a specific topic
#
# Author:
# Matt McCormick

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)
Something went wrong with that request. Please try again.