Giter Site home page Giter Site logo

sms's Introduction

Laravel SMS Gateway

Software License Latest Version on Packagist Total Downloads StyleCI Build Status Code Climate Quality Score

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

List of supported gateways:

Install

Via Composer

$ composer require tzsk/sms

Configure

If you are using Laravel 5.5 or higher then you don't need to add the provider and alias.

In your config/app.php file add these two lines.

# In your providers array.
'providers' => [
    ...
    Tzsk\Sms\Provider\SmsServiceProvider::class,
],

# In your aliases array.
'aliases' => [
    ...
    'Sms' => Tzsk\Sms\Facade\Sms::class,
],

Now run php artisan vendor:publish to publish config/sms.php file in your config directory.

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.

Chose 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 wan to use SNS.
'default' => 'sns',

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

// Eg. for SNS.
'drivers' => [
    'sns' => [
        // Fill all the credentials here.
        'key' => 'Your AWS SNS Access Key',
        'secret' => 'Your AWS SNS Secret Key',
        'region' => 'Your AWS SNS Region',
        'sender' => 'Your AWS SNS Sender ID',
        'type' => 'Tansactional', // Or: 'Promotional'
    ],
    ...
]

Textlocal Configuration:

Textlocal is added by default. You just have to change the creadentials in the textlocal driver section.

AWS SNS Configuration:

In case you want to use AWS SNS. Then you have to pull a composer library first.

composer require aws/aws-sdk-php

Nexmo Configuration:

In case you want to use Nexmo. Then you have to pull a composer library first.

composer require nexmo/client

Clockwork Configuration:

In case you want to use Clockwork. Then you have to pull a composer library first.

composer require mediaburst/clockworksms

Twilio Configuration:

In case you want to use Twilio. Then you have to pull a composer library first.

composer require twilio/sdk

Then you just have to change the creadentials in the twilio driver section.

Melipayamak Configuration:

In case you want to use Melipayamak. Then you have to pull a composer library first.

composer require melipayamak/php

Kavenegar Configuration:

In case you want to use Kavenegar. Then you have to pull a composer library first.

composer require kavenegar/php

SMS Gateway Me Configuration:

In case you want to use SMS Gateway Me. Then you have to pull a composer library first.

composer require smsgatewayme/client

Usage

In your code just use it like this.

# On the top of the file.
use Tzsk\Sms\Facade\Sms;

...

# In your Controller.
Sms::send("this message", function($sms) {
    $sms->to(['Number 1', 'Number 2']); # The numbers to send to.
});
# OR...
Sms::send("this message")->to(['Number 1', 'Number 2'])->dispatch();

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

# Here gateway is explicit : 'twilio' or 'textlocal' or any other driver in the config.
# The numbers can be a single string as well.

# If you are not a Laravel's facade fan, you can use sms helper:

sms()->send("this message", function($sms) {
    $sms->to(['Number 1', 'Number 2']); # The numbers to send to.
});

sms()->send("this message")->to(['Number 1', 'Number 2'])->dispatch();

sms()->via('gateway')->send("this message", function($sms) {
    $sms->to(['Number 1', 'Number 2']);
});

sms()->via('gateway')->send("this message")->to(['Number 1', 'Number 2'])->dispatch();

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 Tzsk\Sms\SmsBuilder;
use Illuminate\Bus\Queueable;
use Tzsk\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 repicients and body of the notification.
     *
     * @param  mixed  $notifiable
     * @return SmsBuilder
     */
    public function toSms($notifiable)
    {
        return (new SmsBuilder)->via('gateway') # via() is Optional
            ->send('this message')
            ->to('some number');
    }
}

Tip: You can use the same Builder Instance in the send method.

$builder = (new SmsBuilder)->via('gateway') # via() is Optional
    ->send('this message')
    ->to('some number');

Sms::send($builder);

# OR...
$builder = (new SmsBuilder)->send('this message')
    ->to(['some number']);

Sms::via('gateway')->send($builder);

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.

'drivers' => [
    'textlocal' => [...],
    'twilio' => [...],
    'my_driver' => [
        ... # Your Config Params here.
    ]
]

Now you have to create a Driver Map Class that will be used to send the SMS. In your driver, You just have to extend Tzsk\Sms\Abstracts\Driver.

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

namespace App\Packages\SMSDriver;

use Tzsk\Sms\Abstracts\Driver;

class MyDriver extends Driver 
{
    # You will have to make 2 methods.
    /**
    * 1. __constructor($settings) # {Mandatory} This settings is your Config Params that you've set.
    * 2. send() # (Mandatory) This is the main message that will be sent.
    *
    * Example Given below:
    */

    /**
    * @var object
    */
    protected $settings;

    /**
    * @var mixed
    */
    protected $client;

    /**
    * Your Driver Config.
    *
    * @var array $settings
    */
    public function __construct($settings)
    {
        $this->settings = (object) $settings;
        # Initialize any Client that you want.
        $this->client = new Client(); # Guzzle Client for example.
    }

    /**
    * @return object Ex.: (object) ['status' => true, 'data' => 'Client Response Data'];
    */
    public function send()
    {
        $this->recipients; # Array of Recipients.
        $this->body; # SMS Body.

        # Main logic of Sending SMS.
        ...
    }

}

Once you crate that class you have to specify it in the sms.php Config file map section.

'map' => [
    ...
    'my_driver' => App\Packages\SMSDriver\MyDriver::class,
]

Note:- You have to make sure that the key of the map array is identical to the key of the drivers array.

Change log

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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

sms's People

Contributors

andvenaa avatar dependabot-preview[bot] avatar dijonneto avatar emanuelecoppola avatar khanzadimahdi avatar mohammadhsn avatar roshedgostarandev1 avatar sae13 avatar scrutinizer-auto-fixer avatar tzsk avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.