What are Variables?

Variables are a way to store small pieces of information and use that information later in your application.   

What can you do with Variables?

Variables allow you to make your voice application more dynamic and more personal.  Here are a few things that variables let you do:

  • Capture data from your user
  • Retrieve data from external sources
  • Keep track of a player's score
  • Execute parts of your project multiple times
  • Give a response only when certain conditions are met
  • Dynamically route users through your project based on their input
  • And many, many more...

Managing Variables

You can manage all the variables in your project in the variable management tool.  To open this tool, click on the Settings button at the top left of your project canvas and then click the variables icon. 

Built-in Variables

Voice Apps comes with many built-in variables that you can use in your application.  These variables are created and populated automatically for you.

You will see these built-in variables at the bottom of the variable management tool.

You can hover over each variable to see a description of the variable.

Custom Variables

You can create your own variables to use in your project.

Create a Variable

To create a new variable, just type the name of the variable in the "Enter a new variable" box and press enter.

Delete a Variable

To delete a variable, click the "x" to the right of the variable.

Setting the Value of Variables

When your project runs for the first time, Voice Apps sets any custom variables you've defined to null.  You can think of null as "nothing".   To do anything meaningful with your variables, you'll want to set them to something more useful.

You can set the value of your variables in a number of ways:

Setting Variables with Set State

You can set variables using the Set State function on the Data tab.

Setting Variables with JS Code

You can also set variables using javascript with the JS Code function.

Setting Variables Directly from User Input

Another way to set the value of variables is by getting the value directly from your users.

We can ask the user for their favorite color, for example, and then capture their answer directly into a variable.  We do this using a feature called Slots.

Slots allow us to take part of the user's response and store it in a variable.

When we ask the user "What is your favorite color?", they might respond with one of the following:

"blue"
"my favorite color is blue"
"blue is my favorite color"

We can replace the "blue" in these responses with a variable to tell Voice Apps that we want to capture whatever the users says for that part of the response into our variable.

Using Variables

Now that you've learned how to create and set variables, let's learn how to use them in our voice application.

Variables in Responses

We'll use the Voice Response function on the Response tab for demonstration purposes, but you can use variables almost anywhere you enter information in Voice Apps.

To use a variable, simply type an open curly bracket "{".  The variable dropdown will appear to let you select any of the variables in your project.   Choose a variable from the dropdown to add it to your response.

Voice Apps will replace the variable with its current value when the block executes.

Variables as Conditionals

You can also use variables in the "when" clauses of Response Groups and Next Actions.

In the above example, we will only ask the user for a review on their 15th visit.

Conclusion

Variables are a very powerful feature of Voice Apps.  You can use them to build some really amazing applications.

Did this answer your question?