2016-01-08 14:44:37 +01:00
|
|
|
|
local command = 'wikipedia <query>'
|
|
|
|
|
local doc = [[```
|
|
|
|
|
/wikipedia <query>
|
|
|
|
|
Returns an article from Wikipedia.
|
|
|
|
|
Aliases: /w, /wiki
|
|
|
|
|
```]]
|
2015-08-18 11:55:25 +02:00
|
|
|
|
|
|
|
|
|
local triggers = {
|
2016-01-08 04:30:12 +01:00
|
|
|
|
'^/wikipedia[@'..bot.username..']*',
|
|
|
|
|
'^/wiki[@'..bot.username..']*',
|
|
|
|
|
'^/w[@'..bot.username..']*$',
|
|
|
|
|
'^/w[@'..bot.username..']* '
|
2015-08-18 11:55:25 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
local action = function(msg)
|
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
|
local input = msg.text:input()
|
2015-08-18 11:55:25 +02:00
|
|
|
|
if not input then
|
2015-11-25 03:22:04 +01:00
|
|
|
|
if msg.reply_to_message and msg.reply_to_message.text then
|
|
|
|
|
input = msg.reply_to_message.text
|
|
|
|
|
else
|
2016-01-08 14:44:37 +01:00
|
|
|
|
sendMessage(msg.chat.id, doc, true, msg.message_id, true)
|
2015-11-25 03:22:04 +01:00
|
|
|
|
return
|
|
|
|
|
end
|
2015-08-18 11:55:25 +02:00
|
|
|
|
end
|
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
|
local gurl = 'https://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=1&q=site:wikipedia.org%20'
|
|
|
|
|
local wurl = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exchars=4000&exsectionformat=plain&titles='
|
|
|
|
|
|
|
|
|
|
local jstr, res = HTTPS.request(gurl .. URL.escape(input))
|
2015-08-18 11:55:25 +02:00
|
|
|
|
if res ~= 200 then
|
2015-11-26 11:34:16 +01:00
|
|
|
|
sendReply(msg, config.errors.connection)
|
2015-11-25 03:22:04 +01:00
|
|
|
|
return
|
2015-08-18 11:55:25 +02:00
|
|
|
|
end
|
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
|
local jdat = JSON.decode(jstr)
|
2016-01-08 14:44:37 +01:00
|
|
|
|
if not jdat.responseData then
|
|
|
|
|
sendReply(msg, config.errors.connection)
|
|
|
|
|
return
|
|
|
|
|
end
|
2015-11-26 11:34:16 +01:00
|
|
|
|
if not jdat.responseData.results[1] then
|
|
|
|
|
sendReply(msg, config.errors.results)
|
|
|
|
|
return
|
|
|
|
|
end
|
2016-01-13 19:00:17 +01:00
|
|
|
|
--
|
2016-02-26 05:14:25 +01:00
|
|
|
|
local url = jdat.responseData.results[1].unescapedUrl
|
2015-11-25 03:22:04 +01:00
|
|
|
|
local title = jdat.responseData.results[1].titleNoFormatting:gsub(' %- Wikipedia, the free encyclopedia', '')
|
|
|
|
|
|
2016-02-26 05:14:25 +01:00
|
|
|
|
-- 'https://en.wikipedia.org/wiki/':len ≡ 30
|
|
|
|
|
jstr, res = HTTPS.request(wurl .. url:sub(31))
|
2015-08-18 11:55:25 +02:00
|
|
|
|
if res ~= 200 then
|
2015-11-25 03:22:04 +01:00
|
|
|
|
sendReply(msg, config.error.connection)
|
|
|
|
|
return
|
2015-08-18 11:55:25 +02:00
|
|
|
|
end
|
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
|
local text = JSON.decode(jstr).query.pages
|
2015-08-18 11:55:25 +02:00
|
|
|
|
for k,v in pairs(text) do
|
|
|
|
|
text = v.extract
|
|
|
|
|
break -- Seriously, there's probably a way more elegant solution.
|
|
|
|
|
end
|
2015-08-23 08:46:34 +02:00
|
|
|
|
if not text then
|
2015-11-26 11:34:16 +01:00
|
|
|
|
sendReply(msg, config.errors.results)
|
2015-11-25 03:22:04 +01:00
|
|
|
|
return
|
2015-08-23 08:46:34 +02:00
|
|
|
|
end
|
|
|
|
|
|
2016-02-20 11:57:57 +01:00
|
|
|
|
text = text:gsub('</?.->', '')
|
|
|
|
|
local l = text:find('\n')
|
2015-08-23 08:46:34 +02:00
|
|
|
|
if l then
|
2016-02-23 22:32:34 +01:00
|
|
|
|
text = text:sub(1, l-1)
|
2015-08-23 08:46:34 +02:00
|
|
|
|
end
|
2015-08-18 11:55:25 +02:00
|
|
|
|
|
2016-01-08 14:44:37 +01:00
|
|
|
|
title = title:gsub('%(.+%)', '')
|
|
|
|
|
--local output = '[' .. title .. '](' .. url .. ')\n' .. text:gsub('%[.+]%','')
|
|
|
|
|
--local output = '*' .. title .. '*\n' .. text:gsub('%[.+]%','') .. '\n[Read more.](' .. url .. ')'
|
2016-02-26 05:50:51 +01:00
|
|
|
|
local esctitle = title:gsub("[%^$()%%.%[%]*+%-?]","%%%1")
|
|
|
|
|
local output = text:gsub('%[.+%]',''):gsub(esctitle, '*%1*') .. '\n'
|
2016-01-08 14:44:37 +01:00
|
|
|
|
if url:find('%(') then
|
|
|
|
|
output = output .. url:gsub('_', '\\_')
|
|
|
|
|
else
|
|
|
|
|
output = output .. '[Read more.](' .. url .. ')'
|
|
|
|
|
end
|
|
|
|
|
|
2016-01-13 19:00:17 +01:00
|
|
|
|
--
|
|
|
|
|
--[[ Comment the previous block and uncomment this one for full-message,
|
|
|
|
|
-- "unlinked" link previews.
|
|
|
|
|
-- Invisible zero-width, non-joiner.
|
|
|
|
|
local output = '[](' .. jdat.responseData.results[1].url .. ')'
|
|
|
|
|
]]--
|
2015-08-18 11:55:25 +02:00
|
|
|
|
|
2016-02-20 11:07:20 +01:00
|
|
|
|
sendMessage(msg.chat.id, output, true, nil, true)
|
|
|
|
|
|
2015-08-18 11:55:25 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
action = action,
|
2015-11-25 03:22:04 +01:00
|
|
|
|
triggers = triggers,
|
2016-01-08 14:44:37 +01:00
|
|
|
|
doc = doc,
|
|
|
|
|
command = command
|
2015-08-18 11:55:25 +02:00
|
|
|
|
}
|