lbhurtado/missive

为 Laravel 项目添加 SMS 领域 - 路由、模型、迁移、作业、通知等。

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

README

Latest Version on Packagist Build Status Quality Score Total Downloads

为 Laravel 项目添加 SMS 领域 - 路由、模型、迁移、事件、作业、操作等。

安装

您可以通过 composer 安装此软件包

composer require lbhurtado/missive

发布供应商文件

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

可选,如果您想使用 SMS 收费

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

然后在 missive.php 中取消注释中间件

继承模型

namespace App;

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

class Contact extends BaseContact
{
    use HasEngageSpark;
}

自定义表和类,例如 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,
                    ],
                ],
            ]
        ]
]

在 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"));
    });
});

用法

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();

将 OTP 验证添加到您的联系人中

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
}

短信中继

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"
}'

短信 OTP 验证

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"
}'

短信充值

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"
}'

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全

如果您发现任何安全相关问题,请通过电子邮件 lester@hurtado.ph 而不是使用问题跟踪器。

鸣谢

许可

MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。

Laravel 包模板

此软件包是使用 Laravel 包模板 生成的。