ka4ivan / laravel-notification-channel-whatsapp
Whatsapp Notifications Channel for Laravel
Requires
- php: >=8.1
- illuminate/notifications: ~9.0 || ~10.0 || ~11.0 || ~12.0
- illuminate/support: ~9.0 || ~10.0 || ~11.0 || ~12.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^9.0 || ^10.0
README
This package makes it easy to send notifications using the Whatsapp Messenger with Laravel.
Contents
Installation
You can install the package via composer:
composer require ka4ivan/laravel-notification-channel-whatsapp
Setting up your Whatsapp Bot
This document describes the steps you must take to become a Tech Provider Whatsapp
Set config
Next we need to add tokens to our Laravel configurations. Create a new Whatsapp section inside config/services.php
and place the page token there:
// config/services.php 'whatsapp' => [ 'access_token' => env('WHATSAPP_ACCESS_TOKEN', ''), 'number_id' => env('WHATSAPP_NUMBER_ID', ''), 'api_version' => env('WHATSAPP_API_VERSION', '22.0'), ],
Usage
You can now use the Whatsapp channel in your via()
method, inside the InvoicePaid class. The to($recipientId)
Whatsapp ID (Phone Number) method defines the Whatsapp user, you want to send the notification to.
use NotificationChannels\Whatsapp\WhatsappChannel; use NotificationChannels\Whatsapp\WhatsappMessage; use Illuminate\Notifications\Notification; class ChannelConnected extends Notification { public function via($notifiable) { return [WhatsappChannel::class]; } public function toWhatsapp($notifiable) { return WhatsappMessage::create() ->to($notifiable->whatsapp_id) // Optional ->previewUrl(false) // Optional ->text('Congratulations, the communication channel is connected'); } }
The notification will be sent from your Whatsapp page, whose page token you have configured earlier. Here's a screenshot preview of the notification inside the chat window.
Message Examples
Basic Text Message
return WhatsappMessage::create('You have just paid your monthly fee! Thanks');
Routing a message
You can either send the notification by providing with the page-scoped user id of the recipient to the to($recipientId)
Whatsapp ID (Phone Number) method like shown in the above example or add a routeNotificationForWhatsapp()
method in your notifiable model:
/** * Route notifications for the Whatsapp channel. * * @return int */ public function routeNotificationForWhatsapp() { return $this->whatsapp_id; }
Available Message methods
to($recipientId)
: (string) User (recipient) Whatsapp ID (Phone Number).text('')
: (string) Notification message.previewUrl(true)
: (boolean) Link Preview.
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.