Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pokedex Bot: Pokemon description and evolution endpoint in server.js
if (pokemon_species_endpoint.indexOf(specs) !== -1 || intent.displayName == 'evolution') {
const { data } = await axios_instance.get(`/pokemon-species/${pokemon}`);
const evolution_chain_id = data.evolution_chain.url.split('/')[6];
const text = data.flavor_text_entries.find(item => {
return item.language.name == 'en';
});
let fulfillmentText;
if (specs == 'description') {
fulfillmentText = `${pokemon}:\n\n ${text.flavor_text}`;
Object.assign(response_obj, {
fulfillmentText
});
}
if (intent.displayName == 'evolution') {
const evolution_response = await axios_instance.get(`/evolution-chain/${evolution_chain_id}`);
const evolution_requirement = parameters.evolutions;
let pokemon_evolutions = [evolution_response.data.chain.species.name];
fulfillmentText = `${pokemon} has no evolution`;
if (evolution_response.data.chain.evolves_to.length) {
pokemon_evolutions.push(evolution_response.data.chain.evolves_to[0].species.name);
}
if (evolution_response.data.chain.evolves_to[0].evolves_to.length) {
pokemon_evolutions.push(evolution_response.data.chain.evolves_to[0].evolves_to[0].species.name);
}
let evolution_chain = pokemon_evolutions.join(' -> ');
const order_in_evolution_chain = pokemon_evolutions.indexOf(pokemon);
const next_form = pokemon_evolutions[order_in_evolution_chain + 1];
const previous_form = pokemon_evolutions[order_in_evolution_chain - 1];
const evolution_text = {
'evolution_chain': `${pokemon}'s evolution chain is: ${evolution_chain}`,
'first_evolution': (pokemon == pokemon_evolutions[0]) ? `This is already the first form` : `${pokemon_evolutions[0]} is the first evolution`,
'last_evolution': (pokemon == pokemon_evolutions[pokemon_evolutions.length - 1]) ? `This is already the final form` : pokemon_evolutions[pokemon_evolutions.length - 1],
'next_form': `${pokemon} evolves to ${next_form}`,
'previous_form': `${pokemon} evolves from ${previous_form}`
};
if (evolution_text[evolution_requirement]) {
fulfillmentText = evolution_text[evolution_requirement];
}
Object.assign(response_obj, {
fulfillmentText
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.