From d404941560086654c7d4781f6ed7a67365513295 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Wed, 5 Oct 2016 19:29:15 +0200 Subject: [PATCH] =?UTF-8?q?CallbackQuery=20f=C3=BCr=20Cats!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/plugins/cats.lua | 58 ++++++++++++++++++++++++++++++++++------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/otouto/plugins/cats.lua b/otouto/plugins/cats.lua index a95dd3a..4b8d8b0 100644 --- a/otouto/plugins/cats.lua +++ b/otouto/plugins/cats.lua @@ -23,8 +23,8 @@ function cats:init(config) *]]..config.cmd_pat..[[cat* _gif_: Postet eine zufällige, animierte Katze]] end - local apikey = cred_data.cat_apikey or "" -- apply for one here: http://thecatapi.com/api-key-registration.html +local BASE_URL = 'http://thecatapi.com/api/images/get' function cats:inline_callback(inline_query, config, matches) if matches[1] == 'gif' then @@ -59,16 +59,54 @@ function cats:inline_callback(inline_query, config, matches) utilities.answer_inline_query(inline_query, results, 30) end -function cats:action(msg, config, matches) - if matches[1] == 'gif' then - local url = 'http://thecatapi.com/api/images/get?type=gif&apikey='..apikey - local file = download_to_file(url, 'miau.gif') - utilities.send_document(msg.chat.id, file, nil, msg.message_id) +function cats:get_cat(gif) + if gif then + local url = BASE_URL..'?type=gif&apikey='..apikey + file = download_to_file(url, 'miau.gif') else - local url = 'http://thecatapi.com/api/images/get?type=jpg,png&apikey='..apikey - local file = download_to_file(url, 'miau.png') - utilities.send_photo(msg.chat.id, file, nil, msg.message_id) + local url = BASE_URL..'?type=jpg,png&apikey='..apikey + file = download_to_file(url, 'miau.png') + end + return file +end + +function cats:callback(callback, msg, self, config, input) + utilities.answer_callback_query(callback, 'Miau!') + utilities.send_typing(msg.chat.id, 'upload_photo') + if string.isempty(input) then + local file = cats:get_cat() + if not file then + utilities.answer_callback_query(callback, 'Beim Herunterladen ist ein Fehler aufgetreten :(', true) + return + end + utilities.send_photo(msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Nochmal!","callback_data":"cats:"}]]}') + else + local file = cats:get_cat(true) + if not file then + utilities.answer_callback_query(callback, 'Beim Herunterladen ist ein Fehler aufgetreten :(', true) + return + end + utilities.send_document(msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Nochmal!","callback_data":"cats:gif"}]]}') end end -return cats +function cats:action(msg, config, matches) + utilities.send_typing(msg.chat.id, 'upload_photo') + if matches[1] == 'gif' then + local file = cats:get_cat(true) + if not file then + utilities.send_reply(msg, config.errors.connection) + return + end + utilities.send_document(msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Nochmal!","callback_data":"cats:gif"}]]}') + else + local file = cats:get_cat() + if not file then + utilities.send_reply(msg, config.errors.connection) + return + end + utilities.send_photo(msg.chat.id, file, nil, msg.message_id, '{"inline_keyboard":[[{"text":"Nochmal!","callback_data":"cats:"}]]}') + end +end + +return cats \ No newline at end of file