This package makes it easy to send Whatsapp notification using wppconnect-server, whatsapp-http-api or any simple whatsapp api 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
- Installation
- Usage
- Available Methods
- Alternatives
- Changelog
- Testing
- Security
- Contributing
- Credits
- License
You can install the package via composer:
composer require felipedamacenoteodoro/laravel-whatsapp-notification-channel
Publish the config file:
php artisan vendor:publish --tag=whatsapp-notification-channel-config
Set your whatsapp-http-api/wppconnect-server or custom session and configure your Laravel Whatsapp Session:
# config/whatsapp-notification-channel.php
'whatsapp-bot-api' => [
'whatsappApiServer' => env('WHATSAPP_API_SERVER'), // wppconnect-server, whatsapp-http-api, default
'whatsappSessionFieldName' => env('WHATSAPP_API_SESSION_FIELD_NAME', ''), //Session field name
'whatsappSession' => env('WHATSAPP_API_SESSION', ''), // session value
'whatsappApiKey' => env('WHATSAPP_API_KEY'), // header api key
'whatsappBearerToken' => env('WHATSAPP_BEARER_TOKEN'), // authorization bearer token
'base_uri' => env('WHATSAPP_API_BASE_URL', ''), // Your base url api
/**
Methods will be automatically mapped according to whatsappApiServer, but if you have a different API
you can map them here, example: sendMessage will be replaced by the sendText method of the api
*/
'mapMethods' => [], // 'sendMessage' => 'sendText', 'sendDocument' => 'sendFile',
],
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.
You can now use the channel in your via()
method inside the Notification class.
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])
}
}
public function toWhatsapp($notifiable)
{
return WhatsappFile::create()
->to($notifiable->whatsapp_number) // Optional
->content('Audio') // Optional Caption
->audio('/path/to/audio.mp3');
}
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');
}
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');
}
public function toWhatsapp($notifiable)
{
return WhatsappLocation::create()
->latitude('40.6892494')
->longitude('-74.0466891');
}
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');
}
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');
}
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;
}
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.
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));
Also you can send messages manually
$message = WhatsappMessage::create()
->to('11111111111@c.us')
->content("Hello there!\nYour invoice has been *PAID*");
app()->make(\NotificationChannels\Whatsapp\Whatsapp::class)->send($message);
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());
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.
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.
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
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|array $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.file64(string $file, string $filename)
: Helper method to attach a file on base64.photo(string $file)
: Helper method to attach a photo.photo64(string $file, string $filename)
: Helper method to attach a photo on base64.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.
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.
For simple use, please consider using whatsapp-api instead.
Please see CHANGELOG for more information what has changed recently.
If you discover any security related issues, please email felipe.devops@gmail.com instead of using the issue tracker.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.