Using WhatsApp Business API Cloud with AWS Lambda.

Alejandro Castañeda Ocampo
Globant
Published in
6 min readOct 5, 2022
Click here to test the final project.

In this opportunity, we are going to implement a translator from Spanish to English WhatsApp bot, using the WhatsApp business API released by Meta in May 19th 2022, this API allows all people who have an account in Meta for developers to send WhatsApp message easy and faster with a friendly integration.

The following sections will be reviewed in this article:

  1. Create an application inside of Meta developer’s console.
  2. Attach the WhatsApp product.
  3. Send test message from Meta developers console.
  4. Build the webhook.
  5. Deploy the webhook as a lambda function.
  6. Configure the lambda function URL as a webhook in Meta developer’s console.
  7. Conclusions.
  1. Create an application inside of Meta developer’s console: https://developers.facebook.com/docs/development/create-an-app/?locale=es_LA This link provides all necessary information to create an application in Meta developer’s console.
Example application panel in meta developers platform, Pruebas 2 was the app created to this exercise.

2. Attach the WhatsApp product: select the created app and attach it to the WhatsApp option, you should find the WhatsApp product in the menu and then click in the Getting Started button, if everything is working fine, you should see the next similar screen:

On the left you should see the WhatsApp module, select getting started.

3. Send test message from Meta developer’s console: At this point, we will be able to send messages using the graphic interface of Meta, sending a WhatsApp default template message to test the functionality, meta gives us a test number to send the messages, this number has some limitations, but for the test environment it’s enough.

In this part, we can to send template message to test the functionality.

4. Build the webhook: For this goal we are going to use Node Js in lambda function to set up the webhook in Meta developer’s console, this way, all messages that the bot receives will be processed by the function, remember that the goal is to receive the Spanish messages, translate them to English and return the message to the user.

For the translation module, we will use a free library provided by NPM called Translate https://www.npmjs.com/package/translatte.

The code will be deployed in GitHub as public repository.

Webhook functionality to response translate message.

5. Deploy the webhook as a lambda function: To deploy the code shared above, we need to clone the repository, execute the npm install command, it’s so important to use the 14 or higher node version, then, when the dependencies are installed, we will compress the files (.zip) to upload by console to AWS lambda module (You will find the instructions below).

To deploy the lambda function inside AWS we have to navigate to the lambda functions module select the function section and next select create function, in the next page select the first option (Author from scratch), assign the function name, for the runtime I recommended select the Node.js 14x, the architecture select x86_64, in the advanced setting section, select enable function URL and for the auth type, select the none option, then select the option ‘create function’.

Now we are going to add an extra setting to grant the correct operation of the webhook, in the general settings section we’ll change the timeout for the execution, I consider that 30 seconds is enough, then in the environment variables section we need to add 2 variables:

  • VERIFY_TOKEN=VERIFY
  • WHATSAPP_TOKEN= This value, we can get it from the Meta console for developers.

Next we must upload the zip file with the code and libraries of lambda function, to achieve it, we must select the code section and select the ‘upload from’ and next select the ‘.zip option’ and select the zip file from our machine.

If the instructions shared above were confused, I’m going to add screenshots describing the steps.

5.1 Search the lambda module and go in.

5.2. Select the functions option and next select create function.

5.3. Select the first option (Author from scratch), assign the name, select the runtime and architecture.

5.4. In the advanced settings section select enable function url, for the auth type select none, finally select create function option.

5.5. Add extra configuration when the lambda is created, in the configuration section in general configuration, change the timeout to 30 seconds.

5.6. In the same settings section, click on Environment variables option and add the variables.

6. Configure the lambda function URL as a webhook in Meta developers console: To end the project and test the functionality, we just need to set up the lambda function as webhook in the Meta developers console, to achieve it, we will take the function URL and follow the next steps.

6.1 In Meta developer’s console, select the option ‘Configure Webhooks’.

6.2. In the input Callback URL, we will add the lambda function URL and add ‘/webhook’ endpoint, in the verify token type write ‘VERIFY’, this text will be equal to the environment variable defined in lambda function.

6.3. Next in the section webhook fields, select the manage option and subscribe the option messages.

Now our bot not only is available to send messages, but also it can listen for the user messages, translate the text and send a translated response, the chatbot will be able to interact with the user and send the response quickly.

Example of interaction with the bot.

7. Conclusions:

  • With the new release of Meta about the API cloud, that is open to all users, now is most easy to create WhatsApp chatbot without provider intermediary.
  • We can to use AWS Lambda as webhook, we don’t need to create any network infrastructure, only with a little configuration we’ll have an endpoint to process request like in this example.
  • Meta has Free tier conversations, the first 1,000 conversations your WhatsApp Business account has each month are free of charge. Free tier conversations can be business-initiated or user-initiated. If you do not use all 1,000 free conversations each month, the remaining conversations do not carry over to the following month.

Thanks for reading, and I hope that it has been to your liking.

--

--