This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathnews.coffee
53 lines (45 loc) · 1.44 KB
/
news.coffee
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(/'/g, "'").replace(/`/g, "'").replace(/"/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)