Routing

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

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 setHandler method.

This is how a simple handler looks like:

The handlers make use of JavaScript Promises and 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:

Tutorial: Make an API Call with Jovo and async/await

Routing in a Jovo project consists of three key concepts:

Intents

Each intent from your Language Model can be added as a function, similarly to HelloWorldIntent and MyNameIsIntent above.

Learn more about Intents here.

States

Jovo comes with built-in state handling that allows you to react to intents differently based on the context a user is currently in:

Learn more about States here.

Input

In the MyNameIsIntent of the example above, the user's first name is passed as input, which can be accessed with this.$inputs.name.value.

Learn more about Input here.

Intent Redirects

Jovo offers the ability to redirect incoming intents to others. For example, the sample voice app uses this to go from LAUNCH to HelloWorldIntent:

You can use the following methods to redirect intents:

Learn how to pass data between intents.

toIntent

Use toIntent to jump into a new intent within the same request.

toStateIntent

Similar to 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.

toStatelessIntent

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.

Advanced Routing

As explained in the introduction above, routing is usually done with handlers, which can be added with the app.setHandler method in the src/app.js file:

For complex projects that include many intents and states, this can get quite complicated quickly. In this section, additional routing methods are explained:

Separate Handlers

You can add multiple handlers by passing more than one object to the setHandler method:

This allows you to have the handlers separated into different files (as modules), which can then be added to setHandler by using require:

The stateless.js file could look like this:

Platform Handlers

For cases where the experience differs on Alexa and Google Assistant, you can use the methods setAlexaHandler and setGoogleActionHandler to overwrite the default handlers.

Here is an example that offers different output for the two platforms:

Event Listeners

Event Listeners offer a way for you to react on certain events like onRequest and onResponse.

Find out more about Event Listeners here.

Routing Helpers

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.

getMappedIntentName

While 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:

getRoute

This method allows you to access additional information about the whole routing:

Comments and Questions

Any specific questions? Just drop them below or join the Jovo Community Forum.

Join Our Newsletter

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