How to Repeat Responses using the Jovo User Context Object

Learn how to easily add a repeat functionality to your voice application using the Jovo User Context (available since Jovo v1.2).

Code

You can find the full code example of this tutorial here: jovo-templates/tutorials/repeat. Or download it using the Jovo CLI:

Why you should add a Repeat Intent

One of the biggest difficulties of recreating human to human conversations with an assistant is the fact that humans have context. Conversations are different depending on the location, current news, and so much more. These things are truly hard to implement, but there's one thing, actually the simplest as well as most essential thing you can easily add to your voice application.

I am talking about repeating yourself if the the person on the other end missed what you just said. One of the great things about voice apps is that they allow you to interact with them while you're busy with your hands (e.g. cleaning up). However, this advantage may also be one of the biggest disadvantages of voice: You're never sure if your user is actively listening or doing something else as well. This is why it is strongly encouraged to add the functionality to repeat yourself with a RepeatIntent. It's such an useful feature, that Amazon created a built-in intent (AMAZON.RepeatIntent) for that and Google listed it as a best practice.

As easy as it sounds, until today it was still a manual task to store all the necessary information in session attributes or a database so that they are later available to use. The newly introduced User Context feature makes the implementation of the repeat feature easier.

How does the User Context work?

The Jovo User Context automatically saves the essential data of the past interaction pairs (request and response) in your database. This includes the intent, state, inputs (slots/entities), output speech, reprompt, and timestamp.

The pairs are stored inside an array, which has the most recent pair at index 0 and the least recent at index size - 1. For example, you can get the latest output speech using either this.user().context.prev[0].response.speech or this.user().getPrevSpeech(0).

By default, only the last interaction pair is stored in the database (which is enough for implementing the repeat functionality below).

Configuration

You are also able to freely decide how many interaction pairs, as well as what exactly you want to save, by changing your application's configuration, which has the following default values:

The size value defines how many pairs are saved. If you don't want to save anything you can simply set size to 0. To disable the storage of certain elements, simply change their value to false. Here's an example:

Implementing the Repeat Functionality

If you haven't installed the Jovo CLI yet, please do, since I will make use of its features to save us time. You can learn more about the installation here.

In addition to that, I will create a new project in this tutorial, so if you want to add the RepeatIntent to an exisiting project simply skip ahead to "Adding the RepeatIntent to your Language Model".

Creating a Jovo Project

Alright, let's create a new project and initialize both Amazon Alexa and the Google Assistant as our platforms. We will just call it RepeatTutorial:

Adding the RepeatIntent to your Language Model

To spare us the hassle of adding the RepeatIntent to both Dialogflow and the Alexa Developer Console, we will simply add the intent to the Jovo Language Model.

We will use the built-in intent AMAZON.RepeatIntent by Amazon and create a RepeatIntent on Dialogflow. Open the language model in your project's models folder and add the following to the intents array:

Now we can use the Jovo CLI to build the language model for both platforms:

Deploying your Language Models

Next up, we're going to deploy the language models to their respective developer portals.

To deploy your Alexa Skill you have to install and initialize the ASK CLI first:

That's it. You can now deploy your Alexa Skill to the developer portal:

Deploying to Dialogflow takes a little bit more work, since you have to go through a bigger authentication process. But the Jovo CLI offers you a workaround, by letting you export your Dialogflow agent as a zip file, which you can then import at the Dialogflow website. After running $ jovo deploy, you should be able to find the zip file under /platforms/googleAction/ folder.

To import the zip you have to go an existing agents (create a new one real quick) settings page and select the Export and Import tab:

After that choose the Restore from ZIP option and upload your zip file:

Implementing the RepeatIntent in your Code

With version 1.2 of the Jovo framework you can simply use the repeat() method to repeat your applications last response. Let's add that together with the RepeatIntent itself to our handler:

If you want to access the speech and reprompt directly, you can also use the user class' getter methods:

Alright, that's pretty much it for the RepeatIntent. I will skip the testing part, since there is not much to say there.

Feel feel to play around with the other things the user context feature has to offer, as well as the other features introduced with the update to V1.2.

Any questions? You can reach us on Twitter or Slack.


Kaan Kilic

Technical Content Marketing Associate at Jovo

Comments and Questions

Any specific questions? Just drop them below. Alternatively, you can also fill out this feedback form. Thank you!

Join Our Newsletter

Be the first to get our free tutorials, courses, and other resources for voice app developers.