Expressions in @MenuBuilderBot

Expressions in @MenuBuilderBot

MegaHubTelegram

Expressions allow you to perform simple mathematical calculations directly in your bot messages, in the Conditions and buttons, as well as in Actions. In expressions, you can use constants, digital variables, as well as functions of time variables.


πŸ“œ The translation of this materials into your native language can be automatically done using the Google translator. Follow the link and select the desired language in the title:
https://translate.googleusercontent.com/translate_c?depth=1&sl=en&tl=es&u=https://telegra.ph/Expressions-in-MenuBuilderBot-EN-09-05



⬛ How does expressions look like


Expressions must be enclosed in curly braces: {expression}. Nesting expressions into one another is not allowed.

All necessary values and operations must be in one expression, enclosed in curly braces. You can use spaces inside expressions if you like - they will be ignored. Use parentheses "()" if you need to perform multiple independent calculations in one expression.


Example:

1. {2+3} ➨ 5

2. {2*3 + 5} ➨ 11

3. {(10-1) * (11-2)} ➨ 81

4. {%balance% * 67 * 1.02} ➨ Depends on the state of the user's balance


⬛ Allowed values


In Expressions, you can use:

1. Numerical values with "." dot for decimals:

1
100
100.99
0.81

2. Values of Digital variables (balance, etc.) using macros:

%balance%

%my_variable%

3. Service macros with digital values (ref. Systems, etc.):

%refcount%
%userid%


⬛ Available operations


You can use the following operations (the list may be expanded in the future):

1. Math operators: +, -, *, /

{2 + 1}, {3 * 3}

2. Unary operators: +, -

{-1 * 3}

3. Division with round down to the nearest integer: //

{ 10 // 3 } ➨ 3

4. Power (x power of y): ** or pow(x, y)

Use "pow (x, y)" function - if "**" breaks markdown formatting.

{2 ** 5} ➨ 32 (2 to the power of 5)
{pow(2, 5)} ➨ 32 (same entry only with function)


⬛ Math Functions


min(A, B, C, D...)

Will select the minimum value from the list.

The maximum length of the list is 20 values. If the list is longer, it will always return 0 (zero).


max(A, B, C, D...)

Will select the maximum value from the list.

The maximum length of the list is 20 values. If the list is longer, it will always return 0 (zero).


⬛ Rounding functions


round(xxx.xx)
"Bank rounding" to the nearest integer
{round(100.01)} = 100
{round(100.99)} = 101


round(xxx.xx, y)
"Bank rounding" to "y" decimal places
{round(100.01, 1)} = 100.0
{round(100.11, 1)} = 100.1
{round(100.99, 2)} = 100.99
{round(100.99, 1)} = 101.0


floor(xxx.xx)
round down to the nearest integer
{floor(100.11)} = 100
{floor(100.99)} = 100


ceil(xxx.xx)
round up to the nearest integer
{ceil(100.11)} = 101
{ceil(100.99)} = 101


⬛ Random values


randi(A, B)

(random integer) between A and B (including A and B)


randf(A, B)

(random fractional) number between A and B (including A and B)


rands()

(random sign) number "1" with a random sign: -1 or +1
Special function for expression {randi(0,1) * 2 - 1}


randl(A, B, C, D...)

randl("A", "B", "C", "D")

(random list) VALUE from a given list

It can work with both Numbers and Text. When using text, it must be enclosed in quotation marks.

The maximum length of the list is 20 values. If the list is longer, it will always return 0 (zero).


When used in an expression, randi(A, B) will be replaced with a random number and you can apply any other supported operators/functions to it.

Usage example in post:

You got {randi(1,10) * 2} apples


⬛ String functions


β—Ύ bool_symb() and bool_symbn()

Boolean to Symbol - allows you to change the text in a message, condition or Inline button, depending on whether the specified condition is met or not. The bool_symbn() (Boolean to Symbol New) function, unlike bool_symb(), shows the result from a New Line - allowing you to arrange your messages in a column. All features described for bool_symb() are equally apply to bool_symbn().

❗ WARNING: this function consumes 0.1 "Function Tokens". If Function Tokens are not available will show "---" a dash.


bool_symb((A), "B", "C")

If A - is TRUE or 1, will show B-string.
If A - is FALSE or 0, will show C-string.


bool_symb can be used in message texts, in expressions and inline button titles. This function can work with both String and Numeric data. If a number is supplied as parameters B and C, then it can be used in the calculation of expressions. The parameter in B and C can be formed in different ways: Text, Number, Variable macros.


β€’ Example 1: If you want to show two types of messages (text):

If mynum value is 1, it will show βœ…, otherwise ❌.


β€’ Example 2: When you need to show a message (text) only if the condition is true, leave the second quotes empty:

If mynum value is 1, it will show βœ…, otherwise it will show nothing.


β€’ Example 3: When you need to show a message (text) only if there is data in the Numeric variable:

If mynum variable has any numeric data, it will show βœ…, otherwise it will show nothing.


β€’ Example 4: When you need to show a message (text) only if there is data in the Text variable:

If mytext variable has any textual data, it will show βœ…, otherwise it will show nothing. Notice that if you refer to Text Variable - you nee to take the macros ofvarioable in "quotation".


β—Ύ int_symb ()

Integer to Symbol - allows you to replace the numbers of numeric parameters passed to it with the specified symbols in the appropriate quantities. Allows you to set the maximum number of characters displayed.

❗ WARNING: this function consumes 0.1 "Function Tokens". If Function Tokens are not available will show "---" a dash.


int_symb (A, "B", C)
Where A is the passed numeric Parameter (can be a numeric variable).
B is the character to replace the number of the passed numeric Parameter.
C is the maximum number of characters to display in a message.


β€’ Example 1: will show 5 characters "πŸ˜€" out of maximum 10:

Result : πŸ˜€πŸ˜€πŸ˜€πŸ˜€πŸ˜€


β€’ Example 2: will show 10 characters "😑" out of maximum 10 despite the fact that the passed Parameter is 15, the remaining 5 characters will be displayed as numbers:

Result : 😑😑😑😑😑😑😑😑😑😑 +5


β€’ Example 3: Numeric variable %num% contains 5 as its value. Thus, the function will show 5 characters "🀯" out of maximum 10:

Result: 🀯🀯🀯🀯🀯


β—Ύ format_num()

The function of formatting the output of the number in the message. Allows you to add zeros to the number of digits both to the left of the integer part and to the right of the fractional part - ignoring the rules of the standard mathematical notation.

❗ WARNING: this function consumes 0.1 "Function Tokens". If Function Tokens are not available will show "---" a dash.


format_num(A, B, C)

A - the number itself.

B - number of digits in fractional part

C - number of digits in integer part (optional)


β€’ Example 1: integer and fractional:


If the number of digits in the original number is more than specified in the function, they will remain unchanged.

β€’ Example 2: the number of digits is more than specified in the function:


If there is no need to format the integer part, the third parameter is set to zero or not indicated at all.

β€’ Example 3: fractional only with and without parameter:


⬛ Functions "Chewed Magic" for Einsteins and their Friends



β—Ύ addl ()

Add Loop - Adding a value with cyclic overflow. Allows you to loop through the values ​​with the specified parameters by running them in a circle and resetting the value when the specified maximum is reached.


addl (A, B, C)
A - current number.
B - added number (delta).
C - overflow threshold (limit).


Let's take a look at how the function works using the clock as an example:
{add_loop (20, 10, 24)} == 6
In the example above, it is now 20 o'clock pm (20:00 military), we add 10 hours , the maximum number of hours in a day is 24 - so the overflow theshold is 24. So 20 pm (20:00) + 10 hours gets 6 am (06:00). The function, while passing the theshold of 24 hours, resets the number to 0 (zero) and continues from there.


β€’ Example 1: sequential search (with a step of 1) numbers from 0 to 4 for a BONUS button that adds value.

What happens in the expression:
{-%num% + (%num% + 1)} == 1

A bonus button for the %num% variable with such an expression for accrual, each time it is pressed, will sequentially write the results 0, 1, 2, 3, 4, 0, 1 ... and so on, in a circle. The result of each step must be written to the %num% variable, changing its value.


Further analysis of the function is NOT mandatory for everyone! If you do not care how it works, or you have read and did not understand anything, just use the above expression for the bonus buttons and do not bother yourself.

Let's analyze the behavior of the function... the part of the expression {-1 * %num%} as a result of the calculation will give a negative value of the variable %num% itself (in general, you can use the expression {-%num%}, it will give the same result).

What is it for?

The end result of the expression should be one number that the Bonus button will add to the existing one in the variable it is configured to work with. The bonus button always only adds values - this is the way its mechanics work (for Actions where it is possible to directly assign (set) a value to variables, all these difficulties are not needed).

As a result of the calculation in the expression {addl (%num%, 1, 4)} we get the sum of the current value of the %num% variable and the 1 (delta), which is our function step. If the Bonus button simply adds a new expression value to the variable, we will get a result greater than what we want {%num% + (%num% + 1)}, which is not what we want. Therefore, before the bonus button adds the result to the variable, the value already existing in the variable must be subtracted from this amount, so the Bonus button will add only the remainder (the difference). The easiest way to do this is by adding the sum to the negative value of the variable itself - because adding with a negative number is equivalent to subtracting.

Let's imagine that the current value of %num% is 2 (and its next value, respectively, should be 3). Then the expression {addl (%num%, 1, 4)}, will look like this: {addl (2, 1, 4)} is 2 + 1 = 3. In this case, the bonus button should add the result 3 to the value of the variable already equal to 2, as a result, in the variable, instead of 3, we will get 5. To prevent this from happening, before adding a value using the Bonus button, in the expression itself we first take the negative value of our variable %num% and add its value to it increased by 1. Thus, getting 1 as the result of the calculation and the Bonus button adding this value will give us 3 (three) - the desired value in the %num% variable.


β—Ύ flag_get(), flag_set(), flag_del()

Setting and using Flags. Numeric variable can be used for Flags. While using as a flag variable, you can look at the value of the variable, but there is no practical sense. One variable can contain up to 30 flags.


Find out if there is a flag in a variable:
flag_get(A, B) - Main notation
A - Macro of variable name
B - Serial number of the flag from 1 to 30.

Returns 0 (zero) if there is no flag and > 0 if there is a flag.

Where to use: messages in conjunction with bool_symb(), bool_symbn(), conditions for buttons and Actions.

β€’ Example 1: condition on a button

If cell #1 is not zero and it contains a flag, then the user is subscribed to mailing list #1

If there is no flag in cell #2, then the user is NOT subscribed to mailing list #2


Set the flag in the variable:
flag_set(A, B)

A - Macros of variable name
B - Serial number of the flag from 1 to 30.

Where to use: buttons for changing values ​​(bonus, exchange1)


β€’ Example 2: free subscription (bonus button, bonus value)

Set the flag in cell #1 (for example, subscribe to newsletter number 1)


β€’ Example 3: buying access (exchange button1, used as accrual value)

Set a flag in cell #2 (for example, buying access #2)


β€’ Example 4: To avoid a double purchase, it is better to use the condition

It will check if there is a flag in cell #2 and if not, then it will allow further actions.



Clear the flag in the variable:
flag_del(A, B)

A - Macro of variable name
B - serial number of the flag from 1 to 30.

Where to use: bonus button, possibly exchange1 if you know what for...


β€’ Example 5: Unsubscribing from a mailing list

Will remove the flag in cell #1 (Unsubscribe from mailing list #1)


⬛ Examples of messages


1. Your Balance {%balance% * 100} USD

2. You have %refcount% referrals.

Write us and you'll get 0.5$ for every 10 refs and 2$ bonus for every 100 refs.
You will get: {%refcount%//10 * 0.5 + %refcount%//100 * 2}


⬛ Restrictions


Expressions cannot be placed inside other expressions.

An example of a WRONG expression:

{a + {b + c}}

The expression provided is "Nested" and will not be parsed correctly. All incorrect expressions will be left unprocessed or may be partially processed - in any case, the result will be incorrect.

Use parentheses "()" if you need to perform multiple independent calculations in one expression.

An example of a correct expression:

{a + (b + c)}



⬛ Additional Inforamtion


β—Ύ Expressions are used in Button Conditions.

Read more about the Conditions in a separate guide.

β—Ύ The functions of Time variables can be used in Expressions (if they return a numeric value).

For more information about Numeric variables and how to work with them, read the Guide to Working with Date and Time.


https://telegra.ph/Menu-Builder---All-Help-Materials-02-22

Report Page