2016-07-17 13:22:27 +02:00
local cats = { }
2016-07-18 18:37:29 +02:00
cats.command = ' kitty [gif] '
2016-07-17 13:22:27 +02:00
function cats : init ( config )
if not cred_data.cat_apikey then
2016-08-01 21:07:27 +02:00
print ( ' Fehlender Key: cat_apikey. ' )
2016-08-15 23:14:28 +02:00
print ( ' cats.lua wird aktiviert, aber mit einem Key gibt es mehr Features. ' )
2016-07-17 13:22:27 +02:00
end
cats.triggers = {
2016-07-18 18:37:29 +02:00
" ^/[Kk][Ii][Tt][Tt][Yy]$ " ,
" ^/[Kk][Ii][Tt][Tt][Yy] (gif)$ "
2016-07-17 13:22:27 +02:00
}
2016-08-01 21:07:27 +02:00
cats.inline_triggers = {
" ^[Kk][Ii][Tt][Tt][Yy] (gif)$ " ,
" ^[Kk][Ii][Tt][Tt][Yy]$ "
}
2016-07-17 13:22:27 +02:00
cats.doc = [ [ *
2016-07-18 18:37:29 +02:00
] ] .. config.cmd_pat .. [ [ kitty * : Postet eine zufällige Katze
* ] ] .. config.cmd_pat .. [[kitty* _gif_: Postet eine zufällige, animierte Katze]]
2016-07-17 13:22:27 +02:00
end
local apikey = cred_data.cat_apikey or " " -- apply for one here: http://thecatapi.com/api-key-registration.html
2016-08-01 21:07:27 +02:00
function cats : inline_callback ( inline_query , config , matches )
if matches [ 1 ] == ' gif ' then
img_type = ' gif '
2016-08-15 23:14:28 +02:00
id = 100
2016-08-01 21:07:27 +02:00
else
img_type = ' jpg '
2016-08-15 23:14:28 +02:00
id = 200
2016-08-01 21:07:27 +02:00
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
2016-08-15 23:14:28 +02:00
results = results .. ' {"type":"gif","id":" ' .. id .. ' ","gif_url":" ' .. data [ n ] . url .. ' ","thumb_url":" ' .. data [ n ] . url .. ' "} '
id = id + 1
2016-08-01 21:07:27 +02:00
else
2016-08-15 23:14:28 +02:00
results = results .. ' {"type":"photo","id":" ' .. id .. ' ","photo_url":" ' .. data [ n ] . url .. ' ","thumb_url":" ' .. data [ n ] . url .. ' "} '
id = id + 1
2016-08-01 21:07:27 +02:00
end
if n < # data then
results = results .. ' , '
end
end
local results = results .. ' ] '
2016-08-24 15:38:29 +02:00
utilities.answer_inline_query ( inline_query , results , 30 )
2016-08-01 21:07:27 +02:00
end
2016-07-17 13:22:27 +02:00
function cats : action ( msg , config )
if matches [ 1 ] == ' gif ' then
local url = ' http://thecatapi.com/api/images/get?type=gif&apikey= ' .. apikey
local file = download_to_file ( url , ' miau.gif ' )
2016-08-24 15:38:29 +02:00
utilities.send_document ( msg.chat . id , file , nil , msg.message_id )
2016-07-17 13:22:27 +02:00
else
local url = ' http://thecatapi.com/api/images/get?type=jpg,png&apikey= ' .. apikey
local file = download_to_file ( url , ' miau.png ' )
2016-08-24 15:38:29 +02:00
utilities.send_photo ( msg.chat . id , file , nil , msg.message_id )
2016-07-17 13:22:27 +02:00
end
end
2016-08-01 21:07:27 +02:00
return cats