{"id":21415,"date":"2019-06-11T06:25:53","date_gmt":"2019-06-11T05:25:53","guid":{"rendered":"https:\/\/developer.kore.ai\/?p=21415"},"modified":"2020-12-09T10:16:12","modified_gmt":"2020-12-09T10:16:12","slug":"using-session-and-context-variables-in-tasks","status":"publish","type":"post","link":"https:\/\/multisite.korebots.com\/v9-0\/docs\/bots\/bot-builder-tool\/dialog-task\/using-session-and-context-variables-in-tasks\/","title":{"rendered":"Using Session Variables in Tasks"},"content":{"rendered":"<section class=\"l-section wpb_row height_auto\"><div class=\"l-section-h i-cf\"><div class=\"g-cols vc_row via_grid cols_1 laptops-cols_inherit tablets-cols_inherit mobiles-cols_1 valign_top type_default stacking_default\"><div class=\"wpb_column vc_column_container\"><div class=\"vc_column-inner\"><div class=\"wpb_text_column\"><div class=\"wpb_wrapper\"><p>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\u00a0that defines the scope of the variable.<\/p>\n<p>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.\u00a0In addition, a dialog task can access the <code>context<\/code> object with additional system variables. For more information, see the\u00a0<a href=\"\/docs\/bots\/bot-builder-tool\/dialog-task\/context-object\/\">Context Object<\/a>.<\/p>\n<p>You can use session variables where you define JavaScript for tasks and the User Prompt editor on the JavaScript tab.<\/p>\n<\/div><\/div><div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div><div class=\"wpb_text_column\"><div class=\"wpb_wrapper\"><h2><span class=\"ez-toc-section\" id=\"Session_Variables\"><\/span>Session Variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>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.<\/p>\n<p>The JavaScript syntax to GET or PUT a key\/value pair for each context type is:<\/p>\n<pre class=\"lang:default decode:true\">   \"EnterpriseContext\" : {\r\n       \"get\" : function(key){...},\/\/get the specified key\r\n       \"put\" : function(key, value, ttl){...} \/\/put the value at the key for the specified ttl, ttl is in minutes\r\n   },\r\n   \"BotContext\" : {\r\n       \"get\" : function(key){...},\/\/get the specified key\r\n       \"put\" : function(key, value, ttl){...} \/\/put the value at the key for the specified ttl, ttl is in minutes\r\n   },\r\n   \"UserContext\" : {\r\n       \"get\" : function(key){...},\/\/get the specified key\r\n   },\r\n   \"UserSession\" : {\r\n       \"get\" : function(key){...},\/\/get the specified key\r\n       \"put\" : function(key, value, ttl){...} \/\/put the value at the key for the specified ttl, ttl is in minutes\r\n   },\r\n   \"BotUserSession\" : {\r\n       \"get\" : function(key){...},\/\/get the specified key\r\n       \"put\" : function(key, value, ttl){...} \/\/put the value at the key for the specified ttl, ttl is in minutes\r\n   }<\/pre>\n<p>For example:<\/p>\n<pre class=\"lang:default decode:true\">\u00a0 \u00a0 BotContext.put(\"topicSessionVariable\",\"music\",2000);\r\n\u00a0 \u00a0 UserSession.put(\"firstName\",\"Mary\",20000);\r\n\u00a0 \u00a0 UserContext.get(\"firstName\");<\/pre>\n<\/div><\/div><div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div><div class=\"wpb_text_column\"><div class=\"wpb_wrapper\"><h2><span class=\"ez-toc-section\" id=\"Session_Variable_Types\"><\/span>Session Variable Types<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The following types of session variables are available on the Bots Platform:<\/p>\n<ul>\n<li><strong>EnterpriseContext<\/strong> &#8211; A\u00a0key\/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.\u00a0You can persist the repository data as <strong>Gitrepository (Enterprise Context)<\/strong> with the following JavaScript code:\n<pre class=\"lang:default decode:true\">var userRepository = {\r\n\"title\": _labels_[repository],\r\n\"value\": repository\r\n};\r\nEnterpriseContext.put('Gitrepository', userRepository, 200000);<\/pre>\n<\/li>\n<li><strong>BotContext<\/strong> &#8211; 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 <strong>currency (Bot Context)<\/strong> with the following JavaScript code:\n<pre class=\"lang:default decode:true\">var defaultCurrency = { TODO Custom JavaScript for location-based currency }\r\nBotContext.put('currency', defaultCurrency, 200000);<\/pre>\n<\/li>\n<li><strong>UserContext<\/strong> &#8211; 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:\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li><strong>UserContext.get(&#8220;_id&#8221;)<\/strong> &#8211;\u00a0The Kore.ai user ID.<\/li>\n<li><strong>UserContext.get(&#8220;emailId&#8221;)<\/strong> &#8211;\u00a0The email address associated with the user ID.<\/li>\n<li><strong>UserContext.get(&#8220;lastName&#8221;)<\/strong> &#8211;\u00a0The last name of the user.<\/li>\n<li><strong>UserContext.get(&#8220;firstName&#8221;)<\/strong> &#8211;\u00a0The first name of the user.<\/li>\n<li><strong>UserContext.get(&#8220;profImage&#8221;)<\/strong> &#8211;\u00a0The image or avatar filename of the user.<\/li>\n<li><strong>UserContext.get(&#8220;profColor&#8221;)<\/strong> &#8211;\u00a0The account color for the user.<\/li>\n<li><strong>UserContext.get(&#8220;activationStatus&#8221;)<\/strong> &#8211; The account status of the user. Can be:\n<ul>\n<li><code>active<\/code> \u2013 The user is active and can interact with other Kore.ai users.<\/li>\n<li><code>inactive<\/code> \u2013 The user is not active, but user data is retained in the system.<\/li>\n<li><code>suspended<\/code> \u2013 The user is suspended by an administrator. The user cannot log on to Kore.ai, however, messages can still be sent to the suspended user.<\/li>\n<li><code>locked<\/code> \u2013 The user exceeded the maximum number of login attempts.<\/li>\n<\/ul>\n<\/li>\n<li><strong>UserContext.get(&#8220;jTitle&#8221;)<\/strong> &#8211;\u00a0The title of the user, if defined.<\/li>\n<li><strong>UserContext.get(&#8220;orgId&#8221;)<\/strong> &#8211;\u00a0The organizational ID of the user account, if defined.<\/li>\n<li><strong>UserContext.get(&#8220;customData&#8221;)<\/strong> &#8211; Use this to pass user information to web channels, currently only for webSDK,\u00a0<a href=\"https:\/\/developer.kore.ai\/docs\/bots\/sdks\/kore-ai-web-sdk-tutorial\/\" target=\"_blank\" rel=\"noopener noreferrer\">see here for more<\/a>.<\/li>\n<li><strong>UserContext.get(&#8220;identities&#8221;)<\/strong> &#8211; Alternate user IDs, if defined.\n<ul>\n<li><code>val<\/code> &#8211; The alternate ID<\/li>\n<li><code>type<\/code> &#8211; The type of alternate ID.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>For example,\u00a0you can PUT a value into the session using the <code>UserSession<\/code> variable where\u00a0the key\u00a0is defined as <code>fullName<\/code> based on the GET from the two\u00a0<code>UserContext<\/code>\u00a0variables.<\/p>\n<pre class=\"lang:default decode:true\">var name = UserContext.get(\"firstName\")+UserContext.get(\"lastName\");\r\nUserSession.put(\"fullName\") = name;<\/pre>\n<\/li>\n<li><strong>UserSession<\/strong> &#8211; A\u00a0key\/value pair that you can define\u00a0for this specific user for all bots in an enterprise. For example, you may want to store a user location to make it\u00a0available to all Bots, such as a user home address for commerce, transportation, and home\u00a0delivery services.\u00a0For example, you can persist default location\u00a0data as <strong>HomeLocation (UserSession)<\/strong> with the following JavaScript code:\n<pre class=\"lang:default decode:true\">var location = {\r\n \"title\": labels[location],\r\n \"value\": {\r\n \"latitude\": location.latitude,\r\n \"longitude\": request.location.longitude\r\n }\r\n};\r\nUserSession.put('HomeLocation', location, '20000');<\/pre>\n<\/li>\n<li><strong>BotUserSession<\/strong> &#8211; 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 <strong>HomeLocation (BotUserSession)<\/strong>\u00a0and <strong>DestinationLocation (BotUserSession)<\/strong> with the following JavaScript code:\n<pre class=\"lang:default decode:true\">var homelocation = {\r\n \"title\": labels[request.sourceLocation],\r\n \"value\": {\r\n \"latitude\": request.sourceLocation.latitude,\r\n \"longitude\": request.sourceLocation.longitude\r\n }\r\n};\r\nBotUserSession.put('HomeLocation', homelocation, '20000');\r\nvar destlocation = {\r\n \"title\": labels[request.destLocation],\r\n \"value\": {\r\n \"latitude\": request.destLocation.latitude,\r\n \"longitude\": request.destLocation.longitude\r\n }\r\n};\r\nBotUserSession.put('DestinationLocation', destlocation, '20000\u2019);<\/pre>\n<\/li>\n<\/ul>\n<\/div><\/div><div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div><div class=\"wpb_text_column\"><div class=\"wpb_wrapper\"><h2><span class=\"ez-toc-section\" id=\"Standard_Keys\"><\/span>Standard Keys<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>In addition to session and context keys, there\u00a0are Kore.ai variable placeholders for reusable data. Select one of:<\/p>\n<ul>\n<li><strong>_labels_<\/strong> &#8211; 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.\u00a0You can use the\u00a0<code>_labels_<\/code> 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 <code>_labels_<\/code> key as, for example:\n<pre class=\"lang:default decode:true\">{\r\n    \"_labels_\": {\r\n        \"15379386734832\": \"roadlabs.com\",\r\n        \"26377329985341\": \"Test Project\",\r\n        \"workspace\": \"roadlabs.com\",\r\n        \"project\": \"Test Project\"\r\n    },\r\n    \"_fields_\": {\r\n        \"workspace\": \"15379386734832\",\r\n        \"project\": \"26377329985341\"\r\n    }\r\n}<\/pre>\n<p>To use the <code>_labels_<\/code> key in a response:<\/p>\n<pre class=\"lang:default decode:true\">print('&lt;a href=\"https:\/\/app.asana.com\/0\/' + workspace.id + '\/' + id + '\/f\" target=\"_blank\"&gt;' + title + '&lt;\/a&gt; in workspace '+_labels_[workspace.id]);<\/pre>\n<\/li>\n<li><strong>_tenant_<\/strong> &#8211; Used to return the tenant for the enterprise when defined. For example, <em>JIRA<\/em> requires a tenant for URLs, such as <span style=\"color: #0000ff;\">koreteam<\/span>, in\u00a0<span style=\"color: #0000ff;\">https:\/\/<strong>koreteam<\/strong>.atlassian.net\/browse\/BBF-3265<\/span>. You can use the <code>_tenant_<\/code> key to build a link in a task response such as:\n<pre class=\"lang:default decode:true\">var title = request.fields.issuetype.name + ' &lt;a href =\"https:\/\/' + _tenant_ + '\/browse\/' + response.key + '\" target = \"_blank\"&gt;' + request.fields.summary + '&lt;\/a&gt; \u00a0has been created.';<\/pre>\n<\/li>\n<li><strong>_fields_<\/strong> &#8211; 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 <em>JIRA<\/em> action task, the end-user is prompted to enter a workspace name. You can use the <code>_fields_<\/code> key to store the end-user input as:\n<pre class=\"lang:default decode:true\">_fields_[\"workspace\"]<\/pre>\n<\/li>\n<li><strong>_last_run<\/strong> &#8211; Used to return the UTC date timestamp of a web service poll using ISO 8601 format, for example,\u00a0<span style=\"color: #0000ff;\">2016-03-05T12:44:38+00:00<\/span>. For example, if a web service request returns all activity in a payload response, you can use the <code>_last_run<\/code> key to filter results displayed before or after the value for <code>_last_run<\/code>.<\/li>\n<\/ul>\n<\/div><\/div><div class=\"w-separator size_small with_line width_default thick_1 style_solid color_border align_center\"><div class=\"w-separator-h\"><\/div><\/div><\/div><\/div><\/div><\/div><\/section>\n","protected":false},"excerpt":{"rendered":"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\u00a0that 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...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/posts\/21415"}],"collection":[{"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/comments?post=21415"}],"version-history":[{"count":11,"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/posts\/21415\/revisions"}],"predecessor-version":[{"id":25644,"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/posts\/21415\/revisions\/25644"}],"wp:attachment":[{"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/media?parent=21415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/categories?post=21415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/tags?post=21415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}