Conditions

We need conditions to show a variable or text depending on certain conditions.

Comparing a variable with a value or another variable

Let's imagine that we have variable "gender", in which we will write "male" or "female" through a fork, depending on what the client wrote to the bot.

Now we need to send different texts to clients depending on their gender.

We can use block helper eq for comparing variables:

Hey!
{{#eq gender "female"}}
Hello, madam. Thank you for writing to us.
{{/eq}}
{{#eq gender "male"}}
Hello, sir. Thank you for writing to us.
{{/eq}}What's happening here?

We opened block helper #eq and used variable "gender" and string "female" with it.

The example shows that we can use so-called literals instead of variables, i.e. we can compare not only variables, but also strings and numbers at once.

Strings should be written in double program quotes ".

And after the body, we closed the block /eq. You should always close block helpers, otherwise an error will occur in the template, and the bot will not send anything.

A lot of block helpers that are used for comparisons have a so-called alternative block.

Let's look at our example again. There are only two options: we can only have male or female.

We may not make a separate block for each option but use a special separator {{else}}:

Hey!
{{#eq gender "female"}}
Hello, madam. Thank you for writing to us.
{{else}}
Hello, sir. Thank you for writing to us.
{{/eq}}

If string "woman" is written in variable "gender", the bot will write: Hello, madam. Thank you for writing to us., and in all other cases it'll say: Hello, sir. Thank you for writing to us.

Comparison "less" or "greater"

To compare two numbers and determine which number is greater, and which one is less, there are two block helpers #lt and #gt, respectively.

lt - less than

gt - greater than

Imagine that we ask the client how old he is in the bot, and add another variable in which we write "age".

Let's say we want to warn that only a client over 16 years old can use the bot. If the client is younger, we will ask him not to use the bot.

{{#lt age 16}}
Unfortunately, the bot cannot be used by children under 16
{{/lt}}

You can do the same with helper #gt and separator {{else}}:

{{#gt age 15}}
{{else}}
Unfortunately, the bot cannot be used by children under 16
{{/gt}}

It seems a bit illogical because we need those who are 16 years old and older, and we got those who are strictly greater than 16.

In order to include 16 in such cases, there are additional helpers #lte (less than or equal to) and #gte (greater than or equal to).

Let's change the last example to #gte and add a greeting for target customers:

{{#gte age 16}}
Wow! Welcome, you are so many years old!
{{else}}
Unfortunately, you are not yet 16 years old
{{/gte}}

By the way, block helpers can be nested inside each other. Let's combine two examples:

{{#gte age 16}}
Wow! Welcome!
{{#eq gender "female"}}
You are so grown up, madam!
{{else}}
You are so grown up, sir!
{{/eq}}
{{else}}
Unfortunately, you are not yet 16 years old
{{/gte}}
Comparison helpers for numeric values work only with numbers, and strings are written to variables in the bot components. Therefore, you need to use helper toInt inside these helpers to convert the values of variables from a string to a number. For example: {{#gte (toInt var1)(toInt var2)}}1{{else}}0{{/gte}}

Let's check the user age in this way, as in the previous example, the bot can be used starting from the age of 16.

1. Ask and write the user age to var1 variable using User Input.

2. Add Variable Entry. Let's call the variable var2, add 16 to Value , it is the minimum age to get into the bot.

3. Below we will add another variable, we will call it age, then we will check it in Fork.

Let's convert the variable values written above from a string to a number using toInt helper:

{{#gte (toInt var1)(toInt var2)}}1{{else}}0{{/gte}}

In our case, we compare var1 and var2, if your variables are named differently, then change the names of the variables to yours in Value.

4. Add Fork in the variable Name, where Fork will take the value from, check age.

5. In Fork Settings, we will add Goal 1 with value 1 and the transition to the screen for those who are older than 16 and Goal 2 with value 0 and the transition to the screen for those who are younger than 16.

6. Let’s add transition screensseparately for users under 16 years old and for those who are older.

All comparison helpers

#eq - are the variables equal?

{{#eq variable1 variable2}}
If the variables are equal
{{else}}
Optional text if the variables are not equal
{{/eq}}

#lt - is the first variable less than the second one?

{{#lt variable1 variable2}}
If the first variable is less
{{else}}
Optional text if the second variable is less
{{/lt}}

#gt - is the first variable greater than the second?

{{#gt variable1 variable2}}
If the first variable is greater
{{else}}
Optional text if the second variable is greater
{{/gt}}

#if - does the variable exists and does it have "true value"?

{{#if variable}}
If the variable exists and it isn't an empty string — "", or 0, or false, or null, this text will be visible
{{else}}
Optional text, for the opposite condition
{{/if}}

#lte - is the first variable is less than or equal to the second one?

{{#lte age 16}}
Unfortunately, the bot cannot be used by children under 16
{{/lte}}

#gte - is the first variable greater than or equal to the second one?

{{#gte age 16}}
Wow! Welcome, you are so many years old!
{{/gte}}

Botmother supports other comparison helpers, but does not guarantee their work in future updates.

To the beginning ↑