awssat/discord-notification-channel

laravel的Discord通知渠道

1.4.4 2024-07-06 23:15 UTC

This package is auto-updated.

Last update: 2024-09-06 23:41:36 UTC


README

简介

通过Laravel通知渠道发送Discord或Slack消息的有效负载

特性

  • 支持使用new (new SlackMessage)$this->toSlack($notifiable)支持Slack有效负载
  • 支持Discord Webhook有效负载
  • 易于使用

安装

通过Composer

composer require awssat/discord-notification-channel

使用方法

在通知中,您应该在via方法中定义discord渠道

public function via($notifiable)
{
    return ['mail', 'discord'];
}

您应该有一个toDiscord方法

    public function toDiscord($notifiable)
    {
        return (new DiscordMessage)
            ->from('Laravel')
            ->content('Content')
            ->embed(function ($embed) {
                $embed->title('Discord is cool')->description('Slack nah')
                    ->field('Laravel', '9.0.0', true)
                    ->field('PHP', '8.0.0', true);
            });
    }

toDiscord方法可以接收DiscordMessageSlackMessage

Slack消息示例

    public function toDiscord($notifiable)
    {
        return (new SlackMessage)
                ->content('One of your invoices has been paid!');
    }

或者,如果您想,也可以从toSlack方法运行它

    public function toSlack($notifiable)
    {
        return (new SlackMessage)
                ->content('One of your invoices has been paid!');
    }

    public function toDiscord($notifiable)
    {
        return $this->toSlack($notifiable);
    }

有关Laravel Slack消息的更多示例,请参阅https://laravel.net.cn/docs/6.x/notifications#slack-notifications

路由Discord通知

为了将Discord通知路由到正确的位置,在您的notifiable实体上定义一个routeNotificationForDiscord方法。这应该返回通知应该交付的Webhook URL。在此处阅读Webhook Discord文档https://support.discordapp.com/hc/en-us/articles/228383668-Intro-to-Webhooks

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * Route notifications for the Discord channel.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return string
     */
    public function routeNotificationForDiscord($notification)
    {
        return 'https://discordapp.com/api/webhooks/.......';
    }
}