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

58 lines
1.5 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
local link = 'http://ponyfac.es/'..data.faces[1].id
local pic = data.faces[1].image
return link, pic
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 data = json.decode(res).faces
-- truly randomize
math.randomseed(os.time())
-- random max json table size
local i = math.random(#data)
local link = 'http://ponyfac.es/'..data[i].id
local pic = data[i].image
return link, pic
end
function ponyfaces:action(msg, config, matches)
if tonumber(matches[1]) ~= nil then
local id = matches[1]
link, pic = ponyfaces:get_ponyface_by_id(id)
else
local tag = matches[1]
link, pic = ponyfaces:get_ponyface(tag)
end
if not pic then
utilities.send_reply(msg, config.errors.results)
return
end
utilities.send_typing(msg.chat.id, 'upload_photo')
utilities.send_photo(msg.chat.id, pic, link, msg.message_id)
end
return ponyfaces