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
-
In the Alexa Developer Console, click the Code tab. The code editor opens the
index.js
orlambda_function.py
file.There are two pieces to a handler:
canHandle()
(Node.js) orcan_handle()
(Python) functionhandle()
function
The
canHandle()
(Node.js) orcan_handle()
(Python) function is where you define what requests the handler responds to. Thehandle()
function returns a response to the user.If your skill receives a request, the
canHandle()
(Node.js) orcan_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) orcan_handle()
(Python) function within theLaunchRequestHandler
will let the SDK know it can fulfill the request. In computer terms, thecanHandle()
returns true to confirm it can do the work.Tip: In the code editor, search for text by pressing CTRL+F (Command+F on a Mac). A search window opens. This is helpful for searching for pieces of code within the editor.
- Within the
LaunchRequestHandler
object, find thehandle()
function, and the line that beginsconst speakOutput
. -
Replace that line with the following line.
const speakOutput = 'Hello! Welcome to Cake Time. That was a piece of cake! Bye!';
Tip: If you are not familiar with programming, the text between the 'single quotes' is called a string. It can also be put within "double quotes." To change a string's text, replace the text between the quotation marks.Tip: When you replace existing text or add new text to the code, blank lines might be introduced just before or after the text. Blank lines don't affect the code, but you can remove them. -
Within the
LaunchRequestHandler
, on the line under the speech text you just replaced, findhandlerInput.responseBuilder
. This function will help build the response to the user. -
On the next line, find
.speak(speakOutput)
. Note thespeakOutput
variable, which you defined earlier. When you call the.speak()
function, it tellsresponseBuilder
to speak the value ofspeakOutput
to the user. -
Within
responseBuilder
, find the.reprompt()
function. Make sure that you search in theLaunchRequestHandler
, within thehandle()
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. -
Just below the line you commented out in the
LaunchRequestHandler
, find the.getResponse()
function. This function converts theresponseBuilder
'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.
- Within the
LaunchRequestHandler
class, find thehandle()
function, and the line that beginsspeak_output =
. -
Replace that line with the following line.
speak_output = "Hello! Welcome to Cake Time. That was a piece of cake! Bye!"
-
Within the
LaunchRequestHandler
, on the line under the speech text you just replaced, findhandler_input.response_builder
. This function will help build the response to the user. -
On the next line, find
.speak(speak_output)
. Note thespeak_output
variable, which you defined earlier. When you call the.speak()
function, it tellsresponse_builder
to speak the value ofspeak_output
to the user. -
Within
response_builder
, find the.ask()
function. Make sure that you search in theLaunchRequestHandler
, within thehandle()
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. -
Just below the line you commented out in the
LaunchRequestHandler
, find the.response
statement. This function converts theresponse_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.
-
At the top of the screen, click Save.
-
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
-
Click the Test tab. The test simulator opens.
Note: An alert might appear that requests that you use your computer's microphone. Click Allow to enable testing the skill with your voice, just as if you were talking to an Alexa-enabled device. -
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."
Tip: When you test your skill in the developer console, you don't need to provide the wake word (usually "Alexa"). You can just enter or say, "Open Cake Time." However, when you test your skill on an Alexa-enabled device, you need the wake word: "Alexa, 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
- Click the Build tab.
-
From the left-hand panel, click Invocation.
-
Under Skill Invocation Name, in the box, delete the existing name, and then enter a new name.
Tip: For a new name, consider "cake game," "cake time quiz," or "cake time quiz game." If you're concerned that those names won't resolve the naming conflict, enter a pair of random words that might never be used as a skill name, like "broccoli buttercup." - After you enter a new name in the box, in the upper-left, click Save Model.
- Wait a few seconds for the developer console to save your changes, and then click Build Model. The build might take a few minutes.
- After the build is complete, click the Test tab.
- 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]."