craftsys / msg91-laravel-notification-channel
为 MSG91 APIs 提供的 Laravel 通知通道。
Requires
- php: ^7.1.3|^8.0|^8.1
- craftsys/msg91-laravel: ^0.15.0
- illuminate/notifications: ~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ~3.8|^4.0|^5.0|^6.0|^7.0|^8.0|^9.0
- phpunit/phpunit: ^7.0|^8.0|^9.4|^10.0
README
Laravel 通知通道用于 Msg91 API(Laravel Msg91 客户端的封装)
目录
安装
先决条件
- php^7.1
- laravel^5|^6|^7|^8|^9|^10
该包仅在 5.8+、^6.0、^7.0、^8.0、^9.0、^10.0 上进行了测试。如果您在 laravel (5.0<5.8) 上发现任何错误,请提交问题。
composer require craftsys/msg91-laravel-notification-channel
如果您只想在 Laravel 中集成 Msg91 API 而不使用通知通道,请使用 Msg91 Laravel。
配置
接下来,您需要在您的 config/services.php 配置文件中添加一些配置选项。您可以将下面的示例配置复制过去开始使用
// along with other services 'msg91' => [ 'key' => env("MSG91_KEY") ]
所有可用的配置可以在 msg91-php 客户端配置页面 上找到
使用
如果通知支持以短信的形式发送,您应该在通知类上定义一个 toMsg91 方法。此方法将接收一个 $notifiable 实体,并根据您需要发送消息或发送 OTP,返回一个 Craftsys\Notifications\Messages\Msg91SMS 或 Craftsys\Notifications\Messages\Msg91OTP 实例。
注意:电话号码必须使用国际格式,即必须包含国家代码。
<?php namespace App\Notifications; use Illuminate\Notifications\Notification; use Craftsys\Notifications\Messages\Msg91SMS class OrderPicked extends Notification { /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { // add "msg91" channel to the channels array return ['msg91']; } /** * Get the Msg91 / SMS representation of the notification. * * @param mixed $notifiable * @return \Craftsys\Notifications\Messages\Msg91SMS */ public function toMsg91($notifiable) { return (new Msg91SMS) ->flow('your_flow_id_here') // you can also set variable's values for your flow template // assuming you have ##order_id## variable in the flow ->variable('order_id', $notifiable->latestOrder->id); } }
短信
// your Notification public function toMsg91($notifiable) { return (new \Craftsys\Notifications\Messages\Msg91SMS) ->flow("your_flow_id"); } // with variables public function toMsg91($notifiable) { return (new \Craftsys\Notifications\Messages\Msg91SMS) ->flow("your_flow_id") ->variable('name', $notifiable->name) ->variable('status', "Overdue"); }
OTP
// your Notification public function toMsg91($notifiable) { return (new \Craftsys\Notifications\Messages\Msg91OTP) ->from('12123'); // ->otp(12123) // set a custom otp // ->resend() // if this is a resend otp notification }
验证 OTP
该包包括 Laravel Msg91 Client,因此您可以使用该包提供的所有 API,例如验证 OTP、不使用通知通道发送 OTP 等。
您可以使用 Msg91 外观来访问客户端,如下所示
$otp_to_verify = 112312; Msg91::otp($otp_to_verify)->to(919999999998)->verify();
路由短信通知
当通过 msg91 通道发送通知时,通知系统会自动在通知实体上查找 phone_number 属性。如果您想自定义通知发送到的电话号码,可以在实体上定义一个 routeNotificationForMsg91 方法,如 laravel 文档 中建议的那样。
class User { use Notifiable; /** * Route notifications for the Msg91 channel. * * @param \Illuminate\Notifications\Notification $notification * @return string */ public function routeNotificationForMsg91 ($notification) { return $this->phone; } }
您也可以在 toMsg91 方法中设置接收者,如下所示
public function toMsg91($notifiable) { return (new \Craftsys\Notifications\Messages\Msg91SMS) ->to(91992123123) // you can also pass an array for bulk notifications ->flow('your_flow_id'); }
高级使用
这些消息 Msg91SMS 和 Msg91OTP 继承自 \Craftsys\Msg91\SMS\Options 和 \Craftsys\Msg91\OTP\Options,因此当您构建通知消息时,所有配置方法都是可用的。这些都是可选的,您可以使用任何顺序使用它们。例如:
public function toMsg91($notifiable) { return (new \Craftsys\Notifications\Messages\Msg91OTP) ->digits(6) // set the digits in otp message ->expiresInMinutes(1) // change the expiry time ->from("SNCBD"); // set a custom sender id }