This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/plugins/xkcd.lua
topkecleon 26c1299374 Rewrote help.lua and added markup support to several plugins.
help.lua has been rewritten to support "/help command".
New variable "command" has been added to plugins for the syntax (w/out slash) to be displayed in main
help message. "doc" will be displayed upon "/help command".

Output of >12 plugins has been reformated to utilize markup.
There is a fairly standard style throughout plugins.

get_word() in utilities.lua now has defaults for nil arguments.
2016-01-08 08:44:37 -05:00

60 lines
1.3 KiB
Lua
Executable File

local command = 'xkcd [query]'
local doc = [[```
/xkcd [query]
Returns an xkcd strip and its alt text. If there is no query, it will be randomized.
```]]
local triggers = {
'^/xkcd[@'..bot.username..']*'
}
local action = function(msg)
local input = msg.text:input()
local jstr, res = HTTP.request('http://xkcd.com/info.0.json')
if res ~= 200 then
sendReply(msg, config.errors.connection)
return
end
local latest = JSON.decode(jstr).num
local res_url
if input then
local url = 'https://ajax.googleapis.com/ajax/services/search/web?v=1.0&safe=active&q=site%3axkcd%2ecom%20' .. URL.escape(input)
local jstr, res = HTTPS.request(url)
if res ~= 200 then
sendReply(msg, config.errors.connection)
return
end
local jdat = JSON.decode(jstr)
if #jdat.responseData.results == 0 then
sendReply(msg, config.errors.results)
return
end
res_url = jdat.responseData.results[1].url .. 'info.0.json'
else
res_url = 'http://xkcd.com/' .. math.random(latest) .. '/info.0.json'
end
local jstr, res = HTTP.request(res_url)
if res ~= 200 then
sendReply(msg, config.errors.connection)
return
end
local jdat = JSON.decode(jstr)
local output = '[' .. jdat.num .. '](' .. jdat.img .. ')\n' .. jdat.alt
sendMessage(msg.chat.id, output, false, nil, true)
end
return {
action = action,
triggers = triggers,
doc = doc,
command = command
}