2.18 Nodes - Processing Nodes - Script Node Print

  • 0

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.

5.2 Send Message with options

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

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

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

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

Document image

In the above image, following options are available that can be used as required

  • Default - 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

Force validation

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

Document image

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

Document image

Document image

5.2.5 Dynamic Send Message with options with Array type attribute

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

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

5.3 Send Carousel

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.

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.

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

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


Was this answer helpful?

« Back