Build a Skill in 20 Minutes

Step 2: Add a greeting

In Step 2, you'll learn how to modify the skill code to provide a custom greeting.

Step 2 has two sub-steps:

  • First, modify the Skill Code
  • Then, test your Skill

First, modify the skill code

The first thing a user wants to do with the skill is open it. The intent for opening the skill is built into the experience, so you don't need to define this intent in your VUI.

However, you do need to respond to the intent in your data access layer. In this step, you will update your access layer code to greet the user when they open the skill.

To have your skill greet the user

  1. In the Alexa Developer Console, click the Code tab. The code editor opens the index.js or lambda_function.py file.

    There are two pieces to a handler:

    • canHandle() (Node.js) or can_handle() (Python) function
    • handle() function

    The canHandle() (Node.js) or can_handle() (Python) function is where you define what requests the handler responds to. The handle() function returns a response to the user.

    If your skill receives a request, the canHandle() (Node.js) or can_handle() (Python) function within each handler determines whether or not that handler can service the request.

    In this case, the user wants to launch the skill, which is a LaunchRequest. Therefore, the canHandle() (Node.js) or can_handle() (Python) function within the LaunchRequestHandler will let the SDK know it can fulfill the request. In computer terms, the canHandle() returns true to confirm it can do the work.

  1. Within the LaunchRequestHandler object, find the handle() function, and the line that begins const speakOutput.
  2. Replace that line with the following line.

    const speakOutput = 'Hello! Welcome to Cake Time. That was a piece of cake! Bye!';
    
  3. Within the LaunchRequestHandler, on the line under the speech text you just replaced, find handlerInput.responseBuilder. This function will help build the response to the user.

  4. On the next line, find .speak(speakOutput). Note the speakOutput variable, which you defined earlier. When you call the .speak() function, it tells responseBuilder to speak the value of speakOutput to the user.

  5. Within responseBuilder, find the .reprompt() function. Make sure that you search in the LaunchRequestHandler, within the handle() function.

    If you want the skill to listen for the user's response, you would use the .reprompt() function. In this case, you want the skill to speak, and then exit. Therefore, you omit this line of code for now.

    Find the line that begins .reprompt(), and then enter a double forward slash (//) at the beginning of the line. This turns the line into a comment, meaning the line is ignored when the code runs.

  6. Just below the line you commented out in the LaunchRequestHandler, find the .getResponse() function. This function converts the responseBuilder's work into the response that the skill will return. Remember the line that started with return? Think of it like hitting the Send button - it sends the response.

    After you make all of these changes, the LaunchRequestHandler should look like the following example:

    const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest';
    },
    handle(handlerInput) {
        const speakOutput = 'Hello! Welcome to Cake Time. That was a piece of cake! Bye!';
    
        return handlerInput.responseBuilder
            .speak(speakOutput)
            // .reprompt(speakOutput)
            .getResponse();
    }
    };
    

    You have built the code that will handle a LaunchRequest for this skill.

  1. Within the LaunchRequestHandler class, find the handle() function, and the line that begins speak_output =.
  2. Replace that line with the following line.

    speak_output = "Hello! Welcome to Cake Time. That was a piece of cake! Bye!"
    
  3. Within the LaunchRequestHandler, on the line under the speech text you just replaced, find handler_input.response_builder. This function will help build the response to the user.

  4. On the next line, find .speak(speak_output). Note the speak_output variable, which you defined earlier. When you call the .speak() function, it tells response_builder to speak the value of speak_output to the user.

  5. Within response_builder, find the .ask() function. Make sure that you search in the LaunchRequestHandler, within the handle() function.

    If you want the skill to listen for the user's response, you would use the .ask() function. In this case, you want the skill to speak, and then exit. Therefore, you omit this line of code for now.

    Find the line that begins .ask(), and then enter a pound sign (#) at the beginning of the line. This turns the line into a comment, meaning the line is ignored when the code runs.

  6. Just below the line you commented out in the LaunchRequestHandler, find the .response statement. This function converts the response_builder's work into the response that the skill will return. Remember the line that started with return? Think of it like hitting the Send button - it sends the response.

    After you make all of these changes, the LaunchRequestHandler should look like the following example:

    class LaunchRequestHandler(AbstractRequestHandler):
    """Handler for Skill Launch."""
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
    
        return ask_utils.is_request_type("LaunchRequest")(handler_input)
    
    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        speak_output = "Hello! Welcome to Cake Time. That was a piece of cake! Bye!"
    
        return (
            handler_input.response_builder
                .speak(speak_output)
                # .ask(speak_output)
                .response
        )
    

    You have built the code that will handle a LaunchRequest for this skill.

  1. At the top of the screen, click Save.

  2. Click Deploy.

Then, test your Skill

Now it's time to test the skill you built. Start by activating the test simulator.

To activate the test simulator

  1. Click the Test tab. The test simulator opens.

  2. In the top left corner of the page, from the drop-down menu next to Test is disabled for this skill, select Development.

    There are two ways to test your skill in the developer console:

    • Type what the user would say into the box at the top left. Be precise - spelling matters!
    • Speak to the skill by clicking and holding the microphone icon, and then speaking.

    So far, the skill has one intent: LaunchRequest. This function responds to the user when they ask Alexa to open or launch the skill. The user says, "Alexa, open Cake Time." The developer console automatically set Cake Time as the invocation name for the skill. You can change the invocation name, but leave the name as is for this exercise.

    To test your skill in the developer console

    • In the box at the top left, enter open Cake Time (not case sensitive), and then press ENTER. Or, click and hold the microphone icon, and then say, "Open Cake Time."

    When you opened the skill, did Alexa say, "Hello! Welcome to Cake Time. That was a piece of cake! Bye!"?

    To troubleshoot your test, if the test failed

    • If you hear something different from what you put in to test, make sure you wait for the code you changed to save, and then deploy.

    If that's not the problem, your skill might be in conflict with another skill named "Cake Time." As our beginner workshop, many people have created a skill with that same name.

    To resolve a naming conflict with another skill

    1. Click the Build tab.
    2. From the left-hand panel, click Invocation.

      Build Tab

    3. Under Skill Invocation Name, in the box, delete the existing name, and then enter a new name.

      Save Model Button

    4. After you enter a new name in the box, in the upper-left, click Save Model.
    5. Wait a few seconds for the developer console to save your changes, and then click Build Model. The build might take a few minutes.
    6. After the build is complete, click the Test tab.
    7. In the box at the top left, enter open [new name] (not case sensitive), and then press ENTER. Or, click and hold the microphone icon, and then say, "Open [new name]."

Was this page helpful?