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:
    var myvariable = context.policySelected; 
    where policySelected is a variable set by another interaction in this bot or by a linked bot.

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}
Note: In messages you do not need access the variable through the context object.

Variables in Action interactions

For variables defined in an Action interaction, select Set Variable from the Action Type dropdown. You can access the variable through the 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

For variables returned by a REST API call, you access the variables through the variable set on the Settings tab of the Action interaction. This variable stores the API response. The variable path will depend on the structure of the response. For example, if the response to the API call is stored in a variable called accountDetails then you might access the balance in a message like this:
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:
  • facebook
  • websocket
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 the context 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:

var secondItem = context.authCodeParts[1]

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: