From 6228b1312d80f39690420fb31a13b723383fb6c5 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Mon, 15 Aug 2016 17:56:55 +0200 Subject: [PATCH] =?UTF-8?q?InlineQuery=20f=C3=BCr=20Preview?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/plugins/preview.lua | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/otouto/plugins/preview.lua b/otouto/plugins/preview.lua index 6948487..345d09a 100644 --- a/otouto/plugins/preview.lua +++ b/otouto/plugins/preview.lua @@ -4,11 +4,47 @@ preview.command = 'preview ' function preview:init(config) preview.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('preview', true).table + preview.inline_triggers = { + "^pr (https?://[%w-_%.%?%.:/%+=&%~%%#]+)$" + } preview.doc = [[* ]]..config.cmd_pat..[[preview* __ Erstellt einen Preview-Link]] end +function preview:inline_callback(inline_query, config, matches) + local preview_url = matches[1] + local res, code = https.request('https://brawlbot.tk/apis/simple_meta_api/?url='..URL.escape(preview_url)) + if code ~= 200 then utilities.answer_inline_query(self, inline_query) return end + local data = json.decode(res) + if data.remote_code >= 400 then utilities.answer_inline_query(self, inline_query) return end + + if data.title then + title = data.title + else + title = 'Kein Titel' + end + + if data.description then + description = data.description + description_in_text = '\n'..description + else + description_in_text = '' + description = 'Keine Beschreibung verfügbar' + end + + if data.only_name then + only_name = data.only_name + else + only_name = preview_url:match('^%w+://([^/]+)') -- we only need the domain + end + + local message_text = ''..title..''..description_in_text..'\n— '..only_name + + local results = '[{"type":"article","id":"77","title":"'..title..'","description":"'..description..'","url":"'..preview_url..'","thumb_url":"https://anditest.perseus.uberspace.de/inlineQuerys/generic/internet.jpg","thumb_width":150,"thumb_height":150,"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(self, inline_query, results, 3600, true) +end + function preview:action(msg) local input = utilities.input_from_msg(msg) if not input then