{"id":17957,"date":"2016-12-08T20:35:41","date_gmt":"2016-12-08T20:35:41","guid":{"rendered":"https:\/\/developer.kore.com\/?post_type=docs&#038;p=9213"},"modified":"2023-12-18T13:28:38","modified_gmt":"2023-12-18T13:28:38","slug":"working-with-the-script-node","status":"publish","type":"post","link":"https:\/\/developer.kore.ai\/v10-1\/docs\/bots\/bot-builder-tool\/dialog-task\/working-with-the-script-node\/","title":{"rendered":"Script Node"},"content":{"rendered":"<section class=\"l-section wpb_row height_auto\"><div class=\"l-section-h i-cf\"><div class=\"g-cols vc_row type_default valign_top\"><div class=\"vc_col-sm-12 wpb_column vc_column_container\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<p>The <strong>Script Node<\/strong> allows you to write JavaScript code in a dialog task.<span style=\"font-weight: 400;\">You can use the script node to perform the following actions:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Manipulate user input parameters before executing an API call.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Manipulate the parameters from an API response payload before continuing with the dialog.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Display custom error messages to the user.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Make decisions based on complex business rules.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">When you test a dialog in the Bot Builder, script node errors are displayed in the <\/span><strong>Error in Script Node<\/strong><span style=\"font-weight: 400;\"> dialog with the line number and column number, along with the associated <\/span><span style=\"font-weight: 400;\">Context<\/span><span style=\"font-weight: 400;\"> object. You can update and test the script directly in the <\/span><strong>Error in Script Node<\/strong><span style=\"font-weight: 400;\"> dialog as shown in the following illustration.<\/span><\/p>\n<h1><span class=\"ez-toc-section\" id=\"Add_the_Node\"><span style=\"font-weight: 400;\">Add the Node<\/span><\/span><\/h1>\n<p><span style=\"font-weight: 400;\">Script nodes can only be added as part of a Bot Action node. Therefore, before you can add a Script node, you need to add a Bot Action node. <\/span><a href=\"https:\/\/developer.kore.ai\/v10-1\/docs\/bots\/bot-builder-tool\/dialog-task\/bot-action-node\/\"><span style=\"font-weight: 400;\">Read more about Bot Action Nodes<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Keeping this in mind, the setup a Script node in a dialog task involves the following steps:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><strong>Select the Virtual Assistant <\/strong><span style=\"font-weight: 400;\">you want to work with.<\/span><\/li>\n<li style=\"font-weight: 400;\"><strong>Open the dialog task <\/strong><span style=\"font-weight: 400;\">to add the Script node to.<\/span><\/li>\n<li style=\"font-weight: 400;\"><strong>Add the script node<\/strong><span style=\"font-weight: 400;\"> in the designated place. For steps in adding nodes, <\/span><a href=\"https:\/\/developer.kore.ai\/v10-1\/docs\/bots\/chatbot-overview\/using-the-dialog-builder-tool\/#Adding_Nodes\"><span style=\"font-weight: 400;\">refer here<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">The Script window is displayed with the <\/span><strong>Component Properties<\/strong><span style=\"font-weight: 400;\"> tab selected by default.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">You can configure the <\/span><strong>Connection Properties, <\/strong><a href=\"#Connections_Properties\"><span style=\"font-weight: 400;\">refer here for details<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ol>\n<p><a ref=\"magnificPopup\" href=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134358\/add-script-node.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-large wp-image-45056\" src=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134358\/add-script-node-1024x456.png\" alt=\"\" width=\"640\" height=\"285\" srcset=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134358\/add-script-node-1024x456.png 1024w, https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134358\/add-script-node-300x134.png 300w, https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134358\/add-script-node.png 1922w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<h1><span class=\"ez-toc-section\" id=\"Configure_the_Node\"><span style=\"font-weight: 400;\">Configure the Node<\/span><\/span><\/h1>\n<h2><span class=\"ez-toc-section\" id=\"Component_Properties\"><span style=\"font-weight: 400;\">Component Properties<\/span><\/span><\/h2>\n<table style=\"height: 30px; width: 100%; background-color: #fffbea;\" width=\"100%\">\n<tbody>\n<tr style=\"height: 30px;\">\n<td style=\"height: 30px;\" width=\"100%\"><strong><em>Note<\/em><\/strong><em><span style=\"font-weight: 400;\">: The configurations you set up or edit in these sections reflect in all other dialog tasks that use this node.<\/span><\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">To configure the Component Properties section, please follow the steps below:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">On the <\/span><strong>Component Properties<\/strong><span style=\"font-weight: 400;\"> tab, under the <\/span><strong>General Settings<\/strong><span style=\"font-weight: 400;\"> section, enter a <\/span><strong>Name<\/strong><span style=\"font-weight: 400;\"> and <\/span><strong>Display Name<\/strong><span style=\"font-weight: 400;\"> for the script node. Node names cannot have spaces.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Under the <\/span><strong>Script Definition<\/strong><span style=\"font-weight: 400;\"> section, click <\/span><strong>Define Script<\/strong><span style=\"font-weight: 400;\"> to add JavaScript.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">On the <\/span><strong>Add Script<\/strong><span style=\"font-weight: 400;\"> dialog box, enter your JavaScript; then click<\/span><strong> Save<\/strong><span style=\"font-weight: 400;\">. <a href=\"https:\/\/developer.kore.ai\/v10-1\/docs\/bots\/bot-builder-tool\/dialog-task\/working-with-the-script-node\/#JavaScript_Examples\">See below for JavaScript code examples<\/a>.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">In the <\/span><strong>Variable Namespaces<\/strong><span style=\"font-weight: 400;\"> section, associate the variable namespaces to execute this node and its transitions. This option is visible only when the variable namespace is enabled for the bot. You can go with the task level settings or customize it for this node. For more information, refer to <\/span><a href=\"https:\/\/developer.kore.ai\/v10-1\/docs\/bots\/bot-settings\/bot-management\/managing-namespace\/\"><span style=\"font-weight: 400;\">Managing Namespace<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ol>\n<p><a ref=\"magnificPopup\" href=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134457\/script-component-properties.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-45059\" src=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134457\/script-component-properties.png\" alt=\"\" width=\"446\" height=\"722\" srcset=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134457\/script-component-properties.png 446w, https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134457\/script-component-properties-185x300.png 185w\" sizes=\"auto, (max-width: 446px) 100vw, 446px\" \/><\/a><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Instance_Properties\"><span style=\"font-weight: 400;\">Instance Properties<\/span><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Use the Instance Properties to define any user tags that you want to set for the Script node.<\/span><\/p>\n<table style=\"height: 30px; width: 100%; background-color: #fffbea;\" width=\"100%\">\n<tbody>\n<tr style=\"height: 30px;\">\n<td style=\"height: 30px;\" width=\"100%\"><strong><em>Note<\/em><\/strong><em><span style=\"font-weight: 400;\">: The settings in the Instance Properties tab are specific to the current dialog task and do not reflect in other dialog tasks that use this node.<\/span><\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ol>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">On the Script node properties panel, click the <\/span><strong>Instance Properties<\/strong><span style=\"font-weight: 400;\"> tab.<\/span><\/li>\n<li style=\"font-weight: 400;\"><span style=\"font-weight: 400;\">Under the <\/span><strong>Custom<\/strong> <strong>Tags <\/strong><span style=\"font-weight: 400;\">section, add tags to build custom profiles of your VA\u2019s conversations. <\/span><a href=\"https:\/\/developer.kore.ai\/v10-1\/docs\/bots\/bot-builder-tool\/dialog-task\/custom-meta-tags\/\"><span style=\"font-weight: 400;\">Click here for more<\/span><\/a><span style=\"font-weight: 400;\">.<\/span><\/li>\n<\/ol>\n<p><a ref=\"magnificPopup\" href=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134606\/script-instance-properties.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-45062\" src=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134606\/script-instance-properties.png\" alt=\"\" width=\"451\" height=\"659\" srcset=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134606\/script-instance-properties.png 451w, https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134606\/script-instance-properties-205x300.png 205w\" sizes=\"auto, (max-width: 451px) 100vw, 451px\" \/><\/a><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Connections_Properties\"><span style=\"font-weight: 400;\">Connections Properties<\/span><\/span><\/h2>\n<p><span style=\"font-weight: 400;\">Since they are contained within Bot Action nodes, Script nodes cannot be connected on their own to any other nodes outside the Bot Action node to which they belong. Connections can only be made with nodes contained within the same Bot Action node.\u00a0<\/span><\/p>\n<p><span style=\"font-weight: 400;\">From the node\u2019s <\/span><strong>Connection Properties <\/strong><span style=\"font-weight: 400;\">tab, you can configure the following:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\"><strong>Script Node Connections<\/strong><span style=\"font-weight: 400;\">: The node within the Bot Action to execute next, after the Script node runs.\u00a0<\/span><\/li>\n<li style=\"font-weight: 400;\"><strong>Bot Action Connections<\/strong><span style=\"font-weight: 400;\">, where you can update the node to trigger after the Bot Action group is executed. Updates made here will apply to all nodes within the same Bot Action.\u00a0<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">For both connection types you can write conditional statements based on the values of any Entity or Context Objects in the dialog task, or you can use Intents for transitions.\u00a0<\/span><\/p>\n<table style=\"height: 30px; width: 100%; background-color: #fffbea;\" width=\"100%\">\n<tbody>\n<tr style=\"height: 30px;\">\n<td style=\"height: 30px;\" width=\"100%\"><strong><em>Note<\/em><\/strong><em><span style=\"font-weight: 400;\">: The conditions configured here are applicable only for this instance and will not affect this node when being used in any other dialog.<\/span><\/em><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><span style=\"font-weight: 400;\">To set up node connection conditions, please follow the steps outlined in <\/span><a href=\"https:\/\/developer.kore.ai\/v10-1\/docs\/bots\/bot-builder-tool\/dialog-task\/nodes-conditions\/\"><span style=\"font-weight: 400;\">Adding <\/span><em><span style=\"font-weight: 400;\">IF-Else<\/span><\/em><span style=\"font-weight: 400;\"> Conditions to Node Connections.\u00a0<\/span><\/a><\/p>\n<p><a ref=\"magnificPopup\" href=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134740\/script-connections-properties.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-45065\" src=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134740\/script-connections-properties.png\" alt=\"\" width=\"459\" height=\"583\" srcset=\"https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134740\/script-connections-properties.png 459w, https:\/\/kore-wordpress.s3.us-east-2.amazonaws.com\/developer.kore.ai\/v10-1\/wp-content\/uploads\/sites\/35\/20220920134740\/script-connections-properties-236x300.png 236w\" sizes=\"auto, (max-width: 459px) 100vw, 459px\" \/><\/a><\/p>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<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>\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<h1><span class=\"ez-toc-section\" id=\"JavaScript_Examples\"><span style=\"font-weight: 400;\">JavaScript Examples<\/span><\/span><\/h1>\n<p>Using JavaScript, you can customize your dialog task by processing data before or after an API call. For example, directing the dialog task flow. You can use the <code>Context<\/code> object type-ahead feature to identify and select dynamic variables as shown in the following illustration. For more information, refer to <a href=\"\/docs\/bots\/bot-builder-tool\/dialog-task\/context-object\/\">Context Object<\/a>.<br \/>\n<a ref=\"magnificPopup\" href=\"\/wp-content\/uploads\/sites\/35\/DIYDialogScriptNodeAddScriptDialogTypeAhead.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-11919\" src=\"\/wp-content\/uploads\/sites\/35\/DIYDialogScriptNodeAddScriptDialogTypeAhead.png\" alt=\"Type Ahead Context Object\" width=\"473\" height=\"401\" \/><\/a><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Manipulating_Data\">Manipulating Data<\/span><\/h2>\n<p>In JavaScript, you can fetch data from the session data using session variables. For example, you can GET the user ID using <code>context.session.UserContext.identities<\/code>, and then PUT the data into <code>context<\/code> as shown in the following example.<\/p>\n<pre class=\"lang:default decode:true\">var x = context.session.UserContext.identities;\r\nvar isEmailFound = false;\r\nfor (var i = 0; i &lt; x.length; i++) {\r\n    if (x[i].type === \"mapped\") {\r\n        var identity = x[i].val;\r\n        var arr = identity.split(\"\/\");\r\n        var pattern = \/^cs\/i;\r\n        var result = arr[0].match(pattern);\r\n        if (result) {\r\n            isEmailFound = true;\r\n            context.UserSession.put(\"rtmEmail\", arr[1], '20000');\r\n        }\r\n    }\r\n}\r\n...<\/pre>\n<p>For more information, refer to <a href=\"\/docs\/bots\/bot-builder-tool\/dialog-task\/using-session-and-context-variables-in-tasks\/\">Using Session and Context Variables in Tasks<\/a>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Handling_Conditions\">Handling Conditions<\/span><\/h2>\n<p>The following code example returns the customer ID using a <code>Context<\/code> object variable from the Service node response body when the ID is not provided in the <code>data.results<\/code><\/p>\n<pre>var data = context.getcontactsService.response.body;\r\nif (data &amp; amp; &amp; amp; typeof(data.results) != 'undefined') {\r\n context.customerID = data.results.customerId;\r\n} else {\r\n context.customerID = context.Usersession.customerID;\r\n}<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Handling_Flow\">Handling Flow<\/span><\/h2>\n<p>The next Script node code example validates the bank transfer amount that does not exceed limits for the type of account selected using <code>Context<\/code> object variables.<\/p>\n<pre>var valid = 0;\r\nvar i = 0;\r\nwhile (context.accdata.length - i) {\r\n if (context.accdata[i].accountType == context.entities.FromAccountName) {\r\n  if ((context.accdata[i].amount - context.entities.Amount) &lt; 0) { valid = 3; } else { if (context.entities.Amount &gt; context.accdata[i].transferLimit) {\r\n    valid = 0;\r\n   } else {\r\n    valid = 2;\r\n   }\r\n  }\r\n }\r\n i++;\r\n}\r\ncontext.canProceed = valid;<\/pre>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<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>\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<h4>Related Link<\/h4>\n<ul>\n<li><a href=\"https:\/\/developer.kore.ai\/v10-1\/docs\/bots\/advanced-topics\/koreutil-libraries\/\"><strong>koreUtil libraries<\/strong><\/a> (Pre-written JavaScript functions to make common or complex tasks easy to implement)<\/li>\n<\/ul>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n<div class=\"ult-spacer spacer-6ae984688ebea\" data-id=\"6ae984688ebea\" data-height=\"20\" data-height-mobile=\"20\" data-height-tab=\"20\" data-height-tab-portrait=\"\" data-height-mobile-landscape=\"\" style=\"clear:both;display:block;\"><\/div><\/div><\/div><\/div><\/div><\/div><\/section>\n","protected":false},"excerpt":{"rendered":"The Script Node allows you to write JavaScript code in a dialog task.You can use the script node to perform the following actions: Manipulate user input parameters before executing an API call. Manipulate the parameters from an API response payload before continuing with the dialog. Display custom error messages to the user. Make decisions based...","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-17957","post","type-post","status-publish","format-standard","hentry","category-dialog-task"],"_links":{"self":[{"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/posts\/17957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/comments?post=17957"}],"version-history":[{"count":18,"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/posts\/17957\/revisions"}],"predecessor-version":[{"id":60787,"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/posts\/17957\/revisions\/60787"}],"wp:attachment":[{"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/media?parent=17957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/categories?post=17957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.kore.ai\/v10-1\/wp-json\/wp\/v2\/tags?post=17957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}