zarulizham / laravel-openwa
Laravel wrapper for the OpenWA WhatsApp gateway API
Fund package maintenance!
Requires
- php: ^8.4
- illuminate/contracts: ^11.0||^12.0||^13.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^11.0.0||^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
- spatie/laravel-ray: ^1.35
README
A Laravel wrapper for the OpenWA WhatsApp gateway API — list sessions, send text/image/document messages, and a whatsapp Notification channel.
Installation
You can install the package via composer:
composer require zarulizham/laravel-openwa
Publish the config file:
php artisan vendor:publish --tag="openwa-config"
Set your OpenWA server details in .env:
OPENWA_BASE_URL=https://openwa.prooffice.com.my/api
OPENWA_API_KEY=your-api-key
OPENWA_TIMEOUT=30
OPENWA_SESSION_NAME=my-bot
OPENWA_SESSION_NAME is optional. When set, getLatestReadySessionId() (and any send call that omits an explicit session id) only considers the session with that name — instead of the most recently connected ready session across all of them. The resolved session id is cached for 3 hours.
Usage
use ZarulIzham\OpenWa\Facades\OpenWa; // List all sessions $sessions = OpenWa::getSessions(); // Get the id of the most recently connected session with status "ready" $sessionId = OpenWa::getLatestReadySessionId(); // Send a text message (auto-resolves the latest ready session if omitted) OpenWa::sendText('628123456789@c.us', 'Hello from OpenWA!'); // Send an image OpenWa::sendImage('628123456789@c.us', [ 'url' => 'https://example.com/image.jpg', 'caption' => 'Check this out!', ]); // Send a document OpenWa::sendDocument('628123456789@c.us', [ 'url' => 'https://example.com/invoice.pdf', 'filename' => 'invoice.pdf', ]);
Notifications
Add a routeNotificationForWhatsapp() method to your notifiable model:
public function routeNotificationForWhatsapp(): string { return $this->phone.'@c.us'; }
Implement ZarulIzham\OpenWa\Notifications\WhatsAppNotification on your notification and return an OpenWaMessage from toWhatsApp():
use Illuminate\Notifications\Notification; use ZarulIzham\OpenWa\Notifications\OpenWaMessage; use ZarulIzham\OpenWa\Notifications\WhatsAppNotification; class OrderShipped extends Notification implements WhatsAppNotification { public function via($notifiable): array { return ['openwa']; } public function toWhatsApp($notifiable): OpenWaMessage { return OpenWaMessage::text("Your order #{$this->order->id} has shipped!"); } }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.