Alexa Skill Recipe: Using Session Attributes to Enable Repeat Responses
Amit Jotwani Mar 28, 2018
Share:
Build Dialog Management Node.js Skill Recipes Tips & Tools Tutorial
Blog_Header_Post_Img

Creating an Alexa skill is like cooking a delicious meal. There are many recipes to choose from and several ingredients that go into each one. The Alexa Skill-Building Cookbook on GitHub gives you the recipes and ingredients to build engaging Alexa skills, using short code samples and guidance for adding features to your voice experience. With each installment of the Alexa skill recipe series, we’ll introduce you to a new recipe that can help you improve your voice design and skill engagement. You’re the chef. Let’s get cooking!

One critical part of the way we converse with each other in everyday life is by remembering what the other person just said. This allows us to continue our conversation using new bits of information. Having the ability to “remember" and then “repeat" the last part of a conversation is an intrinsic part of natural conversation.

Let’s say you’re having a conversation with a friend. You either missed something they said or didn’t quite hear it right, or perhaps you got distracted. You ask them to “repeat” the last thought (sometimes to the dismay of our friends), and then carry on with the conversation. This is something that comes very naturally to us, and it’s a good design practice to keep in mind as you’re building Alexa skills. It’s important to give your skill the ability to remember the last speech response, and then, if asked for, repeat that response.

For example, when getting ready in the morning, I often ask Alexa for the weather, only to realize halfway through the briefing that I got distracted (digging for a pair of socks) and didn’t quite hear it. So, I stop her midway by saying, “Alexa, repeat that please,”, or “what was that again?” Alexa responds by starting the briefing over for me. Just like in real life, Alexa’s ability to repeat the last response is something users appreciate and expect from a skill.

To implement repeat functionality for your skill, all you need to do is include AMAZON.RepeatIntent in your interaction model, and then use Session Attributes in your code to remember the last speech response that was relayed back to the customer. Let’s get started.

What is AMAZON.RepeatIntent?

AMAZON.RepeatIntent is one of the many standard built-in intents that lets the user request to repeat the last action. In our skill, it will let our customers ask for the last fact to be repeated.

Built-in intents are a great way to capture common, general user actions such as stopping (AMAZON.StopIntent), canceling (AMAZON.CancelIntent, asking for help (AMAZON.HelpIntent), providing a positive response (AMAZON.YesIntent), and providing a negative response (AMAZON.NoIntent). These intents are supported for skills in all supported languages: English (Australia, Canada, India, the UK, and the US), French, German, and Japanese.

Adding Repeat Functionality to a Fact Skill

In this skill recipe, we’ll take this simple fact skill (written in Node.js), and add the repeat functionality to it so the user can ask Alexa to repeat the last fact.

What we really want to achieve with the repeat functionality for our fact skill is this kind of conversation:

User: Alexa, ask space facts to tell me a fact <Session Starts, Intent: GetNewFactIntent>

Alexa: The Sun contains 99.86% of the mass in the Solar System. Would you like another fact? <Save this fact as a Session Attribute>

User: Yes <Intent: AMAZON.YesIntent>

Alexa: Jupiter has the shortest day of all the planets. Would you like another fact? <Save this fact as a Session Attribute>

User: Repeat that <Intent: AMAZON.RepeatIntent>

Alexa: Jupiter has the shortest day of all the planets. Would you like another fact? <Retrieve the last fact from Session Attribute>

User: No <Intent: AMAZON.NoIntent>

Alexa: Goodbye <Session Ends>

 

Here’s what we will do to achieve this:

  1. Add the AMAZON.RepeatIntent to our interaction model using the Skill Builder
  2. Set Session Attributes: Add one line of code to our GetNewFactIntent handler that will save the last spoken response to the Session Attributes
  3. Add a new handler for the AMAZON.Repeat Intent
  4. Get Session Attributes: Add one line of code to AMAZON.Repeat Intent handler to retrieve the last spoken response from the Session Attributes
  5. Garnish#1: Extend the utterances for AMAZON.RepeatIntent
  6. Garnish #2: To make our skill truly conversational, we will also add Yes/No intents so the user can continue to ask for more facts

 

First, we’ll add the AMAZON.RepeatIntent intent to our interaction model through the skill builder on developer.amazon.com.

Alexa Blog
Alexa Blog

Be sure to save and build your interaction model after you’ve added the new intent to your skill.

Now that we’ve added the AMAZON.RepeatIntent, we are ready to write some code to add repeat functionality to our basic fact skill, available on GitHub.

Turn 1: “Alexa, ask space facts to give me a fact”

The customer launches your skill by saying, “Alexa, ask space facts to give me a fact.” Alexa recognizes this as an “IntentRequest" for your skill, with the intent name being matched to “GetNewFactIntent”. Let’s edit our ‘GetNewFactIntent” handler function to generate a new fact. Example: “The Sun contains 99.86% of the mass in the Solar System.” We’ll add that fact as a session attribute (we’ll call it “lastSpeech”), and then respond back with the fact.

Step 1: Setting the Session Attributes

To keep track of any session-level data (in our case, "lastSpeech”), all you need to do is add it as a key/value pair inside the sessionAttributes property of the JSON response from your skill. The session attributes will then be passed back to you with the next request JSON inside the session’s object, which you can use later. The Alexa Node.js SDK makes it easy to do this, with a single line of code:

Copied to clipboard
this.attributes. =  

Here’s how we do that inside our GetNewFactIntent handler:

Copied to clipboard
    'GetNewFactIntent': function () { 
        //generate a new fact using an array of available facts (data)
        const factArr = data; 
        const factIndex = Math.floor(Math.random() * factArr.length); 
        const randomFact = factArr[factIndex]; 
        const speechOutput = GET_FACT_MESSAGE + randomFact; 
         
        this.attributes.lastSpeech = randomFact //adding the last fact to the session attributes, so we can use it to repeat it if requested by the user. 
        this.response.cardRenderer(SKILL_NAME, randomFact); 
        this.response.speak(speechOutput + " Would you like another fact?").listen("Would you like another fact?"); 
        this.emit(':responseReady'); 
    } 

The code above generates a new fact, and the speech that Alexa should respond back (outputSpeech) with. It also adds lastSpeech key/value pair inside the sessionAttributes object in the JSON response. The sessionAttributes object will now be available to our skill as part of the next request sent to our skill, which we will access through our AMAZON.RepeatIntent.

Turn 2: “repeat that please”

In the last response, Alexa responded with something like "The Sun contains 99.86% of the mass in the Solar System. Would you like another fact?” This reprompted the user to either say “yes” or “no”. Our user, of course, didn’t hear that, and wants Alexa to repeat it. He does that by saying, “say that again.” Alexa matches this to our AMAZON.RepeatIntent. Let’s now get our session attributes, and respond back using the “lastSpeech” attribute we set earlier.

Step 2: Getting the Session Attributes (AMAZON.RepeatIntent)

Copied to clipboard
    'AMAZON.RepeatIntent': function () { 
        this.response.speak(this.attributes.lastSpeech + " Would you like another fact?").listen("Would you like another fact?"); 
        this.emit(':responseReady'); 
    } 

The code above gets the “lastSpeech” attribute from the JSON request, and uses it to generate the repeat response (outputSpeech). The customer can now ask for the same fact as many times, by simply using one of the built-in phrases, like “repeat that” or “say that again.” You can of course add more phrases to the AMAZON.RepeatIntent by extending the intent. Let’s see how.

Garnish #1: Extending the Amazon.RepeatIntent Utterances

When I first wrote this skill, the repeat intent was able to handle utterances like “repeat that” and “say that again,” but it wasn’t able to handle “what was that again” and “I didn’t get that,” which I noticed I was using a lot. Common utterances it can handle by default are: “repeat,” “say that again,” and “repeat that.”

You may also realize that you want to add a few utterances that you find your customers use often. To handle this, you can extend the AMAZON.RepeatIntent by adding your utterances to it, like “what’s that” and “I didn’t get that.”

Garnish #2: Add Yes/No Intents

To make it a truly conversational skill, let’s add two more built-in intents to handle the user prompt so they can continue asking for more facts:

Copied to clipboard
    'AMAZON.YesIntent': function () { 
        this.emit("GetNewFactIntent"); 
    }, 
    'AMAZON.NoIntent': function () { 
        this.response.speak(STOP_MESSAGE); 
        this.emit(':responseReady'); 
    } 

You can see the AMAZON.RepeatIntent in action in the Alexa Team Lookup Skill template. And for more recipes, visit the Alexa Skill-Building Cookbook on GitHub.

More Resources

Check out these additional resources help make your skills more natural and conversational using built-in intents, and session attributes:

Build Engaging Skills, Earn Money with Alexa Developer Rewards

Every month, developers can earn money for eligible skills that drive some of the highest customer engagement. Developers can increase their level of skill engagement and potentially earn more by improving their skill, building more skills, and making their skills available in in the US, UK and Germany. Learn more about our rewards program and start building today. Download our guide or watch our on-demand webinar for tips to build engaging skills.