Assign a variable

Use the Write Variable component if you know the variable values in advance or if you need to calculate, modify or transform variables received from other places such as Queries, Forks, or User Inputs.

The component:

  1. Writes values to variables without waiting for a message from the bot user.
  2. Allows you to create multiple variables in one component at once.
  3. Writes variables with a specific data type.
  4. Adds the created variable to the beginning, end, or to a known position in the lists

To create a variable, you need to add the Write Variable component to the screen, specify a Name, select one of the Types and specify a Value suitable for the selected type.

Name

You can write anything, but we recommend using only Latin letters, numbers and underscores to separate words.

Type

The component supports creating a variable of the following types:

  • String is just any text, emoji, or an empty string.
  • Number that can be used in mathematical templates.
  • Logical — true or false.
  • Empty Object is a container to combine other variables (also called Dictionary).
  • Empty List is a list that in the future can be filled with data from other variables (the list is also called Array).
  • NULL is an empty value, usually a stub for data that will appear in the future.
  • JSON Object is for writing variables to an array or an object from the JSON format.
  • Increment number - adds the specified number to the existing variable value.
  • Decrement number - subtracts the specified number from the existing variable value.

Empty object

This is a container for future data. After creating a variable in the Write Variable component, you can add something to this object by specifying the name of the nested variable separated by a dot.

For example, if we name a variable with an empty object my_object, we can create a new variable my_object.inner_object and write something to it. inner_object will become a variable nested in my_object. You can also access it through the point:

{{my_object.inner_object}}

The same can be done in any other component that allows you to create new variables, for example, in User Input or Request.

This data type can also be used to clear the filled variable in which you expect an object.

For example, you can create a questionnaire in the bot, write the answers to a separate, selected object, and you write an empty object to a variable with data from the questionnaire to give the user the opportunity to completely reset this questionnaire and go through it again.

Empty list

It is similar to an empty object, but creates a list for future filling or use with helpers.

To write something to the created list, you can do the same as with objects, just use the index of the element to be written instead of the name of the nested variable. To write something to an empty list, you can use 0 — the first element index.

Let's say we created a my_list variable with an empty list and want to write a message to it that the user will send us. We can add the User Input component and specify my_list.0. in the variable Name where the input is written. Then what the user writes will fall into the first element of the list.

If we want the data sent by a user to be always added to the end of the list, we can write a template that will get the length of the list, and write by this index. An index equal to the list length is just the index at the end of the list. Let's write such a template into Variable Name in the User Input component:

my_list.{{my_list.length}}

Now everything that the user writes will be added to the end of the list.

You can also write a new item to the list via List Control in another Write Variable.

Object from JSON

This data type allows you to write any valid JSON to a variable.

It is useful if you need to make and write down a complex object or a list.

Value

The value of the variable must correspond to the selected type. If you have choose the Number type, only numbers can be written to the value, if you choose the Logical type, only true or false and a valid JSON object can be in the Object from JSON type.

There can be an assembled template in the value for almost every type.

If there is a template in Value, it will be assembled during the component execution by the bot and brought to the desired type.

If type conversion is not possible, for example, you are trying to write an emoji to a number, the bot will stop the current execution with an error and cancel the changes in the Users section at the time of the last successful message.

This property can be obtained from a template.

List control

Properties from the List control section allows you to write a variable to a previously created or new list.

It may be necessary for questionnaires, various lists, baskets with goods. Lists are useful in a large number of situations.

By default, list management is disabled, in this case the variable will simply be written to the Name you specified.

If you activate any of the List Management options, the Variable Name property will indicate the variable with the list you want to manage, i.e. the behavior of this property slightly changes.

Create a new one and add to it

Botmother will create a variable, put a new list there, the first element of which will write the variable value.

The variable name to which the new list will be written, Botmother will take Name from the property.

Add to the end

Botmother will try to find the list in the variable from the Name property and write the value with a new element at the end of the list.

If a variable with a list could not be found (there is no such variable or there is no list in it), Botmother will stop the current execution of the bot and cancel changes in the status of the current user at the time of the last successful message.

Add at the beginning

It behaves similarly to Add to the end of the list, it only adds to the beginning of the list, not to the end.

Add to the end or create a new list and add to it

It behaves similarly to Add to the end of the list but it does not stop the bot if the list is not found in the specified variable name.

Instead, it creates a new list and adds a value to it.

Add to the top or create a new list and add to it

Behaves similarly to Add to the top of the list but it does not stop the bot if the list is not found in the specified variable name.

Instead, it creates a new list and adds a value to it.

Write to a specific position

Botmother will try to find the list in the variable from the Name property and write the value with a new element to the specified position. The element that is already in this position will be replaced.

Position in the list

It is written in the corresponding property.

This property can be obtained from a template.

If a variable with a list can’t be found (there is no such variable or there is no list in it), Botmother will stop the current execution of the bot and cancel changes in the status of the current user at the time of the last successful message.

If the position is beyond the size of the list, Botmother follows the rules:

  • the position is longer than the length of the list — insert the value at the end of the list;
  • position less than zero — insert the value at the beginning of the list.

Add after a certain position

Botmother will try to find the list in the variable from the Name property and write the value with a new element after the specified position. The element that is already in this position will not be changed, instead a new one will be inserted.

Add before a certain position

Behaves similarly to Add after a certain position but the value will be written before a certain position, not after.

To the beginning ↑