Receive WhatsApp messages using Node js
How to Receive WhatsApp messages using Node.js and webhook
Last updated
How to Receive WhatsApp messages using Node.js and webhook
Last updated
Welcome to our hands-on tutorial on setting up your server to receive WhatsApp messages using Node.js and a webhook. This method can vastly simplify your interaction with customers and make your business more responsive. Let's dive in!
By the way, we have the source code of a great NodeJS chatbot: https://github.com/Whapi-Cloud/nodejs-whatsapp-chatbot. It can handle incoming messages, try it!
Webhooks are the perfect tool for getting real-time updates, as they send data whenever a particular event happens. In this guide, we'll walk you through crafting a simple Node.js app to serve as your webhook, which will receive incoming WhatsApp messages.
Detailed manual about webhooks here
Node.js: Ensure you have Node.js installed on your server or local machine. This tutorial uses Node.js v14.16.1, but the code should be compatible with other versions.
Express: Express is a web application framework for Node.js that we will use to handle incoming HTTP requests. You can install it using npm (Node.js package manager) with the command npm install express
.
A Whapi.Cloud Account: We'll be using the Whapi.Cloud service to interact with WhatsApp. If you're not registered already, sign up here.
Before we delve into coding, set up a webhook URL on Whapi.Cloud by following these steps:
Log in to your Whapi.Cloud account and navigate to your Channel Dashboard.
Select 'Settings', then 'Webhook'.
Specify the URL where Whapi.Cloud will send the incoming messages. This should point to the Node.js app we're about to create.
Don't forget to save your changes.
When utilizing the Whapi.Cloud WhatsApp API and configuring a webhook URL, incoming messages are delivered in a well-defined JSON structure.
Sample Node.js Script
Let's create a new file named webhook.js
in your project's root directory. Below is the code for your webhook:
The Node.js script, using the Express framework, initiates a server ready to accept incoming POST requests on the /webhook
endpoint. Upon receiving a request, indicating a new WhatsApp message, the script deduces the sender's phone number and the content of the message. While the current script logs this information, the code can be enrichedโfor instance, by merging with a database or devising automated response logic centered on the message's essence.
For this script to function, make sure you have Express and body-parser installed in your Node.js environment:
Run the script to spark the server, which, by default, will be accessible at http://127.0.0.1:3000/
. Configure your webhook URL to route to this endpoint to start processing incoming messages seamlessly.
Congratulations! You've successfully configured a Node.js webhook to accept incoming WhatsApp messages. By integrating this powerful tool, you'll make your customer interaction processes more efficient, particularly when dealing with large volumes of messages.
Feel free to tinker with your new setup and discover its potential. If you stumble upon any issues or have any questions, don't hesitate to reach out. Our team is always ready to assist!