- Bitly: Inline-Query

- Expand: Inline-Query
This commit is contained in:
Andreas Bielawski 2016-07-14 15:16:24 +02:00
parent 595f27a918
commit a30e35b853
2 changed files with 51 additions and 5 deletions

View File

@ -18,6 +18,7 @@ function bitly:init(config)
"j.mp/([A-Za-z0-9-_-]+)", "j.mp/([A-Za-z0-9-_-]+)",
"andib.tk/([A-Za-z0-9-_-]+)" "andib.tk/([A-Za-z0-9-_-]+)"
} }
bitly.inline_triggers = bitly.triggers
end end
local BASE_URL = 'https://api-ssl.bitly.com/v3/expand' 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) function bitly:expand_bitly_link (shorturl)
local access_token = cred_data.bitly_access_token local access_token = cred_data.bitly_access_token
local url = BASE_URL..'?access_token='..access_token..'&shortUrl=https://bit.ly/'..shorturl local url = BASE_URL..'?access_token='..access_token..'&shortUrl=https://bit.ly/'..shorturl
local res,code = https.request(url) local res, code = https.request(url)
if code ~= 200 then return "HTTP-FEHLER" end if code ~= 200 then return nil end
local data = json.decode(res).data.expand[1] local data = json.decode(res).data.expand[1]
cache_data('bitly', shorturl, data) cache_data('bitly', shorturl, data)
return data.long_url return data.long_url
end 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) function bitly:action(msg, config, matches)
local shorturl = matches[1] local shorturl = matches[1]
local hash = 'telegram:cache:bitly:'..shorturl local hash = 'telegram:cache:bitly:'..shorturl
if redis:exists(hash) == false then 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 return
else else
local data = redis:hgetall(hash) local data = redis:hgetall(hash)

View File

@ -7,6 +7,9 @@ function expand:init(config)
expand.triggers = { expand.triggers = {
"^/expand (https?://[%w-_%.%?%.:/%+=&]+)$" "^/expand (https?://[%w-_%.%?%.:/%+=&]+)$"
} }
expand.inline_triggers = {
"^ex (https?://[%w-_%.%?%.:/%+=&]+)$"
}
expand.doc = [[* expand.doc = [[*
]]..config.cmd_pat..[[expand* _<Kurz-URL>_: Verlängert Kurz-URL (301er/302er)]] ]]..config.cmd_pat..[[expand* _<Kurz-URL>_: Verlängert Kurz-URL (301er/302er)]]
@ -14,10 +17,26 @@ end
expand.command = 'expand <Kurz-URL>' expand.command = 'expand <Kurz-URL>'
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 response_body = {}
local request_constructor = { local request_constructor = {
url = matches[1], url = long_url,
method = "HEAD", method = "HEAD",
sink = ltn12.sink.table(response_body), sink = ltn12.sink.table(response_body),
headers = {}, headers = {},
@ -25,6 +44,11 @@ function expand:action(msg, config, matches)
} }
local ok, response_code, response_headers, response_status_line = http.request(request_constructor) 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 if ok and response_headers.location then
utilities.send_reply(self, msg, response_headers.location) utilities.send_reply(self, msg, response_headers.location)
return return