{"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":"2021-08-04T07:34:47","modified_gmt":"2021-08-04T06:34:47","slug":"working-with-the-script-node","status":"publish","type":"post","link":"https:\/\/multisite.korebots.com\/v9-0\/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 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>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<\/div><\/div><div class=\"wpb_text_column\"><div class=\"wpb_wrapper\"><p>When you test a dialog in the bot builder, 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\"><\/span>Set up a Script Node<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Setting up a script node in a dialog task involves the following steps:<\/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=\"Set-Up\"><\/span>Set-Up<span class=\"ez-toc-section-end\"><\/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 Bot Action node. For details on the Bot 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\"><\/span>Add Node<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ol>\n<li>Open the dialog task to add the Script node.<\/li>\n<li>Add 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<\/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=\"Configure_Node\"><\/span>Configure Node<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Component_Properties\"><\/span>Component Properties<span class=\"ez-toc-section-end\"><\/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 bot. 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<\/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=\"JavaScript_Examples\"><\/span>JavaScript Examples<span class=\"ez-toc-section-end\"><\/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\/22\/DIYDialogScriptNodeAddScriptDialogTypeAhead.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-11919\" src=\"\/wp-content\/uploads\/sites\/22\/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<\/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>[vc_raw_html]JTNDZGl2JTIwY2xhc3MlM0QlMjJ3LXBvc3QtZWxtJTIwcG9zdF9uYXZpZ2F0aW9uJTIwbGF5b3V0X3NpbXBsZSUyMGludl9mYWxzZSUyMiUzRSUwQSUwOSUzQ2ElMjBjbGFzcyUzRCUyMnBvc3RfbmF2aWdhdGlvbi1pdGVtJTIwb3JkZXJfZmlyc3QlMjB0b19wcmV2JTIyJTIwaHJlZiUzRCUyMiUyRmRvY3MlMkZib3RzJTJGYm90LWJ1aWxkZXItdG9vbCUyRmRpYWxvZy10YXNrJTJGd29ya2luZy13aXRoLXRoZS1zZXJ2aWN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w_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><\/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":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/posts\/17957"}],"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=17957"}],"version-history":[{"count":22,"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/posts\/17957\/revisions"}],"predecessor-version":[{"id":29415,"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/posts\/17957\/revisions\/29415"}],"wp:attachment":[{"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/media?parent=17957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/categories?post=17957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/multisite.korebots.com\/v9-0\/wp-json\/wp\/v2\/tags?post=17957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}