2016-04-08 23:12:02 +02:00
|
|
|
local cats = {}
|
|
|
|
|
|
|
|
local HTTP = require('socket.http')
|
2016-06-07 06:31:34 +02:00
|
|
|
local utilities = require('otouto.utilities')
|
2016-04-08 23:12:02 +02:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function cats:init(config)
|
|
|
|
if not config.thecatapi_key then
|
2016-04-08 23:12:02 +02:00
|
|
|
print('Missing config value: thecatapi_key.')
|
|
|
|
print('cats.lua will be enabled, but there are more features with a key.')
|
|
|
|
end
|
2016-04-12 05:55:46 +02:00
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
cats.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('cat').table
|
2015-11-25 03:22:04 +01:00
|
|
|
end
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
cats.command = 'cat'
|
2016-07-25 11:03:35 +02:00
|
|
|
cats.doc = 'Returns a cat!'
|
2015-08-23 08:46:34 +02:00
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function cats:action(msg, config)
|
2015-08-23 08:46:34 +02:00
|
|
|
|
|
|
|
local url = 'http://thecatapi.com/api/images/get?format=html&type=jpg'
|
2016-05-27 02:26:30 +02:00
|
|
|
if config.thecatapi_key then
|
|
|
|
url = url .. '&api_key=' .. config.thecatapi_key
|
2015-08-23 08:46:34 +02:00
|
|
|
end
|
|
|
|
|
2015-11-25 03:22:04 +01:00
|
|
|
local str, res = HTTP.request(url)
|
2015-08-23 08:46:34 +02:00
|
|
|
if res ~= 200 then
|
2016-06-07 09:04:27 +02:00
|
|
|
utilities.send_reply(self, msg, config.errors.connection)
|
2015-11-25 03:22:04 +01:00
|
|
|
return
|
2015-08-23 08:46:34 +02:00
|
|
|
end
|
|
|
|
|
2016-03-04 23:08:21 +01:00
|
|
|
str = str:match('<img src="(.-)">')
|
2016-02-22 21:53:17 +01:00
|
|
|
local output = '[Cat!]('..str..')'
|
2015-08-23 08:46:34 +02:00
|
|
|
|
2016-05-29 19:08:39 +02:00
|
|
|
utilities.send_message(self, msg.chat.id, output, false, nil, true)
|
2015-08-23 08:46:34 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-04-08 23:12:02 +02:00
|
|
|
return cats
|