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 operation / expression block, you can specify an arbitrary number of operations for the bot to calculate. These operations are expressions 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.

Every expression has a value. This means that expressions can be complex.

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.
  • Multiline comments. Multiline comments start with /* and end with */.
Usage example
// These are single-line comments
$x = time()

/**
 * And these are
 * multiline
 * comments
 */
say("Time is " + $x)

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 of all variables.

There are three types of variables. They are distinguished by the scope:

  • Variables that are 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 values of intermediate operations and calculations.
  • Variables that are 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 variables
// Variables $p and $r are only available in the thread where 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 where 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 character other than in Russian or English. However, there is a way to specify any name you want. For this, enclose the variable name in parentheses:

Usage example
$('日本') = 123         // Creates a variable with name 日本
$x = $('日本')          //  $x is now 123
$(math.rand(1, 2)) = 0 // Dynamically creates a variable with name 1 or 2 and assigns 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 color names. 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.

Usage example
// Create three variables with names x1, x2 and x3
$x1 = "red"
$x2 = "blue"
$x3 = "green"

// Random number in the range 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:

Usage example
// $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 one 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:

 SequenceDescription
\nTranslation to new line
\rCarriage return
\tHorizontal tab
\"Double quote

In single quotes all characters are treated as is except for the sequence \'  that means a single quote character.

Usage example
// 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.

Usage example
$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 object properties or methods, 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:

Usage example
// $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:

Usage example
$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:

MethodDescriptionExampleResult
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 returns false.
(1, 2, 3).has(0)
(1, 2, 3).has(3)
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)
(1, 2, 3).get(3)

2
nil

contains(item)Returns true, if a turple contains item. Otherwise returns false.
(1, 2, 3).contains(3)
(1, 2, 3).contains(0)

true
false

index(item)Returns the index of the element item or returns -1 if such element is absent.
(1, 2, 3).index(3)
(1, 2, 3).index(0)

2
-1

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'))
('a', 'b').equals(('b', 'a'))
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:

Usage example
$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:

MethodDescriptionExampleResult
count()
Getting the number of elements on a list.
[1, 2, 3].count()
3
first()
Getting the first element on a list.
[1, 2, 3].first()
1
last()
Getting the last element on 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)
[1, 2, 3].has(3)
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)
[1, 2, 3].get(3)

2
nil

contains(item)Returns true, if a turple contains item. Otherwise returns false.
[1, 2, 3].contains(1)
[1, 2, 3].contains(0)

true
false

index(item)Returns the index of the element item or returns -1 if such element is absent.
[1, 2, 3].index(1)
[1, 2, 3].index(0)

0
-1

toTuple()
Returns a tuple that contains elements on the list.
[1, 2, 3].toTuple()

(1, 2, 3)

clear()
Deletes all elements on the list.
$x = [1, 2, 3]
$x.clear()

$x contains empty list []

reverse()
Reverses the order of elements on 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 on the list.
$x = [1, 2, 3]
$y = $x.shift()

$y will contain 1 and $x will be equal to [2, 3]

pop()
Deletes and returns the last element on the list.
$x = [1, 2, 3]
$y = $x.pop()

$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 = $x.set(1, 0)
$z = $x.set(5, 'a')

$y contains [1, 0, 3]
$z contains [1, 2, 3, nil, nil, 'a']

remove(index)

Deletes the element at the specified position on the list and returns its value.

$x = [1, 2, 3]
$y = $x.remove(1)

$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'])
['a', 'b'].equals(['b', 'a'])
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:

Usage example
$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:

MethodDescriptionExampleResult
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.clear()
$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)
{'a': 1, 'b': 2, 'c': 3}.contains(4)

true
false

key(item)

Returns the key that corresponds to the value of item. Otherwise returns nil.

{'a': 1, 'b': 2, 'c': 3}.key(3)
{'a': 1, 'b': 2, 'c': 3}.key(4)

c
nil

has(key)

Returns true if the element with the specified key exists. Otherwise returns false.

{'a': 1, 'b': 2, 'c': 3}.has('a')
{'a': 1, 'b': 2, 'c': 3}.has('d')

true
false

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')
{'a': 1, 'b': 2, 'c': 3}.get('d')

2
nil

set(key, item)Sets the dictionary element with the given key.
$x = {1: true, 2: nil}
$x.set(2, false)

$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 = $x.remove('a')

$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'])
{'a': 'b'}.equals(['b': 'a'])
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.

Usage example
$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.

OperationPriorityAssociativityDescription
**12RightRaising to the power
+11-Unary plus
-11-Unary minus
~11Bitwise inversion
!10Logical negation
*9LeftMultiplication
/9LeftDivision
\9LeftInteger division
%9LeftRemainder of the division
+8Left

Addition, concatenation, union

-8LeftSubtraction
<7Less than
<=7Less or equal
>7More than
>=7More or equal
==6Equal
!=6Not equal
&5LeftBitwise AND
^4LeftBitwise XOR
|3LeftBitwise OR
&&2LeftLogical AND
||1LeftLogical OR
=0LeftAssignment
+=0LeftAssignment addition
-=0LeftDifference with assignment
*=0LeftMultiplication with assignment
/=0LeftDivision with assignment
\=0LeftInteger division with assignment
%=0LeftRemainder of division with assignment
**=0LeftExponentiation with assignment
&=0LeftBinary AND with assignment
^=0LeftBinary XOR with assignment
|=0LeftBinary 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, the system will attempt to convert it to a number (see below).

ExampleResult
+$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 division 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.

ExampleResult
~$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

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:

TypeValueResult
NILnil0
Logicaltrue1
Logicalfalse0
String""0
String"123.5"123.5
String"abc"error
Stringanyerror

Logical operations

Logical 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).

ExampleResult
!$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 conversion of other data types to a boolean value:

TypeValueResult
NILnilfalse
Number0false
Numberany value that is not 0true
String""false
Stringany string that is not emptytrue
Objectanytrue

Operations with strings

Only one operation is used to work with strings. This operation is called the addition or concatenation of strings.

ExampleResult
"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:

TypeValueResult
NILnil""
Number123.45"123.45"
Logicaltrue"true"
Logicalfalse"false"
Objectanyserialization to string

Comparison operations

Comparison operations are used to work with any data types. Calculation results are boolean values.

ExampleResult
$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.

ExampleResult
(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.

ExampleResult
$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.

ExampleResultExpression value
($x, $y, $z) = (1, 2, 3)
$x = 1
$y = 2
$z = 3
(1, 2, 3)
[$x, $y] = ($y, $x)
$x = $y
$y = $x
[$y, $x]
{'a': $x, 'b': $y} = {'b': true, 'a': false}
$x = false
$y = true
{'a': false, 'b': true}
($x, $y, $z) += [1, 2, 3]
$x = $x + 1
$y = $y + 2
$z = $z + 3
($x + 1, $y + 1, $z + 1)
{3: $x, 7: $y} *= (2, 3)
$x = $x * 2
$y = $y * 3
{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
.

  • Нет меток

1 комментарий

  1. Анонимный

    массив нельзя называть как "ram", видимо это системная переменная