diff --git a/otouto/bot.lua b/otouto/bot.lua index 3102899..3320d9c 100644 --- a/otouto/bot.lua +++ b/otouto/bot.lua @@ -78,7 +78,7 @@ end function bot:on_msg_receive(msg, config) -- The fn run whenever a message is received. -- remove comment to enable debugging - -- vardump(msg) + -- vardump(msg) -- Cache user info for those involved. if msg.date < os.time() - 5 then return end -- Do not process old messages. @@ -155,7 +155,9 @@ function bot:process_inline_query(inline_query, config) -- When an inline query end if inline_query.query == '' then return end - + if inline_query.query:match('"') then + inline_query.query = inline_query.query:gsub('"', '\\"') + end for _, plugin in ipairs(self.plugins) do match_inline_plugins(self, inline_query, config, plugin) end @@ -165,7 +167,6 @@ function bot:run(config) bot.init(self, config) -- Actually start the script. while self.is_started do -- Start a loop while the bot should be running. - local res = bindings.getUpdates(self, { timeout=20, offset = self.last_update+1 } ) if res then for _,v in ipairs(res.result) do -- Go through every new message. diff --git a/otouto/plugins/echo.lua b/otouto/plugins/echo.lua index 8cbd862..b59133d 100644 --- a/otouto/plugins/echo.lua +++ b/otouto/plugins/echo.lua @@ -16,9 +16,6 @@ end function echo:inline_callback(inline_query, config, matches) local text = matches[1] local results = '[' - if text:match('"') then - text = text:gsub('"', '\\"') - end -- enable custom markdown button if text:match('%[.*%]%(.*%)') or text:match('%*.*%*') or text:match('_.*_') or text:match('`.*`') then diff --git a/otouto/plugins/wikipedia.lua b/otouto/plugins/wikipedia.lua index daecd42..8588f8b 100644 --- a/otouto/plugins/wikipedia.lua +++ b/otouto/plugins/wikipedia.lua @@ -10,11 +10,15 @@ wikipedia.command = 'wiki ' function wikipedia:init(config) wikipedia.triggers = { - "^/[Ww]iki(%w+) (search) (.+)$", - "^/[Ww]iki (search) ?(.*)$", - "^/[Ww]iki(%w+) (.+)$", - "^/[Ww]iki ?(.*)$", - "(%w+).wikipedia.org/wiki/(.+)" + "^/[Ww]iki(%w+) (search) (.+)$", + "^/[Ww]iki (search) ?(.*)$", + "^/[Ww]iki(%w+) (.+)$", + "^/[Ww]iki ?(.*)$", + "(%w+).wikipedia.org/wiki/(.+)" + } + wikipedia.inline_triggers = { + "^wiki(%w+) (.+)", + "^wiki (.+)" } wikipedia.doc = [[* ]]..config.cmd_pat..[[wiki* __: Gibt Wikipedia-Artikel aus @@ -153,6 +157,39 @@ function wikipedia:wikisearch(text, lang) end +function wikipedia:snip_snippet(snippet) + local snippet = snippet:gsub("", "") + local snippet = snippet:gsub("", "") + return snippet +end + +function wikipedia:inline_callback(inline_query, config, matches) + if matches[2] then + lang = matches[1] + query = matches[2] + else + lang = 'de' + query = matches[1] + end + local url = 'https://'..lang..'.wikipedia.org/w/api.php?action=query&list=search&srsearch='..URL.escape(query)..'&format=json&prop=extracts&srprop=snippet' + local res, code = https.request(url) + if code ~= 200 then return end + local data = JSON.decode(res).query + + if data.searchinfo.totalhits == 0 then return end + + local results = '[' + for num in pairs(data.search) do + local title = data.search[num].title + results = results..'{"type":"article","id":"'..math.random(100000000000000000)..'","title":"'..title..'","description":"'..wikipedia:snip_snippet(data.search[num].snippet)..'","url":"https://'..lang..'.wikipedia.org/wiki/'..URL.escape(title)..'","thumb_url":"https://anditest.perseus.uberspace.de/inlineQuerys/wiki/logo.jpg","thumb_width":95,"thumb_height":86,"input_message_content":{"message_text":"'..config.cmd_pat..'wiki'..lang..'@'..self.info.username..' '..title..'"}}' + if num < #data.search then + results = results..',' + end + end + local results = results..']' + utilities.answer_inline_query(self, inline_query, results, 3600) +end + function wikipedia:action(msg, config, matches) local search, term, lang if matches[1] == "search" then