| Install | |
|---|---|
composer require devsfort/laravel-whatsapp-chat |
|
| Latest Version: | v2.0.0-alpha |
| PHP: | >=8.1 |
A comprehensive WhatsApp integration package for Laravel with support for Meta WhatsApp Business API and WhatsAppJS Client, featuring real-time chat functionality, OTP verification, notifications, and attachment handling.
For WhatsAppJS Client mode: This package includes the Node.js gateway service in the whatsapp-client-gateway directory. You must set it up and run it before testing Client mode. See the Gateway Setup section below.
For Meta Business API mode: No additional services required - just configure your API credentials.
composer require devsfort/laravel-whatsapp-chat
php artisan whatsapp-chat:install
The installation command will:
Add your WhatsApp Business API credentials to .env:
WHATSAPP_MODE=business
# Meta WhatsApp Business API
WHATSAPP_ACCESS_TOKEN=your_access_token_here
WHATSAPP_PHONE_NUMBER_ID=your_phone_number_id
WHATSAPP_ADMIN_PHONE_NUMBER=+1234567890
WHATSAPP_WEBHOOK_VERIFY_TOKEN=your_webhook_verify_token
WHATSAPP_WEBHOOK_URL=https://yourdomain.com/webhook/whatsapp
WHATSAPP_USE_MOCK_MODE=false
Add your WhatsApp Client configuration to .env:
WHATSAPP_MODE=client
# WhatsAppJS Client (Node.js Gateway)
WHATSAPP_CLIENT_URL=http://localhost:3000
WHATSAPP_CLIENT_TOKEN=your_bearer_token_here
WHATSAPP_CLIENT_ACCOUNT=default
WHATSAPP_CLIENT_WEBHOOK_SECRET=your_hmac_secret_here
Note: For WhatsAppJS Client mode, you'll need to run the Node.js gateway service separately. See the WhatsAppJS Client Setup section.
Add the required fields to your users table migration:
Schema::table('users', function (Blueprint $table) {
$table->string('whatsapp_number')->nullable();
$table->boolean('whatsapp_verified')->default(false);
$table->timestamp('whatsapp_verified_at')->nullable();
$table->string('whatsapp_verification_code')->nullable();
$table->string('whatsapp_status')->nullable(); // active, unreachable, unknown
});
Add to your User model's $fillable array:
protected $fillable = [
// ... existing fields
'whatsapp_number',
'whatsapp_verified',
'whatsapp_verified_at',
'whatsapp_verification_code',
'whatsapp_status',
];
php artisan migrate
The package configuration is published to config/whatsapp-chat.php. Key settings:
return [
// Provider selection
'mode' => env('WHATSAPP_MODE', 'business'), // 'business' or 'client'
// Business API config
'access_token' => env('WHATSAPP_ACCESS_TOKEN'),
'phone_number_id' => env('WHATSAPP_PHONE_NUMBER_ID'),
'admin_phone_number' => env('WHATSAPP_ADMIN_PHONE_NUMBER'),
'webhook_verify_token' => env('WHATSAPP_WEBHOOK_VERIFY_TOKEN'),
'use_mock_mode' => env('WHATSAPP_USE_MOCK_MODE', true),
// Client config
'client' => [
'base_url' => env('WHATSAPP_CLIENT_URL', 'http://localhost:3000'),
'token' => env('WHATSAPP_CLIENT_TOKEN'),
'default_account' => env('WHATSAPP_CLIENT_ACCOUNT', 'default'),
'webhook_secret' => env('WHATSAPP_CLIENT_WEBHOOK_SECRET'),
],
// User model configuration
'user_model' => env('WHATSAPP_USER_MODEL', 'App\Models\User'),
'admin_field' => env('WHATSAPP_ADMIN_FIELD', 'type'),
'admin_value' => env('WHATSAPP_ADMIN_VALUE', 'admin'),
// Re-engagement settings
're_engagement' => [
'enabled' => env('WHATSAPP_RE_ENGAGEMENT_ENABLED', true),
'template_name' => env('WHATSAPP_RE_ENGAGEMENT_TEMPLATE', 'account_creation'),
],
];
The default mode uses Meta's official WhatsApp Business Cloud API.
Features:
Setup:
WHATSAPP_MODE=business in .envUses WhatsApp Web via a Node.js gateway service.
Features:
Setup:
WHATSAPP_MODE=client in .env✅ The WhatsAppJS Client gateway is included in this package in the whatsapp-client-gateway directory. You need to set it up and run it before testing Client mode.
The gateway is included in the package. Navigate to it:
cd vendor/devsfort/laravel-whatsapp-chat/whatsapp-client-gateway
# OR if you published it:
cd whatsapp-client-gateway
If you have the gateway code, follow these steps:
Copy .env.example to .env:
PORT=3000
LARAVEL_URL=http://localhost:8000
LARAVEL_WEBHOOK_SECRET=your-hmac-secret-here
WHATSAPP_CLIENT_TOKEN=your-bearer-token-here
DEFAULT_ACCOUNT=default
Important: The WHATSAPP_CLIENT_TOKEN and LARAVEL_WEBHOOK_SECRET must match the values in your Laravel .env file:
WHATSAPP_CLIENT_TOKEN = WHATSAPP_CLIENT_TOKEN (in Laravel)LARAVEL_WEBHOOK_SECRET = WHATSAPP_CLIENT_WEBHOOK_SECRET (in Laravel)# Development
npm run dev
# Production
npm start
/admin/whatsapp-settings/{masterKey}The gateway service must provide these endpoints:
GET /health - Health checkGET /connection - Connection statusGET /qr - QR code for authenticationPOST /send/text - Send text messagePOST /send/media - Send media messagePOST /mark-read - Mark message as readPOST /disconnect - Disconnect clientPOST /restart - Restart clientAll endpoints (except /health) require Bearer token authentication.
The gateway sends webhooks to Laravel at:
/api/whatsapp/client/incoming - Incoming messages/api/whatsapp/client/status - Status updates/api/whatsapp/client/connection - Connection eventsAll webhooks include HMAC signature in X-WhatsApp-Webhook-Signature header.
use DevsFort\LaravelWhatsappChat\Services\WhatsAppService;
$whatsappService = app(WhatsAppService::class);
// Send text message
$result = $whatsappService->sendTextMessage('+1234567890', 'Hello!');
// Send attachment
$result = $whatsappService->sendAttachmentMessage(
'+1234567890',
'image',
$mediaId, // For business mode
'Caption here',
$s3Url, // For client mode
$fileSize
);
// Send OTP verification
$result = $whatsappService->sendOTPVerification('+1234567890', '123456');
use DevsFort\LaravelWhatsappChat\WhatsApp\Contracts\WhatsAppProvider;
$provider = app(WhatsAppProvider::class);
// Check connection status
$health = $provider->health();
// Send message
$result = $provider->sendText('+1234567890', 'Hello!');
// Get provider name
$providerName = $provider->providerName(); // 'business' or 'client'
The package integrates with Laravel's notification system:
use App\Notifications\MessageReceivedNotification;
$user->notify(new MessageReceivedNotification($messageData, $sender));
Create your notification class using the provided stubs:
php artisan make:notification MessageReceivedNotification
Then implement the toWhatsApp() method:
public function toWhatsApp($notifiable)
{
return [
'to' => $notifiable->whatsapp_number,
'message' => 'Your message here'
];
}
GET /chat - Chat interfaceGET /chat/conversations - Get conversation listGET /chat/messages/{conversationId} - Get messages for conversationPOST /chat/send - Send a text messagePOST /chat/send-attachment - Send an attachmentPOST /chat/mark-read/{conversationId} - Mark messages as readPOST /chat/assign-number - Assign external number to user (admin only)GET /chat/status - Get connection status (admin only)GET /profile/whatsapp-verification - Verification pagePOST /profile/whatsapp-verification/send - Send OTP verification codePOST /profile/whatsapp-verification/verify - Verify OTP codePOST /profile/whatsapp-verification/remove - Remove WhatsApp numberGET /admin/whatsapp-settings/{masterKey} - Settings pageGET /admin/whatsapp-settings/qr-code - Get QR code (client mode)POST /admin/whatsapp-settings/disconnect - Disconnect client (client mode)POST /admin/whatsapp-settings/restart - Restart client (client mode)GET /webhook/whatsapp - Meta API webhook verificationPOST /webhook/whatsapp - Meta API webhook handlerPOST /api/whatsapp/client/incoming - Client incoming message webhookPOST /api/whatsapp/client/status - Client status update webhookPOST /api/whatsapp/client/connection - Client connection event webhookhttps://yourdomain.com/webhook/whatsappmessages, message_deliveries, message_reads eventsThe client webhook uses HMAC signature verification. Make sure:
WHATSAPP_CLIENT_WEBHOOK_SECRET matches in both Laravel and gatewayX-WhatsApp-Webhook-Signature headerhttps://yourdomain.com/api/whatsapp/client/incomingThe package uses Laravel Broadcasting for real-time features. Configure your broadcasting driver in .env:
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=your_app_id
PUSHER_APP_KEY=your_app_key
PUSHER_APP_SECRET=your_app_secret
PUSHER_APP_CLUSTER=your_cluster
# Check WhatsApp token status
php artisan whatsapp:token-status
# Process queued messages
php artisan whatsapp:process-queue
# Process queue for specific number
php artisan whatsapp:process-queue --from=1234567890
If your User model uses different field names, configure them:
// config/whatsapp-chat.php
'user_model' => 'App\Models\User',
'admin_field' => 'role', // Instead of 'type'
'admin_value' => 'administrator', // Instead of 'admin'
'user_whatsapp_number_field' => 'phone', // Instead of 'whatsapp_number'
'user_whatsapp_verified_field' => 'phone_verified', // Instead of 'whatsapp_verified'
Configure custom notification classes:
'notification_class' => 'App\Notifications\CustomWhatsAppNotification',
'notification_channel_class' => 'App\Notifications\Channels\CustomWhatsAppChannel',
"Provider not found"
WHATSAPP_MODE is set correctly in .envcomposer dump-autoload"Client connection failed"
WHATSAPP_CLIENT_TOKEN matches in both services"Messages not appearing in real-time"
"WhatsApp messages not sending"
"QR code not showing"
Enable debug mode in your configuration:
'use_mock_mode' => true, // For development
If you're upgrading from an older version:
.env with new configuration optionsphp artisan config:cleargit checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)This package is open-sourced software licensed under the MIT license.
For support, please open an issue on GitHub or contact us at support@devsfort.com.