Receive WhatsApp messages using PHP
How to Receive WhatsApp messages using PHP and webhook
Welcome to this tutorial, where we will guide you through the process of receiving WhatsApp messages using PHP and a Webhook. This is an incredibly valuable tool for businesses looking to automate their customer interactions on WhatsApp. Let's get started!
By the way, we have the source code of a great PHP chatbot: https://github.com/Whapi-Cloud/php-whatsapp-chatbot. It can handle incoming messages, try it!
Introduction
Webhooks are HTTP callbacks that receive data when a certain event occurs, making them perfect for receiving messages in real-time. In this guide, we will create a simple PHP script that will serve as our Webhook to receive incoming messages.
Detailed manual about webhooks here
Requirements
PHP: You will need PHP installed on your server or local machine. This tutorial will use PHP 7.4, but the code should work with other versions.
cURL extension: This PHP extension is used to make HTTP requests. Make sure you have this extension enabled.
Whapi.Cloud account: To interact with WhatsApp, we will use the Whapi.Cloud service. Sign up for an account if you haven't done so already.
Setup Webhook on Whapi.Cloud
Before we start coding, you will need to set up a Webhook URL on Whapi.Cloud.
Log in to your Whapi.Cloud account and go to your Channel Dashboard.
Click on 'Settings', then 'Webhook'.
Here, you can specify the URL where Whapi.Cloud should send the incoming messages. The URL should point to the PHP script we will create in the next step.
Save your changes.
When messages arrive, you can process them to generate automatic responses, log them in databases, or even forward them to other platforms.
PHP Script
When a new message arrives, Whapi.Cloud can forward it to your webhookURL. This URL should point to a PHP script on your server. The incoming data will be in JSON format.
Sample Message Format
Sample PHP Script to Process the Message:
Create a PHP file named webhook.php
in the root directory of your project. Below is the code for the webhook:
The provided PHP script processes incoming WhatsApp messages forwarded by the Whapi.Cloud service to a specified webhook URL. Upon receiving a message, the script extracts the sender's phone number (chat_id
) and the message content (text
). Depending on the message content, it can generate predefined responses, log the message to a file, or be extended to save messages to a database, send email notifications, and more.
Extending the Script:
Database Logging: Use the PHP Data Objects (PDO) extension to connect to your database and store the incoming messages.
Email Forwarding: Integrate with PHPMailer or the built-in
mail()
function to forward messages to an email address.Automatic Responses: Set up predefined responses based on the incoming message's content.
Integration with Other Systems: You can forward the message to CRM systems, ticketing systems, or any other platform your business uses.
Conclusion
That's it! You've now set up a PHP webhook to receive incoming WhatsApp messages. This powerful tool can significantly streamline your communication process, especially when handling high volumes of customer inquiries.
Feel free to experiment with your new setup and explore the possibilities. And if you encounter any issues or have any questions, don't hesitate to get in touch. We're always here to help!
Last updated