2016-04-11 06:04:47 +02:00
local xkcd = { }
2016-05-08 01:30:48 +02:00
xkcd.command = ' xkcd [i] '
2016-08-14 04:26:44 +02:00
xkcd.base_url = ' https://xkcd.com/info.0.json '
2016-09-05 13:59:57 +02:00
xkcd.strip_url = ' https://xkcd.com/%s/info.0.json '
2016-05-27 05:28:44 +02:00
function xkcd : init ( config )
2016-09-05 13:59:57 +02:00
xkcd.triggers = {
" xkcd.com/(%d+) " ,
2016-06-20 17:58:46 +02:00
" ^/xkcd (%d+) " ,
2016-09-05 13:59:57 +02:00
" ^/xkcd (r) " ,
" ^/xkcd "
2016-06-20 17:58:46 +02:00
}
2016-09-05 13:59:57 +02:00
xkcd.doc = [ [ *
] ] .. config.cmd_pat .. [[xkcd* _[i]_: Gibt den aktuellen XKCD-Comic aus, oder die Nummer, wenn eine gegeben ist. Wenn "r" übergeben wird, wird ein zufälliger Comic zurückgegeben.]]
local jstr = https.request ( xkcd.base_url )
if jstr then
local data = json.decode ( jstr )
if data then
xkcd.latest = data.num
end
2016-06-20 17:58:46 +02:00
end
2016-09-05 13:59:57 +02:00
xkcd.latest = xkcd.latest or 1700
2016-06-20 17:58:46 +02:00
end
2015-07-03 00:15:52 +02:00
2016-08-22 21:33:01 +02:00
function xkcd : action ( msg , config , matches )
2016-09-05 13:59:57 +02:00
if matches [ 1 ] == ' r ' then
input = math.random ( xkcd.latest )
elseif tonumber ( matches [ 1 ] ) then
input = tonumber ( matches [ 1 ] )
else
input = xkcd.latest
end
local url = xkcd.strip_url : format ( input )
local jstr , code = https.request ( url )
if code == 404 then
utilities.send_reply ( msg , config.errors . results )
elseif code ~= 200 then
utilities.send_reply ( msg , config.errors . connection )
else
local data = json.decode ( jstr )
local output = string.format (
' <b>%s</b> (<a href="%s">%s</a>) \n <i>%s</i> ' ,
utilities.html_escape ( utilities.fix_utf8 ( data.safe_title ) ) ,
utilities.html_escape ( data.img ) ,
data.num ,
utilities.html_escape ( utilities.fix_utf8 ( data.alt ) )
)
utilities.send_message ( msg.chat . id , output , false , nil , ' html ' )
end
2015-07-03 00:15:52 +02:00
end
2016-09-05 13:59:57 +02:00
return xkcd