39min

Script Node

1. Introduction

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.

2. 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. 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.

3. 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

4. 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.

5. 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

NOTE: 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. 

5.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.

JS
|

5.2 Send Message with options

1.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.



JS
|

2. 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’

JS
|

Note: Above list of the dynamic dropdown will only work for website chatbots, for other channels it will be shown as buttons

3. 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,



JS
|
Document image

The below script will show options as an interactive button on Whatsapp for 360dialog users,

JS
|
Document image
Document image

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.

JS
|

5.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.



JS
|

5.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

Document image

3. Copy the value beside the option Path Key into the script

Document image



JS
|



5.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

Document image

3. Copy the value beside the option Path Key into the script



JS
|



5.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

JS
|

Subtraction

JS
|

Multiplication

JS
|

Division

v)

JS
|
JS
|

Explanation:

The 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:

  1. Title: This will be the text that’s displayed on the button of the carousel
  2. Type: One can either choose the type to be postback, which will trigger a path or web_url which will open a web page.
  3. 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 –

  1. To define the attributes in the “Script node” you will have to use the # button.
  2. attribute1 and attribute2 are for sample reference.



Updated 05 Jul 2022
Did this page help you?
Yes
No