定制 rolfhaug/laravel-front-sms 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rolfhaug/laravel-front-sms

Composer 安装命令:

composer require rolfhaug/laravel-front-sms

包简介

A Laravel package for sending SMS through Front SMS Gateway (fro.no)

README 文档

README

Send SMS to your users with Laravel and Front SMS Gateway API through the traditional notification interface of Laravel.

The package support outbound and inbound SMS, as well as and incoming delivery reports.

Quick usage example

  1. Create new SMS message
php artisan make:sms WelcomeMessage

Adjust your newly created message.

<?php

namespace App\Notifications\Sms;

use RolfHaug\FrontSms\Notifications\SmsNotification;

class WelcomeMessage extends SmsNotification
{
    public $message = 'Hi %s, today was a good day!';

    public function getMessage($notifiable)
    {
        return vsprintf($this->message, [
            $notifiable->name,
        ]);
    }
}

Send it through the notifiable user.

$user = App\User::first();

$user->notify(new \App\Notifications\Sms\WelcomeMessage);

Installation

The Laravel Front SMS package can be installed via composer:

composer require rolfhaug/laravel-front-sms

The package will automatically register a service provider.

You need to publish the config and migration files:

php artisan vendor:publish --provider="RolfHaug\FrontSms\FrontSmsServiceProvider"

If you do not have a phone column on the user model already. Publish the user related migrations:

php artisan vendor:publish --provider="RolfHaug\FrontSms\FrontSmsServiceProvider" --tag=user-migrations

Please note: If you're serving only one market, you can optionally delete the country_code migration file and rely on the config setting instead. More on this later.

Then run the migrations

php artisan migrate

If you use Artisan’s migrate:rollback or migrate:refresh commands you should edit the migration files and uncomment the down methods.

Configure

Add your configuration in your .env file.

# You'll get these fron Front in your settings page
FRONT_SERVICE_ID=
FRONT_SENDER_ID=

# Password if you're not authenticating by IP
FRONT_PASSWORD=

# ISO 3166-2 Geographic Region code (E.g. GB, NO, SE)
FRONT_DEFAULT_REGION=

# Debugging: print messages to the log instead of sending to Front
FRONT_FAKE_MESSAGES=false

If you want to receive Delivery Reports or Inbound SMS Messages you must exclude the SMS api routes from CSRF Verification.

// app/Http/Middleware/VerifyCsrfToken.php

class VerifyCsrfToken extends Middleware
{
    protected $except = [
        'sms/*',
    ];
}

Register your Delivery report URL in your Front settings page

yourdomain.com/sms/report

Register your Incoming SMS URL in your Front settings page

yourdomain.com/sms/inbound

Then add the RolfHaug\FrontSms\Traits\Smsable trait to your user model.

<?php

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use RolfHaug\FrontSms\Traits\Smsable;

class User extends Authenticatable
{
    use Notifiable, Smsable;

    // ...
}

Required data

Country Code

All messages must have a known geographic region (called country code), so we can format the recipients number correctly. This can be set three ways:

  1. By specifying an E.164 formatted phone number. E.g. +4790012345
  2. By specifying a country_code field on the user model (database column or Eloquent accessor)
  3. Setting default region in the config file. E.g. FRONT_DEFAULT_REGION=NO

If you're serving one market, it is recommended to stick to option 3. If you're serving multiple markets, it's recommended to go with option 2.

Phone field

Your user model (or notifiable model) must have a phone field.

The field name for the phone can be customized in the .env file. If you for instance want to use a field called telephone instead, you can add the following to your .env file.

notifiablePhoneKey=telephone

Traits

If you want to send SMS to your User model, you must use the Notifiable trait and the Smsable trait (see paths in above example).

Notifiable is a native Laravel trait. The Smsable trait adds two methods getFormattedPhone() and getCountryCode() that are necessary to successfully send SMS. These methods validate and format the recipients number before sending the SMS.

Create new messages

The package provides a convenient command to create new messages.

php artisan make:sms WelcomeMessage

This will create and scaffold a new SMS notification class under the App/Notifications/Sms directory.

<?php

namespace App\Notifications\Sms;

use RolfHaug\FrontSms\Notifications\SmsNotification;

class WelcomeMessage extends SmsNotification
{
    public $message = 'Hi %s, today was a good day!';

    public function getMessage($notifiable)
    {
        return vsprintf($this->message, [
            $notifiable->name,
        ]);
    }
}

This can be sent through a user that uses the Notifiable and Smsable traits, as mentioned above.

$user = App\User::first();
$user->notify(new WelcomeMessage);

You are free to store your classes in a different directory. It should extend the RolfHaug\FrontSms\Notifications\SmsNotification class.

You can also use the SmsNotification class directly.

$message = new SmsNotification('This is my text message.');

// Send it to a user
$user = App\User::first();
$user->notify($message);

Inbound Messages

Inbound messages can be access through the RolfHaug\FrontSms\FrontInboundMessage model.

use RolfHaug\FrontSms\FrontInboundMessage;

$messages = FrontInboundMessage::where('sent_at', now()->startOfDay())->get();

If you prefer to use another model name, you could create a new model in your project and extend the FrontInboundMessage model.

namespace App\Models;

class IncomingSms extends FrontInboundMessage {}

Then get all inbound messages like this:

$messages = App\Models\IncomingSms::all();

Tips

It is recommended to use vsprintf in the getMessage function to compile dynamic data from the notifiable (user), like the example above.

If you want to pass in additional dependencies, it can be done in the constructor.

<?php

namespace App\Notifications\Sms;

use RolfHaug\FrontSms\Notifications\SmsNotification;
use App\Order;

class OrderConfirmation extends SmsNotification
{
    private $order;

    public function __construct($message = null, Order $order)
    {
        $this->order = $order;
        parent::__construct($message);
    }

    public $message = 'Hi %s, thank you for your order! You can download your receipt here %s';

    public function getMessage($notifiable)
    {
        return vsprintf($this->message, [
            $notifiable->name,
            $this->order->receiptLink()
        ]);
    }
}

Success 🎉

You are now ready to send SMS to your users.

Route - Delivery Reports

Front will send delivery reports on outbound SMS messages to the url you defined in your Front settings page. By default the route should be set to yourdomain.com/sms/reports.

You can override it by creating your own POST route and use RolfHaug\FrontSms\Http\Controllers\DeliveryStatusController@store.

Route::post('custom/path/to/sms/report', [\RolfHaug\FrontSms\Http\Controllers\DeliveryStatusController::class, 'store'])->name('sms.report.store');

Route - Inbound SMS Messages

Front will send inbound SMS messages to the url you defined in your Front settings page. By default the route should be set to yourdomain.com/sms/inbound.

You can override it by creating your own POST route and use RolfHaug\FrontSms\Http\Controllers\InboundMessageController@store.

Route::post('custom/path/to/sms/report', [\RolfHaug\FrontSms\Http\Controllers\InboundMessageController::class, 'store'])->name('sms.inbound.store');

Testing

The package use the Laravel Notification Interface, so you can test the notifications with Notification Fake.

There are many ways to test on-demand notifications, one way to do it like this:

use RolfHaug\FrontSms\Channels\SmsChannel;
use App\Notifications\Sms\MySmsNotification

// Send anonymous / on-demand notification
Notification::route(SmsChannel::class, '+4799887766')->notify(new MySmsNotification($dependencies));

Tests could look something like this:

use Illuminate\Support\Facades\Notification
use Illuminate\Notifications\AnonymousNotifiable
use RolfHaug\FrontSms\Channels\SmsChannel;
use App\Notifications\Sms\MySmsNotification

Notification::fake();
// Trigger SMS

// Use assertSentOnDemand "helper"
Notification::assertSentOnDemand(MySmsNotification::class, function($notification, $channels, $notifiable){
    // Do any inspections necessary
    return $notifiable->routes[SmsChannel::class] === '+4799887766';
});

// Or use "raw" assertSentTo
Notification::assertSentTo(new AnonymousNotifiable(), MySmsNotification::class, function ($notification, $channels, $notifiable){
    // Do any inspections necessary
    return $notifiable->routes[SmsChannel::class] === '+4799887766';
});

Debugging

Set FRONT_FAKE_MESSAGES=true in your .env file to get messages outputted in the Laravel Log. Messages will not be sent to Front with this feature enabled.

Incoming SMS to local environment

If you want to test your system by receiving actual DeliveryReports or Inbound SMS Messages you can use ngrok.

  1. Create a free account claim your static domain
  2. Claim your static domain from ngrok
  3. Enter your static domain in the Front settings page your-ngrok-domain.com/sms/report and your-ngrok-domain.com/sms/inbound
  4. Activate ngrok in your local environment ngrok http --domain=your-ngrok-domaincom 80
  5. Inbound SMS and Delivery reports will now arrive in your local environment

rolfhaug/laravel-front-sms 适用场景与选型建议

rolfhaug/laravel-front-sms 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.98k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 06 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 rolfhaug/laravel-front-sms 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rolfhaug/laravel-front-sms 我们能提供哪些服务?
定制开发 / 二次开发

基于 rolfhaug/laravel-front-sms 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.98k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 7
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-06-05