jordanhavard / laravel-sms-clicksend
Laravel 9 的 ClickSend 通知通道
2.3
2023-11-05 23:04 UTC
Requires
- php: ^8.0
- clicksend/clicksend-php: 4.0.*
- guzzlehttp/guzzle: ^7.5
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
README
此包简化了使用 Laravel 9.x 和 clicksend.com 发送通知的过程。使用 ClickSend PHP API 封装器 [https://github.com/ClickSend/clicksend-php]
内容
安装
通过 composer 安装此包
composer require jordanhavard/laravel-sms-clicksend
该包将被自动发现。
使用以下命令发布 config/clicksend.php
配置文件
php artisan vendor:publish --provider="JordanHavard\ClickSend\ClickSendServiceProvider"
使用
在您的通知类中,使用 via()
方法内的 ClickSendChannel。示例
namespace App\Notifications; use Illuminate\Notifications\Notification; use JordanHavard\ClickSend\ClickSendMessage; use JordanHavard\ClickSend\ClickSendChannel; class ClickSendTest extends Notification { public $token; /** * Create a notification instance. * * @param string $token */ public function __construct($token) { $this->token = $token; } public function via($notifiable) { return [ClickSendChannel::class]; } public function toClickSend($notifiable) { // statically create message object: $message = ClickSendMessage::create("SMS test to user #{$notifiable->id} with token {$this->token} by ClickSend"); // OR instantiate: $message = new ClickSendMessage("SMS test to user #{$notifiable->id} with token {$this->token} by ClickSend"); // available methods: $message->content("SMS test to user #{$notifiable->id} with token {$this->token} by ClickSend"); $message->from('+6112345678'); $message->custom('123e4567-e89b-12d3-a456-426655440000'); // this can be useful for tracking message responses return $message; } }
在可通知模型(用户)中,包含返回接收者手机号码的 routeNotificationForClickSend()
方法
... public function routeNotificationForClickSend() { return $this->phone; } ...
从控制器发送通知
$user = User::find(1); try { $user->notify(new ClickSendTest('ABC123')); } catch (\Exception $e) { // do something when error return $e->getMessage(); }
事件(尚未实现)
以下事件由通知触发。默认情况下
- Illuminate\Notifications\Events\NotificationSending
- Illuminate\Notifications\Events\NotificationSent
当提交失败时,此通道会触发一个
- Illuminate\Notifications\Events\NotificationFailed
要监听这些事件,请在 app/Listeners
文件夹中创建监听器类,例如记录失败的短信
namespace App\Listeners; use Illuminate\Notifications\Events\NotificationFailed; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Support\Facades\Log; use JordanHavard\ClickSend\ClickSendChannel; class NotificationFailedListener { /** * Create the event listener. * * @return void */ public function __construct() { // } /** * Notification failed event handler * * @param NotificationFailed $event * @return void */ public function handle(NotificationFailed $event) { // Handle fail event for ClickSend // if($event->channel == ClickSendChannel::class) { echo 'failed'; dump($event); $logData = [ 'notifiable' => $event->notifiable->id, 'notification' => get_class($event->notification), 'channel' => $event->channel, 'data' => $event->data ]; Log::error('Notification Failed', $logData); } // ... handle other channels ... } }
然后在 app/Providers/EventServiceProvider.php
中注册监听器
... protected $listen = [ 'Illuminate\Notifications\Events\NotificationFailed' => [ 'App\Listeners\NotificationFailedListener', ], 'Illuminate\Notifications\Events\NotificationSent' => [ 'App\Listeners\NotificationSentListener', ], 'Illuminate\Notifications\Events\NotificationSending' => [ 'App\Listeners\NotificationSendingListener', ], ]; ...
API 客户端(尚未实现)
要从 ClickSendApi 获取客户端以访问 ClickSend API 的其余部分
$client = app(ClickSendApi::class)->getClient(); // then get for eaxample yor ClickSend account details: $account = $client->getAccount()->getAccount(); // or list of countries: $countries = $client->getCountries()->getCountries();
更新日志
有关最近更改的更多信息,请参阅 更新日志
测试
不完整
$ composer test
贡献
请参阅 贡献指南 以获取详细信息。
鸣谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件