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
Raw Normal View History

2016-08-25 00:30:25 +02:00
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
2016-08-25 00:30:25 +02:00
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
2016-08-25 00:30:25 +02:00
-- 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
2016-08-25 00:30:25 +02:00
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)
2016-08-25 00:30:25 +02:00
else
local tag = matches[1]
link, pic = ponyfaces:get_ponyface(tag)
2016-08-25 00:30:25 +02:00
end
if not pic then
2016-08-25 00:30:25 +02:00
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)
2016-08-25 00:30:25 +02:00
end
return ponyfaces