$session
This object is used for storing arbitrary session data.
After all bot reactions have been executed, the $session
structure is saved to the internal database.
When a new session begins, all data is erased.
A new session can be initiated from the script using the $jsapi.startSession
method.
There is a limit on the amount of data stored in the $session
object.
If this limit is exceeded, the current script is aborted and the bot stops responding to the client.
Special fields
contextPath
is the current dialog context. The platform uses this attribute to restore the context on each subsequent user request.
How to use
- Storing the user’s name:
require: name/nameEn.sc
module = sys.zb-common
theme: /
state: Hello
q!: * my name is $Name *
script:
$session.name = $parseTree._Name.name;
a: Hi, {{$session.name}}!
- Storing and generating a random number:
state: Game
script:
$session.number = $jsapi.random(100) + 1;
a: My number is {{$session.number}}.
go!: /Check
- Storing goods quantities in the
$session.quantity
variable and adding parameters in the$session.cart
array:
state: GetQuantity
script:
$session.cart = [];
$session.quantity = parseInt($request.query);
$session.cart.push({name: $session.pizzaName, id: $session.pizzaId, quantity: $session.quantity});
a: Would you like anything else?
- Storing the chat ID depending on the channel type:
state: ChatId
script:
if ($request.channelType.indexOf("chatwidget") > -1) {
$session.chatId = $request.channelUserId;
} else {
$session.chatId = $request.data.chatId;
}