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/miku/plugins/ponyfaces.lua
2016-08-25 00:30:25 +02:00

56 lines
1.4 KiB
Lua

local ponyfaces = {}
function ponyfaces:init(config)
ponyfaces.triggers = {
"^/ponyfaces? (.+)$",
"^/pf (.+)$",
"ponyfac.es/(%d+)"
}
ponyfaces.doc = [[*
]]..config.cmd_pat..[[pf* _<Tags>_: Sucht auf Ponyfac.es mit diesen Tags]]
end
ponyfaces.command = 'pf <Tags>'
local BASE_URL = 'http://ponyfac.es/api.json'
function ponyfaces:get_ponyface_by_id(id)
local url = BASE_URL..'/id:'..id
local res, code = http.request(url)
if code ~= 200 then return nil end
local data = json.decode(res)
if not data then return nil end
return data.faces[1].image
end
function ponyfaces:get_ponyface(tag)
local url = BASE_URL..'/tag:'..tag
local res, code = http.request(url)
if code ~= 200 then return nil end
local pf = json.decode(res).faces
-- truly randomize
math.randomseed(os.time())
-- random max json table size
local i = math.random(#pf)
local link_image = pf[i].image
return link_image
end
function ponyfaces:action(msg, config, matches)
if tonumber(matches[1]) ~= nil then
local id = matches[1]
url = ponyfaces:get_ponyface_by_id(id)
else
local tag = matches[1]
url = ponyfaces:get_ponyface(tag)
end
if not url then
utilities.send_reply(msg, config.errors.results)
return
end
utilities.send_typing(msg.chat.id, 'upload_photo')
local file = download_to_file(url, 'pf.png')
utilities.send_photo(msg.chat.id, file, nil, msg.message_id)
end
return ponyfaces