brandonjbegle/slack-app-laravel-notification-channel

Laravel 通知渠道用于 Slack 应用。

v0.1.0 2022-11-13 21:53 UTC

This package is auto-updated.

Last update: 2024-09-14 01:49:55 UTC


README

第一方 Laravel Slack 频道仅支持有限的 Incoming Webhooks 集成。此包使用 Chat Post Message API 将消息发送到您工作区中的任何频道,前提是您有频道 ID。

我还制作了一个 Nova 4 包,用于轻松检索 Slack 频道 ID 以在此包中使用 此处

此包是一个 WIP。已实现一些块类型以简化消息的编写,但目前只有一种(SectionBlock)允许(目前仅限)数组。我在此处提供了一个示例通知 示例

安装

您可以通过 composer 将此包安装到使用 Nova 的 Laravel 应用中

composer require brandonjbegle/slack-app-laravel-notification-channel

现在发布配置文件

php artisan vendor:publish --provider="BrandonJBegle\SlackNotificationChannel\SlackAppChannelServiceProvider"

创建一个 Slack 应用,将其安装到您的 workspace 中,并获取您的 bot token。 完整说明请在此处

将密钥和令牌添加到您的 .env 文件中

SLACK_OAUTH_TOKEN=############################

使用方法

创建通知

class HealthStatusChangedNotification extends Notification
{
    use Queueable;

    private $site;
    private $prevStatus;
    private $newStatus;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(Site $site, $prevStatus, $newStatus)
    {
        $this->site = $site;
        $this->prevStatus = $prevStatus;
        $this->newStatus = $newStatus;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param mixed $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [SlackAppChannel::class];
    }

    /**
     * Get the mail representation of the notification.
     *
     * @param mixed $notifiable
     * //     * @return SlackAppMessage
     */
    public function toSlackApp($notifiable)
    {
        $now = now()->setTimezone('America/New_York')->toDayDateTimeString();
        return (new SlackAppMessage())
            ->text($this->site->name . " health status changed: {$this->prevStatus} to {$this->newStatus}")
            ->blocks([
                (new HeaderBlock)
                    ->content('Health Status Changed'),
                (new ContextBlock)
                    ->elements([
                        (new ContextBlockImage)->text('text')->url('https://' . $this->site->name . '/favicon.ico'),
                        (new ContextBlockText)->type('plain_text')->content($this->site->name),
                    ]),
                (new SectionBlock)
                    ->text("Status changed from {$this->prevStatus} to {$this->newStatus}")
                    ->fields([
                        [
                            'type' => 'mrkdwn',
                            'text' => "*{$now}*"
                        ],
                    ])
            ]);
    }
}

将 Notifiable trait 添加到您的模型中

class Site extends Model
{
    use Notifiable;

将 routeNotificationForSlackApp 方法添加到您的模型中

public function routeNotificationForSlackApp()
    {
        // If you use the Nova 4 field, this will get the id of the Channel, otherwise simply return the id
        if ($this->slack_notification_channel) {
            return $this->slack_notification_channel['value'] ?? null ;
        }
        return null;
    }