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/otouto/plugins/cats.lua

38 lines
971 B
Lua
Raw Normal View History

local cats = {}
local HTTP = require('socket.http')
2016-06-07 06:31:34 +02:00
local utilities = require('otouto.utilities')
local bindings = require('otouto.bindings')
2016-05-27 02:26:30 +02:00
function cats:init(config)
2016-08-14 04:46:18 +02:00
if not config.thecatapi_key then
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-08-14 04:46:18 +02:00
cats.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('cat').table
end
cats.command = 'cat'
cats.doc = 'Returns a cat!'
2016-05-27 02:26:30 +02:00
function cats:action(msg, config)
2016-08-14 04:46:18 +02:00
local url = 'http://thecatapi.com/api/images/get?format=html&type=jpg'
if config.thecatapi_key then
url = url .. '&api_key=' .. config.thecatapi_key
end
2016-08-14 04:46:18 +02:00
local str, res = HTTP.request(url)
if res ~= 200 then
utilities.send_reply(msg, config.errors.connection)
2016-08-14 04:46:18 +02:00
return
end
2016-08-14 04:46:18 +02:00
str = str:match('<img src="(.-)">')
bindings.sendPhoto{chat_id = msg.chat.id, photo = str}
end
return cats