Javascript
Last updated
Last updated
Here you have the possibility to manipulate the context information of the conversation and its database using Javascript. It is ideal to set more advanced rules that would not be possible using the "Filter" step.
We mentioned a lot here about database, custom objects and records. So if you want to understand better access the Data section.
Let's mention below the features available when using code:
During the conversation flow, information about the context of the conversation can be saved in one of these steps or actions:
Text or Card Message:
By 'Save Reply', the information can be a field of the User object or some other custom object. If it is the user the information will be saved directly in the database, otherwise it will be stored temporarily as a variable of the context of the conversation, being accessible through the name of the object followed by the name of the field, for example: adress.zip
Intentions:
In the case of NLP, I have some intention matching, entities of this intention will be temporarily stored as context variables of the conversation, being able to be accessed added 'context.objetcs.' the name of the variable of the intentions, as an example: context.objetcs.meal
You have the option here to query custom object records queries. To make them available during code execution. The result will be returned as array type. To access the result of a query simply add '__s' to the right side of the object name, for example: request__s
For now the query execution options are:
Record Limit: responsible for limiting the number of records returned by the query.
Condition of Time: if the number of records in the query is greater than the limit, it defines a return order of the records: for more recent or older.
In addition to the custom fields each query record will have the following system fields available:
id: Record ID
obj_id: Record Object ID
parents: controller object of the current object (or parent of the current object)
parents.id: Controller object ID
parents.name: Controller object name
grandparents: Controller object of the controller object of the current object (or grandfather of the current object)
grandparents.id: Controller object ID
grandparents.name: Controller object name
createdAt: Time (in milliseconds) of when the record was created.
updatedAt: Time (in milliseconds) of when the record was updated.
Here's an example of how to handle the query return:
By default the User object record for the current user and Chat Context variables will always be available. So you do not have to query for the User object. To access the current user ID system field, just use: user.id. And to access, for example, a context variable called current_product just use: context.objetcs.current_product
Any change in the value of the custom fields of these objects will be reflected in the database after the code has been executed.
You can also create new custom object records. Just set these records as javascript object type (JSON format). Remember to fill in the required fields. And when you have ready, add them to the respective object that can be used as follows: Object Name + '__c', for example: item_of_order_c
Here is an example of how to create a record:
If you are creating a record of an object that has a Controller object (that is, it has a relationship of 1 to N), you need to enter the ID of this controller record in its field. Otherwise this record will not be created.
There is also the option to exclude custom object records. Just do the query, as mentioned above, take the id of the records that should be deleted, and add them to the respective object that can be used as follows: Object name + '__d', eg: request__d
Here's an example of how to delete a record:
If you are deleting a record of an object that has some Controlled object below it (that is, it has a relationship of 1 to N), all records controlled by it will be deleted as well.
There is an alternative to sending messages to the user using Javascript code. It is important for cases, for example, where message sending depends on available information after a query has been performed. Here's an example of how to send a message:
Below is the information you need to provide to create a text message. When used in the code it must be with the JavaScript object type (JSON format).
type: always put 'text'. Required.
text: enter the text you want to send here. Required and 320 character limit.
quick_replies: array with information of each quick reply. Not mandatory and limit up to 10 replies.
caption: text that appears visible to the user. Required and limit of up to 20 characters.
type: can be 'text' representing simple text. Or it can be 'context_variable' representing a JavaScript object type containing information that can be accessed during the time of the current conversation context. Required.
value: depends on the type above, if it is 'text', fill in the text here. If 'context_variable' fill in the object in JSON format here. Required.
Here we have the option to send messages that contain cards.
Below is the information you need to provide to create a greeting card message. When used in the code it must be with the JavaScript object type (JSON format).
type: always put 'cards'. Required.
cards: array with information from each card. Required.
title: title of the card. Required and limit of up to 80 characters.
subtitle: subtitle. No Mandatory and limit of up to 80 characters
image_url: card image url. No Required and limit of up to 120 characters.
buttons: array with information for each button. No Mandatory and up to 3 button limit.
caption:text that appears visible to the user. Required and limit of up to 20 characters
type: can be 'url' or 'context_variable' representing a JavaScript object type containing information that can be accessed during the time of the current conversation context. Required.
value: depends on the type above, if it is 'url', fill here the valid url. If 'context_variable' fill in the object in JSON format here. Required.
There is a 15-second execution time limit of the javascript code. So if you pass this limit the execution of the code will be interrupted and an error will be returned that can be consulted in the conversation history.
You can send media via JS using the template below: