ghasedak/laravel-notification

Ghasedak SMS Notifications Channel for Laravel

1.0.0 2020-10-13 13:09 UTC

This package is auto-updated.

Last update: 2024-11-14 17:18:48 UTC


README


Logo

Ghasedak Laravel Notification

Easy-to-use SDK for implementing Ghasedak SMS Notification in your Laravel projects.
Explore the docs »

Web Service Documents · REST API . Report Bug · Request Feature


contributors forks stars issues license

Table of Contents

Install

The easiest way to install is by using Composer:

composer require ghasedak/laravel-notification

Composer is a dependency manager for PHP which allows you to declare the libraries your project depends on, and it will manage (install/update) them for you. If you are not familiar with Composer, you can read its documentations and download it via getcomposer.org.

Setting up Ghasedak service

To setup Ghasedak service properly you need an API key. To get that you should have a Ghasedak account. Register and get your API key.
Then you need to set simple configuration by adding following code to your services.php file.

// config/services.php

'ghasedak' => [
    'api_key' => env("GHASEDAK_API_KEY"),
    'linenumber' => env('LINE_NUMBER', null),
]

Add environmental variables to .env file

As final step of installing the package, you must add previousley defined variables to .env file.

GHASEDAK_API_KEY=your_api_key
LINE_NUMBER=your_line_number

Don't forget to replace your_api_key and your_line_number with actual information.

Usage

To use notifications in Laravel you should first create one with simple Artisan command:

php artisan make:notification SendSimpleNotification

Then you can use the channel in your via() method inside the notification you just created:

namespace App\Http\Notifications;

use Ghasedak\LaravelNotification\GhasedakChannel;
use Ghasedak\LaravelNotification\GhasedakSimpleSms;
use Illuminate\Notifications\Notification;


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

    public function toSms($notifiable)
    {
        // send simple message
        return (new GhasedakSimpleSms)->message('Hello, World!')->linenumber('300xxxxx');
    }
}

As default phone field is set for receptor, but you can add routeNotificationForSms method to your Notifiable model to customize phone number:

public function routeNotificationForSms()
{
    return $this->phone;
}

Parameters of GhasedakSimpleSms() method

Sending Notifications

Notifications may be sent using the notify method of the Notifiable trait:

use Ghasedak\LaravelNotification\GhasedakSimpleSms;

$user->notify(new GhasedakSimpleSms());

Example

Here is a sample code for SendSimple Notification with support for custom parameters instead of using a fixed template:

// App\Http\Notifications\SendSimpleNotification.php

namespace App\Http\Notifications;

use Ghasedak\LaravelNotification\GhasedakChannel;
use Ghasedak\LaravelNotification\GhasedakSimpleSms;
use Illuminate\Notifications\Notification;

class SendSimpleNotification extends Notification
{
    public function __construct($params)
    {
        $this->params = $params;
    }

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

    public function toSms($notifiable)
    {
        return (new GhasedakSimpleSms)
            ->message($this->params['message'])
            ->linenumber($this->params['linenumber'])
            ->senddate($this->params['senddate'] ?? null)
            ->checkid($this->params['checkid'] ?? null);
    }
}

Send notification using notify method:

$arr = array(
            'message' => 'Hello, World!', // message 
            'linenumber' => '3000xxxxx', // choose a line number from your account
        );
$user->notify(new GhasedakSimpleSms($arr));

One-Time Passwords (OTP)

The One-Time-Password (OTP) Interface is used to perform a mobile authentication or to implement Two-Factor-Authentication (2FA).

$params = ['1', '2'];
$arr = array(
    'type' => 1,
    'template' => 'template',
    'params' => $params
);
$user->notify(new SendOTPNotification($arr));

Parameters

Example

The following is a sample code for SendOTP Notification with support for custom parameters instead of using a fixed template:

// App\Http\Notifications\SendOTPNotification.php

namespace App\Http\Notifications;

use Ghasedak\LaravelNotification\GhasedakChannel;
use Ghasedak\LaravelNotification\GhasedakOTPSms;
use Illuminate\Notifications\Notification;

class SendOTPNotification extends Notification
{
    public function __construct($params)
    {
        $this->params = $params;
    }

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

    public function toSms($notifiable)
    {
        return (new GhasedakOTPSms)
            ->type($this->params['type'])
            ->template($this->params['template'])
            ->params($this->params['params']);
    }

}

Send notification using notify method:

$params = ['param1', 'param2', 'param3'];   
$arr = array(
    'type' => 1,      // 1 for text message and 2 for voice message
    'template' => 'my-template', // name of the template which you've created in you account
    'params' => $params, // parameters (supporting up to 10 parameters) 
);
$user->notify(new SendSimpleNotification($arr));

:)

License

Freely distributable under the terms of the MIT license.