InlineQuerys für das Wikipedia-Plugin (@BOTNAME wiki(optionale Sprache) Suchbegriff)

This commit is contained in:
Andreas Bielawski 2016-07-13 14:33:52 +02:00
parent 76a7923519
commit 678642d877
3 changed files with 46 additions and 11 deletions

View File

@ -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.

View File

@ -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

View File

@ -10,11 +10,15 @@ wikipedia.command = 'wiki <Begriff>'
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* _<Begriff>_: Gibt Wikipedia-Artikel aus
@ -153,6 +157,39 @@ function wikipedia:wikisearch(text, lang)
end
function wikipedia:snip_snippet(snippet)
local snippet = snippet:gsub("<span class%=\"searchmatch\">", "")
local snippet = snippet:gsub("</span>", "")
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