shadowbane / laravel-wablas
Send Simple WhatsApp message via Wablas API
Installs: 333
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
Type:project
Requires
- php: ^8.0|^8.1
- ext-json: *
- guzzlehttp/guzzle: ^7.0
- illuminate/notifications: ^8.0
- illuminate/support: ^8.0
This package is auto-updated.
Last update: 2022-04-07 04:26:52 UTC
README
About
This package provides easy integration with Wablas Indonesia, provider for sending WhatsApp message via HTTP API.
Installation
install the package via composer:
composer require shadowbane/laravel-wablas
Publishing Config
php artisan vendor:publish --provider="Shadowbane\LaravelWablas\LaravelWablasServiceProvider"
Usage
Configuration
add the following value to your .env
file
WABLAS_ENDPOINT=
WABLAS_TOKEN=
WHATSAPP_NUMBER_FIELD=
WHATSAPP_NUMBER_JSON_FIELD=
DEBUG_WHATSAPP_NUMBER=
WABLAS_ENDPOINT
Fill it with the url for Wablas API Endpoint.
WABLAS_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 'production' and 'APP_DEBUG' is set to true, to prevent sending it to real user.
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\LaravelWablas\Exceptions\FailedToSendNotification; use Shadowbane\LaravelWablas\LaravelWablasChannel; use Shadowbane\LaravelWablas\LaravelWablasMessage; 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 [LaravelWablasChannel::class]; } /** * @param $notifiable * * @throws FailedToSendNotification * * @return LaravelWablasMessage */ public function toWhatsapp($notifiable) { return LaravelWablasMessage::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 LaravelWablasMessage
instance
use Shadowbane\LaravelWablas\LaravelWablasMessage; ... public function toWhatsapp($notifiable) { return LaravelWablasMessage::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\LaravelWablas\LaravelWablasChannel; use Shadowbane\LaravelWablas\LaravelWablasMessage; 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 [LaravelWablasChannel::class]; } /** * @param $notifiable * * @return LaravelWablasMessage */ public function toWhatsapp($notifiable) { return LaravelWablasMessage::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\LaravelWablas\LaravelWablasMessage; ... public function toWhatsapp($notifiable) { return LaravelWablasMessage::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.