From 595f27a918a435d8a0a5fb4db57c1e5edeee459e Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Thu, 14 Jul 2016 11:48:48 +0200 Subject: [PATCH] =?UTF-8?q?-=20InlineQuery=20f=C3=BCr=209gag=20-=20bImages?= =?UTF-8?q?:=20Ausdruck=20-=20Core:=20Leere=20InlineQuerys=20werden=20jetz?= =?UTF-8?q?t=20auch=20=C3=BCbergeben?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/bot.lua | 1 - otouto/plugins/9gag.lua | 27 +++++++++++++++++++++++++-- otouto/plugins/bImages.lua | 2 +- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/otouto/bot.lua b/otouto/bot.lua index fd831d8..a93ca3d 100644 --- a/otouto/bot.lua +++ b/otouto/bot.lua @@ -154,7 +154,6 @@ function bot:process_inline_query(inline_query, config) -- When an inline query if not is_whitelisted then return end end - if inline_query.query == '' then return end if inline_query.query:match('"') then inline_query.query = inline_query.query:gsub('"', '\\"') end diff --git a/otouto/plugins/9gag.lua b/otouto/plugins/9gag.lua index bd6b87e..766e8bf 100644 --- a/otouto/plugins/9gag.lua +++ b/otouto/plugins/9gag.lua @@ -10,22 +10,45 @@ ninegag.command = '9gag' function ninegag:init(config) ninegag.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('9gag', true):t('9fag', true).table + ninegag.inline_triggers = { + "^9gag" + } ninegag.doc = [[* ]]..config.cmd_pat..[[9gag*: Gibt ein zufälliges Bild von den momentan populärsten 9GAG-Posts aus]] end +local url = "http://api-9gag.herokuapp.com/" + function ninegag:get_9GAG() - local url = "http://api-9gag.herokuapp.com/" local b,c = HTTP.request(url) if c ~= 200 then return nil end local gag = JSON.decode(b) -- random max json table size - local i = math.random(#gag) local link_image = gag[i].src + local i = math.random(#gag) + + local link_image = gag[i].src local title = gag[i].title local post_url = gag[i].url return link_image, title, post_url end +function ninegag:inline_callback(inline_query, config) + local res, code = HTTP.request(url) + if code ~= 200 then return end + local gag = JSON.decode(res) + + local results = '[' + for n in pairs(gag) do + local title = gag[n].title:gsub('"', '\\"') + results = results..'{"type":"photo","id":"'..math.random(100000000000000000)..'","photo_url":"'..gag[n].src..'","thumb_url":"'..gag[n].src..'","caption":"'..title..'","reply_markup":{"inline_keyboard":[[{"text":"9GAG aufrufen","url":"'..gag[n].url..'"}]]}}' + if n < #gag then + results = results..',' + end + end + local results = results..']' + utilities.answer_inline_query(self, inline_query, results, 300) +end + function ninegag:action(msg, config) utilities.send_typing(self, msg.chat.id, 'upload_photo') local url, title, post_url = ninegag:get_9GAG() diff --git a/otouto/plugins/bImages.lua b/otouto/plugins/bImages.lua index 12d8fbd..5642528 100644 --- a/otouto/plugins/bImages.lua +++ b/otouto/plugins/bImages.lua @@ -48,7 +48,7 @@ function bImages:getImages(query) if images[n].encodingFormat == 'jpeg' then -- Inline-Querys MUST use JPEG photos! local photo_url = images[n].contentUrl local thumb_url = images[n].thumbnailUrl - results = results..'{"type":"photo","id":"'..math.random(100000000000000000)..'","photo_url":"'..photo_url..'","thumb_url":"'..thumb_url..'","photo_width":'..images[n].width..',"photo_height":'..images[n].height..',"reply_markup":{"inline_keyboard":[[{"text":"Bing aufrufen","url":"'..images[n].webSearchUrl..'"},{"text":"Bild aufrufen","url":"'..photo_url..'"}]]}},' + results = results..'{"type":"photo","id":"'..math.random(100000000000000000)..'","photo_url":"'..photo_url..'","thumb_url":"'..thumb_url..'","photo_width":'..images[n].width..',"photo_height":'..images[n].height..',"reply_markup":{"inline_keyboard":[[{"text":"Bing aufrufen","url":"'..images[n].webSearchUrl..'"},{"text":"Bild öffnen","url":"'..photo_url..'"}]]}},' end end