2016-06-15 18:00:59 +02:00
|
|
|
local bitly = {}
|
|
|
|
|
|
|
|
function bitly:init(config)
|
|
|
|
if not cred_data.bitly_access_token then
|
|
|
|
print('Missing config value: bitly_access_token.')
|
|
|
|
print('bitly.lua will not be enabled.')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
bitly.triggers = {
|
|
|
|
"bit.ly/([A-Za-z0-9-_-]+)",
|
|
|
|
"bitly.com/([A-Za-z0-9-_-]+)",
|
|
|
|
"j.mp/([A-Za-z0-9-_-]+)",
|
|
|
|
"andib.tk/([A-Za-z0-9-_-]+)"
|
|
|
|
}
|
2016-07-14 15:16:24 +02:00
|
|
|
bitly.inline_triggers = bitly.triggers
|
2016-06-15 18:00:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2016-07-14 15:16:24 +02:00
|
|
|
local res, code = https.request(url)
|
|
|
|
if code ~= 200 then return nil end
|
2016-06-15 18:00:59 +02:00
|
|
|
local data = json.decode(res).data.expand[1]
|
|
|
|
cache_data('bitly', shorturl, data)
|
|
|
|
return data.long_url
|
|
|
|
end
|
|
|
|
|
2016-07-14 15:16:24 +02:00
|
|
|
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
|
|
|
|
|
2016-08-24 15:38:29 +02:00
|
|
|
if not url then abort_inline_query(inline_query) return end
|
2016-07-14 15:16:24 +02:00
|
|
|
|
2016-08-07 21:20:48 +02:00
|
|
|
local results = '[{"type":"article","id":"2","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..'"}}]'
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.answer_inline_query(inline_query, results, 3600)
|
2016-07-14 15:16:24 +02:00
|
|
|
end
|
|
|
|
|
2016-06-15 18:00:59 +02:00
|
|
|
function bitly:action(msg, config, matches)
|
|
|
|
local shorturl = matches[1]
|
|
|
|
local hash = 'telegram:cache:bitly:'..shorturl
|
|
|
|
if redis:exists(hash) == false then
|
2016-07-14 15:16:24 +02:00
|
|
|
local longurl = bitly:expand_bitly_link(shorturl)
|
|
|
|
if not longurl then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply( msg, config.errors.connection)
|
2016-07-14 15:16:24 +02:00
|
|
|
return
|
|
|
|
end
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, longurl)
|
2016-06-15 18:00:59 +02:00
|
|
|
return
|
|
|
|
else
|
|
|
|
local data = redis:hgetall(hash)
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, data.long_url)
|
2016-06-15 18:00:59 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return bitly
|