forked from github/hubot-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudapp.coffee
45 lines (41 loc) · 1.18 KB
/
cloudapp.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
# Description:
# Allow Hubot to show what's lurking behind a CloudApp link
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# http://cl.ly/* - Detects the drop's type and displays it or prints its content if it's an image or text file respectively
#
# Author:
# lmarburger
module.exports = (robot) ->
robot.hear /(https?:\/\/cl.ly\/image\/[A-Za-z0-9]+)(\/[^\/]+)?/i, (msg) ->
return if msg.match[2] # Ignore already embedded images.
link = msg.match[1]
msg
.http(link)
.headers(Accept: "application/json")
.get() (err, res, body) ->
unless res.statusCode is 200
msg.send "No drop at #{link}! It may have been deleted."
return
drop = JSON.parse body
switch drop.item_type
when 'image'
msg.send drop.content_url
when 'text'
send_drop_content msg, drop.content_url
send_drop_content = (msg, url) ->
msg
.http(url)
.get() (err, res, body) ->
if res.statusCode is 302
# Follow the breadcrumbs of redirects.
send_drop_content msg, res.headers.location
else
body += "\n" unless ~body.indexOf("\n")
msg.send body