pkvas/laravel-discord-alerts

向 Discord 发送消息

资助包维护!
spatie

dev-main 2023-11-24 16:58 UTC

This package is auto-updated.

Last update: 2024-09-24 18:50:07 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,
];

用法

要向 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

变更日志

有关最近更改的更多信息,请参阅 变更日志

贡献

有关详细信息,请参阅 贡献指南

安全漏洞

请参阅 我们的安全策略 了解如何报告安全漏洞。

鸣谢

许可协议

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