From 7ce4411a65d835689e2d4c360e16223506c4a152 Mon Sep 17 00:00:00 2001 From: Andreas Bielawski Date: Wed, 13 Jul 2016 01:00:32 +0200 Subject: [PATCH] =?UTF-8?q?-=20Inline-Querys=20werden=20jetzt=20unterst?= =?UTF-8?q?=C3=BCtzt!=20->=20Das=20erste=20Plugin=20mit=20Inline-Query-Sup?= =?UTF-8?q?port=20ist=20"Echo",=20das=20wie=20@bold=20funktioniert=20->=20?= =?UTF-8?q?Neue=20Variable=20zur=20Config=20hinzugef=C3=BCgt,=20mit=20der?= =?UTF-8?q?=20man=20bestimmen=20kann,=20ob=20nur=20whitelisted=20User=20od?= =?UTF-8?q?er=20alle=20Inline-Querys=20benutzen=20k=C3=B6nnen=20-=20AFK=20?= =?UTF-8?q?zeigt=20AFK-Text=20jetzt=20fett=20an?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 +++++ config.lua.example | 4 ++++ otouto/bot.lua | 46 +++++++++++++++++++++++++++++++++-------- otouto/plugins/afk.lua | 4 +++- otouto/plugins/echo.lua | 38 ++++++++++++++++++++++------------ otouto/utilities.lua | 13 ++++++++++++ 6 files changed, 87 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index bb04575..8e9b72d 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,10 @@ Brawlbot v2 ist freie Software; du darfst in modifizieren und weiterverbreiten, * * * # Für User ## Setup +### Ubuntu und Debian +Ubuntu und Debian liefern Luarocks nur für Lua 5.1 aus. Um Luarocks für Lua 5.2 zu verwenden, folge bitte der [Anleitung auf StackOverflow](http://stackoverflow.com/a/20359102) + +### Setup Du benötigst **Lua 5.2+**, eine aktive **Redis-Instanz** und die folgenden **LuaRocks-Module**: * luasocket * luasec @@ -28,6 +32,7 @@ Du benötigst **Lua 5.2+**, eine aktive **Redis-Instanz** und die folgenden **Lu * dkjson * lpeg * redis-lua +* fakeredis * oauth * xml * feedparser diff --git a/config.lua.example b/config.lua.example index a4e4eb7..5f7c9c9 100644 --- a/config.lua.example +++ b/config.lua.example @@ -21,6 +21,10 @@ Sende /hilfe, um zu starten -- The symbol that starts a command. Usually noted as '/' in documentation. cmd_pat = '/', + -- false = only whitelisted users can use inline querys + -- NOTE that it doesn't matter, if the chat is whitelisted! The USER must be whitelisted! + enable_inline_for_everyone = true, + errors = { -- Generic error messages used in various plugins. generic = 'An unexpected error occurred.', connection = 'Verbindungsfehler.', diff --git a/otouto/bot.lua b/otouto/bot.lua index 1289972..d468e56 100644 --- a/otouto/bot.lua +++ b/otouto/bot.lua @@ -76,15 +76,6 @@ function bot:init(config) -- The function run when the bot is started or reloade end -function bot:process_inline_query(inline_query, config) -- When an inline query is received - -- remove comment to enable debugging - if inline_query.query == '' then return end - local result, error = bindings.request(self, 'answerInlineQuery', { - inline_query_id = inline_query.id, - results = '[{"type":"article","id":"'..math.random(100000000000000000)..'","thumb_url":"https://anditest.perseus.uberspace.de/b.jpg","title":"Fett","description":"*'..inline_query.query..'*","input_message_content":{"message_text":"*'..inline_query.query..'*","parse_mode":"Markdown"}}]' - } ) -end - function bot:on_msg_receive(msg, config) -- The fn run whenever a message is received. -- remove comment to enable debugging -- vardump(msg) @@ -152,6 +143,23 @@ function bot:on_callback_receive(callback, msg, config) -- whenever a new callba end end +-- NOTE: To enable InlineQuerys, send /setinline to @BotFather +function bot:process_inline_query(inline_query, config) -- When an inline query is received + -- remove comment to enable debugging + -- vardump(inline_query) + + if not config.enable_inline_for_everyone then + local is_whitelisted = redis:get('whitelist:user#id'..inline_query.from.id) + if not is_whitelisted then return end + end + + if inline_query.query == '' then return end + + for _, plugin in ipairs(self.plugins) do + match_inline_plugins(self, inline_query, config, plugin) + end +end + function bot:run(config) bot.init(self, config) -- Actually start the script. @@ -207,6 +215,26 @@ function pre_process_msg(self, msg, config) return new_msg end +function match_inline_plugins(self, inline_query, config, plugin) + for _, trigger in pairs(plugin.inline_triggers or {}) do + if string.match(string.lower(inline_query.query), trigger) then + local success, result = pcall(function() + for k, pattern in pairs(plugin.inline_triggers) do + matches = match_pattern(pattern, inline_query.query) + if matches then + break; + end + end + print(plugin.name..' triggered') + return plugin.inline_callback(self, inline_query, config, matches) + end) + if not success then + print(result) + end + end + end +end + function match_plugins(self, msg, config, plugin) for _, trigger in pairs(plugin.triggers or {}) do if string.match(msg.text_lower, trigger) then diff --git a/otouto/plugins/afk.lua b/otouto/plugins/afk.lua index 1483fe2..c403340 100644 --- a/otouto/plugins/afk.lua +++ b/otouto/plugins/afk.lua @@ -95,7 +95,9 @@ function afk:pre_process(msg, self) redis:hset(hash, 'afk', false) if afk_text then redis:hset(hash, 'afk_text', false) - utilities.send_message(self, msg.chat.id, user_name..' ist wieder da (war: '..afk_text..' für '..duration..')!') + local afk_text = afk_text:gsub("%*","") + local afk_text = afk_text:gsub("_","") + utilities.send_message(self, msg.chat.id, user_name..' ist wieder da (war: *'..afk_text..'* für '..duration..')!', true, nil, true) else utilities.send_message(self, msg.chat.id, user_name..' ist wieder da (war '..duration..' weg)!') end diff --git a/otouto/plugins/echo.lua b/otouto/plugins/echo.lua index eb5d28c..aff73d8 100644 --- a/otouto/plugins/echo.lua +++ b/otouto/plugins/echo.lua @@ -6,25 +6,37 @@ echo.command = 'echo ' function echo:init(config) echo.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('echo', true).table + echo.inline_triggers = { + "^e (.*)" + } echo.doc = [[* ]]..config.cmd_pat..[[echo* __: Gibt den Text aus]] end +function echo:inline_callback(inline_query, config, matches) + local text = matches[1] + local results = '[' + + -- enable custom markdown button + if text:match('%[.*%]%(.*%)') or text:match('%*.*%*') or text:match('_.*_') or text:match('`.*`') then + results = results..'{"type":"article","id":"'..math.random(100000000000000000)..'","thumb_url":"https://anditest.perseus.uberspace.de/inlineQuerys/echo/custom.jpg","title":"Eigenes Markdown","description":"'..text..'","input_message_content":{"message_text":"'..text..'","parse_mode":"Markdown"}},' + end + + local results = results..'{"type":"article","id":"'..math.random(100000000000000000)..'","thumb_url":"https://anditest.perseus.uberspace.de/inlineQuerys/echo/fett.jpg","title":"Fett","description":"*'..text..'*","input_message_content":{"message_text":"*'..text..'*","parse_mode":"Markdown"}},{"type":"article","id":"'..math.random(100000000000000000)..'","thumb_url":"https://anditest.perseus.uberspace.de/inlineQuerys/echo/kursiv.jpg","title":"Kursiv","description":"_'..text..'_","input_message_content":{"message_text":"_'..text..'_","parse_mode":"Markdown"}},{"type":"article","id":"'..math.random(100000000000000000)..'","thumb_url":"https://anditest.perseus.uberspace.de/inlineQuerys/echo/fixedsys.jpg","title":"Feste Breite","description":"`'..text..'`","input_message_content":{"message_text":"`'..text..'`","parse_mode":"Markdown"}}]' + utilities.answer_inline_query(self, inline_query, results, 0) +end + function echo:action(msg) - - local input = utilities.input(msg.text) - - if not input then - utilities.send_message(self, msg.chat.id, echo.doc, true, msg.message_id, true) - else - local output - if msg.chat.type == 'supergroup' then - output = '*Echo:*\n"' .. utilities.md_escape(input) .. '"' - end - utilities.send_message(self, msg.chat.id, input, true, nil, true) + local input = utilities.input(msg.text) + if not input then + utilities.send_message(self, msg.chat.id, echo.doc, true, msg.message_id, true) + else + local output + if msg.chat.type == 'supergroup' then + output = '*Echo:*\n"' .. utilities.md_escape(input) .. '"' end - - + utilities.send_message(self, msg.chat.id, input, true, nil, true) + end end return echo diff --git a/otouto/utilities.lua b/otouto/utilities.lua index eacaa30..8d4157e 100644 --- a/otouto/utilities.lua +++ b/otouto/utilities.lua @@ -191,6 +191,19 @@ function utilities:get_chat_administrators(chat_id) } ) end +-- https://core.telegram.org/bots/api#answerinlinequery +function utilities:answer_inline_query(inline_query, results, cache_time, is_personal, next_offset, switch_pm_text, switch_pm_parameter) + return bindings.request(self, 'answerInlineQuery', { + inline_query_id = inline_query.id, + results = results, + cache_time = cache_time, + is_personal = is_personal, + next_offset = next_offset, + switch_pm_text = switch_pm_text, + switch_pm_parameter = switch_pm_parameter + } ) +end + -- get the indexed word in a string function utilities.get_word(s, i) s = s or ''