From c428343d24ceec12ecf5392cb34d5a336d1503cc Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Wed, 27 Jul 2016 17:35:04 +0200 Subject: [PATCH] =?UTF-8?q?InlineQuery=20f=C3=BCr=20cat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/plugins/cats.lua | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/otouto/plugins/cats.lua b/otouto/plugins/cats.lua index 4fd259c..cb3be6b 100644 --- a/otouto/plugins/cats.lua +++ b/otouto/plugins/cats.lua @@ -1,6 +1,7 @@ local cats = {} -local HTTP = require('socket.http') +local https = require('ssl.https') +local json = require('dkjson') local utilities = require('otouto.utilities') cats.command = 'cat [gif]' @@ -16,6 +17,11 @@ function cats:init(config) "^/cat (gif)$" } + cats.inline_triggers = { + "^cat (gif)$", + "^cat$" + } + cats.doc = [[* ]]..config.cmd_pat..[[cat*: Postet eine zufällige Katze *]]..config.cmd_pat..[[cat* _gif_: Postet eine zufällige, animierte Katze]] @@ -24,6 +30,35 @@ end local apikey = cred_data.cat_apikey or "" -- apply for one here: http://thecatapi.com/api-key-registration.html +function cats:inline_callback(inline_query, config, matches) + if matches[1] == 'gif' then + img_type = 'gif' + else + img_type = 'jpg' + end + local url = 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%27http%3A%2F%2Fthecatapi.com%2Fapi%2Fimages%2Fget%3Fformat%3Dxml%26results_per_page%3D50%26type%3D'..img_type..'%26apikey%3D'..apikey..'%27&format=json' -- no way I'm using XML, plz die + local res, code = https.request(url) + if code ~= 200 then return end + local data = json.decode(res).query.results.response.data.images.image + if not data then return end + if not data[1] then return end + + local results = '[' + + for n in pairs(data) do + if img_type == 'gif' then + results = results..'{"type":"gif","id":"'..math.random(100000000000000000)..'","gif_url":"'..data[n].url..'","thumb_url":"'..data[n].url..'"}' + else + results = results..'{"type":"photo","id":"'..math.random(100000000000000000)..'","photo_url":"'..data[n].url..'","thumb_url":"'..data[n].url..'"}' + end + if n < #data then + results = results..',' + end + end + local results = results..']' + utilities.answer_inline_query(self, inline_query, results, 120) +end + function cats:action(msg, config) if matches[1] == 'gif' then local url = 'http://thecatapi.com/api/images/get?type=gif&apikey='..apikey