mohdradzee/laravel-wati-notification

A custom Laravel notification channel for WATI WhatsApp API

v1.0.3 2025-07-02 07:30 UTC

This package is auto-updated.

Last update: 2025-07-09 05:48:14 UTC


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