Data types of variables

There are different types of variables. If you have already written in any programming language, this topic will be simple and familiar to you, and if not, please give your full attention.

Botmother can work with six types of variables:

  • strings or text;
  • numbers;
  • logical values;
  • "nothing";
  • dictionaries or objects;
  • lists or arrays.

The types are divided into simple and composite ones.

Strings, numbers, logical values, and "nothing" are simple types. And lists and dictionaries are composite types.

String or text

Strings are everywhere. The text you are reading now consists of strings. Strings are a basic and simple data type that Botmother can work with.

If you write a variable, then without additional manipulations and parameters in Botmother, it will be written as a string.

Strings as a data type are slightly different from usual lines in books or on web pages. A regular line is a text from edge to edge, and in variables a string is any type of text. There may be nothing in the string at all: not a single letter, not a single emoji, no other characters, even spaces. Such a string is called empty.

To output a string in a message or use it in any other template, you just need to wrap the name of the variable in which it was written in curly brackets: {{variable_name}}, where variable_name is the variable name.

You can also get the length of strings: {{variable_name.length}}.

Please note that if there is an emoji in the text, the length of the string may be more than the number of characters in it visually. One emoji can take 2-8 characters.

Number

Numbers are also a simple data type. With numbers, you can perform mathematical operations in templates. For example, let's create two variables a and b, choose the Number type for them and write 3 and 5 in them. In the message template (or any other template) we can add them together: {{add a b}} outputs 8.

There are other mathematical operations:

  • subtraction {{subtract a b}};
  • multiplication {{multiply a b}};
  • division {{divide a b}}.

Operations can be nested into each other using a nested template in parentheses:

a + b × 2 = {{add a (multiply b 2)}}

A number can be stored in a string. In other words, we can make a variable with the String type, but write a number into it. In this case, it is impossible to use this variable with mathematical operations, because Botmother will assume that it is not a number in front of it but a string. Some operations like subtraction may work correctly, but it is better to write a variable as a number, otherwise a mathematical expression may lead to an unexpected result, or the bot will fail with an error.

You can convert a string to a number directly in an expression using toFloat — {{add (toFloat a) (toFloat b)}}. But we recommend writing the variable immediately with the Number type.

Logical Value

This type is needed for different conditional constructions. Only true and false can be written in it.

You can use a logical type in Requests, as well as in Calling condition. Logical variables can also be used in conditional template constructs, for example #if. Let's say we have a variable variable_name, and first we wrote true there.

{{#if variable_name}}
In variable_name — true
{{else}}
In variable_name — false
{{/if}}

Then such a template will print: In the variable variable_name — true. If we write into the variable varialbe_name — false and execute this template again, it will print: In the variable variable_name — false.

Some data types under certain conditions can also be used in #if similarly to true or false:

  • String type, value is an empty string, it is perceived as false;
  • Number type, value is 0, it is perceived as false;
  • NULL type is perceived as false;
  • the variable does not exist — it is perceived as false;
  • any value except false itself is perceived as true.

NULL or nothing

This is an empty value, usually a stub for data that will appear in the future. NULL can also be used in the request body or Conditional template.

It is usually used if you need to create a variable, but fill it with something later, and also, on the contrary, to grind the data that is already stored in some variable.

Dictionary or object

This is a container for other variables. It can be used in Requests for sending the request body to third-party resources, as well as for filling and reading in other components, for example, in User Input.

This is a composite data type, that’s why the dictionary consists of other variables that are written into it.

The object is similar to an ordinary dictionary, which many could keep while learning a foreign language. You can think of an object as a table where keys are listed in one column and values corresponding to these keys are listed in the other.

This connection is called key: value.

For example, we can imagine an object that stores information about a user:

Key Value
name James
role admin
age 18
gender male
isRoot true

The values can be of different types: these can be strings (James), numbers (18), and flags (true or false).

Other dictionaries can be the values that can be nested into each other up to 10 levels.

There are two ways to create an object in Assign a variable: choose the Empty object type or an Object from JSON type and write a valid JSON or template into the value, the result of which should be valid JSON. You can read about JSON in this article.

If we created an object (it does not matter — empty or with fields from JSON), in subsequent components we can add something to it by specifying the name of the variable with the object, and then through a dot we can add the name of the new variable inside this object.

For example, we created Empty object in the my_object variable. Then we can ask the user to introduce himself, and in the User Input component we can put a response message inside the object, specifying the variable name like this: my_object.name.

We can also get the recorded value through a dot: {{my_object.name}}, and send it, for example, in a Message.

List or array

By analogy with the object, it can be useful for filling some data in other components of the bot.

This is a composite data type.

The difference with the object is that the list items do not have a name, there is only a certain place in this list, denoted by some number.

The list is numbered from zero. That is, the very first element in the list is received and recorded using zero, the second one using one, etc.

The position in the list is called an index, and the numbering of elements is called indexing.

The list can be thought of as a table where each cell has a number and a value. This is how, for example, you can present a list of guests invited to a party:

0 1 2 3
Steve Courtney Betsy
William

The length of the list is the number of items added to it. An empty list will have zero items.

To find out the length of the list, use length, written with a dot after the name of the variable with the list.

For example, if we create an empty list named my_list, we can find out and get its length using the entry: {{my_list.length}}.

For the list above, with the names of the guests, my_list.length will be equal to four.

There are several ways to write something to the list. For example, through List control in Write Variable or Write Variables components, through User Input, Fork or Request. i.e. through any component that can write data to a variable.

To do this, you need to specify the name that we gave to the empty list when creating it, put a period and specify the index in which we want to write something. The index should not be greater than the length of the list.

Let's say we have a list with names and we want to write the name that the user will send us to the first place in this list. We can add the User Input component and specify names.0. in the Variable Name field. If there is a list in names, empty or already filled, the name sent by the user will be written to its first cell.

You can also add and change lists through List control in the Assign a variable and Assign variables components.

To remove something from the list, you need to change the entry a little. In templates, you need to specify the index in square brackets after the dot: {{names.[0]}}.

You can also use the get — {{get names 0}} helper for this, in which we specify the variable name with a list and an index.

To the beginning ↑