2017 © Pedro Peláez
 

library laravel-sms-clicksend

ClickSend Notifications channel for Laravel 5.4.

image

vladski/laravel-sms-clicksend

ClickSend Notifications channel for Laravel 5.4.

  • Monday, May 1, 2017
  • by vladski
  • Repository
  • 1 Watchers
  • 1 Stars
  • 35 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 2 Versions
  • 25 % Grown

The README.md

ClickSend notifications channel for Laravel 5.4+

This package makes it easy to send notifications using clicksend.com with Laravel 5.4+. Uses ClickSend PHP API wrapper [https://github.com/ClickSend/clicksend-php], (*1)

Contents

Installation

Install the package via composer:, (*2)

composer require vladski/laravel-sms-clicksend

Add the service provider to config/app.php:, (*3)

...
'providers' => [
    ...
    NotificationChannels\ClickSend\ClickSendServiceProvider::class,
],
...

Add your ClickSend username, api_key and optional default sender sms_from to your config/services.php:, (*4)

...
'clicksend' => [
    'username' => env('CLICKSEND_USERNAME'),
    'api_key'  => env('CLICKSEND_API_KEY'),
    'sms_from' => env('CLICKSEND_SMS_FROM'), // optional
],
...

Usage

Use ClickSendChannel in via() method inside your notification classes. Example:, (*5)

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use NotificationChannels\ClickSend\ClickSendMessage;
use NotificationChannels\ClickSend\ClickSendChannel;

class ClickSendTest extends Notification
{

    public $token;

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

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

    public function toClickSend($notifiable)
    {
        // statically create message object:

        $message = ClickSendMessage::create("SMS test to user #{$notifiable->id} with token {$this->token} by ClickSend");

        // OR instantiate:

        $message = new ClickSendMessage("SMS test to user #{$notifiable->id} with token {$this->token} by ClickSend");

        // available methods:

        $message->content("SMS test to user #{$notifiable->id} with token {$this->token} by ClickSend");
        $message->from('+6112345678'); // override sms_from from config

        return $message;
    }
}

In notifiable model (User), include method routeNotificationForClickSend() that returns recipient mobile number:, (*6)

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

From controller then send notification standard way:, (*7)


$user = User::find(1); try { $user->notify(new ClickSendTest('ABC123')); } catch (\Exception $e) { // do something when error return $e->getMessage(); }

Events

Following events are triggered by Notification. By default: - Illuminate\Notifications\Events\NotificationSending - Illuminate\Notifications\Events\NotificationSent, (*8)

and this channel triggers one when submission fails for any reason: - Illuminate\Notifications\Events\NotificationFailed, (*9)

To listen to those events create listener classes in app/Listeners folder e.g. to log failed SMS:, (*10)


namespace App\Listeners; use Illuminate\Notifications\Events\NotificationFailed; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Support\Facades\Log; use NotificationChannels\ClickSend\ClickSendChannel; class NotificationFailedListener { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Notification failed event handler * * @param NotificationFailed $event * @return void */ public function handle(NotificationFailed $event) { // Handle fail event for ClickSend // if($event->channel == ClickSendChannel::class) { echo 'failed'; dump($event); $logData = [ 'notifiable' => $event->notifiable->id, 'notification' => get_class($event->notification), 'channel' => $event->channel, 'data' => $event->data ]; Log::error('Notification Failed', $logData); } // ... handle other channels ... } }

Then register listeners in app/Providers/EventServiceProvider.php, (*11)

...
protected $listen = [

    'Illuminate\Notifications\Events\NotificationFailed' => [
        'App\Listeners\NotificationFailedListener',
    ],

    'Illuminate\Notifications\Events\NotificationSent' => [
        'App\Listeners\NotificationSentListener',
    ],

    'Illuminate\Notifications\Events\NotificationSending' => [
        'App\Listeners\NotificationSendingListener',
    ],
];
...

API Client

To access the rest of ClickSend API you can get client from ClickSendApi:, (*12)

$client = app(ClickSendApi::class)->getClient();

// then get for eaxample yor ClickSend account details:
$account =  $client->getAccount()->getAccount();

// or list of countries:
$countries =  $client->getCountries()->getCountries();

Changelog

Please see CHANGELOG for more information what has changed recently., (*13)

Testing

Incomplete bash $ composer test, (*14)

Contributing

Please see CONTRIBUTING for details., (*15)

Credits

License

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

The Versions