Giter Site home page Giter Site logo

laravel-msg91's Introduction

Laravel MSG91 - MSG91 Integration For Laravel

About

This package integrates MSG91's SMS solution nicely with Laravel 5 adding support for Notification & Validator as well.

Registration

Sign up for MSG91 and get the auth key from your account. You can find the auth key from Dashboard > API key after signing in.

Installation

composer require androidneha/laravel-msg91

Laravel < 5.5

Once the package is installed, open your app/config/app.php configuration file and locate the providers key. Add the following line to the end:

Laravel\Msg91\ServiceProvider::class

Next, locate the aliases key and add the following line:

'Msg91' => Laravel\Msg91\Facade::class,

Configuration

Put the credentials and preferences in ENV with the keys MSG91_AUTH_KEY, MSG91_DEFAULT_SENDER, MSG91_DEFAULT_ROUTE, MSG91_DEFAULT_COUNTRY. If you want to customize this, publish the default configuration which will create a config file config/msg91.php.

$ php artisan vendor:publish

Usage

Basic

  • Send an SMS to one or more numbers.
<?php

$result = Msg91::sms('919999999999', 'Hello there!');
 
$result = Msg91::sms('919999999999', 'Hello there!', 'TEST12');
 
$result = Msg91::sms(null, [
    ['to' => ['919999999999', '918888888888'], 'message' => 'Hello there!'],
    ['to' => ['917777777777'], 'message' => 'Come here!'],
], 'TEST12');
  • Send OTP to a number.
<?php

$result = Msg91::otp('919999999999');
   
$result = Msg91::otp('919999999999', 'TEST12');
   
$result = Msg91::otp('919999999999', 'TEST12', '##OTP## is your OTP, Please dont share it with anyone.');
  • Verify OTP sent to a number.
<?php

$result = Msg91::verify('919999999999', 1290); // returns true or false

Notification

Include msg91 in your notification's channels:

<?php

/**
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['msg91'];
}

Define the toMsg91 method:

<?php

use Laravel\Msg91\Message\Msg91Message;

public function toMsg91()
{
    return (new Msg91Message)
        ->message(__('This is just a test message.'))
	->sender('MESG91') // [Optional] - Will pick default sender ID from MSG91_DEFAULT_SENDER or config
	->transactional(); // or promotional() [Optional] - Will pick default route from MSG91_DEFAULT_ROUTE or config
}

Default routeNotificationForMsg91 method in your notifiable class:

<?php

public function routeNotificationForMsg91($notification)
{
    return $this->phone_number;
}

Finally send the notification:

<?php

$notifiable = /* some class */
$notifiable->notify(new App\Notifications\Msg91TestNotification());

For sending the notification to an arbitrary number, use below syntax:

<?php
use Illuminate\Support\Facades\Notification

Notification::route('msg91', '919876543210')
    ->notify(new App\Notifications\Msg91TestNotification());

Validator

You can validate sent OTPs using provided validation rule named msg91_otp as shown below:

<?php

use Illuminate\Support\Facades\Validator

$data = ['number' => '9876543210', 'otp' => '1234'];

$validator = Validator::make($data, [
    'number' => ['required', 'digits:10'],
    'otp' => ['required', 'digits:4', 'msg91_otp'], // default key for source number is 'number', you can customize this using 'msg91_otp:key_name'
]);

if ($validator->fails()) {
    // report errors
}

License

See LICENSE file.

laravel-msg91's People

Contributors

androidneha avatar vaibhavpandeyvpz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

martianatwork

laravel-msg91's Issues

Undefined type 'Laravel\Msg91\ServiceProvider'

Can you check this on Laravel 7x ? Undefined type 'Laravel\Msg91\ServiceProvider' on app.php as your classes names are different, & you had mentioned different classes in the composer. Totally messed up codes.

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.