How do I set up a Webhook for WhatsApp notifications?
What is a Webhook?
To communicate between two systems, in our case it can be your WhatsApp bot and Chat API, we need to configure their interaction so that information about status changes in one is available to the other. For example, tracking changes in the status of messages-sent, delivered and read.
Working through the API, you have the ability to send GET and POST requests to track changes, but this approach is not very convenient, since you will need to send requests with a constant frequency, which still does not exclude the risk of finding out about status changes too late. That is why there is a webhook, by configuring which you will receive notifications for events. Depending on the setting, you will receive notifications about messages, their statuses, the status of the instance, and even about missed calls. The webhook is called when an event occurs, so you don't need to send requests every time to find out, for example, about new messages or the status of your connected phone. These notifications will come automatically as soon as this event occurs.
You need to place your webhook script on the hosting and specify the URL address in your personal account. You can specify both the URL and the IP address.
Examples of Webhook implementation:
- An example of creating a WhatsApp bot in Java;
- WhatsApp bot with Google Tables API on NodeJS;
- Creating a Whatsapp bot in C#;
- Creating a Whatsapp bot on Node.js;
- Creating a Whatsapp bot in Python;
- Creating a Whatsapp bot in PHP.
These tutorials contain options for implementing a Webhook script in different programming languages, you can use them as examples when starting your Webhook server. How to specify and test a Webhook address using the Chat API tools, as well as more subtle aspects of configuring a Webhook, we will explain below.
How can I test the Webhook?
To check and test the operation of the Webhook and make sure that the server is sending data, we offer you two options:
1. Built-in testing tools for the ChatAPI personal account
To do this, go to the testing section in your personal account. To check the Webhook, we will be interested in two sections - "Webhook Simulation" and "Webhook Verification".
This tool allows you to simulate a Post request to the Webhook address you specified.
The address is set in the main menu of the selected instance.
In this case, we use our RequestBin for testing, you can also use it, it is created for this kind of tests. If your instance is already linked to an http or https URL for receiving notifications, for example, if you use the instance to work with the visual bot constructor, then the address will be substituted automatically.
To simulate a Post request, you must have a paid instance. You can select the necessary one in the upper part of the window. The simulator can send three types of requests – "A new message has been received", "Delivered", "Read". A separate ACK notification will be generated for each case.
To send a request, simply select an instance, set the address and click send. The answer will come immediately. If for some reason the data did not come, then most likely there were difficulties in the "Server – Endpoint" bundle. In order to check on which side the problems occurred and debug the work with the Webhook, we suggest using the section "Checking the Webhook".
Checking the Webhook
Webhook verification is a section where you can get acquainted with incoming requests from the server - directly with the data that will come to your http or https URL. Please note! When working with this tool, our RequestBin Webhook address will be automatically substituted. After testing is completed, it will change to the one specified earlier.
With the help of "Webhook Verification", you will be able to get acquainted with the data that will come to you from the server, as well as make sure that the requests are sent correctly. Use the "Webhook Simulator" or "Sending Messages" in the testing section to send requests. This is how the incoming message will look like :
and so the notification that the message was delivered:
There are several free resources that take over the possibility of receiving and processing data and from which you can take the url address for working with the Webhook. For testing, you can use a similar site – we recommend Webhook.Site because they often worked with him and were satisfied.
The principle of operation remains approximately the same. Copy the url and paste the address in your personal account. Save it. After that, you can start sending messages and information will be received from the server to the site.
When sending messages from the phone manually, ACK notification statuses will not be generated for these messages – "Sent"," Delivered","Read". Use the "Sending Messages" section in the testing tools to send messages.
Note that the "Sent-True" response means that the request was processed, and not the fact that the message reached the recipient. It is quite possible that the user did not receive the message, it is certainly possible to determine this through the ACK notification status.
This is how the message itself looks in the Webhook. Site
ASK status "Sent"
ASK status "Delivered"
ASK status "viewed"
As well as the status of the instance
How do I set up a Webhook? What requirements should it meet?
We can help to a lesser extent with setting up your Webhook server, please refer to the instructions for creating bots that we indicated above. The most important criterion for us is the answer of the 200th status. If our server receives this response, it means that everything was done correctly.
What information does the server transmit?
The amount of data sent by the service is quite voluminous. Let's analyze the main parameters.
The sent message:
ID – the ID of a specific message.
BODY – the main content of the message. There can be a text, a file, a link or an alert, for example - "Call-Miss".
FROMME – "True" - You have sent a message. "False" - incoming messages.
SELF-shows where the message was sent from. "0" - the message was sent from the phone, "1" - the message was sent from the API.
ISFORWARDED – the forwarded message. "0" - no," true " - the message being forwarded.
AUTHOR – the author of the message.
TIME – UNIX time. To decrypt it, you can use special converters.
CHATID - is the identifier of a specific dialog.
MESSAGENUMBER – the serial number of the message.
TYPE-a characteristic indicating the form of the message. This can be the text "Chat" or "File".
SENDERNAME – the name of the sender. The name from the user's phone book is specified, if the name is not specified, it will be a number.
CAPTION-the caption is usually attributed to photos.
QUOTED-the parameter of applications to the forwarded messages.
ACK-shows the ACK status that was at the time of processing the message. 1 – sent, 2 – delivered, 3 – viewed.
CHATNAME – the name of the chat that the user registered in WhatsApp.
INSTANCEID – the instance number.
The sent message:
Note that the ACK parameter is missing, because the ACK notification status is not generated for incoming messages.
authenticated - Authorization was completed successfully
- normal - keep your phone connected;
- phone - the phone is not connected;
- computer - attempt to connect to the phone;
- offline - the computer is not connected;
- battery_low - low phone battery;
- syncing - synchronization is underway.
init is the initial status. Enabling
loading-Loading, repeat after 30 seconds.
Additional Webhook Settings
There are a number of settings that affect the amount of data sent by the server, these settings can be changed through the documentation or partially through the main menu of the instance. Let's take a closer look at these parameters.
1-statusNotificationsOn-enables or disables the collection of data about changes in the status or sub-status of the instance. This parameter is used to track the status of the instance. For example, if there are problems with the connection, the server will send the data and you will quickly find out about it.
2-recognotificationson-enables the delivery of ACK notifications about the message status. Sent, delivered and read. With this data, you can build the work of the bot's logic and track the effectiveness of sending messages.
3-chatUpdateOn-changing the chat states.
4-videoUploadOn-enable or disable auto-upload of videos from messages. This information will be displayed in the incoming data from the server.
5-guaranteedHooks – in the standard version of the server, one attempt is made to get the 200th status. If something goes wrong, for example, WhatsApp on the phone goes into the background mode, then the hook will not be delivered. By enabling this parameter, the server will seek to send a hook, making up to 20 attempts.
6-parallelHooks – if you work with a large number of incoming hooks, they can form a queue for delivery. To get all the hooks at once, you can use this parameter. At the same time, please note that enabling this function will increase the load on your script for processing incoming Post requests from the server.
You can read all the Chat API documentation here.
Also, we recommend watching the video guide where we configure Webhook notifications: