lbhurtado/missive

Add SMS domain to a Laravel project - route, models, migrations, jobs, notifications, etc.

v2.3.0 2022-04-16 12:33 UTC

README

Latest Version on Packagist Build Status Quality Score Total Downloads

Add SMS domain to a Laravel project - route, models, migrations, events, jobs, actions, etc.

Installation

You can install the package via composer:

composer require lbhurtado/missive

publish vendor files:

php artisan vendor:publish --provider="LBHurtado\Missive\MissiveServiceProvider"
php artisan notifications:table
php artisan migrate

optionally, if you want sms charging:

composer dumpautoload
php artisan db:seed --class=AirtimeSeeder

then uncomment the middleware in missive.php

inherit models:

namespace App;

use LBHurtado\EngageSpark\Traits\HasEngageSpark;
use LBHurtado\Missive\Models\Contact as BaseContact;

class Contact extends BaseContact
{
    use HasEngageSpark;
}

customize the tables and classes e.g. App\Contact:

[
	'table_names' => [
		'smss'     => 's_m_s_s',
		'contacts' => 'contacts',
		'relays'   => 'relays',
        'topups'   => 'topups',
	],
    'classes' => [
            'models' => [
                'airtime' => \LBHurtado\Missive\Models\Airtime::class,
                'contact' => \App\Contact::class,
                'relay' => \LBHurtado\Missive\Models\Relay::class,
                'sms' => \LBHurtado\Missive\Models\SMS::class
            ],
            'commands' => [
                'sms' => [
                    'create' => \LBHurtado\Missive\Commands\CreateSMSCommand::class
                ]
            ],
            'handlers' => [
                'sms' => [
                    'create' => \LBHurtado\Missive\Handlers\CreateSMSHandler::class
                ]
            ],
            'middlewares' => [
                'sms' => [
                    'relay' => [
                        \LBHurtado\Missive\Validators\CreateSMSValidator::class,
                        \LBHurtado\Missive\Responders\CreateSMSResponder::class,
    //                \LBHurtado\Missive\Actions\Middleware\ChargeSMSMiddleware::class,
                    ],
                    'verify' => [
                        \LBHurtado\Missive\Validators\CreateSMSValidator::class,
                        \LBHurtado\Missive\Responders\CreateSMSResponder::class,
                        \LBHurtado\Missive\Actions\Middleware\VerifyContactHandler::class,
    //                    \LBHurtado\Missive\Actions\Middleware\ChargeSMSMiddleware::class,
                    ],
                    'topup' => [
                        \LBHurtado\Missive\Validators\CreateSMSValidator::class,
                        \LBHurtado\Missive\Responders\CreateSMSResponder::class,
                        \LBHurtado\Missive\Actions\Middleware\TopupMobileHandler::class,
    //                    \LBHurtado\Missive\Actions\Middleware\ChargeSMSMiddleware::class,
                    ],
                ],
            ]
        ]
]

customize the routes in routes/sms.php:

use LBHurtado\EngageSpark\Notifications\Adhoc;

$router = resolve('missive:router');

$router->register('LOG {message}', function (string $path, array $values) use ($router) {
    \Log::info($values['message']);
    
    tap($router->missive->getSMS()->origin, function ($contact) use ($values) {
        $message = $values['message'];
        $contact->notify(new Adhoc("{$contact->mobile}: $message"));
    });
});

Usage

use LBHurtado\Missive\Models\SMS;
use LBHurtado\Missive\Jobs\CreateSMS;
use LBHurtado\Missive\Repositories\SMSRepository;

CreateSMS::dispatch($attributes);
$sms = SMS::first();

$smss = app(SMSRepository::class);
$sms = $smss->first();
use LBHurtado\Missive\Models\Contact;
use LBHurtado\Missive\Jobs\CreateContact;
use LBHurtado\Missive\Repositories\ContactRepository;

CreateContact::dispatch($mobile);
$contact = Contact::first();

$contacts = app(ContactRepository::class);
$contact = $contacts->first();
use LBHurtado\Missive\Models\Relay;
use LBHurtado\Missive\Jobs\CreateRelay;
use LBHurtado\Missive\Repositories\RelayRepository;

CreateRelay::dispatch($mobile);
$relay = Relay::first();

$relays = app(RelayRepository::class);
$relay = $relays->first();

add otp verification to your contacts:

use LBHurtado\Missive\Traits\HasOTP;
use LBHurtado\Missive\Models\Contact;

$contact = Contact::find(1);
$otp = $contact->challenge()->now();
//default period for OTP is 360 seconds
//modify it in .env i.e. DEFAULT_OTP_PERIOD=1000


if ($contact->verify($otp) == true) {
    //code here
}

sms relay

curl -X POST \
  http://laravel.app/api/sms/relay \
  -H 'Accept: */*' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json' \
  -H 'Host: laravel.app' \
  -d '{
    "secret": "CFAWG4KCE44XWACTZZX24Z7LPW99XTWT",
    "from": "+639171234567",
    "to": "+639187654321",
    "message": "LOG test message"
}'

sms OTP verification

curl -X POST \
  http://laravel.app/api/sms/verify \
  -H 'Accept: */*' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json' \
  -H 'Host: laravel.app' \
  -d '{
    "secret": "CFAWG4KCE44XWACTZZX24Z7LPW99XTWT",
    "from": "+639171234567",
    "to": "+639187654321",
    "message": "123456"
}'

sms topup

curl -X POST \
  http://laravel.app/api/sms/topup \
  -H 'Accept: */*' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json' \
  -H 'Host: laravel.app' \
  -d '{
    "secret": "CFAWG4KCE44XWACTZZX24Z7LPW99XTWT",
    "from": "+639171234567",
    "to": "+639187654321",
    "message": "09171234567 25"
}'

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email lester@hurtado.ph instead of using the issue tracker.

Credits

License

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

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.