spatie/laravel-discord-alerts

向 Discord 发送消息

1.5.0 2024-05-16 15:09 UTC

This package is auto-updated.

Last update: 2024-09-08 13:12:44 UTC


README

Latest Version on Packagist run-tests PHPStan Check & fix styling Total Downloads

此包可以快速将警报发送到 Discord。您可以使用它来通知自己在应用中发生的任何重要事件。

想快速将警报发送到 Slack?那么请查看 laravel-slack-alerts

use Spatie\DiscordAlerts\Facades\DiscordAlert;

DiscordAlert::message("You have a new subscriber to the {$newsletter->name} newsletter!");

底层,使用一个任务与 Discord 通信。这可以防止 Discord 崩溃时您的应用失败。

支持我们

我们在创建 最佳开源包 上投入了大量资源。您可以通过 购买我们的付费产品之一 来支持我们。

我们非常感激您从家乡给我们寄明信片,说明您正在使用我们的哪个包。您可以在 我们的联系页面 上找到我们的地址。我们将所有收到的明信片发布在 我们的虚拟明信片墙上

安装

您可以通过 composer 安装此包

composer require spatie/laravel-discord-alerts

您可以设置一个包含有效 Discord webhook URL 的环境变量 DISCORD_ALERT_WEBHOOK。您可以在 Discord API 文档中 了解如何获取 webhook URL

或者,您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="discord-alerts-config"

这是已发布配置文件的内容

return [
    /*
     * The webhook URLs that we'll use to send a message to Discord.
     */
    'webhook_urls' => [
        'default' => env('DISCORD_ALERT_WEBHOOK'),
    ],

    /*
     * This job will send the message to Discord. You can extend this
     * job to set timeouts, retries, etc...
     */
    'job' => Spatie\DiscordAlerts\Jobs\SendToDiscordChannelJob::class,

    /*
     * It is possible to specify the queue connection that should be used to send the alert.
     */
    'queue_connection' => 'redis',
];

用法

要向 Discord 发送消息,只需调用 DiscordAlert::message() 并传递您想要的消息。

DiscordAlert::message("You have a new subscriber to the {$newsletter->name} newsletter!");

发送嵌入

要发送嵌入,您可以调用上面的相同函数。只需将嵌入作为第二个数组添加如下

DiscordAlert::message("You have a new subscriber to the {$newsletter->name} newsletter!", [
    [
        'title' => 'My title',
        'description' => 'My description',
        'color' => '#E77625',
        'author' => [
            'name' => 'Spatie',
            'url' => 'https://spatie.be/'
        ]    
    ]
]);

您也可以将多个嵌入作为一条消息发送。只需注意不要超过 Discord 的限制。

使用多个 webhook

您还可以使用备用 webhook,通过在配置文件中指定额外的 webhook。

// in config/discord-alerts.php

'webhook_urls' => [
    'default' => 'https://hooks.discord.com/services/XXXXXX',
    'marketing' => 'https://hooks.discord.com/services/YYYYYY',
],

可以使用 to 函数选择要使用的 webhook。

use Spatie\DiscordAlerts\Facades\DiscordAlert;

DiscordAlert::to('marketing')->message("You have a new subscriber to the {$newsletter->name} newsletter!");

使用自定义 webhook

to 函数还支持自定义 webhook URL。

use Spatie\DiscordAlerts\Facades\DiscordAlert;

DiscordAlert::to('https://custom-url.com')->message("You have a new subscriber to the {$newsletter->name} newsletter!");

格式化

Markdown

您可以使用标记来格式化消息。有关更多信息,请参阅 Discord API 文档

use Spatie\DiscordAlerts\Facades\DiscordAlert;

DiscordAlert::message("A message **with some bold statements** and _some italicized text_.");

表情符号

您可以使用与 Discord 一样的表情符号代码。这意味着也支持自定义表情符号。

use Spatie\DiscordAlerts\Facades\DiscordAlert;

DiscordAlert::message(":smile: :custom-code:");

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

有关如何报告安全漏洞的信息,请参阅 我们的安全策略

致谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件