Skip to content

0xForked/whatsapp-notification-channel

Repository files navigation

WhatsApp Notifications Channel for Laravel using GoWa as a Worker

Latest Version on Packagist Software License Build Status SensioLabsInsight Quality Score Code Coverage Total Downloads

This package makes it easy to send notifications using WhatsApp Business API over GoWa Worker with Laravel 8.0+

Contents

Installation

You can install the package via composer:

composer require aasumitro/whatsapp-notification-channel

Setting up the WhatsApp Worker

Then, configure your WhatsApp Worker API URL:

// config/services.php
'whatsapp-notification-service' => [
    'service_api_url' => env('WHATSAPP_SERVICE_URL', 'YOUR WORKER API_URL HERE')
],

Register service provider

Then, register whatsapp notification service provider:

// config/services.php

'providers' => [
    ....
    
    \NotificationChannels\WhatsApp\WhatsAppServiceProvider::class
],

Usage

Authentication

use the view component

<x-whatsapp-account></x-whatsapp-account>

You can now use the channel in your via() method inside the Notification class.

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use NotificationChannels\WhatsApp\WhatsAppChannel;
use NotificationChannels\WhatsApp\WhatsAppMessage;

class WhatsappSendTextMessageNotification extends Notification
{
    use Queueable;

    private $message;

    public function __construct(string $message)
    {
        $this->message = $message;
    }

    public function via($notifiable): array
    {
        return [WhatsAppChannel::class];
    }

    public function toWhatsapp($notifiable): WhatsAppMessage
    {
        return WhatsAppMessage::create()
            ->to($notifiable->msisdn)
            ->content($this->message);
    }
}

Routing a Message

You can either send the notification by providing with the Destination number of the recipient to the to($msisdn) 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_phone_number;
}

TODO NEXT

  1. WhatsAppFile (audio, document, image, video) prior
  2. WhatsAppLocation

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

or

$ ./vendor/bin/phpunit ./packages/whatsapp-notification-channel/tests

Security

If you discover any security related issues, please email hello@aasumitro.id instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.