hanymigame/laravel-smsru-channel

Sms.ru Notification Channel for laravel.

dev-main 2022-12-05 08:07 UTC

This package is auto-updated.

Last update: 2024-05-05 11:09:31 UTC


README

Install

composer require hanymigame/laravel-smsru-channel
php artisan vendor:publish --provider="hanymigame\SmsRuChannel\SmsRuServiceProvider"

Add to .env file:

SMS_RU_API_KEY=you_api_key
SMS_RU_SENDER=
SMS_RU_TRANSLIT=0
SMS_RU_TEST=1
SMS_RU_PARTNER_ID=0

Usage

php artisan make:notification SMSNotification

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

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use hanymigame\SmsRuChannel\Messages\SmsRuMessage;
use hanymigame\SmsRuChannel\Channels\SmsRuChannel;

class SMSNotification extends Notification
{
    use Queueable;
    
    public function __construct()
    {
        //
    }
    
    public function via($notifiable)
    {
        return [SmsRuChannel::class];
    }

    public function toSmsRu($notifiable)
    {
        return new SmsRuMessage("Привет! Это тестовое СМС.");
    }

    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

Add in controller, model or command class:

Notification::route('sms_ru_channel', '79301579978')->notify(new SMSNotification());

Edit: app/Providers/EventServiceProvider.php

use Illuminate\Notifications\Events\NotificationSent;
use App\Listeners\ResponseNotification;
use App\Listeners\CheckNotificationStatus;
use Illuminate\Notifications\Events\NotificationSending;

class EventServiceProvider extends ServiceProvider
{

protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        NotificationSending::class => [
            CheckNotificationStatus::class,
        ],
        NotificationSent::class => [
            ResponseNotification::class,
        ],
    ];
    
    ...
    
}

Use command:

php artisan event:generate
php artisan optimize:clear

Add to app/Listeners/ResponseNotification.php

public function handle(NotificationSent $event)
    {
        // $event->channel
        // $event->notifiable
        // $event->notification
        // $event->response
        dump($event->response); // Get response status
    }