Coding Your Skill: Pokemon Name

get_pokemon_name

Our intent calls the function get_pokemon_name, so let's navigate to that function in the code and fill that out.

We start off with the following:

def get_pokemon_name(intent, session):
    session_attributes = {}

    reprompt_text = None

    #TODO: Get the pokemon's name

    return build_response(session_attributes, build_speechlet_response(
        intent['name'], speech_output, reprompt_text, should_end_session))

To start off, we want to recognize what our intent looks like. It's essentially a dictionary that would look like the following:

# You don't have to copy this down!
intent: {
    ...
    'slots': {
        'PokedexNumber': {
            'name': 'PokedexNumber',
            'value': '25'
        }
    }
}

The value in our intent is what we're interested in using. This is the value of the POKEMON_SLOT_NAME, which if you recall, is what we've defined as our slot name when we were configuring our interaction model.

First, we want to make sure that this POKEMON_SLOT_NAME is in the intents. We'll do a quick check like so:

if POKEMON_SLOT_NAME in intent['slots']:

Now, we'll want to get the Pokemon number, or the value in the intent dictionary above.

pokemon_number = intent['slots'][POKEMON_SLOT_NAME]['value']

From the Pokemon number, we want to get the name of the Pokemon. We've provided a get_pokemon function for you already, but here's what it looks like:

def get_pokemon(index):
    """
    Given a pokemon index, return the pokemon name.
    """
    if not POKEMON_PARSED:
        parse_pokemon()

    return POKEMON_DICT[str(index)]

To get the Pokemon's name, we'll call this function with our pokemon number.

pokemon_name = get_pokemon(pokemon_number)

Now that we have the name, we'll want to construct a response for Alexa, rather than just having her say "Pikachu". We'll do this by using a custom speech_output.

speech_output = "Pokedex entry " + pokemon_number + " is named " + pokemon_name + "."

This speech output will also be displayed on your Alexa Companion app after you've made the request, so make sure to properly punctuate the response.

Catching Intent Errors

Now, to tie off the if statement we just built, we want to set up an else. This is in the case that our POKEMON_SLOT_NAME is not in our slots. We'll simply wrap it up with a "Please try again" statement from Alexa.

else:
    speech_output = "I'm not sure what you mean by that. " \
                    "Please try again."

The final thing in this function is the return statement, which builds the speech output into something that Alexa can understand and say out loud to you.

← Previous
Next →