When you define tasks, you can access session variables provided by the Bots Platform, or custom variables that you define, as well as the context that defines the scope of the variable.
For example, some API requests may require you to set session variables before the request is executed, or a dialog task component may need to access a session variable to transition to the next node. In addition, a dialog task can access the context
object with additional system variables. For more information, see the Context Object.
You can use context and session variables where you define JavaScript for tasks, and the User Prompt editor on the JavaScript tab.
Session Variables
This section describes the context variables and the scope of the context variables that you can use in your custom JavaScript code for your tasks.
The JavaScript syntax to GET or PUT a key/value pair for each context type is:
"EnterpriseContext" : { "get" : function(key){...},//get the specified key "put" : function(key, value, ttl){...} //put the value at the key for the specified ttl, ttl is in minutes }, "BotContext" : { "get" : function(key){...},//get the specified key "put" : function(key, value, ttl){...} //put the value at the key for the specified ttl, ttl is in minutes }, "UserContext" : { "get" : function(key){...},//get the specified key }, "UserSession" : { "get" : function(key){...},//get the specified key "put" : function(key, value, ttl){...} //put the value at the key for the specified ttl, ttl is in minutes }, "BotUserSession" : { "get" : function(key){...},//get the specified key "put" : function(key, value, ttl){...} //put the value at the key for the specified ttl, ttl is in minutes }
For example:
BotContext.put("topicSessionVariable","music",2000); UserSession.put("firstName","Mary",20000); UserContext.get("firstName");
Context Types
The following types of context variables are available on the Bots Platform:
- EnterpriseContext – A key/value pair available to all bots and all users in an enterprise. For example, with the GitHub bot, a user will need to access one or more enterprise repositories. You can persist the repository data as Gitrepository (Enterprise Context) with the following JavaScript code:
var userRepository = { "title": _labels_[repository], "value": repository }; EnterpriseContext.put('Gitrepository', userRepository, 200000);
- BotContext – A key/value pair available to all users of this specific bot. For example, you may want to set up a default currency for financial transactions for a session based on the location of a user. You can persist the default currency data as currency (Bot Context) with the following JavaScript code:
var defaultCurrency = { TODO Custom JavaScript for location-based currency } BotContext.put('currency', defaultCurrency, 200000);
- UserContext – A key/value pair available to all bots for a user. These keys are read-only and provided by the system as user data for:
-
- UserContext.get(“_id”) – The Kore.ai user ID.
- UserContext.get(“emailId”) – The email address associated with the user ID.
- UserContext.get(“lastName”) – The last name of the user.
- UserContext.get(“firstName”) – The first name of the user.
- UserContext.get(“profImage”) – The image or avatar filename of the user.
- UserContext.get(“profColor”) – The account color for the user.
- UserContext.get(“activationStatus”) – The account status of the user.
- UserContext.get(“jTitle”) – The title of the user, if defined.
- UserContext.get(“orgId”) – The organizational ID of the user account, if defined.
- UserContext.get(“identities”) – Alternate user IDs, if defined.
For example, you can PUT a value into the session using the
UserSession
variable where the key is defined asfullName
based on the GET from the twoUserContext
variables.var name = UserContext.get("firstName")+UserContext.get("lastName"); UserSession.put("fullName") = name;
-
- UserSession – A key/value pair that you can define for this specific user for all bots in an enterprise. For example, you may want to store a user location to make it available to all Bots, such as a user home address for commerce, transportation, and home delivery services. For example, you can persist default location data as HomeLocation (UserSession) with the following JavaScript code:
var location = { "title": labels[location], "value": { "latitude": location.latitude, "longitude": request.location.longitude } }; UserSession.put('HomeLocation', location, '20000');
- BotUserSession – A key/value pair that you can define to a specific bot based on the inputs by a specific user. For example, you may want to persist a user location for more than one task of a Bot. For a travel bot, the user may be able to book a flight and a hotel based on the same home and destination addresses. For example, you can persist the default home and destination data as HomeLocation (BotUserSession) and DestinationLocation (BotUserSession) with the following JavaScript code:
var homelocation = { "title": labels[request.sourceLocation], "value": { "latitude": request.sourceLocation.latitude, "longitude": request.sourceLocation.longitude } }; BotUserSession.put('HomeLocation', homelocation, '20000'); var destlocation = { "title": labels[request.destLocation], "value": { "latitude": request.destLocation.latitude, "longitude": request.destLocation.longitude } }; BotUserSession.put('DestinationLocation', destlocation, '20000’);
Standard Keys
In addition to session and context keys, there are Kore.ai variable placeholders for reusable data. Select one of:
- _labels_ – Used to return the friendly label in place of a GUID. For example, when user data is requested from a web service API, the ID of a project or workspace returned is a GUID. You can use the
_labels_
key to show the user-friendly name of the GUID to the end-user instead of the GUID. In Kore.ai, a drop-down control stores the response for the_labels_
key as, for example:{ "_labels_": { "15379386734832": "roadlabs.com", "26377329985341": "Test Project", "workspace": "roadlabs.com", "project": "Test Project" }, "_fields_": { "workspace": "15379386734832", "project": "26377329985341" } }
To use the
_labels_
key in a response:print('<a href="https://app.asana.com/0/' + workspace.id + '/' + id + '/f" target="_blank">' + title + '</a> in workspace '+_labels_[workspace.id]);
- _tenant_ – Used to return the tenant for the enterprise when defined. For example, JIRA requires a tenant for URLs, such as koreteam, in https://koreteam.atlassian.net/browse/BBF-3265. You can use the
_tenant_
key to build a link in a task response such as:var title = request.fields.issuetype.name + ' <a href ="https://' + _tenant_ + '/browse/' + response.key + '" target = "_blank">' + request.fields.summary + '</a> has been created.';
- _fields_ – Used to return an action task field input provided by the end-user that is not part of a payload response. For example, in a JIRA action task, the end-user is prompted to enter a workspace name. You can use the
_fields_
key to store the end-user input as:_fields_["workspace"]
- _last_run – Used to return the UTC date timestamp of a web service poll using ISO 8601 format, for example, 2016-03-05T12:44:38+00:00. For example, if a web service request returns all activity in a payload response, you can use the
_last_run
key to filter results displayed before or after the value for_last_run
.