如何为 laravel 通知使用多个 slack webhook
How to use multiple slack webhook for laravel notifications
我正在尝试通过 laravel 通知发送松弛消息。
当 signed up
、leave
等事件发生时使用不同的渠道。
但是这里有个问题,因为Laravel只支持一种Hook URI 或者我就是不知道如何设置各种通道Hook URI
我怎样才能将不同类型的松弛消息发送到不同的频道?
提前致谢。
以下是我的注册通知代码:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\SlackMessage;
/**
* Class SignedUp
* @package App\Notifications
*/
class SignedUp extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [
'slack',
];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
return (new SlackMessage)
->success()
->content("Welcome");
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
}
这是我的用户模型代码:
<?php
namespace Illuminate\Foundation\Auth;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Notifications\Notifiable;
class User extends Model implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, Notifiable;
/**
* Route notifications for the Nexmo channel.
*
* @return string
*/
public function routeNotificationForSlack() : string
{
return 'slack webhook uri';
}
}
添加到Users.php
protected $webhook;
public function routeNotificationForSlack( ) {
if($this->webhook){
return config('slack.channels.'.$this->webhook);
}
}
public function slackChannel($channel){
$this->webhook = $channel;
return $this;
}
config/slack.php(在配置中创建一个slack.php文件)
return [
'channels' => [
'name-of-channel' => 'webhook-url-of-channel',
'name-of-next-channel' => 'webhook-url-of-next-channel'
]
];
向 slack 频道发送通知
$user->slackChannel('name-of-channel')->notify(new Notification());
我正在尝试通过 laravel 通知发送松弛消息。
当 signed up
、leave
等事件发生时使用不同的渠道。
但是这里有个问题,因为Laravel只支持一种Hook URI 或者我就是不知道如何设置各种通道Hook URI
我怎样才能将不同类型的松弛消息发送到不同的频道?
提前致谢。
以下是我的注册通知代码:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\SlackMessage;
/**
* Class SignedUp
* @package App\Notifications
*/
class SignedUp extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [
'slack',
];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return SlackMessage
*/
public function toSlack($notifiable)
{
return (new SlackMessage)
->success()
->content("Welcome");
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
}
这是我的用户模型代码:
<?php
namespace Illuminate\Foundation\Auth;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Illuminate\Notifications\Notifiable;
class User extends Model implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword, Notifiable;
/**
* Route notifications for the Nexmo channel.
*
* @return string
*/
public function routeNotificationForSlack() : string
{
return 'slack webhook uri';
}
}
添加到Users.php
protected $webhook;
public function routeNotificationForSlack( ) {
if($this->webhook){
return config('slack.channels.'.$this->webhook);
}
}
public function slackChannel($channel){
$this->webhook = $channel;
return $this;
}
config/slack.php(在配置中创建一个slack.php文件)
return [
'channels' => [
'name-of-channel' => 'webhook-url-of-channel',
'name-of-next-channel' => 'webhook-url-of-next-channel'
]
];
向 slack 频道发送通知
$user->slackChannel('name-of-channel')->notify(new Notification());