From 4c1248f21d6174225042e541d06dcb957b71c03b Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Wed, 5 Oct 2016 19:13:02 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Unterst=C3=BCtzung=20von=20CallbackQuerys,?= =?UTF-8?q?=20die=20an=20InlineQuerys=20angeh=C3=A4ngt=20sind.=20HINWEIS:?= =?UTF-8?q?=20CallbackQuerys=20werden=20jetzt=20nie=20ignoriert,=20egal,?= =?UTF-8?q?=20wie=20alt=20sie=20sind!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- otouto/bot.lua | 61 +++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/otouto/bot.lua b/otouto/bot.lua index 4b929a0..7c5624e 100644 --- a/otouto/bot.lua +++ b/otouto/bot.lua @@ -110,11 +110,6 @@ function bot:on_callback_receive(callback, msg, config) -- whenever a new callba -- vardump(msg) -- vardump(callback) - if msg.date < os.time() - 3600 then -- Do not process old messages. - utilities.answer_callback_query(callback, 'Nachricht älter als eine Stunde, bitte sende den Befehl selbst noch einmal.', true) - return - end - if not callback.data:find(':') then utilities.answer_callback_query(callback, 'Ungültiger CallbackQuery: Kein Parameter.') return @@ -122,36 +117,40 @@ function bot:on_callback_receive(callback, msg, config) -- whenever a new callba -- Check if user is blocked local user_id = callback.from.id - local chat_id = msg.chat.id if redis:get('blocked:'..user_id) then utilities.answer_callback_query(callback, 'Du darfst den Bot nicht nutzen!', true) return end - -- Check if user is banned - local banned = redis:get('banned:'..chat_id..':'..user_id) - if banned then - utilities.answer_callback_query(callback, 'Du darfst den Bot nicht nutzen!', true) - return - end + -- Check if user is banned, not working for Callbacks from InlineQuerys! + if msg then + local chat_id = msg.chat.id + local banned = redis:get('banned:'..chat_id..':'..user_id) + if banned then + utilities.answer_callback_query(callback, 'Du darfst den Bot nicht nutzen!', true) + return + end - -- Check if whitelist is enabled and user/chat is whitelisted - local whitelist = redis:get('whitelist:enabled') - if whitelist and not is_sudo(callback, config) then - local hash = 'whitelist:user#id'..user_id - local allowed = redis:get(hash) or false - if not allowed then - if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then - local allowed = redis:get('whitelist:chat#id'.. chat_id) - if not allowed then + -- Check if whitelist is enabled and user/chat is whitelisted + local whitelist = redis:get('whitelist:enabled') + if whitelist and not is_sudo(callback, config) then + local hash = 'whitelist:user#id'..user_id + local allowed = redis:get(hash) or false + if not allowed then + if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then + local allowed = redis:get('whitelist:chat#id'.. chat_id) + if not allowed then + utilities.answer_callback_query(callback, 'Du darfst den Bot nicht nutzen!', true) + return + end + else utilities.answer_callback_query(callback, 'Du darfst den Bot nicht nutzen!', true) return end - else - utilities.answer_callback_query(callback, 'Du darfst den Bot nicht nutzen!', true) - return end - end + end + + msg = utilities.enrich_message(msg) end local called_plugin = callback.data:match('(.*):.*') @@ -159,12 +158,18 @@ function bot:on_callback_receive(callback, msg, config) -- whenever a new callba print('Callback Query "'..param..'" für Plugin "'..called_plugin..'" ausgelöst von '..callback.from.first_name..' ('..callback.from.id..')') - msg = utilities.enrich_message(msg) - for n=1, #self.plugins do local plugin = self.plugins[n] if plugin.name == called_plugin then - if is_plugin_disabled_on_chat(plugin.name, msg) then utilities.answer_callback_query(callback, 'Plugin wurde in diesem Chat deaktiviert.') return end + + -- Check if plugin is disabled on this chat + if msg then + if is_plugin_disabled_on_chat(plugin.name, msg) then + utilities.answer_callback_query(callback, 'Plugin wurde in diesem Chat deaktiviert.') + return + end + end + if plugin.callback then plugin:callback(callback, msg, self, config, param) return From d404941560086654c7d4781f6ed7a67365513295 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Wed, 5 Oct 2016 19:29:15 +0200 Subject: [PATCH 2/2] =?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