WhatsApp API, the Webhook

How to receive messages via WhatsApp APIs

Published by Vanessa - June 21 , 2019

Receive WhatsApp messages via APIs

How to be notified about new messages with Webhooks.

If you're looking for an easy solution to manage WhatsApp messages with the APIs, then you've come to the right place.

Let's try to understand what we are talking about...
Everyone knows WhatsApp, but not everyone knows how to send and receive messages via the WhatsApp API. We at Mercury know this well, which is why we want to provide a tool for automatically sending and receiving WhatsApp messages.

mercury-app-whatsapp-api-message-webhook

Let's see how to do it by using Mercury platform. 🤔

First of all, you need to get the API token. Log in to Mercury and reach the Developer section. Then enable the REST API service to get the token.
Let's see this step in the image below.

mercury-app-whatsapp-api-enable

mercury-app-whatsapp-api-token

Authentication

To start the authentication you have to pass the api_token parameter in the query string parameter. You can find the token API within the dashboard.

mercury-app-whatsapp-api-authentication

How to receive a WhatsApp message through the Webhook. ⚓️

Use the webhooks to be notified about message, ack or chat update that happen in a instance_whatsapp. You can set a http or https url and Mercury can sends the event-object, via HTTP POST request to any endpoint URLs that you have defined in your account’s Webhooks settings.
The webhook event object have this structure:

{
"type":"ack",
"data":{...},
"time":1559062461,
"instance_number":"11223"
}

You can try the webhook with the service https://webhook.site
Copy your unique URL that was created:

mercury-app-whatsapp-try-webhook

Open Mercury dashboard in the Developer menu and set the webhook url.

mercury-app-whatsapp-get-webhook-url

Now you can see the notification of new message and the ack in the webiste https://webhook.site

Let's look at an example of the API calls in NodeJs 👇

Get webhook Url.

var request = require("request");

var options = { method: 'GET',
url: 'https://api.mercury.chat/sdk/v1/whatsapp/webhook',
qs: { api_token: 'tokenAPI' } };

request(options, function (error, response, body) {
if (error) throw new Error(error);

console.log(body);
});

Set webhook Url.

var request = require("request");

var options = { method: 'POST',
url: 'https://api.mercury.chat/sdk/v1/whatsapp/webhook',
qs: { api_token: 'tokenAPI' },
headers: { 'Content-Type': 'application/json' },
body:
{ is_webhook_enable: true,
webhook_url: 'https://webhook.site/0d45ed30-17f3-4768-8548-6c5493bd029e' },
json: true };

request(options, function (error, response, body) {
if (error) throw new Error(error);

console.log(body);
});

Remember that the Mercury APIs are available in your favorite programming language. For example, Java, C #, Ruby, Go, Python and many others. Find out all the features of Mercury's WhatsApp API.