laravel - 如何使用多个 slack webhook 进行 Laravel 通知

标签 laravel laravel-5.5 laravel-notification

我正在尝试通过 Laravel 通知发送松弛消息。

当诸如 signed up 之类的事件发生时, leave等使用不同的 channel 发生。

但是这里有一个问题,因为 Laravel 只支持一个 Hook URI 或者我只是不知道如何设置各种 channel Hook URI。

我如何能够将不同类型的 slack 消息发送到不同的 channel ?

先感谢您。

以下是我的注册通知代码:

<?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;
}

配置/slack.php (在配置中创建一个 slack.php 文件)
return [
    'channels' => [
        'name-of-channel' => 'webhook-url-of-channel',
        'name-of-next-channel' => 'webhook-url-of-next-channel'
    ]
];

向松弛 channel 发送通知
$user->slackChannel('name-of-channel')->notify(new Notification());

关于laravel - 如何使用多个 slack webhook 进行 Laravel 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48899027/

相关文章:

laravel - 从Laravel中的通知数据库中提取数据

php - 在 MySQL 和 Laravel 中预订

PHP 内存不足

php - laravel 4.1 用数据库中的数据填充表格

javascript - 拉维尔 : Getting error response from controller but type error on console and response message is not showing on view page

laravel-notification - Laravel 通知中 uuid 背后的原因是什么

javascript - 循环遍历单个 <td> 元素 - Laravel

mysql - Algolia 记录正在更新错误的数据类型

mysql - 在 laravel 中使用 maatwebsite excel 上传之前检查导入文件

Laravel 5.7 使用 Expo 发送通知