{"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-25T17:04:21","modified_gmt":"2021-03-25T17:04:21","slug":"working-with-the-script-node","status":"publish","type":"post","link":"https:\/\/developer.kore.ai\/v8-0\/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 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 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\">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<h3><span class=\"ez-toc-section\" id=\"Add_a_Script_Node_to_the_Dialog_Task\">Add a Script Node to the Dialog Task<\/span><\/h3>\n<ol>\n<li>Open the Dialog Task in which you want to add the Script node.<\/li>\n<li>Click the<b> <\/b><img loading=\"lazy\" decoding=\"async\" class=\"icon alignnone wp-image-9160\" src=\"\/wp-content\/uploads\/sites\/16\/DIYDialogTaskPlusIcon.png\" alt=\"diydialogtaskplusicon\" width=\"15\" height=\"15\" \/> icon next to the respective node.<\/li>\n<li>Select <b>Script <\/b>&gt; <b>New script node<\/b>.\u00a0 You can also select a pre-defined node from the available list.<br \/>\n<a ref=\"magnificPopup\" href=\"\/wp-content\/uploads\/sites\/16\/Navigating.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13756\" src=\"\/wp-content\/uploads\/sites\/16\/Navigating.png\" alt=\"\" width=\"644\" height=\"302\" \/><\/a><\/li>\n<li>The Script window is displayed with the <strong>Component Properties<\/strong> tab selected by default.<\/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=\"Configure_the_Component_Properties\">Configure the 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.<br \/>\n<a ref=\"magnificPopup\" href=\"\/wp-content\/uploads\/sites\/16\/Script.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-13757\" src=\"\/wp-content\/uploads\/sites\/16\/Script.png\" alt=\"\" width=\"554\" height=\"432\" \/><\/a><\/li>\n<li>In the <strong>Variable Namespaces<\/strong> section (v8.0), 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\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=\"Configure_the_Connections_Properties\">Configure the Connections Properties<\/span><\/h3>\n<p>From the node\u2019s Connections tab, you can determine the 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 the below steps:<\/p>\n<ol>\n<li>On the Script window, click the <strong>Connections<\/strong> tab.<\/li>\n<li>Under the <strong>Connection Rules<\/strong> section, you can select a node from the <strong>Default<\/strong> drop-down list.<\/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: Exists, equals to, greater than equals to, less than equals to, not equal to, greater than, and less than. Select the entity, operator using the respective drop-down lists, and type the number in the <b>Value <\/b>box. For 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: Exists, equals to, greater than equals to, less than equals to, not equal to, greater than, and less than. For example,<b>\u00a0<\/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 matches the next user utterance.<\/li>\n<\/ol>\n<\/li>\n<li>In the <b>Then go to<\/b> 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 <em>If<\/em> conditions, click <strong>Add Else If<\/strong> below the last <em>If<\/em> 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. 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\/16\/DIYDialogScriptNodeAddScriptDialogTypeAhead.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-11919\" src=\"\/wp-content\/uploads\/sites\/16\/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>\r\n\t<div class=\"wpb_text_column \">\r\n\t\t<div class=\"wpb_wrapper\">\r\n\t\t\t<div class=\"w-post-elm post_navigation layout_simple inv_false\">\n\t<a class=\"post_navigation-item order_first to_prev\" href=\"https:\/\/developer.kore.ai\/v8-0\/docs\/bots\/bot-builder-tool\/dialog-task\/working-with-the-service-node\/\"><\/p>\n<div class=\"post_navigation-item-arrow\"><\/div>\n<div class=\"post_navigation-item-meta\">Previous<\/div>\n<div class=\"post_navigation-item-title\"><span>Service Nodes<\/span><\/div>\n<p>\t<\/a><br \/>\n<a class=\"post_navigation-item order_second to_next\" href=\"https:\/\/developer.kore.ai\/v8-0\/docs\/bots\/bot-builder-tool\/dialog-task\/working-with-the-agent-transfer-node\/\"><\/p>\n<div class=\"post_navigation-item-arrow\"><\/div>\n<div class=\"post_navigation-item-meta\">Next<\/div>\n<div class=\"post_navigation-item-title\"><span>Agent Transfer Nodes<\/span><\/div>\n<p>\t<\/a><\/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><\/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\/v8-0\/wp-json\/wp\/v2\/posts\/17957","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developer.kore.ai\/v8-0\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/developer.kore.ai\/v8-0\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/developer.kore.ai\/v8-0\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/developer.kore.ai\/v8-0\/wp-json\/wp\/v2\/comments?post=17957"}],"version-history":[{"count":6,"href":"https:\/\/developer.kore.ai\/v8-0\/wp-json\/wp\/v2\/posts\/17957\/revisions"}],"predecessor-version":[{"id":26216,"href":"https:\/\/developer.kore.ai\/v8-0\/wp-json\/wp\/v2\/posts\/17957\/revisions\/26216"}],"wp:attachment":[{"href":"https:\/\/developer.kore.ai\/v8-0\/wp-json\/wp\/v2\/media?parent=17957"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/developer.kore.ai\/v8-0\/wp-json\/wp\/v2\/categories?post=17957"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developer.kore.ai\/v8-0\/wp-json\/wp\/v2\/tags?post=17957"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}