From 595d679d8b183c5bcf17c0aa3714a52b8082d31a Mon Sep 17 00:00:00 2001 From: Akamaru Date: Sun, 24 May 2015 14:54:03 +0200 Subject: [PATCH] update pokedex.lua --- plugins/pokedex.lua | 63 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 49 insertions(+), 14 deletions(-) diff --git a/plugins/pokedex.lua b/plugins/pokedex.lua index 8589e79..43dfb97 100644 --- a/plugins/pokedex.lua +++ b/plugins/pokedex.lua @@ -1,6 +1,21 @@ do -local function get_pokemon(query) +local images_enabled = true; + +local function get_sprite(path) + local url = "http://pokeapi.co/"..path + print(url) + local b,c = http.request(url) + local data = json:decode(b) + local image = data.image + return image +end + +local function callback(extra) + send_msg(extra.receiver, extra.text, ok_cb, false) +end + +local function send_pokemon(query, receiver) local url = "http://pokeapi.co/api/v1/pokemon/" .. query .. "/" local b,c = http.request(url) local pokemon = json:decode(b) @@ -8,24 +23,44 @@ local function get_pokemon(query) if pokemon == nil then return 'Kein Pokemon gefunden.' end - return 'Pokédex ID: ' .. pokemon.pkdx_id .. '\n' - ..'Name: ' .. pokemon.name .. '\n' - ..'Gewicht: ' .. pokemon.weight .. '\n' - ..'Größe: ' .. pokemon.height .. '\n' - ..'Speed: ' .. pokemon.speed .. '\n' - ..'Spezies: ' .. pokemon.species .. '\n' + + local text = 'Pokédex ID: ' .. pokemon.pkdx_id + ..'\nName: ' .. pokemon.name + ..'\nGewicht: ' .. pokemon.weight + ..'\nGröße: ' .. pokemon.height + ..'\nSpeed: ' .. pokemon.speed + ..'\nSpezies: ' .. pokemon.species + + local image = nil + + if images_enabled and pokemon.sprites and pokemon.sprites[1] then + local sprite = pokemon.sprites[1].resource_uri + image = get_sprite(sprite) + end + + if image then + image = "http://pokeapi.co"..image + local extra = { + receiver = receiver, + text = text + } + send_photo_from_url(receiver, image, callback, extra) + else + return text + end end local function run(msg, matches) - local name = string.lower(matches[1]) - return get_pokemon(name) + local receiver = get_receiver(msg) + local query = URL.escape(matches[1]) + return send_pokemon(query, receiver) end return { description = "Pokedex für Telegram", - usage = "/pokedex [Name/ID]", - patterns = {"^/pokedex (.*)$"}, + usage = {"/pokedex [Name/ID]","/pokemon [Name/ID]","/pkmn [Name/ID]"}, + patterns = {"^/pokedex (.*)$","^/pokemon (.+)$","^/pkmn (.+)$"}, run = run - } - - end \ No newline at end of file +} + +end \ No newline at end of file