How to calculate the number of days between two selected dates (WhatsApp)

Ana Updated by Ana

In the following example we are going to select two dates and check the difference in days between them, for WhatsApp bots.

With Make's (Integromat's) new Custom JS module we can now seamlessly run a Javascript function during the flow, and return the value back to our user in WhatsApp without interrupting the conversation.

Before getting started, you'll need to sign up for Rapid API & subscribe to the Custom JS API

We will collect 2 dates, and use the Trigger Automation block to integrate with Make. In Make, we will perform the Javascript calculation and then, trigger 2 requests: one to save the result and the other to come back to the WhatsApp bot to continue the flow.

Here is how the flow is going to look:

  1. We will collect the start and end dates:

Save the user input as @start and @end variables:

  1. Initialize the variable @diff_days as an integer at 0. Then, we will update their value based on the user input, from Make.

  1. Set the @bot_id, it refers to the WhatsApp bot with the number you see from the URL of the browser. We will need it to make the HTTP request from Make to continue the flow in WhatsApp. In this example is: 1617844.

  1. Set the @node_ref variable, it will contain the reference of the block where the flow will continue after the integration is done.

  1. Set the Trigger Automation block with all the variables that will use in Make. To see in-depth how the Trigger Automation block works, please, check this article.

  1. Finally, create a Message block with the variable @diff_days, this will be the block the flow will continue after Make integration.

Let's now create a scenario in Make:

  1. Add a Custom Webhook to our Make scenario and copy the endpoint by clicking on "Copy address to clipboard".

  1. Now we'll go back to our Trigger automation block to add the endpoint:

Then Run once your scenario on Make and after that, test your Trigger Automation, you need to see a 200 status response:

  1. Come back to Make. We'll add the Custom JS next, and add our Rapid API key in the Custom JS module. From the Input field, you will be able to see all the variables collected by the Custom Webhook module:

In the Input we'll add @start, @end our Landbot variables separated by a comma.

And in the Javascript Code we'll add the following:

var day = 24 * 60 * 60 * 1000; var array = input.split(","); return Math.round(Math.abs((new Date(array[0]) - new Date(array[1])) / day));

That formula will take the Input values of the module and return the difference of days. Let's press OK.

  1. Now we'll add an HTTP module (Make a request) to set the variable. In the URL we'll add the endpoint to set the variable, with the @id we sent earlier from Landbot and the name of the variable we want to update, in our case 'diff_days'.
  • https://api.landbot.io/v1/customers/@id/fields/@diff_days/
  • Method we'll select PUT
  • In Headers add:
    - Name: Authorization. Value: Token XXXXXXXXXXX
    - Name: Content-Type. Value: application/json
Get your API Token from theΒ SettingsΒ section of the app

  • In Body type select 'Raw' then in content type select JSON
  • In 'Request Content' we'll add the following:

{

"type": "integer",

"extra": {},

"value":"@OUTPUT"

}

Output is the value coming from the JS module, so you'll need to select it from the dropdown that appears when you click in the body.

Let's press OK.

  1. Our Last module will be another HTTP (Make a request) to assign the user back to the bot.
  • https://api.landbot.io/v1/customers/@id/assign_bot/@bot_id/
  • We'll select the customer @id and @bot_id sent from Landbot, from the dropdown.
  • The method will be PUT again, and the Headers will be the same as the previous module.
  • In the body, we'll add the following, notice that @node_ref also comes from Landbot:

{

"launch": true,

"node": "@node_ref"

}

Let's press OK and test! - Be sure to activate the scenario!

How did we do?

Javascript in WhatsApp

Creating a Loop in WhatsApp

Contact