On this page:
Внимание
BPL operations are used only in chats and incoming calls. To test scripts that use BPL operations, use a chat in the web widget.
In the operations / expressions block, you can specify an arbitrary number of operations for the bot to calculate. These operations are expressions are written using a subset of the conversational bot programming language that is described below.
Structure of operations
All bot operations contained in one block represent one or multiple expressions in the bot programming language. Expressions are separated from each other by an optional character ";", or a newline character (beginning of a new line). Every expression has a value. This means that expressions can be compound.
Comments
When you are writing expressions, you can use comments. Comments are sections of the program that do not participate in calculations but are meant for informational purposes only.
There are two types of comments:
- Single-line comments. Such comments begin with two slashes // and end at the end of the line.
- Multi-line comments. Multiline commentsstart with /* and end with */
Example of comments:
Variables
Variables are used to store data in bot scripts. A variable represents a named location in the bot's memory. It is created the first time it is used in a block of operations. Nil is the default value for all variables.
There are three types of variables. They are distinguished by the scope:
- Variables visible within the same module in one thread. These variables can be used for reading and writing values. Their names begin with the symbol $. Such variables are useful for storing intermediate operations and calculations.
- Variables visible within the same module in different threads. These variables can be used for reading and writing values. Their names begin with the symbol #. Since such variables are visible in all threads of the bot, they are useful for interaction between threads.
- Global variables or environment variables. These variables are defined automatically. They are visible in all threads of all bot modules. These variables are read-only and store information about the global context of the bot. Their names start with the symbol @.
Examples of different types of vaiables:
// Variables $p and $r are only available in the thread in which they are defined $p = 2 * 3.14 * $r // This variable is available in all threads within the current module // (the module where the block of operations is located in which the variable is defined). #count = queue.size() // @communicationType is a read-only global variable. It is available from everywhere. $isChat = @communicationType == "TEXT"
Variable names can contain only numbers, English and Russian letters, and underscores. I.e., you cannot name a variable with a word that contains a space or a character other than Russian and English. However, there is a way to specify any name you want. For this, enclose the variable name in parentheses:
$('日本') = 123 // Created a variable with name 日本 $x = $('日本') // $x is now 123 $(math.rand(1, 2)) = 0 // Dynamically create a variable with name 1 or 2 and assign it value 0
Variable variables
The bot expression language allows you to dynamically calculate the name of a variable while the bot is running. This means that the name of one variable can be the value of another variable. Or a variable name can be any expression whose value is a string.
In the example below, we define three variables that contain names of the colors. Then on the random basis we determine a number in the range from 1 to 3 and then create a name for one of these variables to find out the value of a random color.
// Create three variables with names x1, x2 and x3 $x1 = "red" $x2 = "blue" $x3 = "green" // Random number in the ramge from 1 to 3 $n = math.rand(1, 3) // Get the color dynamically generating the name for a variable with the color and using its value $color = $("x" + $n)
You can also use variable values of different types:
// $x contains the name of variable #y $x = "y" // #y contains the name of the global variable communicationType #y = "communicationType" // Calculated in the following way @#$x -> @#("y") -> @#y -> @("communicationType") -> @communicationType $communicationType = @#$x
Data types
There are three primitive data types. One of them is the object type and another is the special value nil
Numbers
All numbers are treated as real signed numbers with a floating point. The minimum and maximum values are determined by the computing environment where the interpreter of the expression language is functioning.
Strings
Strings represent an arbitrary sequence of characters enclosed in single or double quotes. Double quotes differ from single quotes so that the following special character sequences are recognized within them:
Sequence | Description |
---|---|
\n | Translation to new line |
\r | Carriage return |
\t | Horizontal tab |
\" | Double quote |
In single quotes all characters are treated as is except for the sequence \' that means a single quote character.
// The following command displays a two-line phrase in the chat // Hello // "Medved!" say("Hello\n\"Medved!\"") // Will display a phrase in the chat Hello\n'Medved!' say('Hello\n\'Medved!\'')
Boolean (logical) values
Constants true and false are boolean, i.e. their values can have only one of the following states: true and false. Boolean values are useful in an expression of the conditional block .
$x = true // $x contains TRUE $y = !$x // $y contains FALSE
Objects
In the standard library of the bot programming language, there are functions (operations) whose values are so-called objects. An object is a special value that can have built-in properties and operations or methods. The combination of these properties and methods is determined by the type of the object. To access properties or methods of objects, the dot operator "." is used.
For example, below we receive the first message from the client that is a ClientMessage object. Then we use the message property (message text) of that object:
// $first contains the value of the ClientMessage object $first = queue.first() // Receive the value of the message property $firstMessage = $first.message
Object properties and methods as well as variable values can be calculated:
$n = rand(0, 1) // $n contains either 0 or 1 $method = ["first", "last"].get($n) // $method contains the string first or last (see getting a list element by index) $firstOrLast = queue.$method // Receive the first or last client message // Or even like this $firstOrLast = queue.(["first", "last"].get($n))
There are three types of built-in objects that are collections of arbitrary elements.
Tuples
A tuple is an unchangeable ordered collection of arbitrary elements. You can create a tuple with parentheses:
$items = (1, 2, 2) // Tuple with one element $items = ('a',) // Tuple with one element. The comma is used to distinguish the creation of a tuple from parentheses. $items = () // Tuple without elements (empty tuple)
The following methods are used to work with tuples:
Method | Description | Example | Result |
---|---|---|---|
count() | Getting the number of elements in a tuple | (1, 2, 3).count() | 3 |
first() | Getting the first element in a tuple | (1, 2, 3).first() | 1 |
last() | Getting the last element in a tuple | (1, 2, 3).last() | 3 |
has(index) | Returns true, if the tuple contains an element with index. Otherwise returnes false. | (1, 2, 3).has(0) | true false |
get(index) | Returns an element in a tuple by its index or returns nil if such element is absent | (1, 2, 3).get(1) | 2 |
contains(item) | Returns true, if a turple contains item. Otherwise returns false | (1, 2, 3).contains(3) | true |
index(item) | Returns the index of the element item or returns -1 if such element is absent | (1, 2, 3).index(3) | 2 |
equals(other) | Returns true, if other is exactly the same tuple (i.e. contains the same elements). Otherwise returns false. | ('a', 'b').equals(('a', 'b')) | true false |
hash() | Returns a string that represents the hash of the tuple (a value that identifies the contents of the tuple) | ('a', true).hash() | bb2fadd01f7d0ac9864dc963a61f653f |
Lists
A list is a changeable ordered collection of arbitrary elements. You can create a list with square brackets:
$items = [1, 2, 2] // List with three elements $items = ['a'] // List with one element $items = [] // List without elements (empty list)
The following methods are used to work with lists:
Метод | Описание | Пример | Результат |
---|---|---|---|
count() | Getting the number of elements in a list | [1, 2, 3].count() | 3 |
first() | Getting the first element of a list | [1, 2, 3].first() | 1 |
last() | Getting the last element of a list | [1, 2, 3].last() | 3 |
has(index) | Returns true, if the list contains an element with index. Otherwise returnes false. | [1, 2, 3].has(0) | true false |
get(index) | Returns an element in a tuple by its index or returns nil if such element is absent | [1, 2, 3].get(1) | 2 |
contains(item) | Returns true, if a turple contains item. Otherwise returns false | [1, 2, 3].contains(1) | true |
index(item) | Returns the index of the element item or returns -1 if such element is absent | [1, 2, 3].index(1) | 0 |
toTuple() | Returns a tuple that contains elements of the list | [1, 2, 3].toTuple() | (1, 2, 3) |
clear() | Deletes all elements of the list | $x = [1, 2, 3] | $x contains empty list [] |
reverse() | Reverses the order of elements of the list | [1, 2, 3].reverse() | [3, 2, 1] |
append(item) | Adds the received item value to the end of the list | [1, 2, 3].append(4) | [1, 2, 3, 4] |
prepend(item) | Adds the received item value to the beginning of the list | [1, 2, 3].prepend(0) | [0, 1, 2, 3] |
shift() | Deletes and returns the first element of the list | $x = [1, 2, 3] | $y will contain 1 and $x will be equal to [2, 3] |
pop() | Deletes and returns the last element of the list | $x = [1, 2, 3] | $y will contain 3 and $x will be equal to [1, 2] |
set(index, item) | Adds a value to the specified position on the list. If the position is located outside the list, then it will be expanded with zero values to the necessary size. | $x = [1, 2, 3] | $y contains [1, 0, 3] |
remove(index) | Deletes the element at the specified position on the list and returns its value | $x = [1, 2, 3] | $y will contain 2 and $x will be equal to [1, 3] |
equals(other) | Returns true, if other is exactly the same tuple (i.e. contains the same elements). Otherwise returns false. | ['a', 'b'].equals(['a', 'b']) | true false |
hash() | Returns a string that represents the hash of the list (a value that identifies the contents of the list) | ['a', true].hash() | bb2fadd01f7d0ac9864dc963a61f653f |
Associative arrays (dictionaries)
An associative array or dictionary (map) is a mutable ordered collection of arbitrary key/value pairs. A dictionary is created with curly braces:
$items = {'a': 1, 'b': 2, 'c': 3} // Dictionary of three elements $items = {1: true} // Dictionary of one element $items = {} // Dictionary without any elements (empty dictionary)
The following methods are used to work with dictionaries:
Method | Description | Пример | Результат |
---|---|---|---|
count() | Getting the number of dictionary elements | {1: 'a', 2: 'b', 3: 'c'}.count() | 3 |
clear() | Deleting all elements of a dictionary | $x = {true: false, 'b': true} | $x will contain empty dictionary |
reverse() | Reverses the order of elements in a dictionary | {'a': 1, 'b': 2, 'c': 3}.reverse() | {'c': 3, 'b': 2, 'a': 1} |
values() | Returns a list of all values in a dictionary | {'a': 1, 'b': 2, 'c': 3}.values() | [1, 2, 3] |
keys() | Returns a list of all keys in the dictionary | {'a': 1, 'b': 2, 'c': 3}.keys() | ['a', 'b', 'c'] |
firstKey() | Getting the first key of the dictionary | {'a': 1, 'b': 2, 'c': 3}.firstKey() | 'a' |
lastKey() | Getting the last key of the dictionary | {'a': 1, 'b': 2, 'c': 3}.lastKey() | 'c' |
first() | Getting the first element of the dictionary | {'a': 1, 'b': 2, 'c': 3}.first() | 1 |
last() | Getting the last element of the dictionary | {'a': 1, 'b': 2, 'c': 3}.last() | 3 |
contains(item) | Returns true, if a turple contains item. Otherwise returns false. | {'a': 1, 'b': 2, 'c': 3}.contains(2) | true |
key(item) | Returns the key that corresponds to the value of item. Otherwiese returns nil. | {'a': 1, 'b': 2, 'c': 3}.key(3) | c |
has(key) | Returns true if the element with the specified key exists. Otherwise returns false. | {'a': 1, 'b': 2, 'c': 3}.has('a') | true |
get(key) | Returns the element of the dictionary by its key, or returns nil if such element is absent | {'a': 1, 'b': 2, 'c': 3}.get('b') | 2 |
set(key, item) | Sets the dictionary element with the given key | $x = {1: true, 2: nil} | $x will contain {1: true, 2: false} |
remove(key) | Deletes a dictionary element by key and returns its value | $x = {'a': 1, 'b': 2} | $y will contain 1 and $x will be equal to {'b': 2} |
equals(other) | Returns true if other is exactly the same dictionary (i.e. a dictionary that contains the same keys and values). Otherwise returns false. | {'a': 'b'}.equals(['a': 'b']) | true false |
hash() | Returns a string that represents the hash of the dictionary (a value that identifies the contents of the dictionary) | {'a': true}.hash() | bb2fadd01f7d0ac9864dc963a61f653f |
Special value nil
When a variable is not defined but is already used in an expression, its value becomes nil. This is a special value that means the absence of any value. This value can also be assigned explicitly.
$x = 1 + 2 // $x contains 3 $x = nil // $x contains nil
Operations
The bot expression language has several operation classes. An operation class is determined by its first argument. For example, if the first argument of the "+" operation is a string, then strings are concatenated. If the first argument is a number, then numbers are added.
Priorities of operations
The higher the priority is defined, the earlier the operation is performed. Parentheses are used to increase the priority.
Operation | Priority | Associativity | Description |
---|---|---|---|
** | 12 | right | Raising to the power |
+ | 11 | - | unary plus |
- | 11 | - | unary minus |
~ | 11 | - | bitwise inversion |
! | 10 | - | Logical negation |
* | 9 | left | Multiplication |
/ | 9 | left | Division |
\ | 9 | left | Integer division |
% | 9 | left | Remainder of the division |
+ | 8 | left | Addition, concatenation, union |
- | 8 | left | Subtraction |
< | 7 | - | Less than |
<= | 7 | - | Less or equal |
> | 7 | - | More than |
>= | 7 | - | More or equal |
== | 6 | - | Equal |
!= | 6 | - | Not equal |
& | 5 | left | Bitwise AND |
^ | 4 | left | Bitwise XOR |
| | 3 | left | Bitwise OR |
&& | 2 | left | Logical AND |
|| | 1 | left | Logical OR |
= | 0 | left | Assignment |
+= | 0 | left | Assignment addition |
-= | 0 | left | Difference with assignment |
*= | 0 | left | Multiplication with assignment |
/= | 0 | left | Division with assignment |
\= | 0 | left | Integer division with assignment |
%= | 0 | left | Remainder of division with assignment |
**= | 0 | left | Exponentiation with assignment |
&= | 0 | left | Binary AND with assignment |
^= | 0 | left | Binary XOR with assignment |
|= | 0 | left | Binary OR with assignment |
Arithmetic operations
Арифметические операции предназначены для работы с числами. Если значение участвующее в арифметической операции не является числом, произойдёт попытка его преобразования в число (см. ниже).
Arithmetic operations are used to work with numbers. If the value used in the arithmetic operation is not a number, ther will be an attempt to convert it to a number (see below).
Example | Result |
---|---|
+$a | Conversion of $a to a number |
-$a | Change of sign $a |
$a + $b | Sum of $a and $b |
$a - $b | Difference between $a and $b |
$a * $b | Multiplication of $a by $b |
$a / $b | Division of $a by $b |
$a \ $b | Integer division of $a by $b |
$a % $b | Remainder of devision of $a by $b |
$a ** $b | Raising $a to the power of $b |
Bitwise operations
This is a separate class of operations used to work with bit representations of integers.
Example | Result |
---|---|
~$a | Sets only those bits that are set to $a, and vice versa |
$a & $b | Sets only those bits that are set to $a and $b |
$a | $b | Sets only those bits that are set either to $a or to $b |
$a ^ $b | Устанавливаются только те биты, которые установлены либо только в $a, либо только в $b, но не в обоих одновременно Sets only those bits that are set to either $a or $b, but not to both of them |
Table for conversion of other data types to a number:
Type | Value | Result |
---|---|---|
NIL | nil | 0 |
Logical | true | 1 |
Logical | false | 0 |
String | "" | 0 |
String | "123.5" | 123.5 |
String | "abc" | error |
String | any | error |
Logical operations
Logival operations are used to work with boolean values. If the value is not boolean, then there will be an attempt to convert it to a boolean value (see below).
Пример | Результат |
---|---|
!$a | true if $a are false, otherwise false |
$a && $b | true if $a and $b are true, otherwise false |
$a || $b | false, if $a and $b are false, otherwise true |
Table for convertion of other data types to a boolean value:
Type | Value | Result |
---|---|---|
NIL | nil | false |
Number | 0 | false |
Number | any value that is not 0 | true |
String | "" | false |
String | any string that is not empty | true |
Object | any | true |
Operations with strings
Only one operation is used to work with strings. This operation is called the addition or concatenation of strings.
Example | Result |
---|---|
"a" + "b" | "ab" |
If the first argument of the concatenation operation is not a string, then the addition is performed. If the second argument is not a string, then it is converted to a string according to the following conversion table:
Type | Value | Result |
---|---|---|
NIL | nil | "" |
Number | 123.45 | "123.45" |
Logical | true | "true" |
Logical | false | "false" |
Object | any | serialization to string |
Comparison operations
Comparison operations are used to work with any data types. Calculation results are boolean values.
Example | Result |
---|---|
$a == $b | true if $a is identically equal to $b |
$a != $b | true true if $a is identically not equal to $b |
$a > $b | true if $a is greater than $b |
$a >= $b | true if $a is greater than or equal to $b |
$a < $b | true if $a is less than $b |
$a <= $b | true is less than or equal to $b |
Operations with collections
There is a union operation that is used for embedded object types: tuple, list, and map.
Example | Result |
---|---|
(1, 2) + (3, 4) | (1, 2, 3, 4) |
['a', 'b'] + ['c'] | ['a', 'b', 'c'] |
{'a': 0, 'b': 2} + {'a': 1, 'c': 3} | {'a': 1, 'b': 2, 'c': 3} |
Assignment operations
Assignment operators allow to assign a value to variable.
Example | Result |
---|---|
$a = 123 | $a contains 123 |
$a += 1 | $a = $a + 1 |
$a -= 1 | $a = $a - 1 |
$a *= 2 | $a = $a * 2 |
$a /= 2 | $a = $a / 2 |
$a \= 2 | $a = $a \ 2 |
$a %= 5 | $a = $a % 5 |
$a **= 3 | $a = $a ** 3 |
$a &= 1 | $a = $a & 1 |
$a |= 3 | $a = $a | 3 |
$a ^= 4 | $a = $a ^ 4 |
Apart from assigning a value to a variable, you can also assign a value to several variables at once.
Example | Result | Expression value |
---|---|---|
($x, $y, $z) = (1, 2, 3) | $x = 1 | (1, 2, 3) |
[$x, $y] = ($y, $x) | $x = $y | [$y, $x] |
{'a': $x, 'b': $y} = {'b': true, 'a': false} | $x = false | {'a': false, 'b': true} |
($x, $y, $z) += [1, 2, 3] | $x = $x + 1 | ($x + 1, $y + 1, $z + 1) |
{3: $x, 7: $y} *= (2, 3) | $x = $x * 2 | {3: $x * 2, 7: $y * 3} |
Functions
Function calls are basic blocks that are used to build expressions in an operation block. A function is a part of a bot's functionality. For example, using functions you can work with date and time, perform complex arithmetic operations, convert a text string to intents using the NLU service, work with the client's message queue, etc.
To call a function, you need to specify its name, then list its parameters separated by a comma in parentheses. Names of most functions contain two parts separated by a dot: the name of the module and the name of the function itself.
$number = math.rand(5, 10) // Returns a random number in the range from 5 to 10. math - module name, rand - function name
Like object methods function names can be dynamically calculated during the execution of the bot script.
$funcIdx = math.rand(0, 1) // Returns a random number in the range from 0 to 1 that will be the index of the function name in the tuple $number = math.(('min', 'max').get($funcIdx))(3, 7) // Returns the name of the min or max function and then calls it. This way we calculate either the minimum or maximum value on the random basis