In this tutorial we will learn how to send WhatsApp messages in Laravel.
Integrating the WhatsApp API into Laravel
You can use a package laravel-chat-api
in Laravel to send WhatsApp messages. You can easily communicate with the WhatsApp API with this package.
Follow below steps:-
Step 1- Install the Package
Install the laravel-chat-api
package using Composer:
composer require amirsanni/laravel-chat-api
Step 2- Configure the Package
After installing, you may need to publish the configuration file
php artisan vendor:publish --provider="AmirSanni\ChatApi\ChatApiServiceProvider"
This command will create a chat-api.php
file in the config directory.
Step 3- Set Up WhatsApp API Credentials
Open the config/chat-api.php
file and set your WhatsApp API credentials:
return [
'base_url' => env('CHAT_API_BASE_URL', 'https://api.chat-api.com/instance123/'),
'token' => env('CHAT_API_TOKEN', 'your-chat-api-token'),
];
Step 4- Send WhatsApp Message
Now you can send WhatsApp message from within your Laravel application using the ChatApi
facade:
use AmirSanni\ChatApi\Facades\ChatApi;
// ...
$message = [
'phone' => 'whatsapp_number',
'body' => 'Your message here',
];
$response = ChatApi::sendMessage($message);
// Handle the response as needed
Make sure to replace whatsapp_number
with the actual WhatsApp number you want to send the message to.
Step 5- Handling Responses
The response from the API call will provide information about the sent message. You need to handle the response according to your requirements.