forked from github/hubot-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitly.coffee
44 lines (41 loc) · 1.34 KB
/
bitly.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
# Description:
# Shorten URLs with bit.ly & expand detected bit.ly URLs
#
# Dependencies:
# None
#
# Configuration:
# HUBOT_BITLY_ACCESS_TOKEN
#
# Commands:
# hubot (bitly|shorten) (me) <url> - Shorten the URL using bit.ly
# http://bit.ly/[hash] - looks up the real url
#
# Author:
# sleekslush
# drdamour
# johnwyles
module.exports = (robot) ->
robot.respond /(bitly|shorten)\s?(me)?\s?(.+)$/i, (msg) ->
msg
.http("https://api-ssl.bitly.com/v3/shorten")
.query
access_token: process.env.HUBOT_BITLY_ACCESS_TOKEN
longUrl: msg.match[3]
format: "json"
.get() (err, res, body) ->
response = JSON.parse body
msg.send if response.status_code is 200 then response.data.url else response.status_txt
#TODO: can we make this list more expansive/dynamically generated?
robot.hear /(https?:\/\/(bit\.ly|yhoo\.it|j\.mp|pep\.si|amzn\.to)\/[0-9a-z\-]+)/ig, (msg) ->
msg
.http("https://api-ssl.bitly.com/v3/expand")
.query
access_token: process.env.HUBOT_BITLY_ACCESS_TOKEN
shortUrl: msg.match
.get() (err, res, body) ->
parsedBody = JSON.parse body
if parsedBody.status_code is not 200
msg.send "Lookup failed #{response.status_txt}"
return
msg.send "#{m.short_url} is #{m.long_url}" for m in parsedBody.data.expand