administration.lua: /list -> /glist

wikipedia.lua: Working again, using Wikipedia's search API.
reactions.lua: Bugfix, and added /facepalm.
This commit is contained in:
topkecleon 2016-05-13 16:09:40 -04:00
parent 7df8992abe
commit 3f17f7c516
3 changed files with 48 additions and 22 deletions

View File

@ -1086,7 +1086,7 @@ function administration.init_command(self_)
}, },
{ -- glist { -- glist
triggers = utilities.triggers(self_.info.username):t('list', false).table, triggers = utilities.triggers(self_.info.username):t('glist', false).table,
command = 'glist', command = 'glist',
privilege = 5, privilege = 5,

View File

@ -17,7 +17,8 @@ local mapping = {
['flip'] = '(╯°□°)╯︵ ┻━┻', ['flip'] = '(╯°□°)╯︵ ┻━┻',
['homo'] = ' o', ['homo'] = ' o',
['look'] = 'ಠ_ಠ', ['look'] = 'ಠ_ಠ',
['shots?'] = 'SHOTS FIRED' ['shots?'] = 'SHOTS FIRED',
['facepalm'] = '(-‸ლ)'
} }
local help local help
@ -27,7 +28,7 @@ function reactions:init()
help = 'Reactions:\n' help = 'Reactions:\n'
reactions.triggers = utilities.triggers(self.info.username):t('reactions').table reactions.triggers = utilities.triggers(self.info.username):t('reactions').table
for trigger,reaction in pairs(mapping) do for trigger,reaction in pairs(mapping) do
help = help .. '' .. utilities.INVOCATION_PATTERN..trigger .. trigger:gsub('.%?', '') .. ': ' .. reaction .. '\n' help = help .. '' .. utilities.INVOCATION_PATTERN .. trigger:gsub('.%?', '') .. ': ' .. reaction .. '\n'
table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger) table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger)
table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger..'@'..self.info.username:lower()) table.insert(reactions.triggers, utilities.INVOCATION_PATTERN..trigger..'@'..self.info.username:lower())
end end

View File

@ -17,8 +17,19 @@ function wikipedia:init()
wikipedia.triggers = utilities.triggers(self.info.username):t('wikipedia', true):t('wiki', true):t('w', true).table wikipedia.triggers = utilities.triggers(self.info.username):t('wikipedia', true):t('wiki', true):t('w', true).table
end end
local get_title = function(search)
for i,v in ipairs(search) do
if not v.snippet:match('may refer to:') then
return v.title
end
end
return false
end
function wikipedia:action(msg) function wikipedia:action(msg)
-- Get the query. If it's not in the message, check the replied-to message.
-- If those don't exist, send the help text.
local input = utilities.input(msg.text) local input = utilities.input(msg.text)
if not input then if not input then
if msg.reply_to_message and msg.reply_to_message.text then if msg.reply_to_message and msg.reply_to_message.text then
@ -29,32 +40,38 @@ function wikipedia:action(msg)
end end
end end
local gurl = 'https://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=1&q=site:wikipedia.org%20' -- This kinda sucks, but whatever.
local wurl = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exchars=4000&exsectionformat=plain&titles=' input = input:gsub('#', ' sharp')
local jstr, res = HTTPS.request(gurl .. URL.escape(input)) -- Disclaimer: These variables will be reused.
local jstr, res, jdat
-- All pretty standard from here.
local search_url = 'https://en.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch='
jstr, res = HTTPS.request(search_url .. URL.escape(input))
if res ~= 200 then if res ~= 200 then
bindings.sendReply(self, msg, self.config.errors.connection) bindings.sendReply(self, msg, self.config.errors.connection)
return return
end end
local jdat = JSON.decode(jstr) jdat = JSON.decode(jstr)
if not jdat.responseData then if jdat.query.searchinfo.totalhits == 0 then
bindings.sendReply(self, msg, self.config.errors.connection)
return
end
if not jdat.responseData.results[1] then
bindings.sendReply(self, msg, self.config.errors.results) bindings.sendReply(self, msg, self.config.errors.results)
return return
end end
local url = jdat.responseData.results[1].unescapedUrl local title = get_title(jdat.query.search)
local title = jdat.responseData.results[1].titleNoFormatting:gsub(' %- Wikipedia, the free encyclopedia', '') if not title then
bindings.sendReply(self, msg, self.config.errors.results)
return
end
-- 'https://en.wikipedia.org/wiki/':len() == 30 local res_url = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exchars=4000&exsectionformat=plain&titles='
jstr, res = HTTPS.request(wurl .. url:sub(31))
jstr, res = HTTPS.request(res_url .. URL.escape(title))
if res ~= 200 then if res ~= 200 then
bindings.sendReply(self, msg, self.config.error.connection) bindings.sendReply(self, msg, self.config.errors.connection)
return return
end end
@ -68,20 +85,28 @@ function wikipedia:action(msg)
text = text.extract text = text.extract
end end
-- Remove needless bits from the article, take only the first paragraph.
text = text:gsub('</?.->', '') text = text:gsub('</?.->', '')
local l = text:find('\n') local l = text:find('\n')
if l then if l then
text = text:sub(1, l-1) text = text:sub(1, l-1)
end end
-- This block can be annoying to read.
-- We use the initial title to make the url for later use. Then we remove
-- the extra bits that won't be in the article. We determine whether the
-- first part of the text is the title, and if so, we embolden that.
-- Otherwise, we prepend the text with a bold title. Then we append a "Read
-- More" link.
local url = 'https://en.wikipedia.org/wiki/' .. URL.escape(title)
title = title:gsub('%(.+%)', '') title = title:gsub('%(.+%)', '')
local esctitle = title:gsub("[%^$()%%.%[%]*+%-?]","%%%1") local output
local output = text:gsub('%[.+%]',''):gsub(esctitle, '*%1*') .. '\n' if string.match(text:sub(1, title:len()), title) then
if url:find('%(') then output = '*' .. title .. '*' .. text:sub(title:len()+1)
output = output .. url:gsub('_', '\\_')
else else
output = output .. '[Read more.](' .. url .. ')' output = '*' .. title:gsub('%(.+%)', '') .. '*\n' .. text:gsub('%[.+%]','')
end end
output = output .. '\n[Read more.](' .. url:gsub('%)', '\\)') .. ')'
bindings.sendMessage(self, msg.chat.id, output, true, nil, true) bindings.sendMessage(self, msg.chat.id, output, true, nil, true)