Categories
Speech Assistants

Quick Hack: Random Dialog Paths in Voiceflow

In dialog trees for voice assistants, you often need to introduce some randomness. If the smart speaker doesn’t always repeat the same phrases, the dialog sounds more natural. Many other use cases exist as well, e.g., you might want to ask the user a random question in a quiz.

Random Block in Voiceflow

To enable this functionality, Voiceflow includes a “Random” block. This enables choosing a different path each time. The “no duplicates” option ensures that it’s not going the same path twice.

However, while this works fine in the Voiceflow testing environment, it currently has issues when using the skill live on Amazon Alexa. Additionally, you might sometimes want to have more control over the process – e.g., pre-set the random choices, store them in a database for advanced logging or tease the next item when the skill ends.

Custom Code for Advanced Random Choices

Thankfully, Voiceflow enables writing custom JavaScript code. This allows full control over your skill. In this case, we can use it to create a more advanced random functionality. It pre-creates an array of all possible choices, shuffles it, and saves as a global variable – this variable gets stored over multiple user-sessions. This means that when the user restarts the skill later, the uniqueness of the generated numbers still applies.

Once all random numbers between zero and the self-defined max number have been selected once, all choices are re-added.

This method pre-decides the upcoming choices; with this method, you could look at what will be the next choice and integrate this into your dialog design.

Variables in the Skill

To enable this advanced random functionality, set up two variables in your Voiceflow model:

  • num_available: a global variable, which is an array containing all the remaining random numbers from 0..max. Global variables are stored and retained over multiple sessions and are unique to each user.
  • num_now: a flow variable, which is then defined by Voiceflow and can be set by the custom script to contain the chosen number in this session.
Screenshot of Voiceflow showing the two custom variables added to Voiceflow.
Screenshot of Voiceflow showing the two custom variables added to Voiceflow.

Speech Flow Design

The next step is to add a “Custom Code” block to the flow. In the screenshot below, this is called “Unique Number”. Each JavaScript block has two outcomes – usually, this block should always go to the “success” route. A good dialog design should however also handle all potential failures and react accordingly.

The custom code block of the flow, containing JavaScript to select a unique random number.
The custom code block of the flow, containing JavaScript to select a unique random number.

Copy and paste the following code into the new block:

The maximum number (num_max) is the variable that contains the available choices, ranging from 0 .. < num_max. If you need this to be more flexible, you could of course also create this variable in Voiceflow and set it through a “Set” block.

In Voiceflow, every variable defined in the model is initialized with 0. If the num_available variable contains this value, this means that the skill was run the first time. The code in the if statement then generates an array with the desired numbers.

To provide the ability to preview the net choice, it’s better to shuffle the numbers right away and always take one item from the array later; instead of taking a random number each time a new one is needed. The function shuffleArray() is based on the StackOverflow answer by Laurens Holst.

Testing in the Alexa Developer Console

For quick testing, the connected “Speak”-blocks just announce the selected number (stored in the flow variable num_now). You could of course easily adapt this with an “if condition”-block to switch to different paths in your dialog model.

Next, upload the skill to the Alexa developer console. Repeatedly start the skill to see the generated random numbers. After all numbers have been run through once, all options are available again, in a newly randomized order.

The random number generator tested in Voiceflow
The random number generator tested in Voiceflow