This is one of the most exciting and powerful features of Voice Apps.  Keeping track of user and skill information from one response to another or even from one session to another opens up so many possibilities for your skill.  Imagine asking the user for his or her name and then welcoming them back by name the next time they use your skill.  Or keeping score in a game and taking action when one player wins.  All of this is possible with variables.

What is a variable exactly? 

A variable is just a placeholder for information.  In the above examples, we might store the user's name in a variable named "userName" and we might store how many points the player has in a variable named "playerScore".   Once we set the value of these variables, we can use them in our responses or we can check the value of them and take different actions based on their value.

How to create a variable?

SET STATE
You can create a variable in one of several ways.  The first, and simplest, is using the "Set State" function of the Data tab.  

Enter your variable name in the left-hand input box and then choose an operation.  You can choose from the following operations:

  • = (set) - sets the value to a fixed number or string
  • sum - adds the values of two variables assigns the result to this variable
  • diff - subtracts the values of two variables and assigns the result to this variable
  • += (increase) - adds a fixed numeric value to this variable
  • -= (decrease) - subtracts a fixed numeric value to this variable

For the right-hand "value" box, you can enter a number, text, or another variable name depending on the operation you chose.

SLOT VARIABLES
Another way variables can get created is when you use a slot in "The user can say things like..." phrases of the Intent Activation tab:

In the sample image above, the variable "playerName" is used to capture the player's first name.

API, LOOKUP, AND GET RANDOM FUNCTIONS
You can also create and assign values to variables as the result of calling an external API, looking up a value in a list of key/value pairs, or getting a random value from a list of values.

How do I use a variable?

After a variable has been set, you can use it in many of the functions of Voice Apps.

RESPONSES
You can use variables in your response text.  Just enter the variable name surrounded by double curly brackets:
Welcome back, {{playerName}} 

Similarly, you can use variables in the URLs of the Short Audio Responses.  Again, you just enter the variable name surrounded by double curly brackets:
https://audio.mysite.com/{{soundName}}.mp3 

API FUNCTION
You can use variables in the API URL and the header values of API calls.

How do I use variables to create branching and logic?

You can use variables to determine when to give a response, when to execute another intent, or even when to set another variable.

RESPONSES

In the above example from the Escape the Room sample project (Safe Intent), you can see at the very bottom that this response group is only activated when the variable "isSafeClear" is not equal to "true".  If the user has not already opened the safe and cleared out the contents, we tell them about the safe and the combination lock on the door.

You can see that this response group, however, is activated when the variable "isSafeClear" is equal to true.  So, if the user has already unlocked and cleared out the safe, we remind them about it.

NEXT ACTIONS
You can also use variable to create branching logic in the Next Actions tab of an intent.

In the above example taken from the "Beer Trivia Game" sample project (Score Intent), you can see the first Next Action block is only activated when the variable "questionNumber" is less than the variable "maxQuestions".  So, if we still have more questions to ask in the game, we execute the Next Question intent.

The second Next Action block is only activated when the variable "questionNumber" is equal to the variable "maxQuestions".  In this case, we've answered all of the questions for this game and we execute the WinLose intent which figures out the final score and awards the winner.

SET STATE
Finally, you can use variables to conditionally set other variables.

In the above example, you can see that we only want to increment the "opponentScore" variable when the opponents answer is equal to the correct answer.  

Likewise, we only want to increment the "playerScore" variable when the players answer is equal to the correct answer.

What is the lifespan of variables?

Once a variable is set, it will persist indefinitely unless you specifically set it to "null" or the skill is disabled and then re-enabled. Variables will persist across sessions.

What is the scope of variables?

Variables exist only for the specific user that invokes the skill.  We currently don't support variables that exist globally across users.

Built-In Variables

There are several variables that are created by the Voice Apps platform and you can use them just like you would any other variable:

  • dayOfYear - gives you the day of the year with January 1st equal to 1 and December 31st equal to 365 or 366 if it's a leap year.
  • weekOfYear - gives you the week of the year with values from 1 to 52.
  • more on the way...

A Note on Variable Names

You can choose almost any name for your variable, but we suggest the following rules:

  • Choose a meaningful name that is easy for you and other to recognize.  Instead of using "s" or "scr", use "score" or "playerScore"  or "player1Score".
  • Use "camel case".  With camel case, the first word is all lower case and the first letter of each additional word is capitalized.  This is a very common casing in the developer world and it will be helpful to follow this standard.
  • No spaces or punctuation marks in variable names.
  • Cannot start with a number.  "1stPlayer" is invalid.  Use "firstPlayer" instead.

Conclusion

Variables are an extremely powerful feature that can be used to create extremely complex and interactive skills.  If you have questions, comments, or concerns, please let us know!

Did this answer your question?