shadowbane/laravel-whatsmeow

Send Simple WhatsApp message via Whatsmeow API

v1.0.0 2022-04-26 03:21 UTC

This package is auto-updated.

Last update: 2024-10-26 08:41:33 UTC


README

License: GPL v3 Total Downloads

About

This package provides easy integration with Whatsmeow, my own API for sending WhatsApp message.

Installation

install the package via composer:

composer require shadowbane/laravel-whatsmeow

Publishing Config

php artisan vendor:publish --provider="Shadowbane\Whatsmeow\WhatsmeowServiceProvider"

Usage

Configuration

add the following value to your .env file

WHATSMEOW_ENDPOINT=
WHATSMEOW_TOKEN=
WHATSAPP_NUMBER_FIELD=
WHATSAPP_NUMBER_JSON_FIELD=
DEBUG_WHATSAPP_NUMBER=

WHATSMEOW_ENDPOINT Fill it with the url for Wablas API Endpoint.

WHATSMEOW_TOKEN This is the token generated from your Wablas account.

WHATSAPP_NUMBER_FIELD This is where you store the user's WhatsApp number in users table.

WHATSAPP_NUMBER_JSON_FIELD only fill this if you store user's WhatsApp number on JSON column in database, for example, the data might look like this:

{"whatsapp": 0123456}

DEBUG_WHATSAPP_NUMBER This is used when your APP_ENV is set to 'local' to prevent sending it to real user. It will send to debug number instead.

Sending Text Message

You can send text message using 'via' method inside notification class.

app/notifications/WhatsAppNotification:

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Shadowbane\Whatsmeow\Exceptions\FailedToSendNotification;
use Shadowbane\Whatsmeow\WhatsmeowChannel;
use Shadowbane\Whatsmeow\WhatsmeowMessage;

class WhatsappNotification extends Notification
{
    protected string $phoneNumber;
    protected string $message;

    /**
     * Create a new notification instance.
     *
     * @param string $phoneNumber
     * @param string $message;
     *
     * @return void
     */
    public function __construct(string $phoneNumber, string $message)
    {
        $this->phoneNumber = $phoneNumber;
        $this->message = $message;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     *
     * @return array
     */
    public function via($notifiable)
    {
        return [WhatsmeowChannel::class];
    }

    /**
     * @param $notifiable
     *
     * @throws FailedToSendNotification
     *
     * @return WhatsmeowMessage
     */
    public function toWhatsapp($notifiable)
    {
        return WhatsmeowMessage::create()
            ->to($this->phoneNumber)
            ->content($this->message);
    }
}

Change token

If your application has multiple token for multiple purpose, you can chain token($token) method to your WhatsmeowMessage instance

    use Shadowbane\Whatsmeow\WhatsmeowMessage;
    
    ...

    public function toWhatsapp($notifiable)
    {
        return WhatsmeowMessage::create()
            ->token('this-is-another-token-in-my-application')
            ->to($this->phoneNumber)
            ->content($this->message);
    }

Send Using The Notifiable Trait

If you want to send it via notifiable, you can refer to this example:

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Shadowbane\Whatsmeow\WhatsmeowChannel;
use Shadowbane\Whatsmeow\WhatsmeowMessage;

class WhatsappNotification extends Notification
{
    protected string $phoneNumber;
    protected string $message;

    /**
     * Create a new notification instance.
     *
     * @param string $message;
     *
     * @return void
     */
    public function __construct(string $message)
    {
        $this->message = $message;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     *
     * @return array
     */
    public function via($notifiable)
    {
        return [WhatsmeowChannel::class];
    }

    /**
     * @param $notifiable
     *
     * @return WhatsmeowMessage
     */
    public function toWhatsapp($notifiable)
    {
        return WhatsmeowMessage::create()
            ->content($this->message);
    }
}

Then, you can trigger it with:

use App\Notifications\WhatsappNotification;

$user->notify(new WhatsappNotification($message));

Sending to Multiple Users

This packages allows array to be passed as parameter in to() methods. As Wablas allows comma-separated values as phone number, we automatically implode the array, and send it as comma-separated value to Wablas API.

Example:

    use Shadowbane\Whatsmeow\WhatsmeowMessage;

    ...

    public function toWhatsapp($notifiable)
    {
        return WhatsmeowMessage::create()
            ->token('this-is-another-token-in-my-application')
            ->to([$destination1, $destination2])
            ->content($this->message);
    }

If you prefer to send it to notifiables, you can send it via notification facade.

use Illuminate\Support\Facades\Notification;
use App\Models\User;
use App\Notifications\WhatsappNotification;

Notification::send(User::whereSomeCondition(1)->get(), new WhatsappNotification(123) );

Notes

If you send it to notifiable, please make sure your WHATSAPP_NUMBER_FIELD reflecting the field where you store your user's WhatsApp number.

Changelog

Please see CHANGELOG for more information what has changed recently.

Security

If you discover any security related issues, please send email to adly.shadowbane@gmail.com instead of using the issue tracker.

Contributing

Please see CONTRIBUTING for details.