Portiere Pokedex und Ponyfaces

This commit is contained in:
Andreas Bielawski 2016-08-25 00:30:25 +02:00
parent 39e2333ecf
commit 8eedb1e9ef
2 changed files with 104 additions and 54 deletions

View File

@ -1,66 +1,60 @@
local pokedex = {}
pokedex.command = 'pokedex <query>'
pokedex.command = 'pokedex <Name (englisch/ID>'
function pokedex:init(config)
pokedex.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('pokedex', true):t('dex', true).table
pokedex.doc = [[```
]]..config.cmd_pat..[[pokedex <query>
Returns a Pokedex entry from pokeapi.co.
Alias: ]]..config.cmd_pat..[[dex
```]]
pokedex.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('pokedex', true):t('dex', true):t('pkmn', true).table
pokedex.doc = [[*
]]..config.cmd_pat..[[pokedex* _<Name oder ID>_: Sucht Pokémon im Pokedex (nur englisch)
]]
end
function pokedex:get_sprite(path)
local url = "http://pokeapi.co/"..path
local b, c = http.request(url)
if c ~= 200 then return nil end
local data = json.decode(b)
local image = data.image
return image
end
function pokedex:get_pkmn(query)
local url = "http://pokeapi.co/api/v1/pokemon/" .. query .. "/"
local res, code = http.request(url)
if code ~= 200 then return nil end
local pokemon = json.decode(res)
if not pokemon then return nil end
local text = '<b>Pokédex ID:</b> '..pokemon.pkdx_id
..'\n<b>Name:</b> '..pokemon.name
..'\n<b>Typ:</b> '..pokemon.types[1].name
..'\n<b>Gewicht:</b> '..pokemon.height..' kg'
..'\n<b>Größe:</b> '..pokemon.weight..' cm'
..'\n<b>Geschwindigkeit:</b> '..pokemon.speed
if not pokemon.species:isempty() then
text = text..'\n<b>Spezies:</b> '..pokemon.species
end
return text, pokemon.pkdx_id
end
function pokedex:action(msg, config)
local input = utilities.input_from_msg(msg)
if not input then
utilities.send_reply(msg, pokedex.doc, true)
return
end
bindings.sendChatAction({ chat_id = msg.chat.id, action = 'typing' } )
local input = utilities.input(msg.text_lower)
if not input then
if msg.reply_to_message and msg.reply_to_message.text then
input = msg.reply_to_message.text
else
utilities.send_message(msg.chat.id, pokedex.doc, true, msg.message_id, true)
return
end
end
local url = 'http://pokeapi.co'
local dex_url = url .. '/api/v1/pokemon/' .. input
local dex_jstr, res = http.request(dex_url)
if res ~= 200 then
utilities.send_reply(msg, config.errors.connection)
return
end
local dex_jdat = json.decode(dex_jstr)
local desc_url = url .. dex_jdat.descriptions[math.random(#dex_jdat.descriptions)].resource_uri
local desc_jstr, _ = http.request(desc_url)
if res ~= 200 then
utilities.send_reply(msg, config.errors.connection)
return
end
local desc_jdat = json.decode(desc_jstr)
local poke_type
for _,v in ipairs(dex_jdat.types) do
local type_name = v.name:gsub("^%l", string.upper)
if not poke_type then
poke_type = type_name
else
poke_type = poke_type .. ' / ' .. type_name
end
end
poke_type = poke_type .. ' type'
local output = '*' .. dex_jdat.name .. '*\n#' .. dex_jdat.national_id .. ' | ' .. poke_type .. '\n_' .. desc_jdat.description:gsub('POKMON', 'Pokémon'):gsub('Pokmon', 'Pokémon') .. '_'
utilities.send_message(msg.chat.id, output, true, nil, true)
utilities.send_typing(msg.chat.id, 'typing')
local text, id = pokedex:get_pkmn(input)
if not text then
utilities.send_reply(msg, config.errors.result)
return
end
utilities.send_typing(msg.chat.id, 'upload_photo')
utilities.send_photo(msg.chat.id, download_to_file('http://pokeapi.co/media/img/'..id..'.png'), nil, msg.message_id)
utilities.send_reply(msg, text, 'HTML')
end
return pokedex

View File

@ -0,0 +1,56 @@
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