Using variables in chatbots
You can define variables for use by any interaction in the current bot or in linked bots. Variables are set per session and the values exist only for the duration of the user's session. Some default variables are accessible in Designer and these provide information about the conversation, session and the user.
Defining variables
There are several ways of defining variables. Depending on their intended purpose, you define them when you set up:
- An interaction, such as on the Answers tab of the Question interaction
- An Action interaction; select Set Variable from the Action Type dropdown. You can define variables like this:
where policySelected is a variable set by another interaction in this bot or by a linked bot.var myvariable = context.policySelected;
Regardless of how you define them, most variables are defined on the context
object. Some user data is available from the user object.
Variables in messages
In messages sent to the user, enclose the variable (including any system function that you are using) in single curly brackets like this:
You have {employees} employees. Do you already use e-payslips?
Do you want more information about our {system.toProperCase(product)
}?
User data is accessed through the user object like this:
Hello {user.full_name}
context
object.Variables in Action interactions
context
object: context.myvariable
. In this example, the variable is set to the last message sent by the user:context.selected_item = context.lastSentText;
For variables defined in Action interactions using Condition as the Action Type, use the format {myvariable}
(same as messages).
Variables in the body of a REST call
In the body of a REST call, enclose the variable in double curly brackets (because the REST body is a JSON object). For example: {{myvariable}}
Variables in the response to a REST call
The balance on your account is €{accountDetails.balance}
Conversation variables
The context
object has variables that provide details about the live conversation. Access these variables by enclosing them in single curly brackets, like this {lastSentText}
.
Variable | Description |
---|---|
agentName |
Used in Live Takeover. This is the Communicate login name of the person who takes over the conversation. |
channel |
This is one of the following:
|
lastSentText |
The last reply sent by the user. |
User details
The user
object contains any user information captured by the chatbot. This will be used, for example, on the Live Takeover page. The information available depends on the channel.
These variables are initially set by the Web Client. This ensures that the information is available in the session.
Information consists of:
- full_name
- first_name
- last_name
- locale (en_US)
Session details
Session details are added to thecontext
object by the Communicate servers. For example:Example | Returns |
---|---|
context.accessToken |
The access token that is returned when account linking uses OAuth authentication. |
context.authCode |
The authorization code that is returned when the user links their account. |
context.authCodeParts |
An array of the authcode when multiple items are returned by the account linking process.For example 123456789|123 where 123456789 is the token and 123 is the user ID. You would access the user ID like this:
Arrays have a starting index of 0 so the first part is 0 and the second part is 1. |
context.sessionId |
The current user's session ID. Note that this is read only. |
See also: