vladski/laravel-sms-clicksend

此包已被废弃,不再维护。未建议替代包。

ClickSend 通知通道适用于 Laravel 5.4。

v1.0.1 2017-05-01 13:33 UTC

This package is auto-updated.

Last update: 2020-07-27 09:00:35 UTC


README

此包使用 Laravel 5.4+ 通过 clicksend.com 发送通知变得简单。使用 ClickSend PHP API 包装器 [https://github.com/ClickSend/clicksend-php]

内容

安装

通过 composer 安装此包

composer require vladski/laravel-sms-clicksend

将服务提供者添加到 config/app.php

...
'providers' => [
    ...
    NotificationChannels\ClickSend\ClickSendServiceProvider::class,
],
...

将您的 ClickSend 用户名、api_key 和可选的默认发送者 sms_from 添加到您的 config/services.php

...
'clicksend' => [
	'username' => env('CLICKSEND_USERNAME'),
	'api_key'  => env('CLICKSEND_API_KEY'),
	'sms_from' => env('CLICKSEND_SMS_FROM'), // optional
],
...

使用

在您的通知类中,使用 via() 方法使用 ClickSendChannel。示例

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use NotificationChannels\ClickSend\ClickSendMessage;
use NotificationChannels\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'); // override sms_from from config
       	
       	return $message;
    }
}

在可通知的模型(User)中,包括方法 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 文件夹中创建监听器类,例如记录失败的 SMS

namespace App\Listeners;
	
use Illuminate\Notifications\Events\NotificationFailed;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
use NotificationChannels\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 客户端

要访问 ClickSend API 的其余部分,您可以从 ClickSendApi 获取客户端

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

变更日志

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

测试

不完整

$ composer test

贡献

请参阅 CONTRIBUTING 了解详细信息。

致谢

许可

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