{"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":"2022-04-25T10:13:16","modified_gmt":"2022-04-25T09:13:16","slug":"working-with-the-script-node","status":"publish","type":"post","link":"https:\/\/developer.kore.ai\/v9-2\/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>A <strong>Script Node<\/strong> allows you to write JavaScript code in a dialog task.<br \/>\nYou can use the Script node to perform the following actions:<\/p>\n<ul>\n<li>Manipulate user input parameters before executing an API call.<\/li>\n<li>Manipulate the parameters from an API response payload before continuing with the dialog.<\/li>\n<li>Display custom error messages to the user.<\/li>\n<li>Make decisions based on complex business rules.<\/li>\n<\/ul>\n\r\n\t\t<\/div>\r\n\t<\/div>\r\n\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<p>When you test a dialog in the Kore.ai XO Platform, Script node errors are displayed in the <strong>Error in Script Node<\/strong> dialog with the line number and column number, along with the associated <code>Context<\/code> object. You can update and test the script directly in the <strong>Error in Script Node<\/strong> dialog as shown in the following illustration.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Set_up_a_Script_Node\">Set up a Script Node<\/span><\/h2>\n<p>Setting up a Script node in a dialog task involves the following steps:<\/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<h2><span class=\"ez-toc-section\" id=\"Set-Up\">Set-Up<\/span><\/h2>\n<p>Setting up a script node in a dialog task involves the following steps:<\/p>\n<p>Note: Post v9.0 of the platform, the Script node is categorized under the VA Action node. For details on the VA Action node, <a href=\"\/docs\/bots\/bot-builder-tool\/dialog-task\/bot-action-node\/\" target=\"_blank\" rel=\"noopener noreferrer\">click here<\/a>.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Add_Node\">Add Node<\/span><\/h3>\n<ol>\n<li>Open the dialog task to add the Script node.<\/li>\n<li>Add the script node in the designated place. For steps in adding nodes, <a href=\"\/docs\/bots\/chatbot-overview\/using-the-dialog-builder-tool\/#Adding_Nodes\">refer here<\/a>.<\/li>\n<li>The Script window is displayed with the <strong>Component Properties<\/strong> tab selected by default.<\/li>\n<li>You can configure the <strong>Connection Properties, <\/strong><a href=\"\/docs\/bots\/chatbot-overview\/using-the-dialog-builder-tool\/#Bot_Action_Nodes\" target=\"_blank\" rel=\"noopener noreferrer\">refer here for details<\/a>.<\/li>\n<\/ol>\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<h2><span class=\"ez-toc-section\" id=\"Configure_Node\">Configure Node<\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Component_Properties\">Component Properties<\/span><\/h3>\n<p><b>Note<\/b>: The configurations you set up or edit in these sections reflect in all other dialog tasks that use this node.<\/p>\n<ol>\n<li>On the <strong>Component Properties<\/strong> tab, under the <strong>General Settings<\/strong> section, enter a <strong>Name<\/strong> and <strong>Display Name<\/strong> for the script node. Node names cannot have spaces.<\/li>\n<li>Under the <b>Script Definition<\/b> section, click <strong>Define Script<\/strong> to add JavaScript.<\/li>\n<li>On the <b>Add Script<\/b> dialog box, enter your JavaScript; then click<b> Save<\/b>. See below for JavaScript code examples.<\/li>\n<li>In the <strong>Variable Namespaces<\/strong> 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 VA. You can go with the task level settings or customize it for this node. For more information, refer to <a href=\"\/docs\/bots\/bot-settings\/bot-management\/managing-namespace\/\" target=\"_blank\" rel=\"noopener noreferrer\">Managing Namespace<\/a>.<\/li>\n<\/ol>\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<h2><span class=\"ez-toc-section\" id=\"JavaScript_Examples\">JavaScript Examples<\/span><\/h2>\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\/26\/DIYDialogScriptNodeAddScriptDialogTypeAhead.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-11919\" src=\"\/wp-content\/uploads\/sites\/26\/DIYDialogScriptNodeAddScriptDialogTypeAhead.png\" alt=\"Type Ahead Context Object\" width=\"473\" height=\"401\" \/><\/a><\/p>\n<h4>Manipulating Data<\/h4>\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<h4>Handling Conditions<\/h4>\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<h4>Handling Flow<\/h4>\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>[vc_raw_html]JTNDZGl2JTIwY2xhc3MlM0QlMjJ3LXBvc3QtZWxtJTIwcG9zdF9uYXZpZ2F0aW9uJTIwbGF5b3V0X3NpbXBsZSUyMGludl9mYWxzZSUyMiUzRSUwQSUwOSUzQ2ElMjBjbGFzcyUzRCUyMnBvc3RfbmF2aWdhdGlvbi1pdGVtJTIwb3JkZXJfZmlyc3QlMjB0b19wcmV2JTIyJTIwaHJlZiUzRCUyMiUyRmRvY3M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TNDJTJGc3BhbiUzRSUzQyUyRmRpdiUzRSUwQSUzQ3AlM0UlM0MlMkZhJTNFJTNDJTJGZGl2JTNF[\/vc_raw_html]<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><\/div><\/section>\n","protected":false},"excerpt":{"rendered":"A 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...","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\/v9-2\/wp-json\/wp\/v2\/posts\/17957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.kore.ai\/v9-2\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/developer.kore.ai\/v9-2\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/developer.kore.ai\/v9-2\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/developer.kore.ai\/v9-2\/wp-json\/wp\/v2\/comments?post=17957"}],"version-history":[{"count":11,"href":"https:\/\/developer.kore.ai\/v9-2\/wp-json\/wp\/v2\/posts\/17957\/revisions"}],"predecessor-version":[{"id":41620,"href":"https:\/\/developer.kore.ai\/v9-2\/wp-json\/wp\/v2\/posts\/17957\/revisions\/41620"}],"wp:attachment":[{"href":"https:\/\/developer.kore.ai\/v9-2\/wp-json\/wp\/v2\/media?parent=17957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.kore.ai\/v9-2\/wp-json\/wp\/v2\/categories?post=17957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.kore.ai\/v9-2\/wp-json\/wp\/v2\/tags?post=17957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}