devaspid / laravel-whatsapp-gateway
Laravel package for WhatsApp Gateway API (wa-api-by-asp / https://wa-gateway.asp.web.id). Supports text messages, media, device management, and Laravel Notification Channel.
Package info
github.com/dev-asp-id/laravel-whatsapp-gateway
pkg:composer/devaspid/laravel-whatsapp-gateway
Requires
- php: ^8.2
- illuminate/http: ^10.0|^11.0|^12.0
- illuminate/notifications: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
Requires (Dev)
- mockery/mockery: ^1.6
- orchestra/testbench: ^8.0|^9.0|^10.0
- pestphp/pest: ^2.0|^3.0
- pestphp/pest-plugin-laravel: ^2.0|^3.0
- phpunit/phpunit: ^10.0|^11.0
This package is auto-updated.
Last update: 2026-08-26 05:21:28 UTC
README
Package Laravel untuk integrasi dengan WhatsApp Gateway API wa-api-by-asp. Package ini menyediakan antarmuka yang bersih, modular, dan modern untuk mengirim pesan WhatsApp, mengelola device, serta integrasi penuh dengan Laravel Notification System.
โจ Fitur
- ๐จ Kirim Pesan Teks โ Quick send & Fluent API builder
- ๐ผ๏ธ Kirim Media โ Gambar, Audio/Voice Note, Dokumen (PDF/Excel/ZIP)
- ๐ฑ Device Management โ List, Create, QR Login, Pairing Code, Logout, Reconnect, Status
- ๐ Laravel Notification Channel โ Kirim notifikasi via
$user->notify() - ๐ก๏ธ Typed Exceptions โ
AuthenticationException,ValidationException,DeviceNotFoundException, dll. - ๐ฆ Strongly-typed DTOs โ
MessageResult,DeviceData,QrLoginResult - โก Auto-discovery โ Service Provider & Facade otomatis terdaftar
๐ Persyaratan
- PHP 8.2+
- Laravel 10 / 11 / 12
๐ Instalasi
composer require devaspid/laravel-whatsapp-gateway
Publish config file:
php artisan vendor:publish --tag=whatsapp-gateway-config
Tambahkan environment variables ke .env:
WA_GATEWAY_BASE_URL=https://wa-gateway.asp.web.id/api/v1 WA_GATEWAY_CLIENT_ID=your-client-id WA_GATEWAY_API_KEY=your-api-key WA_GATEWAY_DEFAULT_DEVICE_ID= # Opsional WA_GATEWAY_TIMEOUT=15 WA_GATEWAY_RETRY_TIMES=2 WA_GATEWAY_RETRY_SLEEP=500
๐ Penggunaan
Validasi Koneksi (Ping)
use Devaspid\WhatsappGateway\Facades\Whatsapp; if (Whatsapp::ping()) { echo 'API terhubung!'; }
Kirim Pesan Teks
use Devaspid\WhatsappGateway\Facades\Whatsapp; // Quick send $result = Whatsapp::send('6281234567890', 'Halo! Pesanan Anda telah dikonfirmasi.'); // Fluent API $result = Whatsapp::to('6281234567890') ->usingDevice('dev_01m0xyz...') // opsional ->replyTo('3EB0B430B6F...') // opsional, quote reply ->message('Terima kasih telah berbelanja!') ->sendMessage(); // Cek hasil if ($result->successful()) { echo 'Message ID: ' . $result->messageId(); }
Kirim Media
Gambar dengan Caption
Whatsapp::to('6281234567890') ->image('https://example.com/nota.png') ->caption('Bukti Pembayaran #INV-12345') ->viewOnce(false) ->sendMessage();
Dokumen PDF
Whatsapp::to('6281234567890') ->file('https://example.com/laporan.pdf') ->filename('Laporan_Tahunan_2026.pdf') ->caption('Silakan unduh dokumen terlampir.') ->sendMessage();
Audio / Voice Note
Whatsapp::to('6281234567890') ->audio('https://example.com/voice-greeting.mp3') ->sendMessage();
Device Management
// List semua devices $devices = Whatsapp::devices()->list(); // Buat device baru $device = Whatsapp::devices()->create('Customer Service WA'); // Detail device $device = Whatsapp::devices()->find('dev_01m0xyz...'); // QR Code login $qr = Whatsapp::devices()->getQrCode($device->deviceId); echo $qr->toImgTag(); // <img src="data:image/png;base64,..." /> // Pairing Code $code = Whatsapp::devices()->getPairingCode($device->deviceId, '6281234567890'); echo "Kode Pairing: {$code}"; // Status koneksi $status = Whatsapp::devices()->getStatus($device->deviceId); echo $status->isConnected() ? 'Online' : 'Offline'; // Reconnect, Logout, Delete Whatsapp::devices()->reconnect($device->deviceId); Whatsapp::devices()->logout($device->deviceId); Whatsapp::devices()->delete($device->deviceId);
Laravel Notification Channel
Buat notification class:
namespace App\Notifications; use Illuminate\Notifications\Notification; use Devaspid\WhatsappGateway\Channels\WhatsappChannel; use Devaspid\WhatsappGateway\Messages\WhatsappMessage; class InvoicePaidNotification extends Notification { public function via($notifiable): array { return [WhatsappChannel::class]; } public function toWhatsapp($notifiable): WhatsappMessage { return WhatsappMessage::create() ->to($notifiable->phone_number) ->message("Halo {$notifiable->name}, pembayaran Anda sebesar Rp 150.000 telah kami terima."); } }
Gunakan dari User model:
$user->notify(new InvoicePaidNotification());
Tip: Anda bisa menambahkan method
routeNotificationForWhatsapp()pada model Notifiable untuk mengkustomisasi nomor tujuan:public function routeNotificationForWhatsapp(): string { return $this->whatsapp_number; }
โ ๏ธ Exception Handling
Package ini melempar exception terstruktur yang dapat di-catch:
use Devaspid\WhatsappGateway\Exceptions\AuthenticationException; use Devaspid\WhatsappGateway\Exceptions\ValidationException; use Devaspid\WhatsappGateway\Exceptions\DeviceNotFoundException; use Devaspid\WhatsappGateway\Exceptions\GatewayConnectionException; use Devaspid\WhatsappGateway\Exceptions\RateLimitException; use Devaspid\WhatsappGateway\Exceptions\WhatsappGatewayException; try { Whatsapp::send('6281234567890', 'Hello!'); } catch (AuthenticationException $e) { // 401 โ API key salah } catch (ValidationException $e) { // 422 โ Validasi gagal $errors = $e->getErrors(); // ['phone' => ['Format nomor tidak valid']] } catch (DeviceNotFoundException $e) { // 404 โ Device tidak ditemukan } catch (RateLimitException $e) { // 429 โ Terlalu banyak request } catch (GatewayConnectionException $e) { // 502/503 โ WA engine offline } catch (WhatsappGatewayException $e) { // Catch-all untuk error lainnya }
๐งช Testing
Jalankan test suite:
composer test
Atau langsung:
./vendor/bin/phpunit
Dalam project Anda, gunakan Http::fake() untuk mocking:
use Illuminate\Support\Facades\Http; Http::fake([ 'wa-gateway.asp.web.id/api/v1/messages' => Http::response([ 'success' => true, 'data' => ['message_id' => 'FAKE_MSG_001', 'status' => 'success'], ]), ]); $result = Whatsapp::send('6281234567890', 'Test message'); assertTrue($result->successful());
๐ Struktur Package
src/
โโโ Channels/
โ โโโ WhatsappChannel.php # Laravel Notification Channel
โโโ Contracts/
โ โโโ WhatsappGatewayInterface.php # Interface kontrak
โโโ DTOs/
โ โโโ DeviceData.php # DTO Device
โ โโโ DeviceStatusData.php # DTO Status Device
โ โโโ MessageResult.php # DTO Hasil Pengiriman
โ โโโ QrLoginResult.php # DTO QR Code
โโโ Exceptions/
โ โโโ AuthenticationException.php # 401
โ โโโ DeviceNotFoundException.php # 404
โ โโโ GatewayConnectionException.php # 502/503
โ โโโ RateLimitException.php # 429
โ โโโ ValidationException.php # 422
โ โโโ WhatsappGatewayException.php # Base Exception
โโโ Facades/
โ โโโ Whatsapp.php # Facade
โโโ Messages/
โ โโโ WhatsappMessage.php # Fluent builder teks
โ โโโ WhatsappMediaMessage.php # Fluent builder media
โโโ Services/
โ โโโ DeviceService.php # Device management
โ โโโ MessageService.php # Pengiriman pesan
โโโ WhatsappClient.php # HTTP Client wrapper
โโโ WhatsappGateway.php # Core Manager
โโโ WhatsappServiceProvider.php # Service Provider
config/
โโโ whatsapp-gateway.php # File konfigurasi
๐ง Konfigurasi
File config/whatsapp-gateway.php:
| Key | Env Variable | Default | Keterangan |
|---|---|---|---|
base_url |
WA_GATEWAY_BASE_URL |
https://wa-gateway.asp.web.id/api/v1 |
Base URL API |
client_id |
WA_GATEWAY_CLIENT_ID |
'' |
Client ID dari dashboard |
api_key |
WA_GATEWAY_API_KEY |
'' |
API Key dari dashboard |
default_device_id |
WA_GATEWAY_DEFAULT_DEVICE_ID |
null |
Device ID default |
timeout |
WA_GATEWAY_TIMEOUT |
15 |
Timeout HTTP (detik) |
retry.times |
WA_GATEWAY_RETRY_TIMES |
2 |
Jumlah retry |
retry.sleep |
WA_GATEWAY_RETRY_SLEEP |
500 |
Jeda retry (ms) |
๐ Lisensi
MIT License. Lihat file LICENSE untuk detail.