kstmostofa/laravel-whatsapp
Laravel package to send WhatsApp messages from your app. Provides a simple API to configure credentials and dispatch messages (often via popular gateways like Twilio) with easy setup for notifications and custom message templates.
Installation:
composer require kstmostofa/laravel-whatsapp
php artisan vendor:publish --provider="KstMostofa\WhatsApp\WhatsAppServiceProvider"
Publish the config and migrations:
php artisan migrate
Configure .env:
Add Meta Cloud API or whatsapp-web.js credentials:
WHATSAPP_PROVIDER=meta_cloud # or whatsapp_web
WHATSAPP_META_API_KEY=your_api_key
WHATSAPP_WEB_JS_URL=http://localhost:3000 # Sidecar endpoint
First Use Case: Send a test message via the facade:
use KstMostofa\WhatsApp\Facades\WhatsApp;
WhatsApp::send('+1234567890', 'Hello from Laravel!');
src/Facades/WhatsApp.php
Focus on methods like send(), getChats(), and getMedia().config/whatsapp.php
Adjust provider, credentials, and webhook settings.resources/views/livewire/whatsapp/admin.blade.php
Customize the UI if needed (optional).WhatsApp::send('+1234567890', 'Your OTP is: ' . $otp);
WhatsApp::sendTemplate('+1234567890', 'order_update', [
'order_id' => '#12345',
'amount' => '$99.99'
]);
whatsapp-web.js):
Add a route in routes/web.php:
Route::post('/whatsapp/webhook', [WhatsAppWebhookController::class, 'handle']);
Implement the controller to process events:
public function handle(Request $request) {
$event = WhatsApp::parseWebhook($request);
// Handle message, status updates, etc.
}
@livewire('whatsapp.admin')
Customize the Livewire component in app/Http/Livewire/WhatsApp/Admin.php.Offload message sending to a queue (e.g., for bulk operations):
WhatsApp::queue()->send('+1234567890', 'Delayed message');
Configure the queue in config/whatsapp.php:
'queue' => [
'enabled' => true,
'connection' => 'redis',
],
Laravel Notifications:
Extend the WhatsAppChannel for seamless integration:
use KstMostofa\WhatsApp\Notifications\WhatsAppChannel;
class WhatsAppMessage extends Notification {
public function via($notifiable) {
return [WhatsAppChannel::class];
}
}
Service Providers: Bind custom providers (e.g., for extended functionality):
WhatsApp::extend('custom', function () {
return new CustomWhatsAppProvider();
});
Middleware: Add auth/validation middleware to WhatsApp routes:
Route::middleware(['auth:sanctum'])->group(function () {
Route::post('/whatsapp/send', [WhatsAppController::class, 'send']);
});
Testing:
Use the WhatsAppFake for unit tests:
use KstMostofa\WhatsApp\Testing\WhatsAppFake;
public function test_send_message() {
WhatsAppFake::fake();
WhatsApp::send('+1234567890', 'Test');
WhatsAppFake::assertSent('Test');
}
Dual-Backend Complexity:
whatsapp-web.js sidecar may crash or lose session if not properly monitored.// Recover a lost session
WhatsApp::recoverSession('+1234567890');
Meta Cloud API Quotas:
429 Too Many Requests errors if quotas are exceeded.try {
WhatsApp::send($phone, $message);
} catch (\KstMostofa\WhatsApp\Exceptions\RateLimitException $e) {
sleep($e->retryAfter);
retry();
}
Livewire Admin UI:
resources/views/livewire/whatsapp/ or extract components into reusable widgets.Webhook Delays:
whatsapp-web.js webhooks may be delayed or dropped.Phone Number Formatting:
+) cause API failures.use KstMostofa\WhatsApp\Support\PhoneNumber;
$phone = PhoneNumber::format('1234567890'); // Returns '+1234567890'
Enable Logging:
Add to config/whatsapp.php:
'logging' => [
'enabled' => true,
'channel' => 'single',
],
Check logs in storage/logs/whatsapp.log.
Sidecar Debugging:
For whatsapp-web.js, inspect logs via:
docker logs <container_name> # If using Docker
Or attach to the process:
node --inspect whatsapp-web.js
API Response Inspection: Enable debug mode in the facade:
WhatsApp::debug(true);
Responses will be logged with request/response details.
Custom Providers:
Extend the WhatsAppProvider interface for new backends:
class CustomProvider implements \KstMostofa\WhatsApp\Contracts\WhatsAppProvider {
public function send($phone, $message) { ... }
public function getChats() { ... }
}
Register it in config/whatsapp.php:
'providers' => [
'custom' => \App\Providers\CustomWhatsAppProvider::class,
],
Message Templates: Add custom templates for Meta Cloud API:
WhatsApp::addTemplate('order_confirmation', [
'components' => [
'header' => ['type' => 'text', 'text' => 'Order #{{1}}'],
// ...
],
]);
Webhook Handlers:
Extend the WhatsAppWebhookHandler for custom logic:
WhatsApp::extendWebhookHandler(function ($event) {
if ($event->type === 'message') {
// Custom logic for incoming messages
}
});
Queue Jobs: Publish and extend the queue job:
php artisan vendor:publish --tag=whatsapp-queue-jobs
Customize app/Jobs/SendWhatsAppMessage.php.
Provider Switching: Dynamically switch providers at runtime:
WhatsApp::setProvider('whatsapp_web'); // Fallback to web.js
Media Handling: For large media, use streaming:
WhatsApp::sendMedia($phone, 'image.jpg', [
'caption' => 'Check this out!',
'stream' => true, // Stream file instead of uploading
]);
Rate Limiting: Configure custom rate limits in `config/whatsapp
How can I help you explore Laravel packages today?