На этой странице:
Внимание
Для тестирования сценариев, содержащих BPL-операции, необходимо использовать веб-виджет или интеграцию с каким-либо мессенджером, так как данные операции не работают в тестовом чате внутри редактора.
Создание тестового виджета.В блоке операций/выражений можно указывать произвольное количество операций, которые должен выполнить бот в процессе своей работы. Операции представляют собой выражения, записанные с помощью подмножества языка программирования разговорных ботов, описание которого содержится ниже.
Структура операций
Все операции бота в пределах одного блока операций представляют собой одно или несколько выражений на языке программирования ботов. Выражения отделяются друг от друга необязательным символом ";" либо символом перевода строки (началом новой строки). Каждое выражение имеет значение. Это значит, что выражения могут быть составными.
Комментарии
При записи выражений можно указывать комментарии – участки программы, которые не участвуют в вычислениях, а выполняют лишь информационную функцию. Есть два вида комментариев:
- Однострочные. Такие комментарии начинаются с символов // и заканчиваются окончанием строки.
- Многострочные. Начало многострочного комментария начинается с символов /* и заканчивается символами */.
// Это однострочный комментарий $x = time() /** * А это уже * многострочный * комментарий */ say("Time is " + $x)
Переменные
Для хранения данных в сценариях ботов применяются переменные. Переменная представляет собой именованный участок памяти бота. Она создаётся при первом своем использовании в каком-либо блоке операция. При этом значением по умолчанию для всех переменных является nil.
Существует три вида переменных, различающихся своей областью видимости:
- Переменные, видимые в пределах одного модуля в одном потоке.
Доступны для чтения и записи. Названия таких переменных начинаются с символа $. Такие переменные удобно использовать для хранения значений промежуточных операций и вычислений. - Переменные, видимые в пределах одного модуля в разных потоках.
Доступны для чтения и записи. Названия переменных уровня модуля начинаются с символа #. Поскольку такие переменные видны во всех потоках бота их удобно использовать для взаимодействия между потоками. - Глобальные переменные или переменные окружения.
Автоматически задаваемые переменные, которые видны во всех потоках всех модулей бота. Эти переменные доступны только для чтения и хранят информацию о глобальном контексте работы бота. Их имена начинаются с символа @.
// Переменные $p и $r доступны только в потоке, в котором они определены $p = 2 * 3.14 * $r // Эта переменная доступна во всех потоках в пределах текущего модуля // (модуля, в котором находится блок операций, в котором определена переменная). #count = queue.size() // @communicationType глобальная переменная только для чтения. Доступна везде. $isChat = @communicationType == "TEXT"
Имена переменных могут содержать только цифры, английские и русские буквы, а также символ подчёркивания. Т.е. вы не можете назвать переменную каким-то словом, содержащим пробел или символ отличный от символов русского и английского языков. Тем не менее, существует способ использовать любое название. Для этого необходимо заключить название переменной в круглые скобки.
$('日本') = 123 // Создали переменную c именем 日本 $x = $('日本') // $x теперь равна 123 $(math.rand(1, 2)) = 0 // Динамически создаём переменную с именем 1 или 2 и присваиваем ей значение 0
Переменные переменных
Язык выражений бота позволяет вычислять имя переменной динамически в процессе работы бота. Это означает, что значением одной переменной может быть имя другой переменной или любое выражение, значением которого является строка.
В примере ниже мы определяем три переменные, содержащие названия цветов. Затем мы определяем случайно число от 1 до 3 и далее формируем название одной из переменных с тем, чтобы узнать значение случайного цвета.
// Создадим три переменные с именами x1, x2 и x3 $x1 = "red" $x2 = "blue" $x3 = "green" // Случайное число от 1 до 3 $n = math.rand(1, 3) // Получаем цвет динамически сформировав имя переменной с цветом и взяв её значение $color = $("x" :: $n)
Можно также использовать значения переменных разных типов.
// $x содержит название переменной #y $x = "y" // #y содержит название глобальной переменной communicationType #y = "communicationType" // Вычисляется так @#$x -> @#("y") -> @#y -> @("communicationType") -> @communicationType $communicationType = @#$x
Типы данных
Существует три примитивных типа данных, один объектный тип и специальное значение nil.
Числа
Все числа трактуются как вещественные знаковые с плавающей точкой. Минимальное и максимальное значения определяются той вычислительной средой, в которой работает интерпретатор языка выражений.
Строки
Представляют собой произвольную последовательность символов, заключённую в одинарные или двойные кавычки. Двойные кавычки отличаются от одинарных тем, что внутри них распознаются следующие специальные последовательности символов.
Последовательность | Значение |
---|---|
\n | Перевод строки |
\r | Возврат каретки |
\t | Горизонтальная табуляция |
\v | |
\" | Двойная кавычка |
В одинарных кавычках все символы трактуются как есть за исключением последовательности символов \', которая означает символ одинарной кавычки.
// Следующая команда выведет в чат фразу из двух строк // Привет // "Медвед!" say("Привет\n\"Медвед!\"") // Выведет в чат фразу Привет\n'Медвед!' say('Привет\n\'Медвед!\'')
Булевы (логические) значения
Константы true и false трактуются как булевы, т.е. такие значения, у которых есть только два состояния – истина и ложь. Булевы значения удобно применять в выражениях условного блока.
$x = true // $x содержит ИСТИНА $y = !$x // $y содержит ЛОЖЬ
Объекты
В стандартной библиотеке языка программирования бота существует ряд функций (операций), значением которых являются так называемые объекты. Объект – это особое значение, у которого могут быть встроенные свойства и операции или методы. Набор этих свойств и методов определяется типом объекта. Для доступа к свойствам или методам объектов используется оператор точка ".".
Например, с помощью следующего кода можно получить первое сообщение клиента, которое является объектом ClientMessage и далее получить свойство message (текст сообщения) этого объекта.
// $first содержит значение объекта ClientMessage $first = queue.first() // Получаем значение свойства message $firstMessage = $first.message
Свойства и методы объектов (также как и значения переменных) могут быть вычисляемыми.
$n = rand(0, 1) // $n содержит или 0 или 1 $method = ["first", "last"].get($n) // $method содержит строку first или last (см. получение элемента списка по индексу) $firstOrLast = queue.$method // Получаем первое или последнее сообщение клиента // Или даже так $firstOrLast = queue.(["first", "last"].get($n))
Существует три типа встроенных объектов являющихся коллекциями произвольных элементов.
Кортежи
Кортеж или tuple – это неизменяемая, упорядоченная коллекция произвольных элементов. Создать кортеж можно с помощью круглых скобок.
$items = (1, 2, 2) // Кортеж из трёх элементов. $items = ('a',) // Кортеж из одного элемента. Запятая необходима, чтобы отличать создание кортежа от просто круглых скобок. $items = () // Кортеж не содержащий ни одного элемента (пустой кортеж).
Для работы с кортежами доступны следующие методы.
Метод | Описание | Пример | Результат | |
---|---|---|---|---|
1 | count() | Получение количества элементов кортежа | (1, 2, 3).count() | 3 |
2 | first() | Получение первого элемента кортежа | (1, 2, 3).first() | 1 |
3 | last() | Получение последнего элемента кортежа | (1, 2, 3).last() | 3 |
4 | has(index) | Возвращает true, если кортеж содержит элемент с указанным порядковым номером, и false в противном случае | (1, 2, 3).has(0) | true false |
5 | get(index) | Возвращает элемент кортежа по его порядковому номеру или nil, если такого элемента нет | (1, 2, 3).get(1) | 2 |
6 | contains(item) | Возвращает 1, если кортеж содержит элемент item, и ничего (void) в противном случае. | (1, 2, 3).contains(3) | true |
7 | index(item) | Возвращает порядковый номер элемента item либо -1, если такого элемента нет | (1, 2, 3).index(3) | 2 |
8 | equals(other) | Возвращает true, если other представляет собой точно такой же кортеж (т.е. кортеж, содержащий те же самые элементы), и false в противном случае | ('a', 'b').equals(('a', 'b')) | true false |
9 | hash() | Возвращает строку, представляющую собой хэш кортежа (значение, уникально идентифицирующее содержимое кортежа) | ('a', true).hash() | bb2fadd01f7d0ac9864dc963a61f653f |
10 | copy() | Создаёт поверхностную копию кортежа (т.е. не копирует коллекции внутри кортежа). | $list = ["a", "b"] $copy = (1, 2, $list).copy() $copy.get(2).append("c") | $list будет равен |
11 | deepCopy() | Создаёт полную копию кортежа. | $list = ["a", "b"] | $list будет равен |
12 | merge(collection) | Возвращает новый кортеж полученный из слияния текущего кортежа с коллекций (другим кортежем, списком или словарём) | (1, 2, 3).merge((4, 5)) | (1, 2, 3, 4, 5) |
Списки
Список или list – это изменяемая, упорядоченная коллекция произвольных элементов. Создать список можно с помощью квадратных скобок.
$items = [1, 2, 2] // Список из трёх элементов $items = ['a'] // Список из одного элемента $items = [] // Список не содержащий ни одного элемента (пустой список)
Для работы со списками доступны следующие методы.
Метод | Описание | Пример | Результат |
---|---|---|---|
count() | Получение количества элементов списка | [1, 2, 3].count() | 3 |
first() | Получение первого элемента списка | [1, 2, 3].first() | 1 |
last() | Получение последнего элемента списка | [1, 2, 3].last() | 3 |
has(index) | Возвращает true, если список содержит элемент с указанным порядковым номером, и false в противном случае | [1, 2, 3].has(0) | true false |
get(index) | Возвращает элемент списка по его порядковому номеру или nil, если такого элемента нет | [1, 2, 3].get(1) | 2 |
contains(item) | Возвращает true, если список содержит элемент item, и false в противном случае | [1, 2, 3].contains(1) | true |
index(item) | Возвращает порядковый номер элемента item либо -1, если такого элемента нет | [1, 2, 3].index(1) | 0 |
toTuple() | Возвращает кортеж, состоящий из элементов данного списка | [1, 2, 3].toTuple() | (1, 2, 3) |
clear() | Удаляет все элементы списка | $x = [1, 2, 3] | $x содержит пустой список [] |
reverse() | Меняет порядок следования элементов списка на обратный | [1, 2, 3].reverse() | [3, 2, 1] |
append(item) | Добавляет переданное значение item в конец списка | [1, 2, 3].append(4) | [1, 2, 3, 4] |
prepend(item) | Добавляет переданное значение item в начало списка | [1, 2, 3].prepend(0) | [0, 1, 2, 3] |
shift() | Удаляет и возвращает первый элемент списка | $x = [1, 2, 3] | $y будет содержать 1, а $x станет равным [2, 3] |
pop() | Удаляет и возвращает последний элемент списка | $x = [1, 2, 3] | $y будет содержать 3, а $x станет равным [1, 2] |
set(index, item) | Добавляет значение в указанную позицию списка. Если позиция лежит за пределами списка, то он будет расширен нулевыми значениями до нужного размера. | $x = [1, 2, 3] | $y содержит [1, 0, 3] |
remove(index) | Удаляет элемент списка в указанной позиции и возвращает его значение | $x = [1, 2, 3] | $y будет содержать 2, а $x станет равным [1, 3] |
equals(other) | Возвращает true, если other представляет собой точно такой же список (т.е. список, содержащий те же самые элементы), и false в противном случае | ['a', 'b'].equals(['a', 'b']) | true false |
hash() | Возвращает строку, представляющую собой хэш списка (значение, уникально идентифицирующее содержимое списка) | ['a', true].hash() | bb2fadd01f7d0ac9864dc963a61f653f |
copy() | Создаёт поверхностную копию списка (т.е. не копирует коллекции внутри списка). | $list = ["a", "b"] $copy = [1, 2, $list].copy() $copy.get(2).append("c") | $list будет равен ["a", "b", "c"] $copy будет равен [1, 2, ["a", "b", "c"]] |
deepCopy() | Создаёт полную копию списка. | $list = ["a", "b"] $copy = [1, 2, $list].deepCopy() $copy.get(2).append("c") | $list будет равен ["a", "b"] $copy будет равен [1, 2, ["a", "b", "c"]] |
merge(collection) | Сливает текущий список с коллекций (другим кортежем, списком или словарём) и возвращает его. | ["a", "b"].merge((1, 2)) | ["a", "b", 1, 2] ["a", "b", "a", "c"] ["a", "b", 1, 2] |
Ассоциативные массивы (словари)
Ассоциативный массив или словарь (map) – это изменяемая, упорядоченная коллекция произвольных пар ключ/значение. Создаётся словарь с помощью фигурных скобок.
$items = {'a': 1, 'b': 2, 'c': 3} // Словарь из трёх элементов $items = {1: true} // Cловарь из одного элемента $items = {} // Словарь, не содержащий ни одного значения (пустой словарь)
Для работы со словарями доступны следующие методы.
Метод | Описание | Пример | Результат |
---|---|---|---|
count() | Получение количества элементов словаря | {1: 'a', 2: 'b', 3: 'c'}.count() | 3 |
clear() | Удаление всех элементов словаря | $x = {true: false, 'b': true} | $x будет содержать пустой словарь |
reverse() | Меняет порядок следования элементов словаря на обратный | {'a': 1, 'b': 2, 'c': 3}.reverse() | {'c': 3, 'b': 2, 'a': 1} |
values() | Возвращает список, состоящий из всех значений словаря | {'a': 1, 'b': 2, 'c': 3}.values() | [1, 2, 3] |
keys() | Возвращает список, состоящий из всех ключей словаря | {'a': 1, 'b': 2, 'c': 3}.keys() | ['a', 'b', 'c'] |
firstKey() | Получение первого ключа словаря | {'a': 1, 'b': 2, 'c': 3}.firstKey() | 'a' |
lastKey() | Получение последнего ключа словаря | {'a': 1, 'b': 2, 'c': 3}.lastKey() | 'c' |
first() | Получение первого элемента словаря | {'a': 1, 'b': 2, 'c': 3}.first() | 1 |
last() | Получение последнего элемента словаря | {'a': 1, 'b': 2, 'c': 3}.last() | 3 |
contains(item) | Возвращает true, если словарь содержит элемент item, и false в противном случае | {'a': 1, 'b': 2, 'c': 3}.contains(2) | true |
key(item) | Возвращает ключ, соответствующий значению item, или nil в противном случае | {'a': 1, 'b': 2, 'c': 3}.key(3) | c |
has(key) | Возвращает true, если элемент с указанным ключом сущеcтвует, и false в противном случае | {'a': 1, 'b': 2, 'c': 3}.has('a') | true |
get(key) | Возвращает элемент словаря по его ключу или nil, если такого элемента нет | {'a': 1, 'b': 2, 'c': 3}.get('b') | 2 |
set(key, item) | Устанавливает элемент словаря с заданным ключом | $x = {1: true, 2: nil} | $x будет содержать {1: true, 2: false} |
remove(key) | Удаляет элемент словаря по ключу и возвращает его значение | $x = {'a': 1, 'b': 2} | $y будет содержать 1, а $x станет равным {'b': 2} |
equals(other) | Возвращает true, если other представляет собой точно такой же словарь (т.е. словарь, содержащий те же самые ключи и значения), и false в противном случае | {'a': 'b'}.equals(['a': 'b']) | true false |
hash() | Возвращает строку, представляющую собой хэш словаря (значение, уникально идентифицирующее содержимое словаря) | {'a': true}.hash() | bb2fadd01f7d0ac9864dc963a61f653f |
copy() | Создаёт поверхностную копию словаря (т.е. не копирует коллекции внутри словаря). | $list = ["a", "b"] $copy = {"a": 1, "b": $list}.copy() $copy.get("b").append("c") | $list будет равен ["a", "b", "c"] $copy будет равен {"a": 1, "b": ["a", "b", "c"]} |
deepCopy() | Создаёт полную копию словаря. | $list = ["a", "b"] $copy = {"a": 1, "b": $list}.deepCopy() $copy.get("b").append("c") | $list будет равен ["a", "b"] $copy будет равен {"a": 1, "b": ["a", "b", "c"]} |
merge(collection) | Сливает текущий словарь с коллекций (другим кортежем, списком или словарём) и возвращает его. | {"a": true, "b": 123}.merge((1, 2)) | {"a": true, "b": 123, 0: 1, 1: 2} {"a": true, "b": 123, 0: 1, 1: 2} {"a": 321, "b": 123, "c": false} |
Специальное значение nil
Когда какая-либо переменная не определена, но уже используется в выражении, её значением становится nil. Это специальное значение, означающее отсутствие какого-либо значения. Это значение также можно присвоить явно.
$x = 1 + 2 // $x содержит 3 $x = nil // $x содержит nil
Операции
В языке выражений бота определенно несколько бинарных и унарных операций. Некоторые операции могут быть применены к разным типам данных. Тип результата операции определяется видом операции. Например, операция "::" принимает в качестве аргументов строки. Поэтому все операнды этой операции будут неявно преобразованы к строке.
Приоритеты операций
Чем приоритет выше, тем раньше выполнится операция. Стандартным способом увеличения приоритета являются круглые скобки.
Операция | Приоритет | Ассоциативность | Описание |
---|---|---|---|
** | 12 | Правая | Возведение в степень |
+ | 11 | – | Унарный плюс |
- | 11 | - | Унарный минус |
~ | 11 | – | Побитовая инверсия |
! | 10 | – | Логическое отрицание |
* | 9 | Левая | Умножение |
/ | 9 | Левая | Деление |
\ | 9 | Левая | Целочисленное деление |
% | 9 | Левая | Остаток от деления |
+ | 8 | Левая | Сложение |
- | 8 | Левая | Вычитание |
:: | 8 | Левая | Конкатенация строк |
< | 7 | – | Меньше чем |
<= | 7 | – | Меньше или равно |
> | 7 | – | Больше чем |
>= | 7 | – | Больше или равно |
== | 6 | – | Равно |
!= | 6 | – | Не равно |
& | 5 | Левая | Побитовое И |
^ | 4 | Левая | Побитовое исключающее ИЛИ |
| | 3 | Левая | Побитовое ИЛИ |
&& | 2 | Левая | Логическое И |
|| | 1 | Левая | Логическое ИЛИ |
= | 0 | Правая | Присваивание |
+= | 0 | Правая | Сложение с присваиванием |
-= | 0 | Правая | Разность с присваиванием |
::= | 0 | Правая | Конкатенация с присваиванием |
*= | 0 | Правая | Умножение с присваиванием |
/= | 0 | Правая | Деление с присваиванием |
\= | 0 | Правая | Целочисленное деление с присваиванием |
%= | 0 | Правая | Остаток от деления с присваиванием |
**= | 0 | Правая | Возведение в степень с присваиванием |
&= | 0 | Правая | Бинарное И с присваиванием |
^= | 0 | Правая | Бинарное исключающее И с присваиванием |
|= | 0 | Правая | Бинарное ИЛИ с присваиванием |
Арифметические операции
Арифметические операции предназначены для работы с числами. Если значение, участвующее в арифметической операции, не является числом, то произойдёт попытка его преобразования в число (см. ниже).
Пример | Результат |
---|---|
+$a | Преобразование $a в число |
-$a | Смена знака $a |
$a + $b | Сумма $a и $b |
$a - $b | Разность между $a и $b |
$a * $b | Умножение $a на $b |
$a / $b | Деление $a на $b |
$a \ $b | Целочисленное деление $a на $b |
$a % $b | Остаток от деления $a на $b |
$a ** $b | Возведение $a в степень $b |
Побитовые операции
Это отдельный класс операций, предназначенных для выполнения над битовыми представлениями целых чисел.
Пример | Результат |
---|---|
~$a | Устанавливаются те биты, которые не установлены в $a, и наоборот |
$a & $b | Устанавливает только те биты, которые установлены и в $a, и в $b |
$a | $b | Устанавливает те биты, которые установлены в $a или в $b |
$a ^ $b | Устанавливаются только те биты, которые установлены либо только в $a, либо только в $b, но не в оба одновременно |
Таблица преобразования других типов данных в число.
Тип | Значение | Результат |
---|---|---|
NIL | nil | 0 |
Логический | true | 1 |
Логический | false | 0 |
Строка | "" | 0 |
Строка | "123.5" | 123.5 |
Строка | "abc" | ошибка |
Объект | любой | ошибка |
Логические операции
Логические операции предназначены для работы с булевыми значениями. Если значение не является булевым, то произойдёт попытка его преобразования в булево значение (см. ниже).
Пример | Результат |
---|---|
!$a | true, если $a равно false, и false в противном случае |
$a && $b | true, если $a и $b true, false в противном случае |
$a || $b | false, если $a и $b false, true в противном случае |
Таблица преобразования других типов данных в булево значение.
Тип | Значение | Результат |
---|---|---|
NIL | nil | false |
Число | 0 | false |
Число | Любое ненулевое | true |
Строка | "" | false |
Строка | Любая непустая | true |
Объект | Любой | true |
Операции со строками
Есть только одна операция для работы со строками. Это сложение или конкатенация строк.
Пример | Результат |
---|---|
"a" :: "b" | "ab" |
Если аргумент операции конкатенации не является строкой, то он будет преобразован в строку согласно следующей таблицы преобразования.
Тип | Значение | Результат |
---|---|---|
NIL | nil | "" |
Число | 123.45 | "123.45" |
Логический | true | "true" |
Логический | false | "false" |
Объект | Любой | Сериализация в строку |
Операции сравнения
Операции сравнения выполняются над любыми типами данных и результатом их вычисления являются булевы значения.
Пример | Результат |
---|---|
$a == $b | true, если $a тождественно равно $b |
$a != $b | true, если $a тождественно не равно $b |
$a > $b | true, если $a больше $b |
$a >= $b | true, если $a больше или равно $b |
$a < $b | true, если $a меньше $b |
$a <= $b | true, если $a меньше или равно $b |
Операции с коллекциями
Для встроенных типов объектов tuple, list и map доступна одна операция объединения.
Пример | Результат |
---|---|
(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} |
Операции присваивания
Операции присваивания позволяют назначать переменным какое-либо значение.
Пример | Результат |
---|---|
$a = 123 | $a содержит 123 |
$a += 1 | $a = $a + 1 |
$a -= 1 | $a = $a - 1 |
$a ::= "b" | $a = $a :: "b" |
$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 |
Кроме присваивания значения одной переменной есть возможность присвоить значение сразу нескольким переменным.
Пример | Результат присваивания | Значение выражения присваивания |
---|---|---|
($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} |
Функции
Основными строительными элементами выражений в блоке операций являются вызовы функций. Функция – это некоторая часть функциональности бота. Например, с помощью функций можно оперировать датой и временем, вычислять более сложные арифметические операции, преобразовывать строку текста в намерения, используя сервис NLU, работать с очередью сообщений клиента и т.д.
Чтобы вызвать функцию, нужно указать её имя, затем в круглых скобках перечислить через запятую её параметры. Имена большинства функций состоят из двух частей, разделяемых точкой: названия модуля и названия функции.
$number = math.rand(5, 10) // Получаем случайное число от 5 до 10. math – название модуля, rand – название функции
Также как и в случае методов объектов имена функций могут динамически вычисляться в процессе выполнения сценария бота.
$funcIdx = math.rand(0, 1) // Получаем случайное число 0 или 1, которое будет индексом названия функции в кортеже. $number = math.(('min', 'max').get($funcIdx))(3, 7) // Получаем название функции min или max и далее вызываем её. Таким образом, мы вычисляем случайным образом либо минимальное, либо максимальное значение.
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 */.
// 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 @.
// 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:
$('日本') = 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.
// 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:
// $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:
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 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:
// $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 returns 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:
Method | Description | Example | Result |
---|---|---|---|
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) | 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 on the list. | [1, 2, 3].toTuple() | (1, 2, 3) |
clear() | Deletes all elements on the list. | $x = [1, 2, 3] | $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 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 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 | Example | Result |
---|---|---|---|
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. Otherwise 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 BPL has several binary and unary operations. Some operations can be applied to different data types. The result type of an operation is determined by the type of the operation. For example, the operation "::" takes strings as arguments. Therefore, all operands of this operation will be implicitly converted to a string.
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 |
- | 8 | Left | Subtraction |
:: | 8 | Left | Concatenation |
< | 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 | Right | Assignment |
+= | 0 | Right | Assignment addition |
-= | 0 | Right | Difference with assignment |
::= | 0 | Right | Concatenation with assignment |
*= | 0 | Right | Multiplication with assignment |
/= | 0 | Right | Division with assignment |
\= | 0 | Right | Integer division with assignment |
%= | 0 | right | Remainder of division with assignment |
**= | 0 | Right | Exponentiation with assignment |
&= | 0 | Right | Binary AND with assignment |
^= | 0 | Right | Binary XOR with assignment |
|= | 0 | Right | 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, the system will 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 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.
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 | 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
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).
Example | Result |
---|---|
!$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:
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 an argument of the concatenation operation 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 ::= "b" | $a = $a :: "b" |
$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