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

1.1 Availability 

Script Node will be available in Business Plan.

Script node is available in Add a node modal’s Integration section.

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 behavior 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, use # 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 for 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 behavior. 

[Note: The response structure for JSON API is supported completely for the Script node process function object as well]

5. Script Node Usage for Flow Operations 

A script can do any of the following options – Sending a text message, Send a message with options set, setting attributes in the system for a user, changing the path flow execution by triggering a path. 

Below are the sample object structures for each of the cases listed above

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.

process({
  “data”: {
    “type”: “text”,
    “text”: “Hello World”
  }
});

5.2 Send Message with options

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 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”      
      }
    ]
  }
});

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 titlesubtitleimage_url and a set of buttons associated with it.

Each button object again can be of various types – web_urlpostback or phone_number. Based on the type, the payload value would be set up. Each button would also have a mandatory title.

process({
  “data”: {
    “type”: “carousel”,
    “templates”: [
      {
        “title”: “Card 1”,
        “subtitle”: “Card 1 subtitle”,
        “image_url”: “https://searchengineland.com/figz/800×450.jpg”,
        “buttons”: [
          {
            “payload”: “https://www.mybot.com.my”,
            “title”: “Visit page”,
            “type”: “web_url”
          }
        ],
      },
      {
        “title”: “Card 2”,
        “subtitle”: “Card 2 Subtitle”,
        “image_url”: “https://www.mybot.com.my/img/img.jpg”,
        “buttons”: [
          {
            “payload”: “flow_788224AF8006422BA5E587720DE3B252”,
            “title”: “Trigger path”,
            “type”: “postback”
          }
        ],
      }
    ]
  }
});

5.4 Set Attributes

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”: “dob”,
      “value”: “03/06/1988”
    }
  ]
});

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

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);

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 field under the data object, give the path key which you want to trigger and 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

process({
  “data”: {
    “path_to_follow”: “02C000394F6C4B26B5F534A789D10EC0”
    “path_to_follow_node”: “Send Message”
  }
});

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.

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
}
}
)

Note –
1. To define the attributes in the “Script node” you will have to use the # button.

2. The attribute1 and attribute2 are for sample reference.

Was this article helpful?

Related Articles

Leave A Comment?