Custom Code
Last updated
Last updated
This node gives you the ability to create custom code in JavaScript and launch it whenever this node is triggered. You can use it to manipulate and alter a selected parameter value.
For example, adding the country code to a selected phone number or a change the date of a conversation.
You can manipulate values collected by the agent during the conversation as well as information sent from a webhook.
The AI Studio uses the JS-Interpreter
package to run the JavaScript code. Link to its documentation and its limitations here.
The returned value of the custom code is stored in the Output Parameter. The results should be only primitive types ( 'number', 'string', 'boolean'). You can use the same parameter as before, the value will be simply overridden.
In order to use the custom code node, you will need some knowledge of JavasScript. But don't worry, even if you don't, feel free to use some of the examples from our library below or consult with a JS developer.
Take the example of an insurance company handling a mortgage request. Having collected all details, they want to tell their customers when they can expect to receive a response.
For the sake of our example, they usually respond after 7 days. In the custom code node, we are using JS code that will add 7 days to the original conversation start date.
Step 1: Get conversation start date parameter value. In our case, this will be a value populated in the $DATE parameter. This parameter can be either filled by receiving the value from a third-party service or by the user during the conversation.
Step 2: Increase the value of conversation_start_date days by 7
Step 3: Return the value. The result of your JS code snippet will be stored in the output parameter. You can either choose a new parameter or the same one as before and override the previous value.
If you want to go back in time and subtract some days from the given date in $DATE, just switch the + out for - .
Feel free to copy-paste into your custom code node!
Description:
New Date = Date of right now (time you reach the node)
The custom code node will return '2022-03-15T14:50:22.520Z'
If you want to separate the date from the exact time, see below -
Description:
If you use “New Date” as indicated above, you will get '2022-03-15T14:50:22.520Z' returned as a value, if you want to only use the date, use this code snippet.
The custom code node will return '2022-03-15’
Description:
Use this code if you want to change the way that the date is being presented.
From 2022-03-16 to Wed Mar 16 2022
Use this code snippet if you want to change the way the time is being presented.
From 14:00:00 to 2:00 PM
Description:
Alter the way we present a sequence of numbers (e.g. relevant for phone numbers)
Before > 972-58-650-3020 ($phone1)
The custom code node will return 19725865053020 ($phone1Val)
Description:
Combine separate street name, street number, city, and state as a whole address with a comma ‘,’ in between
Before > fully separate values, e.g. from an API request > Benson Road ($streetname) 15 ($streetnumber) New York ($city) United States ($state)
The custom code node will return Beson Road 15, New York, United States
Description:
The original request shows many different entries of the same category. In our example, “orders”, each holding more information like “order_Number”, “order_Status”, etc, and this custom code counts how many orders there are. Imagine the API response as follows -
The custom code node will return "2" as the value of the output parameter.