hubot-scripts
Version:
Allows you to opt in to a variety of scripts
52 lines (47 loc) • 1.48 kB
text/coffeescript
module.exports = (robot) ->
robot.respond /xkcd(\s+latest)?$/i, (msg) ->
msg.http("http://xkcd.com/info.0.json")
.get() (err, res, body) ->
if res.statusCode == 404
msg.send 'Comic not found.'
else
object = JSON.parse(body)
msg.send object.title, object.img, object.alt
robot.respond /xkcd\s+(\d+)/i, (msg) ->
num = "#{msg.match[1]}"
msg.http("http://xkcd.com/#{num}/info.0.json")
.get() (err, res, body) ->
if res.statusCode == 404
msg.send 'Comic #{num} not found.'
else
object = JSON.parse(body)
msg.send object.title, object.img, object.alt
robot.respond /xkcd\s+random/i, (msg) ->
msg.http("http://xkcd.com/info.0.json")
.get() (err,res,body) ->
if res.statusCode == 404
max = 0
else
max = JSON.parse(body).num
num = Math.floor((Math.random()*max)+1)
msg.http("http://xkcd.com/#{num}/info.0.json")
.get() (err, res, body) ->
object = JSON.parse(body)
msg.send object.title, object.img, object.alt