2016-04-11 06:04:47 +02:00
|
|
|
local preview = {}
|
|
|
|
|
|
|
|
preview.command = 'preview <link>'
|
2016-05-27 05:28:44 +02:00
|
|
|
|
|
|
|
function preview:init(config)
|
|
|
|
preview.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('preview', true).table
|
2016-08-15 17:56:55 +02:00
|
|
|
preview.inline_triggers = {
|
2017-08-14 23:39:44 +02:00
|
|
|
"^[Pp][Rr] (https?://[%w-_%.%?%.,:;/%+=&%~%%#]+)$"
|
2016-08-15 17:56:55 +02:00
|
|
|
}
|
2016-08-15 00:36:33 +02:00
|
|
|
preview.doc = [[*
|
|
|
|
]]..config.cmd_pat..[[preview* _<URL>_
|
|
|
|
Erstellt einen Preview-Link]]
|
2016-04-11 06:04:47 +02:00
|
|
|
end
|
2016-01-13 19:00:17 +01:00
|
|
|
|
2016-09-29 18:13:11 +02:00
|
|
|
local api_key = cred_data.iframely_api_key
|
|
|
|
|
2016-08-15 17:56:55 +02:00
|
|
|
function preview:inline_callback(inline_query, config, matches)
|
|
|
|
local preview_url = matches[1]
|
2016-09-29 18:13:11 +02:00
|
|
|
local res, code = https.request('https://iframe.ly/api/oembed?url='..URL.escape(preview_url)..'&api_key='..api_key)
|
2016-08-24 15:38:29 +02:00
|
|
|
if code ~= 200 then abort_inline_query(inline_query) return end
|
2016-08-15 17:56:55 +02:00
|
|
|
local data = json.decode(res)
|
|
|
|
|
|
|
|
if data.title then
|
2016-11-23 18:57:15 +01:00
|
|
|
title = unescape(data.title:gsub('"', '\\"'))
|
2016-08-15 17:56:55 +02:00
|
|
|
else
|
|
|
|
title = 'Kein Titel'
|
|
|
|
end
|
|
|
|
|
|
|
|
if data.description then
|
2016-11-23 18:57:15 +01:00
|
|
|
description = unescape(data.description:gsub('"', '\\"'))
|
2016-08-15 17:56:55 +02:00
|
|
|
description_in_text = '\n'..description
|
2017-01-30 19:10:33 +01:00
|
|
|
if string.len(description_in_text) > 300 then
|
|
|
|
description_in_text = string.sub(description_in_text, 1, 300)..'...'
|
|
|
|
else
|
|
|
|
description_in_text = description_in_text
|
|
|
|
end
|
2016-08-15 17:56:55 +02:00
|
|
|
else
|
|
|
|
description_in_text = ''
|
|
|
|
description = 'Keine Beschreibung verfügbar'
|
|
|
|
end
|
|
|
|
|
2016-09-29 18:13:11 +02:00
|
|
|
if data.provider_name then
|
|
|
|
provider_name = data.provider_name:gsub('"', '\\"')
|
|
|
|
else
|
|
|
|
provider_name = preview_url:match('^%w+://([^/]+)') -- we only need the domain
|
|
|
|
end
|
|
|
|
|
|
|
|
if data.thumbnail_url then
|
|
|
|
thumb = data.thumbnail_url
|
|
|
|
width = data.thumbnail_width
|
|
|
|
height = data.thumbnail_height
|
2016-08-15 17:56:55 +02:00
|
|
|
else
|
2016-09-29 18:13:11 +02:00
|
|
|
thumb = 'https://anditest.perseus.uberspace.de/inlineQuerys/generic/internet.jpg'
|
|
|
|
width = 150
|
|
|
|
height = 150
|
2016-08-15 17:56:55 +02:00
|
|
|
end
|
|
|
|
|
2016-09-29 18:13:11 +02:00
|
|
|
local message_text = '<b>'..title..'</b>'..description_in_text..'\n<i>- '..provider_name..'</i>'
|
2016-08-15 17:56:55 +02:00
|
|
|
|
2016-09-29 18:13:11 +02:00
|
|
|
local results = '[{"type":"article","id":"77","title":"'..title..'","description":"'..description..'","url":"'..preview_url..'","thumb_url":"'..thumb..'","thumb_width":'..width..',"thumb_height":'..height..',"hide_url":true,"reply_markup":{"inline_keyboard":[[{"text":"Webseite aufrufen","url":"'..preview_url..'"}]]},"input_message_content":{"message_text":"'..message_text..'","parse_mode":"HTML","disable_web_page_preview":true}}]'
|
|
|
|
utilities.answer_inline_query(inline_query, results, data.cache_age)
|
2016-08-15 17:56:55 +02:00
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
function preview:action(msg)
|
2016-08-14 16:30:06 +02:00
|
|
|
local input = utilities.input_from_msg(msg)
|
|
|
|
if not input then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, preview.doc, true)
|
2016-08-14 16:30:06 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
input = utilities.get_word(input, 1)
|
|
|
|
if not input:match('^https?://.+') then
|
|
|
|
input = 'http://' .. input
|
|
|
|
end
|
|
|
|
|
|
|
|
local res = http.request(input)
|
|
|
|
if not res then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, 'Bitte gebe einen validen Link an.')
|
2016-08-14 16:30:06 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if res:len() == 0 then
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_reply(msg, 'Sorry, dieser Link lässt uns keine Vorschau erstellen.')
|
2016-08-14 16:30:06 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Invisible zero-width, non-joiner.
|
|
|
|
local output = '<a href="' .. input .. '">' .. utilities.char.zwnj .. '</a>'
|
2016-08-24 15:38:29 +02:00
|
|
|
utilities.send_message(msg.chat.id, output, false, nil, 'HTML')
|
2016-01-13 19:00:17 +01:00
|
|
|
end
|
|
|
|
|
2016-08-14 16:30:06 +02:00
|
|
|
return preview
|