caherrera/laravel-notifications-infobip-omni

A custom Laravel notifications channel for Infobip API OMNI. Based on princeton255/laravel-notifications-infobip

1.1.3 2021-03-03 21:34 UTC

This package is auto-updated.

Last update: 2021-12-31 00:30:32 UTC


README

Latest Stable Version License Total Downloads

This package makes it easy to send Sms notifications using Infobip service with Laravel 5.5 and above.

Contents

Installation

You can install the package via composer:

composer require caherrera/laravel-notifications-infobip

Setting up your Infobip account

Add this settings to config/services.php:

// config/services.php
...
    'infobip' => [
        'auth'        => env('INFOBIP_AUTH','basic'),
        'username'    => env('INFOBIP_USERNAME'),
        'password'    => env('INFOBIP_PASSWORD'),
        'baseUrl'     => env('INFOBIP_BASE_URL'),
        'apikey'      => env('INFOBIP_PUBLIC_KEY'),
        'scenarioKey' => env('INFOBIP_SCENARIO_KEY'),
    ],
...

To change Base URL to personal use this (See more)

Usage

Now you can use the channel in your via() method inside the notification:

use NotificationChannels\Infobip\InfobipChannel;
use NotificationChannels\Infobip\InfobipMessage;
use Illuminate\Notifications\Notification;

class AccountApproved extends Notification
{
    public function via($notifiable)
    {
        return [InfobipChannel::class];
    }

    public function toInfobip($notifiable)
    {
    	$message = new InfobipMessage();
		$message->setTemplateName("infobip_test_hsm");
		$message->setTemplateNamespace("whatsapp:hsm:it:infobip");
		$message->setTemplateData(["Jhon","Snow"]);
		$message->setLanguage("es");
        return $message;
    }
}

In order to let your Notification know which phone are you sending to, the channel will look for the phone_number attribute of the Notifiable model. If you want to override this behaviour, add the routeNotificationForInfobip method to your Notifiable model.

public function routeNotificationForInfobip()
{
    return '+1234567890';
}

Available Message methods

InfobipMessage

  • setTemplateName(''): Accepts a string value.
  • setTemplateNamespace(''): Accepts a string value.
  • setTemplateData(['','',...]): Accepts an array of string.
  • setLanguage(''): Accepts a string value

Examples

Dispatching the notification

A. Using Laravel's notification facade

use App\Notifications\ExampleInfobipNotification;
use Illuminate\Support\Facades\Notification;

Notification::send($user, new ExampleInfobipNotification());

// where $user implements `Illuminate\Notifications\Notifiable` trait

B. Using the notify() method from Notifiable trait

use App\Notifications\ExampleInfobipNotification;

$user->notify(new ExampleInfobipNotification($invoice));

Example Notification class

<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Caherrera\Laravel\Notifications\Channels\Infobip\Omni\InfobipChannel;
use Caherrera\Laravel\Notifications\Channels\Infobip\Omni\InfobipMessage;

class ExampleInfobipNotification extends Notification
{
    public function via($notifiable)
    {
        return [InfobipChannel::class];
    }

    public function toInfobip($notifiable)
    {
        $message = new InfobipMessage();
		$message->setTemplateName("infobip_test_hsm");
		$message->setTemplateNamespace("whatsapp:hsm:it:infobip");
		$message->setTemplateData(["Jhon","Snow"]);
		$message->setLanguage("es");
        return $message;
        
    }
}

Example Notifiable class

<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;
}

For more details you can check out this link on Laravel documentation

Testing

$ ./vendor/bin/phpunit

Security

If you discover any security related issues, please help me and rise a ticket on issue tracker or simply fix it and I'll merge

Credits

License

The MIT License (MIT).