Alerts Panel - User Guide
...
Nodes
Process and Flow Nodes
Script Node
28 min
the script node allows you to insert custom javascript code which will get executed during the path execution this will allow you to build complex flows with logical operations, arithmetic computations, data processing/transformation, etc all this without the need of having a separate backend infrastructure to run the code execution happens on the platform itself as an output of the script node, you can send a text message, carousel or a message with options prompt also, you can branch the path flow execution based on the output of the script execution setting attributes is another helpful thing you can do with the script node so that the execution output can be used in subsequent flows usage notes usage notes javascript code of up to 2500 characters in length, can be entered in the node and executed the process function which is provided by the system to process the output of the script can be used for determining how the behaviour of the node would be all cases are detailed in the dedicated process function section below this method would be expected to be present in the script which is set up a time window of 10 seconds would be provided for the script execution if the script execution does not complete within this, it would be abruptly terminated the script should be syntactically correct and only scripts that execute successfully would be allowed to be saved (can validate the script using any one of the open source tools available such as programiz, onecompiler and nextleap) errors will be highlighted along with the message for scripts that contain an attribute, you can simulate the execution by passing a value from the console itself you can access the attributes also as part of the script like in other nodes the only difference here is to access the auto complete list of attributes and use the # character instead of the standard curly braces you cannot make external api calls using the script node it is recommended to use the json api for these and use the script node to process or manipulate the output as needed similarly, there would be other restrictions on function availability in the sandboxed environment standard libraries like math, date/time functions, string operations, etc are provided you can also set up an error message for the script node in case, the script execution fails due to either data issues or script failure, this message will be displayed to the user sample use cases sample use cases functionality which involves computation like – interest calculators, temperature converters, currency converters, etc evaluate multiple complex conditions and perform different actions or trigger different paths based on the evaluation string manipulation – concatenation, presence of a substring generate a random number for associating unique values for api calls, if needed get the current system date and time value and perform multiple operations based on that post processing, the transformation of data retrieved from api calls, string or numeric process function process function the process function is a system provided function that is used to convey a special meaning to the platform for processing the output of the script node it is a mandatory element of the script node and would typically be called at the end of the script execution it takes a single json object as a parameter that can be set up as per the intended behaviour script node usage for flow operations script node usage for flow operations a script can do any of the following options – sending a text message, sending a message with options set, setting attributes in the system for a user, or changing the path flow execution by triggering a path below are the sample object structures for each of the cases listed above e while copying the script from this document the quotation ” gets replaced by a left quotation mark “ due to which you face an issue ” invalid or unexpected token”, kindly replace the “ with ” and it should work fine 1 send a message 1 send a message the object has to be set up in the following manner encapsulated within the data object the type field is set to text and the text field will contain the actual text process({ "data" { "type" "text", "text" "hello world" } }); 2 send message with options 2 send message with options 2 1 dynamic options as buttons the type field would be msg options in this case it would contain another attribute – options that are an array of options that are to be displayed the options object will contain text and postback fields the postback field essentially would be the flow key to the path which gets triggered on click process({ "data" { "type" "msg options", "text" "choose an option", “options” \[ { "text" "option a", "postback" "flow 788224af8006422ba5e587720de3b252" }, { "text" "option b", "postback" "flow bbc5ce0c3cd94c2da43fd6f1ae13737a" } ] } }); 2 2 dynamic options as a dropdown list (only for website) to load options in the send message (with options), set a field with “dropdown” true – all the options will load in a dropdown format for the website chatbot and upon selecting the variables will be set with the required value in the below example, ‘option’ as a value will be set for attribute {{option selected}} when the user selects ‘option a’ process({ "data" { "type" "msg options", "text" "choose option", "dropdown" true, "options" \[{ "text" "option a", "postback" "data option selected=optiona" }, { "text" "tax invoice", "postback" "data option selected=optionb" } ] } }); note above list of the dynamic dropdown will only work for website chatbots, for other channels it will be shown as buttons 2 3 enforce validation for send message with options option to enforce validation is available on the send message with option node on path builder in portal in the above image, following options are available that can be used as required deafult ignore user input if it doesn’t match the options and continue validate once show a validation message once if user input doesn’t match with the options else just continue forces validation show validation message and block till a valid user input matching with the options is received to use this in a script kindly use key "validation mode" with values "validate once" and "force validation" below scripts can be used to create dynamic options and also enforce validation validate once process({ "data" { "type" "msg options", "text" "smo via script(validate once)", "validation mode" "validate once", "validation message" "kindly select from the provided options", "options" \[ { "text" "option 1", "postback" "data smo attr1=1" }, { "text" "option 2", "postback" "data smo attr1=2" } ] } }); force validation process({ "data" { "type" "msg options", "text" "smo via script(force validation)", "validation mode" "force validation", "validation message" "kindly select from the provided options to proceed", "options" \[ { "text" "option 1", "postback" "data smo attr1=1" }, { "text" "option 2", "postback" "data smo attr1=2" } ] } }); 2 4 switch between interactive buttons and a numerical list for whatsapp (360dialog) by default, all dynamic options loaded through script node and json api node will be displayed as interactive buttons for 360dialog users use the below script and toggle “is interactive list” as true or false the below script will show options as a numerical list on whatsapp for 360dialog users, process({ "data" { "type" "msg options", "text" "choose option", "dropdown" true, "is interactive list"\ false, "options" \[{ "text" "option a", "postback" "data option selected=optiona" }, { "text" "option b", "postback" "data option selected=optionb" } } ] } }); the below script will show options as an interactive button on whatsapp for 360dialog users, process({ "data" { "type" "msg options", "text" "choose option", "dropdown" true, "is interactive list"\ true, "option list header" "options", "section title" "select", "options" \[{ "text" "option a", "postback" "data option selected=optiona", "description" "select this for option a" }, { "text" "option b", "postback" "data option selected=optionb" } 2 5 dynamic send message with options with array type attribute following is the script for rendering dynamic send message (with options) post a json api call in the sample below {{models}} attribute is an array type attribute created from a json api nodes response this can be used for displaying options without any break point, but for better interface to be used in situations where options are limited var responsedata = {{models}}; var response="{ \\"data\\" { \\"type\\" \\"msg options\\",\\"text\\" \\"please select the home model\\",\\"options\\" \[]}}"; var parse obj = json parse(response); let i = 0; for (var index = i; index < responsedata length; index++) { if ( index ==i+10) { break; } data = parse obj\['data']; data\['options'] push({"text" ""+responsedata\[index] models name,"postback" "data selectedhomemodels="+responsedata\[index] models id}); } if (index==i+10) { parse obj\['data']\['options'] push({"text" "see more","postback" "data selectedhomemodels="+responsedata\[index] models id}); attributes=\[] attributes push({"name" "count", "value" index}); parse obj\['attributes']= attributes; } process(parse obj); 2\ following is the script for rendering dynamic send message (with options) post a json api call in the sample below {{models}} attribute is an array type attribute created from a json api nodes response this can be used for displaying options with a break point after 9 options it will show 10th option as 'see more' var responsedata = {{models}}; var response="{ \\"data\\" { \\"type\\" \\"msg options\\",\\"text\\" \\"please select the home model\\",\\"options\\" \[]}}"; var parse obj = json parse(response); let i = 0; for (var index = i; index < responsedata length; index++) { if ( index ==i+10) { break; } data = parse obj\['data']; data\['options'] push({"text" ""+responsedata\[index] models name,"postback" "data selectedhomemodel="+responsedata\[index] models id}); } if (index==i+10) { parse obj\['data']\['options'] push({"text" "see more","postback" "data selectedhomemodel="+responsedata\[index] models id}); attributes=\[] attributes push({"name" "count", "value" index}); parse obj\['attributes']= attributes; } process(parse obj); 2 6 block text input for sending message with option you can now create send message with option (smo) by blocking user text area by using script node, advance script node or json api node 2 6 1 script node start by going on to “builder” in the left side of the navigation bar click on "+add node" clicking on “+add node” will give you a pop up of all the nodes available for your account, under “process and flow nodes” click on “script” node in your custom script node code add the following code line to block the user input text area "block input text" true for example var options to show = \[{ 	 "text" "test option 1", 	 "postback" "response for test option 1" 	}, { 	 "text" "test option 2", 	 "postback" "response for test option 2" 	}] process({ "data" { "type" "msg options", "text" "please select an option", "options" options to show, "block input text" true }, "attributes" \[] }); 3 send carous 3 send carous el for the carousel, the type would be carousel and the individual cards would be part of the templates array each card object can have multiple elements like title, subtitle, image url and a set of buttons associated with it each button object again can be of various types – web url, postback or phone number based on the type, the payload value would be set up each button would also have a mandatory title vvar content = {{last}}; var templates = \[]; var attributes = \[]; for (var i = 0; i < content length; i++) { var template = {}; var element = content\[i]; template title = element act\[0]; template subtitle = element appeal details + 'appeal year ' + element appeal year + element citations\[0] + 'petitioner ' + element petitioner; var button = { title "read more", type "postback", payload "flow 30f9e7b2dfa4466c8a783ac579d2a903||data abcd=" + element main document id }; template buttons = \[button]; templates push(template); var attribute = { name "attribute name", value element act\[0] }; attributes push(attribute); } var resp = { data { type "carousel", templates templates } }; process(resp); //send carousel for sorted array var s = "{{selecteduserchoice}}"; const arrayofobjects = {{filmss}}; arrayofobjects sort((a, b) => (a name > b name) ? 1 1); const sortedarray = \[]; for (let i = 0; i < arrayofobjects length; i++) { if (i === 0 || arrayofobjects\[i] name !== arrayofobjects\[i 1] name) { sortedarray push(arrayofobjects\[i]); } } var newarray = sortedarray filter(obj => obj id === s); var templates = newarray map(element => { return { "title" element name, "subtitle" 'trailer' + element url trailer, "image url" 'https //www 3delavos com/imagens im/' + element id + ' jpg', "buttons" \[ { "title" "menu principal", "type" "postback", "payload" `flow 480e043fc2db4baabc58501ba5aef32d||data abcd=${element lojaid}` } ] }; }); var resp = { "data" { "type" "carousel", "templates" templates } }; process(resp); 4 set attribute 4 set attribute you can set up an attribute by using the attributes field which takes in an array of attribute objects each attribute object is a very simple object with name and value fields this field can be used in conjunction with the other sections as well process({ "data" { }, "attributes" \[ { "name" "attribute name", "value" "value″ } ] }); 5 trigger a path 5 trigger a path if you want to trigger a path dynamically using your script execution output, you can use the path to follow field under the data object and give the path key which you want to trigger this field can be used in conjunction with the other sections as well you can access the value of “path to follow” by navigating through the following steps – 1 select the path to which you want to redirect to 2\ click on the edit icon 3\ copy the value beside the option path key into the script { response={"data" { "path to follow" "82f04ddd54a645e9874b45e7b951d3e2", "path to follow node" "send message 1"}}; } process(response); 6 trigger a node 6 trigger a node if you want to trigger a node dynamically using your script execution output, you can use the path to follow and path to follow node fields under the data object, give the path key which you want to trigger and the node name you want to trigger in that path this field can be used in conjunction with the other sections as well you can access the value of “path to follow” by navigating through the following steps – 1 select the path to which you want to redirect to 2\ click on the edit icon 3\ copy the value beside the option path key into the script { response={"data" { "path to follow" "82f04ddd54a645e9874b45e7b951d3e2", "path to follow node" "send message 1"}}; } process(response); 7 arithmetic operations 7 arithmetic operations script node can be used to perform arithmetic operations you can collect the data from the users via the “request user data node” and use the attribute values in the script node the following are the sample scripts for addition, subtraction, division, and multiplication addition var a ={{attribute1}} var b = {{attribute2 }} var sum = a + b; process ( { "data" { "type" "text", "text" sum } } ) subtraction var a ={{attribute1}} var b = {{attribute2 }} var difference = a b; process ( { "data" { "type" "text", "text" difference } } ) multiplication var a ={{attribute1}} var b = {{attribute2 }} var product = a b; process ( { "data" { "type" "text", "text" product } } ) division var a ={{attribute1}} var b = {{attribute2 }} var quotient = a / b; process ( { "data" { "type" "text", "text" quotient } } ) 8 creating a dynamic carousel via multiple arrays 8 creating a dynamic carousel via multiple arrays var content = {{sample api}}; var rating={{opt}}; var templates = \[]; for(var i = 0; i < content length; i++) { var template = {}; var element = content\[i]; var r= rating\[i]; template\["title"] = element\["title"]; template\["subtitle"] = element\["subtitle"] + r\["rating"]; var button = {"title" "choose", "type" "postback", "payload" "flow 68642f7e3fb3421e9d399007e40a4346||data abcd="+i}; template\["buttons"]=\[button]; templates push(template); } var resp = {}; resp\["data"] = {"type" "carousel", "templates" templates}; process(resp); explanation two array objects {{sample api}} and {{opt}} are taken from two separate api responses an object by the name template is designed this object contains all the information that will be displayed on a single card of the carousel this object contains a title and subtitle, the values of which have been taken from the api response the object also contains buttons that have the following three fields title this will be the text that’s displayed on the button of the carousel type one can either choose the type to be postback, which will trigger a path or web url which will open a web page payload payload here contains the path key of the path that will be triggered upon clicking the button and an attribute by the name and which in this case will be assigned the number of the card that has been clicked on since we are displaying a carousel with multiple cards, we have created multiple template objects and stored them in an array by the name templates this array is then used to display all the cards on the carousel note – to define the attributes in the “script node” you will have to use the # button attribute1 and attribute2 are for sample reference logs for script node in api logs logs for script node in api logs an error can be thrown by the script node for several valid and invalid reasons in this case, you can track the variables along with the api log in this way, you can effectively debug the error