Skip to content

Commit bdd7aed

Browse files
committed
Merge pull request github#1016 from jongd/patch-1
Fix Giphy script
2 parents 079482e + e0c7cfc commit bdd7aed

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

src/scripts/giphy.coffee

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
# Description:
22
# A way to search images on giphy.com
33
#
4+
# Configuration:
5+
# HUBOT_GIPHY_API_KEY
6+
#
47
# Commands:
58
# hubot gif me <query> - Returns an animated gif matching the requested search term.
69

10+
giphy =
11+
api_key: process.env.HUBOT_GIPHY_API_KEY
12+
base_url: 'http://api.giphy.com/v1'
13+
714
module.exports = (robot) ->
815
robot.respond /(gif|giphy)( me)? (.*)/i, (msg) ->
916
giphyMe msg, msg.match[3], (url) ->
1017
msg.send url
1118

1219
giphyMe = (msg, query, cb) ->
13-
q = q: query
14-
msg.http('http://giphy.com/api/gifs/search')
15-
.query(q)
20+
endpoint = '/gifs/search'
21+
url = "#{giphy.base_url}#{endpoint}"
22+
23+
msg.http(url)
24+
.query
25+
q: query
26+
api_key: giphy.api_key
1627
.get() (err, res, body) ->
17-
response = JSON.parse(body)
18-
images = response.data.gifs
19-
if images.length > 0
20-
image = msg.random images
21-
cb image.original_url
28+
response = undefined
29+
try
30+
response = JSON.parse(body)
31+
images = response.data
32+
if images.length > 0
33+
image = msg.random images
34+
cb image.images.original.url
35+
36+
catch e
37+
response = undefined
38+
cb 'Error'
39+
40+
return if response is undefined

0 commit comments

Comments
 (0)