✈️ Whatsapp Notifications Channel for Laravel

Last update: Jun 26, 2022

Whatsapp Notifications Channel for Laravel

Latest Version on Packagist Software License Total Downloads

This package makes it easy to send Whatsapp notification using Venom 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

Contents

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 Venom Session 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 the content() 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 location
  • description(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 the content() 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 [email protected] 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.

GitHub

https://github.com/felipedamacenoteodoro/laravel-whatsapp-notification-channel
You might also like...

Flexible Flash notifications for Laravel

Flexible Flash notifications for Laravel

Introduction Laravel Notify is a package that lets you add custom notifications to your project. A diverse range of notification design is available.

Jul 3, 2022

A package to simplify automating future notifications and reminders in Laravel

A package to simplify automating future notifications and reminders in Laravel

Laravel Snooze Schedule future notifications and reminders in Laravel Why use this package? Ever wanted to schedule a future notification to go out at

Jun 27, 2022

Push Notifications using Laravel

Push Notifications using Laravel

laravel-push-notification Push Notifications using Laravel PushNotification::send(['deviceToken1', 'deviceToken2',..], 'Notification Message', 'Action

Apr 6, 2022

Laravel package to enable sending push notifications to devices

Laravel Push Notification Package to enable sending push notifications to devices Installation Update your composer.json file to include this package

Jun 16, 2022

Send Firebase push notifications with Laravel php framework.

FCM Notification Channel for Laravel Send Firebase push notifications with Laravel php framework. Installation You can install this package via compos

Jun 29, 2022

This package makes it easy to send notifications using RocketChat with Laravel 9.0+.

laravel-rocket-chat-notifications Introduction This package makes it easy to send notifications using RocketChat with Laravel 9.0+. Contents Installat

May 27, 2022

This package makes it easy to send web push notifications with Laravel.

Web push notifications channel for Laravel This package makes it easy to send web push notifications with Laravel. Installation You can install the pa

Jun 30, 2022

Standalone PHP library for easy devices notifications push.

NotificationPusher Standalone PHP library for easy devices message notifications push. Feel free to contribute! Thanks. Contributors Cédric Dugat (Aut

Jun 15, 2022

:computer: Send notifications to your desktop directly from your PHP script

:computer: Send notifications to your desktop directly from your PHP script

About JoliNotif JoliNotif is a cross-platform PHP library to display desktop notifications. It works on Linux, Windows or MacOS. Requires PHP = 7.2 (

Jun 26, 2022
@Authy notification channel for @Laravel, with the ability to send in-app, sms, and call verification tokens.
@Authy notification channel for @Laravel, with the ability to send in-app, sms, and call verification tokens.

Authy Notification Channel for Laravel Authy notification channel for Laravel, with the ability to send in-app, sms, and call verification tokens. Tab

Feb 14, 2022
Google Chat - Laravel Notification Channel
 Google Chat - Laravel Notification Channel

Google Chat - Laravel Notification Channel This package makes it easy to send notifications using Google Chat , (formerly known as Hangouts Chat) with

May 4, 2022
Laravel SMS Notification Channel

Laravel SMS Notification Channel Installation composer require guangda/laravel-sms-notification-channel env 配置 SMS_PROVIDER=yunpian SMS_SIGNATURE=【签名

Dec 29, 2021
Lark Notification Channel for laravel.

Lark notifications channel for Laravel This package makes it easy to send Lark using the Laravel notification system. Supports 5.5+, 6.x, 7.x and 8.x.

Nov 3, 2021
An SMS notification channel for the PHP framework Laravel.

Laravel SMS notification channel An SMS notification channel for the PHP framework Laravel. Supported SMS gateways: 46elks Cellsynt Telenor SMS Pro Tw

Jan 22, 2022
Laravel FCM (Firebase Cloud Messaging) Notification Channel

Laravel FCM Notification Laravel FCM (Firebase Cloud Messaging) Notification Channel Use this package to send push notifications via Laravel to Fireba

Jun 30, 2022
Laravel Security Notifications

This package adds security notifications to warn your users when significant security events occur so that they aren't the next victim of an attacker.

Feb 8, 2022
Laravel package to launch toast notifications.
Laravel package to launch toast notifications.

Laravel package to launch toast notifications. This package provides assistance when using toast notifications. Using the iziTOAST package, which allo

Mar 14, 2022
Laravel Subscribable Notifications

Laravel Subscribable Notifications This package allows you to subscribe your app Users to your app Notifications and dispatch them without specifying

May 29, 2022
Larafirebase is a package thats offers you to send push notifications or custom messages via Firebase in Laravel.
Larafirebase is a package thats offers you to send push notifications or custom messages via Firebase in Laravel.

Introduction Larafirebase is a package thats offers you to send push notifications or custom messages via Firebase in Laravel. Firebase Cloud Messagin

Jun 28, 2022