Variables

In this article, you will learn:

  1. What is a variable?
  2. What are the different types of variables?
  3. How to create a variable?
  4. How to store and access variables via node?
  5. How to access array/object Variables

1. Variables#

Variables are data that do not have a fixed value (values that change with the flow). In our platform variables are used to store data that may change.

When variables are defined on the platform, they must be assigned a data type. The data types are:

  • Number: To store numbers including floating-point numbers.

10, 3.14, 7009398700, -15, etc.

  • String: To store alphanumeric characters.

'Harry', 'PAN9856', '990*', 'Richa_0', '123starbucks', etc.

  • Object: To store batch data. Example, to store database, API responses, results from date prompt, etc.

{"Name": "Ron", "Phone": 9980808080, "Age": 40, "ID": "Rono555"}

  • Array: To store a list of multiple values of different data types.

[1,2,3,"Text"], ["india", "+91"], etc.

  • Boolean: To store logical data with true or false values (1/0).

Use-case#

Let us consider a simple use case to understand how and where variables can be used:

To update the dates on a travel website, you want to know the name of the user and verify the phone number. Here, variables play a key role, as explained below: Ask for the name of the user and store it in the name variable. Use the variable to address the user. Say "Thanks, Shelly". User enters OTP when prompted. Store it in the OTP variable and use it to verify the user. If the OTP matches, execute the next flow. Otherwise, ask the user to enter OTP again.


2. Variable Types#

You can use the following variables in a bot flow:

  • Custom variables
    • Journey
    • Global
  • System variables
  • Config variables
  • User properties

2.1 Custom Variables#

These are variables defined by bot builders. Custom variables are further subdivided into journey and global variables.

  • Journey Variable: It is accessible only within the journey for which it was created.
  • Global Variable: It is accessible across journeys and in API.

These variables store value only within a session.

In the following example, Email is stored as a Global variable (because it will remain unchanged throughout the flow). State_ride variable (that stores the departure location) is a Journey variable because a user gets directed to a different flow where this information is no longer needed.

2.2 System Variables#

These are fixed sets of variables.

note

The variable names cannot be altered, and new variables cannot be added to this category.

The following are the system variables available on the platform:

VariableData typeUse
sourcestringReturns the channel from which the message was sent.
senderstringReturns the sender ID. eg: mobile number for WhatsApp.
profileobjectReturns the user’s profile as an object.
pageUrlstringReturns the page’s URL.
sentimentobjectReturns the sentiment of the user message- positive, negative, or neutral.
sessionMessageLogUrlstringReturns a URL to the chat’s transcript.
dateobjectReturns the current date and time.
channelProfilestringReturns channel profile id.
channelProfileNamestringReturns channel profile name.

{{{system_variable_name}}} notation is used to access system variables.

info

You can access the user profile using this syntax: {{{profile.field_name}}}.

The following fields are available in the user profile object:

  • city
  • country
  • country_code
  • firstTime (if the user is visiting the bot for the first time)
  • ip
  • latitude
  • longitude
  • name (generated user name)
  • region

Learn more here.

2.3 Config Variables#

A configuration variable is used for calculating specific values. They will retain their value for the bot, irrespective of the users. Config variables can change their value (but not specifically for each session / each user), and they are mainly used for bot settings, authentication tokens, configuration related use cases, etc.

2.4 User Properties#

There are 22 user properties that are tracked by the bot. These values will be different for each instance (specific to each user). Click here to learn more.

note

User properties cannot be modified. New variables cannot be added.


3. Create a Variable#

note

Custom and Config variables can be added.

  1. Open Studio. On the right, click the Variable icon.

  1. On the Custom/Config Variables tab, click +Add Variable.
  2. Enter your preferred Variable name.
  3. Select the respective Data type for the variable.

  1. The sample Value will be displayed automatically. Custom values for that data type to be stored in the variable can also be added.
  2. Click +Add.
info
  • Two types of custom variables can be created - Journey and Global.
  • Journey variables are created to be used for that specific flow. Global variables can be used anywhere on the platform. Select the required variable option before adding a custom variable.

3.1 Create a Variable via nodes#

You can also create global variables with the help of Prompt nodes, which can be used in any flow or node.

To create a variable via Prompt node:

  1. Select Flows > Create flow. For more information on how to create a flow, see Create a flow.

  2. While creating a flow, if you want to store a user response in a variable and that variable does not exist, you can create a new variable via node by clicking Store Response in > Select variable.

  1. Click Create new variable.

  1. Add the variable name and datatype, and click Add. This global variable can be used in any node/flow.

  1. The variable has been successfully added to the node.


4. Store and Access Variables via Nodes#

Action nodes and Prompt nodes are used to store variables (and display them via other node data).

4.1 Store Variables#

There are two different methods to store node data in a variable:

4.1.1 Store response in#

  1. There is an option available at the bottom of each of the action/prompt node- Store Response In.
  2. When this option is clicked, a list of variables are displayed.
  3. Select the variable you want to store the user response for what the bot has asked through the node. For example, when the bot asked the user to enter the name- response can be stored in the global variable - name. Similarly, if the phone number is asked, it can be stored in the variable- phone.

note

Only names of the existing variables are displayed on the store response in dropdown. Create a new variable if you want to add more variables to the dropdown.

4.1.2 Variables -action node#

Variables can also be stored with the help of a Variable node in the flow.

4.2 Access Variables#

A Bot variable can be accessed inside any node using the notation {{{variables.variable_name}} or by clicking on the variables icon.

Variables created above can be accessed using {{{variables.category}}}.

note

Journey and Global variables get expired after 48 hours of inactivity.

The data that is entered in the variable is stored in a particular node, and the same data is retrieved when you use the same variable for the other node. The following is a sample screenshot of Bot that demonstrates the usage of variable: 

drawing

4.3 Variable Datatypes#

Different prompts/action nodes return responses in different formats and datatype. To understand the types of variables that can be stored in each of these nodes, refer to the tables below:

Prompt#

Prompt NodesVariable Datatype
Namestring
Emailstring
Phonestring
Locationobject
Carouselstring
Quick Repliesstring
Dateobject
Questionstring
Feedbackobject
Whatsapp Liststring
Multiselectstring
Image/filestring

Actions#

Action NodesVariable Datatype
APIobject, array, number, string
Database (search)array
Functionobject, array, number, string
Outbound Notificationobject, array, number, string
Notification Statusobject, array, number, string
Raise ticketobject, array, number, string
Modifierobject, array, number, string
Document Searchobject, array, number, string
Set Languageobject, array, number, string
Send OTPobject, array, number, string
Verify OTPobject, array, number, string
Paymentobject, array, number, string
Generate PDFobject, array, number, string

5. Access Array/Object Variables#

Accessing string/number is simple, it is slightly different for array/object datatype. In this example API action node, the response stored is not a simple string or number value.

Consider the following responses from a weather API - this consists of an object and array with different indexing.

{
"coord": {
"lon": 77.2167,
"lat": 28.6667
},
"weather": [
{
"id": 761,
"main": "Dust",
"description": "dust",
"icon": "50d"
}
],
"base": "stations",
"main": {
"temp": 32.54,
"feels_like": 30.55,
"temp_min": 32,
"temp_max": 33,
"pressure": 1002,
"humidity": 21
},
"visibility": 3500,
"wind": {
"speed": 3.09,
"deg": 260,
"gust": 8.23
},
"clouds": {
"all": 0
},
"dt": 1617278187,
"sys": {
"type": 1,
"id": 9161,
"country": "IN",
"sunrise": 1617237667,
"sunset": 1617282517
},
"timezone": 19800,
"id": 1273294,
"name": "Delhi",
"cod": 200
}

To access variable fields and index in a JSON object, array type stored in a variable, follow the steps below:

  • To access any fields of this you can simply type {{variables.variable_name.field_name}}. This can go upto fields of fields.

Use {{variables.API_var.main.temp}} to access temp field.

  • To access array values, use keys. To access the weather description above, you can see it's inside an array [ ] and is the first [0th index] value of the array.

Use {{variables.API_var.weather.0.description}}


What Next?

  • Try building a flow with nodes that store and display variables.
  • Use variables in code and API.