In this section, you will learn more about how to use intents and states to route your users through your voice app.
- Introduction to Routing
- Intent Redirects
- Advanced Routing
- Routing Helpers
Typically, routing in a Jovo app is done in the
app.js file in the
src folder of your project. By default, routing elements are added by using the
This is what a simple handler looks like:
async/await. This means the handler routes through all applicable intents and then returns a response when the handling promise gets resolved. This means that, for asynchronous operations (like API calls), you need to add
async to your handler functions:
Routing in a Jovo project consists of three key concepts:
Each intent from your Language Model can be added as a function, similarly to
Jovo comes with built-in state handling that allows you to react to intents differently based on the context a user is currently in:
MyNameIsIntent of the example above, the user's first name is passed as input, which can be accessed with
Jovo offers the ability to redirect incoming intents to others. For example, the sample voice app uses this to go from
You can use the following methods to redirect intents:
toIntent to jump into a new intent within the same request.
toIntent, you can use
toStateIntent to redirect to an intent inside a specific state.
The routing will look for an intent within the given state, and go there if available. If not, it will go to the fallback option outside your defined states.
If you're inside a state and want to go to a global intent, you can use
toStatelessIntent to do exactly this:
Note: Calling this method will remove the current state from the response.
As explained in the introduction above, routing is usually done with
handlers, which can be added with the
app.setHandler method in the
For complex projects that include many intents and states, this can get quite complicated quickly. In this section, additional routing methods are explained:
You can add multiple handlers by passing more than one object to the
This allows you to have the handlers separated into different files (as modules), which can then be added to
setHandler by using
stateless.js file could look like this:
For cases where the experience differs on Alexa and Google Assistant, you can use the methods
setGoogleActionHandler to overwrite the default handlers.
Here is an example that offers different output for the two platforms:
Event Listeners offer a way for you to react on certain events like
Most information that is necessary for routing can be accessed through the Jovo
$request object. The Jovo context object (
this) offers some additional helpful methods.
this.$request.getIntentName() only makes it possible to access the intent name as it can be found in the request, this method allows you to access the intent after the mapping (see: intentMap) is done:
This method allows you to access additional information about the whole routing: