$session
This object is used for storing arbitrary data related to the current session.
After all bot reactions have been executed, the $session
contents are saved to the internal database and persisted between user requests.
When a session ends, this data is cleared.
caution
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 properties
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;
}