ccoeder/discord-webhook

Discord Webhook 通知通道,适用于 Laravel 5.8

1.1.0 2019-08-20 12:54 UTC

This package is auto-updated.

Last update: 2024-09-29 05:46:10 UTC


README

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage Total Downloads Discord

此包可简化使用 Discord Webhook 发送通知的过程,适用于 Laravel 5.3。

这是从 siemenrotensen/discord-webhook 分支衍生的,因为我需要使用按需通知,所以我添加了对它的支持。

内容

安装

您可以通过 composer 安装此包。

composer require ccoeder/discord-webhook

设置您的 Discord Webhook

按照官方指南 使用 Webhook 设置您的 Discord Webhook。

使用方法

现在您可以在通知中的 via() 方法中使用该频道

use Illuminate\Notifications\Notification;
use NotificationChannels\DiscordWebhook\DiscordWebhookMessage;

class Application extends Notification
{
    public function via($notifiable)
    {
        return ['discord-webhook'];
    }

    public function toDiscordWebhook($notifiable)
    {
        return (new DiscordWebhookMessage())
            ->content('Your guild received a new membership application!');
    }
}

路由消息

为了使通知知道您要针对哪个 Webhook(Discord 频道),请将 routeNotificationForDiscordWebhook 方法添加到您的 Notifiable 模型中

public function routeNotificationForDiscordWebhook()
{
    return 'https://discordapp.com/api/webhooks/{webhook.id}/{webhook.token}';
}

?wait=true 添加到您的 Webhook URL,以接收发送的消息

return 'https://discordapp.com/api/webhooks/{webhook.id}/{webhook.token}?wait=true';

发送带有嵌入的消息

Discord Webhook 允许您向消息添加嵌入的丰富内容

public function toDiscordWebhook($notifiable)
{
    return (new DiscordWebhookMessage())
        ->from('Raid Calendar')
        ->content('**Next Raids**')
        ->embed(function ($embed) {
            $embed->title('Dragon Dungeon')->description('*on Monday*')
                ->field('Raid Leader', 'TheTank', true)
                ->field('Co-Leader', 'HealMePls', true);
        });
}

发送带有文件上传的消息

Discord Webhook 允许您与消息一起上传文件

public function toDiscordWebhook($notifiable)
{
    return (new DiscordWebhookMessage())
        ->content('__Member of the Day:__')
        ->file(\Storage::get('motd_avatar.png'), 'member_of_the_day.png');
}

可用消息方法

  • content($text):消息内容(最多 2000 个字符)。
  • from($username[, $avatar_url]):覆盖 Webhook 的默认用户名和头像(可选)。
  • - tts():以 TTS 消息发送。(目前 Webhook 不支持)
  • file($content, $filename):发送的文件内容。注意:不与嵌入的丰富内容一起使用。
  • embed($callback):定义(最多 10 个)消息的嵌入丰富内容。(请参阅 示例嵌入

可用嵌入方法

  • title($title[, $url]):设置嵌入的标题。
  • description($text):设置嵌入的描述。
  • color($color_int):设置嵌入的颜色代码。
  • footer($text[, $icon_url]):设置页脚信息。
  • image($img_url):设置图像信息。
  • thumbnail($img_url):设置缩略图信息。
  • author($name[, $url, $icon_url]):设置作者信息。
  • field($name, $value[, $inline_bool]):设置(内联)字段信息。

变更日志

请参阅 变更日志 了解最近的变化。

测试

$ composer test

安全

如果您发现任何安全问题,请通过电子邮件 drdrjojo2k@gmail.com 而不是使用问题跟踪器。

贡献

请参阅 贡献 了解详细信息。

鸣谢

许可证

麻省理工学院许可证(MIT)。请参阅许可文件以获取更多信息。