mohdradzee / laravel-wati-notification
A custom Laravel notification channel for WATI WhatsApp API
v1.0.3
2025-07-02 07:30 UTC
Requires
- php: ^8.1
- guzzlehttp/guzzle: ^7.0
- illuminate/notifications: ^10.0
- illuminate/support: ^10.0
Requires (Dev)
- orchestra/testbench: ^8.36
- phpunit/phpunit: ^10.5
README
Send WhatsApp notifications via WATI API using Laravel's native notification system.
โจ Features
- Send WhatsApp messages via WATI using Laravel Notifications
- Custom notification channel (
wati
) - Built-in support for multiple WATI API actions
- Clean, elegant API (
$user->notify(...)
) - Easily extendable for new WATI endpoints
- Emits Laravel's
NotificationFailed
event on HTTP failures
๐ฆ Installation
composer require mohdradzee/wati-notification
If Laravel doesn't auto-discover the service provider, add this manually:
// config/app.php 'providers' => [ mohdradzee\WatiNotification\WatiNotificationServiceProvider::class, ],
โ๏ธ Configuration
Publish the config:
php artisan vendor:publish --tag=wati-config
This creates config/wati.php
:
return [ 'token' => env('WATI_TOKEN'), 'base_url' => env('WATI_BASE_URL', 'https://live-server.wati.io/441627/api/v1'), ];
Add the following to your .env
:
WATI_TOKEN=your_api_token_here WATI_BASE_URL=https://live-server.wati.io/441627/api/v1
๐ ๏ธ Create a Demo Notification
Step 1: Generate Notification
php artisan make:notification ConfirmWhatsappNumber
Step 2: Edit app/Notifications/ConfirmWhatsappNumber.php
namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use mohdradzee\WatiNotification\WatiMessage; class ConfirmWhatsappNumber extends Notification { use Queueable; public function via($notifiable) { return ['wati']; } public function toWati($notifiable) { return WatiMessage::create() ->to($notifiable->phone) ->withAction('add_contact') ->withMeta([ 'fullName' => $notifiable->name, 'customParams' => [ 'source' => 'signup_page', ], ]); } }
๐ค Usage in a Notifiable Model
Make sure your model uses the Notifiable
trait:
use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable; // Ensure `phone` attribute exists }
Then notify the user:
$user = App\Models\User::first(); $user->notify(new App\Notifications\ConfirmWhatsappNumber());
โ WatiMessage API
WatiMessage::create() ->to('601122334455') ->withAction('send_template') ->withTemplate('template_name') ->withBroadcastName('broadcast1') ->withParameters([ ['name' => 'name', 'value' => 'alibaba'] ]);
๐ Strategy Support (Dynamic API Calls)
Each WATI API action is handled by its own strategy class:
Action | Description | Strategy Class |
---|---|---|
send_template |
Send template message | SendTemplateStrategy |
add_contact |
Add a WhatsApp contact | AddContactStrategy |
You can add your own by implementing:
use mohdradzee\WatiNotification\Contracts\WatiApiStrategy; class MyCustomStrategy implements WatiApiStrategy { public function execute(array $data): array { // call your custom WATI endpoint... } }
Then register:
WatiApiExecutor::register('my_action', MyCustomStrategy::class);
๐งช Testing in Tinker
php artisan tinker
$user = App\Models\User::first(); $user->notify(new App\Notifications\ConfirmWhatsappNumber());
๐ License
MIT License ยฉ mohdradzee