The “Formula” step supports a set of built-in functions for working with numbers, strings, dates, and logical conditions. They help perform calculations, transformations, and checks directly within the scenario, without external systems.

For clarity, copy the template via the link – click Copy in BotHelp, specify your account domain, and click Save Flow. Within a few minutes, the template will be copied to your account. You can then edit the blocks to suit your needs.

Mathematical Functions

Arithmetic operations are supported: +, -, *, /

FunctionDescriptionExample
round(x)Rounds a number to the nearest integer (0.5 rounds up).
When using a numeric field, the function round round({%numeric_field%}, 3) rounds only to 3 decimal places (thousandths). For example, the number 2.123456789 becomes 2.123 after rounding.
round(2.5) → 3
int(x)Converts a value to an integer.int("42") → 42
float(x)Converts a string to a floating-point number.float("12.34") → 12.34
str(x)Converts a value to a text string.str(100) → "100"

Let’s look at calculation examples. Variables must be set in advance.

Create a Formula step.

“”””Calculate variable values”””
client.A = 2 – 4 # Data type “Number, negative”
client.B = 1.275 + 5 # Data type “Number, decimal”
client.C = 5 * 10 / 2 # Data type “Number, integer”
client.D = “10” # Data type “String”
client.E = “10.5” # Data type “String”

“”””Working with local variables”””
local1 = 2 # Assigned a local variable
local2 = 3

Added local variables into a user variable

client.F = local1 + local2

“”””Apply mathematical functions”””
client.discount = round(client.B) # Round to nearest integer
client.sum_orders = int(client.D) # Convert string to number
client.life_number = float(client.E) # Convert string to number with decimal
client.forecast = str(client.C) # Convert number to string

In the Message step, display text to the subscriber

— Calculate variable values
A = {%A%}
B = {%B%}
C = {%C%}
D = {%D%}
E = {%E%}
— Working with local variables
F = {%F%}
— Apply mathematical functions
discount = {%discount%}
sum_orders = {%sum_orders%}
life_number = {%life_number%}
forecast = {%forecast%}

Logical and Conditional Functions

FunctionDescriptionExample
if(condition, then_value, else_value)Checks a condition. If it is true, returns then_value; otherwise, returns else_value.if(client.age >= 18, "Взрослый", "Несовершеннолетний")
if(condition, then_value)If else_value is not specified, returns an empty string.if(is_null(client.phone), "Номер не указан")
is_null(var)Checks if a variable is empty.is_null(client.email) → True
and, or, notLogical operators for combining conditions.if(a > 0 and b < 10, "ok")

Boolean values are supported: True, False.

In this example, we calculate the final discount based on previous purchases and the number of completed lessons. Make sure all necessary variables are created in advance.

#1. Loyalty: 20% discount if purchases > 3

base_discount = if(client.sum_orders > 3, 20, 0)

#2. Engagement: 20% for >=10 lessons, 10% for >=5

engagement_discount = if(client.completed_lessons >= 10, 20, if(client.completed_lessons >= 5, 10, 0))

#3. Final discount

client.discount = base_discount + engagement_discount

In line 2 we use the logical if condition:

  • if the variable value (e.g., number of previous purchases) is greater than 3, set base_discount = 20;
  • otherwise (less than 3) — 0.

In line 5 we create a condition for the number of completed lessons. The condition here is double:

  • if value >= 10engagement_discount = 20;
  • if value >= 5engagement_discount = 10;
  • otherwise — 0.

Note: base_discount and engagement_discount are variables without prefixes. They work only within the current “Formula” step and serve as intermediate calculations. They are needed later in line 8 to get the final value.

In line 8 we sum the local variables.

The resulting value is written to the user variable discount, which can be displayed to the subscriber in the next scenario steps.

Working with Strings

FunctionDescriptionExample
len(text)Returns the number of characters in a string.len("Привет") → 6
substring(str, n1, n2)Trims the string: n1 — how many characters to keep from the left, n2 — how many to trim from the right.substring("BotHelp", 3, -2) → "tHel"
upper(text)Converts text to uppercase.upper("привет") → "ПРИВЕТ"
lower(text)Converts text to lowercase.lower("HELLO") → "hello"
split(text, separator)Splits the string by a separator and returns the first part.split("Иван Петров", " ") → "Иван"
match(text, pattern, ignore_case=True)Checks if the string contains the specified word (by pattern).match("купить товар", "купить") → True

Variables of type “Date” and “DateTime” are stored in internal format: YYYY-MM-DD. For example, a user input of 11.11.2011 is stored internally as "2011-11-11". Therefore, when using substring(), the trimming is done according to this format.

To get the expected values:

Option 1: convert the date to the familiar format

date = format_date(client.birth_date, "%d.%m.%Y")

Option 2: trim the date considering the YYYY-MM-DD format

Use the indices of the string "2011-11-11".

All operations are performed in the account timezone.
Multiplying or dividing dates will cause a “Type Error”.

As an example, let’s count the number of characters in a string using len(x). Variables with the required names must be created in advance.

#Count the number of characters
client.answer_count = len(client.answer)

#Check if the number of characters is more than 10. Apply a logical condition – if more than 10, assign 1 to answer_valid. If less, assign 0
client.answer_valid = len(client.answer) >= 10

In the next example, we calculate the life number – the sum of all digits in the birth date.

After obtaining the subscriber’s date, we can pass it in the variable birth_date in the “Formula” step.

  • In line 1, convert the date format to the required form. Since variables of type “Date” and “DateTime” are stored internally as YYYY-MM-DD, even if the user entered 11.11.2011, inside BotHelp it is stored as "2011-11-11".
  • From lines 3 to 10, use substring() to extract the digits of the birth date and store them in local variables.
  • In line 11, sum the extracted digits into life_number to display to the subscriber.

date = format_date(client.birth_date, “%d.%m.%Y”)

d1 = int(substring(date, 0, -9))
d2 = int(substring(date, 1, -8))
d3 = int(substring(date, 3, -6))
d4 = int(substring(date, 4, -5))
d5 = int(substring(date, 6, -3))
d6 = int(substring(date, 7, -2))
d7 = int(substring(date, 8, -1))
d8 = int(substring(date, 9, 0))

client.life_number = d1+d2+d3+d4+d5+d6+d7+d8

Working with Dates and Time

FunctionDescriptionExample
get_current_time()Returns the current date and time in the cabinet’s time zone. Format: %Y-%m-%d %H:%M:%S.globals.now = get_current_time() → "2025-11-11 16:30:00"
addYear(date, years)Adds or subtracts years (taking leap years into account). Use a negative number to subtract.addYear("2020-01-01", 3) → "2023-01-01"
addMonth(date, months)Adds or subtracts months (takes month lengths and leap years into account).addMonth("2025-01-10", 2) → "2025-03-10"
addDays(date, days)Adds or subtracts days. Use a negative number to subtract.addDays("20.08.2019", -99) → "13.05.2019"
addMinutes(date, minutes)Adds or subtracts minutes (for "Date and Time" values).addMinutes(get_current_time(), 20) → "2025-11-11 16:50:00"
format_date(date, "формат")Converts a date to the specified format. Supported formats: %Y-%m-%d, %d.%m.%Y, %Y-%m-%d %H:%M:%S, %d.%m.%Y %H:%M:%S.format_date("2025-10-10", "%d.%m.%Y") → "10.10.2025"
date_diff(date1, date2, unit)Returns the difference between two dates. The unit parameter can be "days" or "minutes".date_diff(client.birth_date, get_current_time(), "days") → 9125

When comparing, data1 is subtracted from data2.

Calculate the subscriber’s age and make a prediction based on it. First, request the birth date and store the answer in a date variable birth_date.

Then in the “Formula” step:

  1. Use int to remove fractions and get the full number of years.
  2. Use date_diff to calculate the difference in days between the previously entered birth_date and the current date get_current_time.
  3. Then use an IF condition to write the corresponding answer into the variable forecast.
client.age_years = int(date_diff(client.birth_date, get_current_time(), "days") / 365)
client.forecast = if(client.age_years >= 30,"Now is the time for consistency and stability 👌","Period of experiments and growth, try something new 💫")

And display the answer to the subscriber in the bot.

You are {%age_years%} years old {%forecast%}

Birth Date Parsing Template

One of the popular requests is working with personal data to create forecasts, horoscopes, or individual offers. We have prepared a ready template that demonstrates the “Formula” in action.

What this template does:

  1. Accepts the user’s birth date in a unified format.
  2. Automatically separates it into three variables: Day, Month, and Year.
  3. Prepares data for generating a personal forecast for the upcoming year.

Why this is useful for your business?

Create unique forecasts or calculations (numerology, astrology, financial indicators).

Link to the ready template

Line Break (\n)

The \n character adds a line break within text.

Example:

client.text ="First line\nSecond line"

Result:

First line
Second line

String Concatenation

  1. Concatenating strings using the + operator

Task: combine multiple values into one string and store the result in a variable. Used when you need to:

  • create text once
  • assemble a final message,
  • create an identifier or signature.

Logic

  1. Concatenation with + works only with strings. Values of other types must be converted to a string using str() beforehand. Example: str(client.A)
  2. Concatenate them using +.
  3. Assign the result to a variable.

Example:

client.result ="Order #" + client.order_id +" accepted"

What is stored in client.result

Order #12345 accepted

Note: the + operator creates a new value. If you execute this code again, the previous value will be overwritten.

  1. Adding text to a string using the += operator

Task: gradually append text to an existing string without overwriting previous values. Used for:

  • logs,
  • message history,
  • accumulating comments.

Logic

  1. The variable already contains text.
  2. Use += to add a new line at the end.
  3. The old value is preserved.

Example:

client.log ="First message"
client.log +="\nSecond message"
client.log +="\nThird message"

What is stored in client.log

First message
Second message
Third message

Note: the += operator does not replace the value, it adds a new one at the end of the string. For visual line separation, use \n (line break).

As an example, we will accumulate the user’s input history in one variable, adding the event time to each entry.

Logic

  1. Write the current time to a local variable using get_current_time().
  2. Concatenate into the output variable (for checking) the data from the variable number_order adding a line break via “\n”. Concatenation can only be done with string variables. If a variable is another type, convert it to string using str().
  3. Add the entry to the end of the log using +=, separating entries with an empty line.
A = get_current_time ()
client.check += ( '\n'+ '\n' + client.number_order + '\n' + ' ◷ ' + A )

Checking if a Field is Filled (is_null)

Check if the subscriber entered a value and:

  • get the technical check result (True / False),
  • display a user-friendly message based on the result.

Logic

  1. Check the variable using is_null().
  2. Store the check result in a separate variable.
  3. Use if() to convert the result to text for the subscriber.
  4. Display the result in a message.

Formula Step Code

client.A = is_null(client.answer)
client.D = if(is_null(client.answer), "Field is empty", "Field is not empty")

Display in the message

{%A%}
{%D%}

What the subscriber sees

If the field is empty:

1
Field is empty

If the field is filled:

0
Field is not empty

Important to know

is_null() returns a logical value. When stored in a variable:

  • 1 means True,
  • 0 means False.
  • The results (1 or 0) can only be stored in a variable of type number or text.

If you did not find the answer to your question, feel free to contact us in the chat inside your dashboard, message us on Telegram at @BotHelpSupportBot, or email us at hello@bothelp.io.

Get 14 Days of Full Access to the Platform

Enjoy the full functionality of the BotHelp platform for creating broadcasts, automated funnels, and chatbots — free for 14 days.

Get 14 Days Free

Была ли статья полезна?

Спасибо за обратную связь!