mohdradzee/laravel-wati-notification
最新稳定版本:v1.0.4
Composer 安装命令:
composer require mohdradzee/laravel-wati-notification
包简介
A custom Laravel notification channel for WATI WhatsApp API
README 文档
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
NotificationFailedevent 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
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-07-09