felipedamacenoteodoro / laravel-whatsapp-notification-channel
Whatsapp Notifications Channel for Laravel
Installs: 2 227
Dependents: 0
Suggesters: 0
Security: 0
Stars: 88
Watchers: 3
Forks: 9
Open Issues: 9
Requires
- php: ^7.2 || ^8.0
- ext-json: *
- guzzlehttp/guzzle: ^6.2 || ^7.0
- illuminate/contracts: ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
- illuminate/notifications: ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
- illuminate/support: ^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0
Requires (Dev)
- mockery/mockery: ^1.3 || ^1.4
- phpunit/phpunit: ^7.0 || ^8.5.21 || ^9.0
This package is auto-updated.
Last update: 2024-10-16 19:15:30 UTC
README
This package makes it easy to send Whatsapp notification using WPPCONNECT SERVER with Laravel.
This package was created based on the telegram notification package.
Thanks to Irfaq Syed for the codebase used here.
The packages is 100% free and opensource, if you are interested in hiring paid support, installation or implementation, Felipe D. Teodoro
Contents
- Installation
- Usage
- Available Methods
- Alternatives
- Changelog
- Testing
- Security
- Contributing
- Credits
- License
Installation
You can install the package via composer:
composer require felipedamacenoteodoro/laravel-whatsapp-notification-channel
Publish config file
Publish the config file:
php artisan vendor:publish --tag=whatsapp-notification-channel-config
Setting up your Whatsapp session
Set your venom session WPPCONNECT SERVER and configure your Whatsapp Session:
# config/whatsapp-notification-channel/services.php 'whatsapp-bot-api' => [ 'whatsappSessionFieldName' => env('WHATSAPP_API_SESSION_FIELD_NAME', ''), //Session field name 'whatsappSession' => env('WHATSAPP_API_SESSION', ''), // session value 'base_uri' => env('WHATSAPP_API_BASE_URL', ''), // Your venom base url api 'mapMethods' => [ 'sendMessage' => 'sendText', 'sendDocument' => 'sendFile', ], /* If you want to change the methods that will be called in the api, you can map them here, example: sendMessage will be replaced by the sendText method of the api */ ],
Proxy or Bridge Support
You may not be able to send notifications if Whatsapp API is not accessible in your country,
you can either set a proxy by following the instructions here or
use a web bridge by setting the base_uri
config above with the bridge uri.
You can set HTTPS_PROXY
in your .env
file.
Usage
You can now use the channel in your via()
method inside the Notification class.
Text Notification
use NotificationChannels\Whatsapp\WhatsappMessage; use Illuminate\Notifications\Notification; class InvoicePaid extends Notification { public function via($notifiable) { return ["whatsapp"]; } public function toWhatsapp($notifiable) { $url = url('/invoice/' . $this->invoice->id); return WhatsappMessage::create() // Optional recipient user id. ->to($notifiable->whatsapp_number) // Markdown supported. ->content("Hello there!\nYour invoice has been *PAID*") // (Optional) Blade template for the content. // ->view('notification', ['url' => $url]) } }
Attach an Audio
public function toWhatsapp($notifiable) { return WhatsappFile::create() ->to($notifiable->whatsapp_number) // Optional ->content('Audio') // Optional Caption ->audio('/path/to/audio.mp3'); }
Attach a Photo
public function toWhatsapp($notifiable) { return WhatsappFile::create() ->to($notifiable->whatsapp_number) // Optional ->content('Awesome *bold* text') ->file('/storage/archive/6029014.jpg', 'photo'); // local photo // OR using a helper method with or without a remote file. // ->photo('https://file-examples-com.github.io/uploads/2017/10/file_example_JPG_1MB.jpg'); }
Attach a Document
public function toWhatsapp($notifiable) { return WhatsappFile::create() ->to($notifiable->whatsapp_number) // Optional ->content('Did you know we can set a custom filename too?') ->document('https://file-examples-com.github.io/uploads/2017/10/file-sample_150kB.pdf', 'sample.pdf'); }
Attach a Location
public function toWhatsapp($notifiable) { return WhatsappLocation::create() ->latitude('40.6892494') ->longitude('-74.0466891'); }
Attach a Video
public function toWhatsapp($notifiable) { return WhatsappFile::create() ->content('Sample *video* notification!') ->video('https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4'); }
Attach a GIF File
public function toWhatsapp($notifiable) { return WhatsappFile::create() ->content('Woot! We can send animated gif notifications too!') ->animation('https://sample-videos.com/gif/2.gif'); // Or local file // ->animation('/path/to/some/animated.gif'); }
Routing a Message
You can either send the notification by providing with the whatapp number of the recipient to the to($whatsapp_number)
method like shown in the previous examples or add a routeNotificationForWhatsapp()
method in your notifiable model:
/** * Route notifications for the Whatsapp channel. * * @return int */ public function routeNotificationForWhatsapp() { return $this->whatsapp_number; }
Handling Response
You can make use of the notification events to handle the response from Whatsapp. On success, your event listener will receive a Message object with various fields as appropriate to the notification type.
For a complete list of response fields, please refer the Venom Whatsapp API's Message object docs.
On-Demand Notifications
Sometimes you may need to send a notification to someone who is not stored as a "user" of your application. Using the
Notification::route
method, you may specify ad-hoc notification routing information before sending the notification. For more details, you can check out the on-demand notifications docs.
use Illuminate\Support\Facades\Notification; Notification::route('whatsapp', 'WHATSAPP_SESSION') ->notify(new InvoicePaid($invoice));
Sending to Multiple Recipients
Using the notification facade you can send a notification to multiple recipients at once.
If you're sending bulk notifications to multiple users, the Whatsapp API will not allow more than 30 messages per second or so. Consider spreading out notifications over large intervals of 8—12 hours for best results.
Also note that your bot will not be able to send more than 20 messages per minute to the same group.
If you go over the limit, you'll start getting
429
errors. For more details, refer Whatsapp Api FAQ.
use Illuminate\Support\Facades\Notification; // Recipients can be an array of numbers or collection of notifiable entities. Notification::send($recipients, new InvoicePaid());
Available Methods
Shared Methods
These methods are optional and shared across all the API methods.
to(int|string $number)
: Recipient's number.session(string $session)
: Session if you wish to override the default session for a specific notification.options(array $options)
: Allows you to add additional params or override the payload.getPayloadValue(string $key)
: Get payload value for given key.
Whatsapp Message methods
For more information on supported parameters, check out these docs.
content(string $content, int $limit = null)
: Notification message, supports markdown. For more information on supported markdown styles, check out these docs.view(string $view, array $data = [], array $mergeData = [])
: (optional) Blade template name with Whatsapp supported Markdown syntax content if you wish to use a view file instead of thecontent()
method.chunk(int $limit = 4096)
: (optional) Message chars chunk size to send in parts (For long messages). Note: Chunked messages will be rate limited to one message per second to comply with rate limitation requirements from Whatsapp.
Whatsapp Location methods
latitude(float|string $latitude)
: Latitude of the location.longitude(float|string $longitude)
: Longitude of the location.title(string $title)
: Title of locationdescription(string $description)
: description of location
Whatsapp File methods
content(string $content)
: (optional) File caption, supports markdown. For more information on supported markdown styles, check out these docs.view(string $view, array $data = [], array $mergeData = [])
: (optional) Blade template name with Whatsapp supported HTML or Markdown syntax content if you wish to use a view file instead of thecontent()
method.file(string|resource|StreamInterface $file, string $type, string $filename = null)
: Local file path or remote URL,$type
of the file (Ex:photo
,audio
,document
,video
,animation
,voice
,video_note
) and optionally filename with extension. Ex:sample.pdf
. You can use helper methods instead of using this to make it easier to work with file attachment.photo(string $file)
: Helper method to attach a photo.audio(string $file)
: Helper method to attach an audio file (MP3 file).document(string $file, string $filename = null)
: Helper method to attach a document or any file as document.video(string $file)
: Helper method to attach a video file.animation(string $file)
: Helper method to attach an animated gif file.
Whatsapp Contact methods
phoneNumber(string $phoneNumber)
: Contact phone number.name(string $name)
: Full name.firstName(string $firstName)
: (optional if you use name param) Contact first name.lastName(string $lastName)
: (optional) Contact last name.
Simple Whatsapp Api
For simple use, please consider using whatsapp-api instead.
Changelog
Please see CHANGELOG for more information what has changed recently.
Security
If you discover any security related issues, please email felipe.devops@gmail.com instead of using the issue tracker.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.