|
1 | 1 | # Description:
|
2 |
| -# Makes ReplyGif easier to use. See http://replygif.net. |
| 2 | +# Show ReplyGifs based on tags. See http://replygif.net. |
3 | 3 | #
|
4 | 4 | # Dependencies:
|
5 |
| -# "cheerio": ">= 0.9.2" |
| 5 | +# None |
6 | 6 | #
|
7 | 7 | # Configuration:
|
8 |
| -# None |
| 8 | +# HUBOT_REPLYGIF_API_KEY: the api key for replygif.net, defaults to public key "39YAprx5Yi" |
9 | 9 | #
|
10 | 10 | # Commands:
|
11 |
| -# http://replygif.net/<id> - Embeds image from ReplyGif with that id. |
12 |
| -# hubot replygif <keyword> - Embeds random ReplyGif with the keyword. |
13 |
| -# hubot replygif me <keyword> - Same as `hubot replygif <keyword>`. |
| 11 | +# hubot replygif <tag> - Embed a random ReplyGif with the given tag. |
| 12 | +# hubot replygif me <tag> - Same as `hubot replygif <tag>`. |
| 13 | +# hubot replygif id <id> - Embed the ReplyGif with the given id |
| 14 | +# hubot replygif me id <id> - Same as `hubot replygif id <id>`. |
14 | 15 | #
|
15 | 16 | # Notes:
|
16 |
| -# None |
| 17 | +# Use 'rg' as shorthand for the 'replygif' command |
17 | 18 | #
|
18 | 19 | # Author:
|
19 |
| -# sumeetjain, meatballhat |
| 20 | +# altschuler (previous non-api version by sumeetjain, meatballhat) |
20 | 21 |
|
21 |
| -cheerio = require 'cheerio' |
| 22 | +apiKey = process.env.HUBOT_REPLYGIF_API_KEY or "39YAprx5Yi" |
22 | 23 |
|
23 |
| -module.exports = (robot) -> |
24 |
| - # Listen for someone to link to a ReplyGif and reply with the image. |
25 |
| - robot.hear /.*replygif\.net\/(i\/)?(\d+).*/i, (msg) -> |
26 |
| - id = msg.match[2] |
27 |
| - msg.send "http://replygif.net/i/#{id}#.gif" |
28 |
| - |
29 |
| - # Listen for a command to look up a ReplyGif by ID. |
30 |
| - robot.respond /replygif( me)? (\d+)/i, (msg) -> |
31 |
| - id = msg.match[2] |
32 |
| - msg.send "http://replygif.net/i/#{id}#.gif" |
| 24 | +apiUrl = "http://replygif.net/api/gifs?api-key=#{apiKey}" |
33 | 25 |
|
34 |
| - # Listen for a command to look up a ReplyGif by tag. |
35 |
| - robot.respond /replygif( me)? (\D+)/i, (msg) -> |
36 |
| - replyGifByTag(msg, msg.match[2]) |
| 26 | +module.exports = (robot) -> |
| 27 | + apiCall = (msg, failMsg, query) -> |
| 28 | + robot.http(apiUrl + query).get() (err, res, body) -> |
| 29 | + try |
| 30 | + gifs = JSON.parse body |
| 31 | + if not gifs? or not gifs.length |
| 32 | + msg.send failMsg |
| 33 | + else |
| 34 | + msg.send (msg.random gifs).file |
37 | 35 |
|
38 |
| -replyGifByTag = (msg, tag) -> |
39 |
| - msg |
40 |
| - .http("http://replygif.net/t/#{tagify(tag)}") |
41 |
| - .header('User-Agent: ReplyGIF for Hubot (+https://github.com/github/hubot-scripts)') |
42 |
| - .get() (err, res, body) -> |
43 |
| - if not err and res.statusCode is 200 |
44 |
| - msg.send msg.random getGifs(body) |
45 |
| - else |
46 |
| - msg.send 'No GIF for you, human.' |
| 36 | + robot.hear /.*replygif\.net\/(i\/)?(\d+)(?!.*\.gif).*/i, (msg) -> |
| 37 | + id = msg.match[2] |
| 38 | + msg.send "http://replygif.net/i/#{id}.gif" |
47 | 39 |
|
48 |
| -getGifs = (body) -> |
49 |
| - $ = cheerio.load(body) |
50 |
| - $('img.gif[src]').map (i, elem) -> |
51 |
| - elem.attribs.src.replace(/thumbnail/, 'i') |
| 40 | + robot.respond /(replygif|rg)( me)? ([\w|\ ]+)/i, (msg) -> |
| 41 | + tag = msg.match[3] |
| 42 | + if tag is "id" then return # hubot's looking for an id |
| 43 | + apiCall msg, "I don't know that reaction", "&tag=#{tag}" |
52 | 44 |
|
53 |
| -tagify = (s) -> |
54 |
| - s.toLowerCase().replace(/\s+/g, '-').replace(/[^-a-z]/g, '') |
| 45 | + robot.respond /(replygif|rg)( me)? id (\d+)/i, (msg) -> |
| 46 | + id = msg.match[3] |
| 47 | + apiCall msg, "I don't any gifs with that id", "&id=#{id}" |
0 commit comments