local derpibooru = {}
function derpibooru:init(config)
derpibooru.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('dp', true):t('derpibooru', true).table
derpibooru.doc = [[*
]]..config.cmd_pat..[[dp* _<Tags>_: Sucht auf Derpibooru mit diesen Tags]]
end
derpibooru.command = 'dp <Tags>'
function derpibooru:get_dp(tag)
local BASE_URL = 'https://derpiboo.ru/'
local url = BASE_URL..'/search.json?q='..tag
local res, code = https.request(url)
if code ~= 200 then return nil end
local dp = json.decode(res).search
if not dp[1] then return nil end
local i = math.random(#dp)
local link_image = 'https:'..dp[i].image
local source = 'https://derpiboo.ru/'..dp[i].id
return link_image, source
function derpibooru:action(msg, config)
local input = utilities.input_from_msg(msg)
if not input then
utilities.send_reply(msg, derpibooru.doc, true)
return
local tag = string.gsub(input, " ", '+' )
local url, source = derpibooru:get_dp(tag)
if not url then
utilities.send_reply(msg, config.errors.results)
utilities.send_typing(msg.chat.id, 'upload_photo')
local file = download_to_file(url)
utilities.send_photo(msg.chat.id, file, source, msg.message_id)
return derpibooru