zsardarov/laravel-msg

MSG.ge integration for Laravel

v1.0.0 2021-03-07 10:11 UTC

This package is auto-updated.

Last update: 2024-04-12 21:05:15 UTC


README

The package for sending SMS messages using MSG.ge API.

Installation

composer require zsardarov/laravel-msg

You can publish config using:

php artisan vendor:publish --provider="Zsardarov\Msg\MsgServiceProvider"

Update the .env file

MSG_ALTERNATIVE_PASSWORD is optional.

MSG_SERVICE_ID=1
MSG_CLIENT_ID=1
MSG_PASSWORD=password
MSG_USERNAME=user
MSG_ALTERNATIVE_PASSWORD=

Usage

use Zsardarov\Msg\MsgService;
use Zsardarov\Msg\Enums\GatewayStatus;
use Zsardarov\Msg\Enums\DeliveryStatus;

class SampleController extends Controller
{
    public function sms(MsgService $sender)
    {        
        $result = $sender->send('9955XXXXXXXX', 'Text');
        
        if ($result->getStatusCode() === GatewayStatus::ACCEPTED) {
            // now we can check status
            $status = $sender->status($result->getMessageId());
            
            if ($status === DeliveryStatus::PENDING || $status === DeliveryStatus::SENT) {
                // message has been sent
            }
        }
    }
}

Notifications

To use it with Laravel notifications, you firstly must add toMsg() method in User model:

class User extends Authenticatable
{
    use Notifiable; 
    
    public function toMsg()
    {
        return $this->mobile;
    }
}

Create notification:

php artisan make:notification SmsNotification
use Illuminate\Notifications\Notification;
use Zsardarov\Msg\Channels\MsgChannel;

class SmsNotification extends Notification
{
    private $message;
    
    public function __construct(string $message) 
    {
        $this->message = $message;
    }
    
    public function via($notifiable)
    {
        return [MsgChannel::class];
    }
    
    public function toSms()
    {
        return $this->message;
    }
}

Then send notification to user:

$user->notify(new SmsNotification('Sample'));