AI

Working with Variables in Botpress: A Practical Approach

The variables are used to store the data that will be used later in your chatbot’s flow. One of the main features that separates Botpress from the other chatbot development tools is its extensive support for variables. Botpress offers eight different variable data types. Besides storing, the variables allow the developers to manipulate the data and make the conversations more dynamic and personalized. In this blog, we will explore the variables in Botpress Studio by looking at different data types, scopes, and practical methods to use them properly.

Understanding Data Types for Variables

Before we dive into the practical aspects of using the variables, let’s familiarize ourselves with the different available data types in Botpress.

  1. String: Strings are used to store the text which can consist of letters, numbers, or special characters. They are commonly used to store the user names or AI-generated messages.
  2. Boolean: Booleans can only hold two values – true or false. They are ideal for storing the binary information such as whether a user is a returning customer or if the user wants to ask something else.
  3. Number: As the name suggests, the number variables are used to store the numeric values which can be either integers or numbers with decimal places. They are useful for storing the phone numbers, area codes, and other numerical data.
  4. Date: The Date variables use the ISO 8601 date/time format. It represents either a single date or a date and time.
  5. Object: An object variable is a collection of key-value pairs which are similar to the dictionaries or maps. They are valuable for storing the complex data structures such as the user profiles or the results of an API call.
  6. Array: The arrays are used to store the collections of similar variables. They can hold the strings or objects, making them versatile for various use cases such as storing a user’s past messages or providing the options for the user to choose from.
  7. Enum: This is a variable with a limited set of predefined choices. They are suitable for scenarios like storing the days of the week or the available items on a food menu.
  8. Pattern: The pattern variables utilize the Regular Expressions (Regex) to store the special patterns which can be used to match specific words or numbers. They are handy for storing the account numbers or flight numbers.

Understanding the Variable Scopes

Various types of variables are included in Botpress, and they vary depending on where they can be accessible in your chatbot’s flow. Let’s explore the different variable scopes, starting from the most limited to the broadest:

Workflow Variables

These variables are defined and used within a single or the same workflow. They are excellent for single-use flows such as storing the AI task outputs, user answers to questions, or organizing the data from the API calls.

These steps can be followed for creating a workflow variable:

Step 1. Select the appropriate workflow in the “Explorer” menu.

Step 2. Double-clicking on an empty area in the workflow editor opens up the Inspector panel.

Step 3. Specify the variable name, pick the variable’s data type, and click the “Add” button to create the variable.

You can add a default (initial) value to your variable from the “Additional Settings” section.

Input and Output Workflow Variables

To exchange an information, workflows may have input and output variables. Inputs are used to receive an information from the external sources or other workflows, while outputs provide an information to the external sources or other workflows. This allows for more flexible communication and data exchange within the bot.

These steps can be followed for marking an input variable:

Step 1. Create the variable inside the workflow.

Step 2. Select the variable by clicking the workflow’s entry node in the Inspector panel.

These steps can be followed for marking an output variable:

Step 1. Create the variable inside the workflow.

Step 2. Select the variable by clicking the workflow’s exit node in the Inspector panel.

Session Variables

All flows can access these variables, but only for a single conversation. They are ideal for storing the data that is used throughout a conversation such as chat history, collected items in a virtual shopping cart, or temporary data from API calls.

To create a session variable, you can write the following code:

session.variablename = "Hello Guys!";

For using your variable in a text card, we need to close it in the curly brackets, e.g., {{session.variablename}}.

User Variables

The user variables follow a user between conversations which allows the data to persist across multiple interactions with the chatbot. They are perfect for storing an information that remains relevant to a user over time such as personal details, tags from past conversations, or language preferences.

To create a user variable:

Step 1. Open the “Chatbot Settings” from the Botpress icon located in the upper-left corner.

Step 2. Select “Variables” from the tabs.

Step 3. Define the user variable by naming it and specifying the data type.

Either @user.variablename or {{user.variablename}} can be used to refer to the “User” variable in a text card.

Bot Variables

All users in the chatbot have access to these variables across all conversations. They are frequently used to store the developer information and configuration like storing endpoints for the API calls, the bot’s version number, or its name.

Follow these steps for creating a Bot variable:

Step 1. Select the “Chatbot Settings” by clicking the Botpress icon in the top-left corner.

Step 2. Select “Variables” from the tabs.

Step 3. Specify the name and data type of the bot variable.

Like user variables, the @bot.variablename or bot.variablename can be used to refer to bot variables in the cards.

Configuration Variables

They are a special type of secure bot variable. You can manage them from the Cloud Dashboard, and there is no need to open the bot. The configuration variables are ideal for storing sensitive information like API tokens, private IP addresses, or database credentials.

To create a configuration variable:

Step 1. Open the “Chatbot Settings” from the Botpress icon located in the upper-left corner.

Step 2. Click on the “Variables” tab.

Step 3. Add a configuration variable and provide its name and value.

To access the configuration variables in the code, you can use the “env.key” where “key” represents the configuration variable name to be retrieved.

Using Variables in the Code

The curly brackets “{{ }}” or @ symbol is not necessary when using variables in the code. The syntax for accessing the variables in the code follows the “variabletype.variablename” pattern. For example:

Code:

  • workflow.phoneNumber
  • session.userAcctId
  • user.firstName
  • bot.endpoint
  • env.apiKey

The variables can be assigned with values or updated in the code. But it’s crucial to ensure that the assigned value matches the variable’s data type to avoid errors.

Conclusion

The variables are the building blocks of dynamic and personalized interactions in Botpress. By using the various data types and understanding the variable scopes, the developers can create chatbots with powerful capabilities. Whether you need to store the user information, connecting with APIs or passing the data between workflows and variables in Botpress provide the necessary flexibility and control.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.