From df68dd1e60fa2c562ed14af85bcddc5af7ad600a Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Sat, 15 Aug 2015 17:30:30 +0200 Subject: [PATCH] Add url shortener script --- package.json | 1 + scripts/shortener.coffee | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 scripts/shortener.coffee diff --git a/package.json b/package.json index 452eb88..73e93c8 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "nock": "^1.7.1", "nodepie": "^0.6.7", "soupselect": "^0.2.0", + "valid-url": "^1.0.9", "weak": "^0.4.0" }, "scripts": { diff --git a/scripts/shortener.coffee b/scripts/shortener.coffee new file mode 100644 index 0000000..16e0c90 --- /dev/null +++ b/scripts/shortener.coffee @@ -0,0 +1,31 @@ +# Description: +# Accorcia un link usando l'api di breve.xyz +# +# Dependencies: +# valid-url +# +# Configuration: +# None +# +# Commands: +# hubot accorcia/breve - accorcia un link +# +# Author: +# Michele Guerini Rocco (rnhmjoj) +# + +{ is_web_uri } = require 'valid-url' + +valid = ['eccoti: ', 'ecco qui: ', 'fatto: ', ''] +invalid = ['eh?', "cos'รจ sta roba?", 'sarebbe un url questo?'] + +module.exports = (robot) -> + robot.respond /breve|accorcia (.+)$/i, (res) -> + url = res.match[1] + if is_web_uri url + robot.http('https://breve.xyz/api') + .header('Content-Type','application/x-www-form-urlencoded') + .post("url=#{url}") (err, r, body) -> + res.send (res.random valid) + (JSON.parse body).link + else + res.send res.random invalid \ No newline at end of file