2016-04-08 23:12:02 +02:00
local cats = { }
2016-07-27 17:35:04 +02:00
local https = require ( ' ssl.https ' )
local json = require ( ' dkjson ' )
2016-06-07 06:31:34 +02:00
local utilities = require ( ' otouto.utilities ' )
2016-04-08 23:12:02 +02:00
2016-06-18 16:41:21 +02:00
cats.command = ' cat [gif] '
2016-05-27 02:26:30 +02:00
function cats : init ( config )
2016-06-17 16:42:54 +02:00
if not cred_data.cat_apikey then
print ( ' Missing config value: cat_apikey. ' )
2016-04-08 23:12:02 +02:00
print ( ' cats.lua will be enabled, but there are more features with a key. ' )
end
2016-04-12 05:55:46 +02:00
2016-06-17 16:42:54 +02:00
cats.triggers = {
" ^/cat$ " ,
" ^/cat (gif)$ "
}
2016-07-27 17:35:04 +02:00
cats.inline_triggers = {
" ^cat (gif)$ " ,
" ^cat$ "
}
2016-06-17 16:42:54 +02:00
cats.doc = [ [ *
] ] .. config.cmd_pat .. [ [ cat * : Postet eine zufällige Katze
* ] ] .. config.cmd_pat .. [[cat* _gif_: Postet eine zufällige, animierte Katze]]
2015-11-25 03:22:04 +01:00
end
2016-06-18 16:41:21 +02:00
2016-06-17 16:42:54 +02:00
local apikey = cred_data.cat_apikey or " " -- apply for one here: http://thecatapi.com/api-key-registration.html
2015-08-23 08:46:34 +02:00
2016-07-27 17:35:04 +02:00
function cats : inline_callback ( inline_query , config , matches )
if matches [ 1 ] == ' gif ' then
img_type = ' gif '
else
img_type = ' jpg '
end
local url = ' https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20xml%20where%20url%3D%27http%3A%2F%2Fthecatapi.com%2Fapi%2Fimages%2Fget%3Fformat%3Dxml%26results_per_page%3D50%26type%3D ' .. img_type .. ' %26apikey%3D ' .. apikey .. ' %27&format=json ' -- no way I'm using XML, plz die
local res , code = https.request ( url )
if code ~= 200 then return end
local data = json.decode ( res ) . query.results . response.data . images.image
if not data then return end
if not data [ 1 ] then return end
local results = ' [ '
for n in pairs ( data ) do
if img_type == ' gif ' then
results = results .. ' {"type":"gif","id":" ' .. math.random ( 100000000000000000 ) .. ' ","gif_url":" ' .. data [ n ] . url .. ' ","thumb_url":" ' .. data [ n ] . url .. ' "} '
else
results = results .. ' {"type":"photo","id":" ' .. math.random ( 100000000000000000 ) .. ' ","photo_url":" ' .. data [ n ] . url .. ' ","thumb_url":" ' .. data [ n ] . url .. ' "} '
end
if n < # data then
results = results .. ' , '
end
end
local results = results .. ' ] '
utilities.answer_inline_query ( self , inline_query , results , 120 )
end
2016-05-27 02:26:30 +02:00
function cats : action ( msg , config )
2016-06-17 16:42:54 +02:00
if matches [ 1 ] == ' gif ' then
local url = ' http://thecatapi.com/api/images/get?type=gif&apikey= ' .. apikey
2016-06-21 16:20:56 +02:00
local file = download_to_file ( url , ' miau.gif ' )
2016-06-17 16:42:54 +02:00
utilities.send_document ( self , msg.chat . id , file , nil , msg.message_id )
else
local url = ' http://thecatapi.com/api/images/get?type=jpg,png&apikey= ' .. apikey
2016-06-21 16:20:56 +02:00
local file = download_to_file ( url , ' miau.png ' )
2016-06-17 16:42:54 +02:00
utilities.send_photo ( self , msg.chat . id , file , nil , msg.message_id )
end
2015-08-23 08:46:34 +02:00
end
2016-04-08 23:12:02 +02:00
return cats