From a30e35b853a17039a7bc0857db3528cd52cd7e1a Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Thu, 14 Jul 2016 15:16:24 +0200 Subject: [PATCH] - Bitly: Inline-Query - Expand: Inline-Query --- otouto/plugins/bitly.lua | 28 +++++++++++++++++++++++++--- otouto/plugins/expand.lua | 28 ++++++++++++++++++++++++++-- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/otouto/plugins/bitly.lua b/otouto/plugins/bitly.lua index d1f9653..5b45ec7 100644 --- a/otouto/plugins/bitly.lua +++ b/otouto/plugins/bitly.lua @@ -18,6 +18,7 @@ function bitly:init(config) "j.mp/([A-Za-z0-9-_-]+)", "andib.tk/([A-Za-z0-9-_-]+)" } + bitly.inline_triggers = bitly.triggers end local BASE_URL = 'https://api-ssl.bitly.com/v3/expand' @@ -25,18 +26,39 @@ local BASE_URL = 'https://api-ssl.bitly.com/v3/expand' function bitly:expand_bitly_link (shorturl) local access_token = cred_data.bitly_access_token local url = BASE_URL..'?access_token='..access_token..'&shortUrl=https://bit.ly/'..shorturl - local res,code = https.request(url) - if code ~= 200 then return "HTTP-FEHLER" end + local res, code = https.request(url) + if code ~= 200 then return nil end local data = json.decode(res).data.expand[1] cache_data('bitly', shorturl, data) return data.long_url end +function bitly:inline_callback(inline_query, config, matches) + local shorturl = matches[1] + local hash = 'telegram:cache:bitly:'..shorturl + if redis:exists(hash) == false then + url = bitly:expand_bitly_link(shorturl) + else + local data = redis:hgetall(hash) + url = data.long_url + end + + if not url then return end + + local results = '[{"type":"article","id":"'..math.random(100000000000000000)..'","title":"Verlängerte URL","description":"'..url..'","url":"'..url..'","thumb_url":"https://anditest.perseus.uberspace.de/inlineQuerys/generic/internet.jpg","thumb_width":165,"thumb_height":150,"hide_url":true,"input_message_content":{"message_text":"'..url..'"}}]' + utilities.answer_inline_query(self, inline_query, results, 3600) +end + function bitly:action(msg, config, matches) local shorturl = matches[1] local hash = 'telegram:cache:bitly:'..shorturl if redis:exists(hash) == false then - utilities.send_reply(self, msg, bitly:expand_bitly_link(shorturl)) + local longurl = bitly:expand_bitly_link(shorturl) + if not longurl then + utilities.send_reply(self, msg, config.errors.connection) + return + end + utilities.send_reply(self, msg, longurl) return else local data = redis:hgetall(hash) diff --git a/otouto/plugins/expand.lua b/otouto/plugins/expand.lua index 60b056e..8f6d422 100644 --- a/otouto/plugins/expand.lua +++ b/otouto/plugins/expand.lua @@ -7,6 +7,9 @@ function expand:init(config) expand.triggers = { "^/expand (https?://[%w-_%.%?%.:/%+=&]+)$" } + expand.inline_triggers = { + "^ex (https?://[%w-_%.%?%.:/%+=&]+)$" + } expand.doc = [[* ]]..config.cmd_pat..[[expand* __: Verlängert Kurz-URL (301er/302er)]] @@ -14,10 +17,26 @@ end expand.command = 'expand ' -function expand:action(msg, config, matches) +function expand:inline_callback(inline_query, config, matches) + local ok, response_headers = expand:url(matches[1]) + if not response_headers.location then + title = 'Konnte nicht erweitern' + url = matches[1] + description = 'Sende stattdessen die kurze URL' + else + title = 'Verlängerte URL' + url = response_headers.location + description = url + end + + local results = '[{"type":"article","id":"'..math.random(100000000000000000)..'","title":"'..title..'","description":"'..description..'","url":"'..url..'","thumb_url":"https://anditest.perseus.uberspace.de/inlineQuerys/generic/internet.jpg","thumb_width":165,"thumb_height":150,"hide_url":true,"input_message_content":{"message_text":"'..url..'"}}]' + utilities.answer_inline_query(self, inline_query, results, 3600) +end + +function expand:url(long_url) local response_body = {} local request_constructor = { - url = matches[1], + url = long_url, method = "HEAD", sink = ltn12.sink.table(response_body), headers = {}, @@ -25,6 +44,11 @@ function expand:action(msg, config, matches) } local ok, response_code, response_headers, response_status_line = http.request(request_constructor) + return ok, response_headers +end + +function expand:action(msg, config, matches) + local ok, response_headers = expand:url(matches[1]) if ok and response_headers.location then utilities.send_reply(self, msg, response_headers.location) return