fowitech/laravel-sms

Persistent sms package for Laravel

dev-main 2024-03-03 15:10 UTC

This package is auto-updated.

Last update: 2024-11-03 16:59:00 UTC


README

GitHub License Latest Version on Packagist GitHub Tests Action Status Total Downloads

This is a Laravel Package for SMS Gateway Integration. Now Sending SMS is easy.

List of supported gateways:

📦 Install

Via Composer

$ composer require fowitech/laravel-sms

⚡ Configure

Publish the config file

$ php artisan vendor:publish --tag="sms"

In the config file you can set the default driver to use for all your SMS. But you can also change the driver at runtime.

Choose what gateway you would like to use for your application. Then make that as default driver so that you don't have to specify that everywhere. But, you can also use multiple gateways in a project.

// Eg. if you want to use Netgsm.
 'driver' => env('SMS_DRIVER', 'netgsm'),

Then fill the credentials for that gateway in the drivers array.

// Eg. for Netgsm.
'netgsm' => [
    'transport' => \Fowitech\Sms\Drivers\Netgsm::class,
    'sender' => env('NETGSM_SENDER', ''),
    'username' => env('NETGSM_USERNAME', ''),
    'password' => env('NETGSM_PASSWORD', ''),
    'options' => [
        // some options
    ]
],

🔥 Usage

In your code just use it like this.

# On the top of the file.
use Sms;

...

# In your Controller.
Sms::message("this message")->to(['Number 1', 'Number 2'])->send();

# If you want to use a different driver.
Sms::via('gateway')->message("this message")->to(['Number 1', 'Number 2'])->send();

# If you want to use options array
$options = [
    'send_date' => now()->addHour()
];
Sms::via('gateway')->message("this message")->to(['Number 1', 'Number 2'])->send($options);

# If you are not a Laravel's facade fan, you can use sms helper:
sms()->message("this message")->to(['Number 1', 'Number 2'])->send();

# If you want to use a different driver.
sms()->via('gateway')->message("this message")->to(['Number 1', 'Number 2'])->send();

😍 Channel Usage

First you have to create your notification using php artisan make:notification command. then SmsChannel::class can be used as channel like the below:

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Fowitech\Sms\Channels\SmsChannel;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;

class InvoicePaid extends Notification
{
    use Queueable;

    /**
     * Get the notification channels.
     *
     * @param  mixed  $notifiable
     * @return array|string
     */
    public function via($notifiable)
    {
        return [SmsChannel::class];
    }

    /**
     * Get the recipients and body of the notification.
     *
     * @param  mixed  $notifiable
     * @return Builder
     */
    public function toSms($notifiable)
    {
        return "Sms message";
    }
}

Custom Made Driver, How To:

First you have to name your driver in the drivers array and also you can specify any config params you want.

'my_driver' => [
    'transport' => \App\Packages\SMSDriver\MyDriver::class,
    'sender' => env('MYDRIVER_SENDER', ''),
    'username' => env('MYDRIVER_USERNAME', ''),
    'password' => env('MYDRIVER_PASSWORD', ''),
    'options' => [
        // some options
    ]
    ... # Your Config Params here.
]

Ex. You created a class : App\Packages\SMSDriver\MyDriver.

namespace App\Packages\SMSDriver;

use Fowitech\Sms\Drivers\Driver;

class MyDriver extends Driver 
{
    public function __construct($options = [])
    {
        $this->sender = config('sms.my_driver.sender');
        $this->username = config('sms.my_driver.username');
        $this->password = config('sms.my_driver.password');
        $this->client = $this->getInstance();
    }

    public function send($options = [])
    {
        # Main logic of Sending SMS.
    }
}

🔬 Testing

composer test