Javascript

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:

Conversation Context Variables

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

Object Query and Record Update

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:

// Sums the total value of the last requests of the current user
var total = 0;
for (var x in pedido__s){
   // Access one order at a time
   var pedido = pedido__s[x];

   // Within each order, accesses the Value field and adds to Total
   total += pedido.valor;
}

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.

Record Creation

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:

// Declares the variable that will have the information of the new Order Item
var novo_item_pedido = {}

// Fill in the fields of the new record
novo_item_pedido.numero = '12345';
novo_item_pedido.produto = context.objetcs.produto_id;
novo_item_pedido.quantidade = context.objetcs.produto_quantidade;
novo_item_pedido.valor = context.objetcs.produto_valor;

// Fill in the controller field that came from the querym in this case the Order ID
if (pedido__s[0]){
   novo_item_pedido.pedido = pedido__s[0].id;

   // Adds the new record to be created
   item_do_pedido__c.push(novo_item_pedido);
}

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.

Deletion of Records

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:

// Scrolls all requests returned by the query
for (var i in pedido__s){

   // Identifies the request with Status Incomplete
   if (pedido__s[i].status === 'incompleto'){

      // Adds the record ID to be deleted
      pedido__d.push( pedido__s[i].id );
   }
}

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.

Send Text Message

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:

// Defines a variable with the information needed to send the message
var message_1 = {
   type: 'text',
   text: 'O total do seu pedido atual é R$ ' + context.objetcs.pedido_total,
   quick_replies: [{
      caption: 'Confirmar',
      type: 'context_variable',
      value: {pedido_confirmado: context.objetcs.pedido}
   }]
};

// Add message to be sent
send_message.push(message_1);

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.

Card Message Submission

Here we have the option to send messages that contain cards.

// Defines a variable with the information needed to send the message
var message_2 = {
   type: 'cards',
   cards: [{
      title: 'Opção 1',
      subtitle: 'Detalhes da opção',
      image_url: '
https://cosmobots.io/static/media/testing.png
',
      buttons: [{
         caption: 'Selecionar',
         type: 'context_variable',
         value: {produto_selecionado: produto__s[0].id}
      },{
         caption: 'Acessar Detalhes',
         type: 'url',
         value: '
www.teste.com
'
      }]
   }],
};

// Add message to be sent
send_message.push(message_2);

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.

Uploading Images / Attachments / Videos

You can send media via JS using the template below:

// Defines a variable with the information needed to send the message
var message_1 = {
   type: 'text',
   text: 'Muito obrigado, ' + user.first_name,
};
var message_2 = {
   type: 'text',
   text: 'Use esse QR code para entrar no evento:',
};
var message_3 = {
   type: 'media',
   mime_type: 'image/png',
   url: user.qr_code
};
// Add message to be sent
send_message.push(message_1);
send_message.push(message_2);
send_message.push(message_3);

Last updated