{"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-03-30T12:45:05","modified_gmt":"2021-03-30T11:45:05","slug":"working-with-the-script-node","status":"publish","type":"post","link":"https:\/\/developer.kore.ai\/v7-1\/docs\/bots\/bot-builder-tool\/dialog-task\/working-with-the-script-node\/","title":{"rendered":"Working with the 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 do the following:<\/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 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=\"Setting_up_a_Script_Node\">Setting 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<h3><span class=\"ez-toc-section\" id=\"Step_1_Adding_a_Script_Node_to_the_Dialog_Task\">Step 1: Adding a Script Node to the Dialog Task<\/span><\/h3>\n<ol>\n<li>Open the <strong>Dialog Task<\/strong> in which you want to add the Script node.<\/li>\n<li>Hover over a node next to which you want to add the node, and click the <strong>plus icon<\/strong> on it.<\/li>\n<li>Select <b>Script &gt; New script node<\/b>.\u00a0 You can also select a predefined Script Node.<br \/>\n<a ref=\"magnificPopup\" href=\"\/wp-content\/uploads\/sites\/12\/Navigating.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13756\" src=\"\/wp-content\/uploads\/sites\/12\/Navigating.png\" alt=\"\" width=\"644\" height=\"302\" \/><\/a><\/li>\n<li>The Component Properties panel opens.<\/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<h3><span class=\"ez-toc-section\" id=\"Step_2_Configuring_the_Component_Properties\">Step 2: Configuring the Component Properties<\/span><\/h3>\n<p><b>Note<\/b>: The configurations you set up or edit in these sections reflect in <span style=\"text-decoration: underline;\">all other dialog tasks<\/span> that use this node<\/p>\n<ol>\n<li>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>The <b>Add Script<\/b> dialog is displayed.<\/li>\n<li>Enter your JavaScript, and then click<b> Save<\/b>. See below for JavaScript code examples.<br \/>\n<a ref=\"magnificPopup\" href=\"\/wp-content\/uploads\/sites\/12\/Script.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13757\" src=\"\/wp-content\/uploads\/sites\/12\/Script.png\" alt=\"\" width=\"554\" height=\"432\" \/><\/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<h3><span class=\"ez-toc-section\" id=\"Step_3_Configuring_the_Connections_Properties\">Step 3: Configuring the Connections Properties<\/span><\/h3>\n<p>From the node\u2019s Connections panel you can determine which node in the dialog task to execute next. You can write the conditional statements based on the values of any Entity or Context Objects in the dialog task, or you can use intents for transitions.<br \/>\nTo setup Component Transitions, follow these steps:<\/p>\n<ol>\n<li>You can select from the available nodes under the\u00a0<strong>Default<\/strong> connections.<\/li>\n<li>To configure a conditional flow, click <b>Add IF<\/b>.<\/li>\n<li>Configure the conditional expression based on one of the following criteria:\n<ol>\n<li><b>Entity<\/b>: Compare an Entity node in the dialog with a specific value using one of these operators: <b>Exists<\/b>, <b>equals to<\/b>, <b>greater than equals to<\/b>, <b>less than equals to<\/b>, <b>not equal to<\/b>, <b>greater than<\/b>, and <b>less than<\/b>. Select the entity, operator using the respective drop-down lists, and type the number in the <b>Value <\/b>box. Example: <i>PassengerCount (entity) greater than (operator) 5 (specified value)<\/i><\/li>\n<li><b>Context<\/b>: Compare a context object in the dialog with a specific value using one of these operators: <b>Exists<\/b>, <b>equals to<\/b>, <b>greater than equals to<\/b>, <b>less than equals to<\/b>, <b>not equal to<\/b>, <b>greater than<\/b>, and <b>less than. <\/b>Example<b>: <\/b><i>Context.entity.PassengerCount<\/i> <i>(Context object) greater than (operator) 5 (specified value)<\/i><\/li>\n<li><b>Intent: <\/b>Select an intent that should match the next user utterance.<\/li>\n<\/ol>\n<\/li>\n<li>In the <b>Then go to<\/b> the drop-down list, select the next node to execute in the dialog flow if the conditional expression succeeds. For example, if the <i>PassengerCount (entity) greater than (operator) 5 (specified value), <\/i><b>Then go to <\/b><i>Offers (sub-dialog).<\/i><\/li>\n<li>In the <b>Else <\/b>drop-down list, select the node to execute if the condition fails.<\/li>\n<\/ol>\n<div class=\"alert alert-info\"><b>Note:<\/b> If you want to write multiple If conditions, click <strong>Add Else If<\/strong> below the last If conditional expression.<\/div>\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 or, 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, see <a href=\"\/v7-1\/docs\/bots\/bot-builder-tool\/dialog-task\/context-object\/\">Context Object<\/a>.<br \/>\n<a ref=\"magnificPopup\" href=\"\/wp-content\/uploads\/sites\/12\/DIYDialogScriptNodeAddScriptDialogTypeAhead.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-11919\" src=\"\/wp-content\/uploads\/sites\/12\/DIYDialogScriptNodeAddScriptDialogTypeAhead.png\" alt=\"Type Ahead Context Object\" width=\"473\" height=\"401\" \/><\/a><\/p>\n<h4>Examples 1: 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, see <a href=\"\/v7-1\/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>Examples 2: 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>Examples 3: Handling Flow<\/h4>\n<p>The next Script node code example validates the bank transfer amount 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><\/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 do the following: 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\/v7-1\/wp-json\/wp\/v2\/posts\/17957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.kore.ai\/v7-1\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/developer.kore.ai\/v7-1\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/developer.kore.ai\/v7-1\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/developer.kore.ai\/v7-1\/wp-json\/wp\/v2\/comments?post=17957"}],"version-history":[{"count":4,"href":"https:\/\/developer.kore.ai\/v7-1\/wp-json\/wp\/v2\/posts\/17957\/revisions"}],"predecessor-version":[{"id":23241,"href":"https:\/\/developer.kore.ai\/v7-1\/wp-json\/wp\/v2\/posts\/17957\/revisions\/23241"}],"wp:attachment":[{"href":"https:\/\/developer.kore.ai\/v7-1\/wp-json\/wp\/v2\/media?parent=17957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.kore.ai\/v7-1\/wp-json\/wp\/v2\/categories?post=17957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.kore.ai\/v7-1\/wp-json\/wp\/v2\/tags?post=17957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}