revolution/laravel-notification-discord-webhook 是一个通过 Discord Webhook 从 Laravel Notifications 向 Discord 发送通知的包。
Discord 官方 Bot API 需要持续维持 WebSocket 连接,配置较为复杂。而 Webhook 只需获取 URL 即可发送通知。如果你只是想将应用事件通知到团队的 Discord 频道,Webhook 是最合适的选择。
该包要求 PHP 8.3 及以上、Laravel 12.0 及以上。
composer require revolution/laravel-notification-discord-webhook
获取 Discord 的 Webhook URL
你可以从 Discord 服务器的 设置 → 整合 → Webhook 创建 Webhook URL。详情请参见 Discord 官方指南。
config/services.php
'discord' => [
'webhook' => env('DISCORD_WEBHOOK'),
],
.env
DISCORD_WEBHOOK=https://discord.com/api/webhooks/...
基本使用
创建 Notification 类
实现 toDiscordWebhook() 方法,并在 via() 中返回 DiscordChannel::class。
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordChannel;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordMessage;
class DiscordNotification extends Notification implements ShouldQueue
{
use Queueable;
public function __construct(protected string $content)
{
//
}
public function via($notifiable): array
{
return [DiscordChannel::class];
}
public function toDiscordWebhook(object $notifiable): DiscordMessage
{
return DiscordMessage::create(content: $this->content);
}
}
发送方式
On-Demand 通知
如果不希望绑定到某个用户模型来发送通知,可以使用 Notification::route()。
use Illuminate\Support\Facades\Notification;
Notification::route('discord-webhook', config('services.discord.webhook'))
->notify(new DiscordNotification('部署已完成。'));
从 User 模型发送通知
如果需要按用户使用不同的 Webhook URL,可以定义 routeNotificationForDiscordWebhook()。
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
public function routeNotificationForDiscordWebhook($notification): string
{
return $this->discord_webhook;
}
}
$user->notify(new DiscordNotification('测试通知'));
消息类型
文本消息
最简单的发送方式。
public function toDiscordWebhook(object $notifiable): DiscordMessage
{
return DiscordMessage::create(content: $this->content);
}
Embed 消息
使用 DiscordEmbed 可以展示包含标题、URL 等的富文本内容。
use Revolution\Laravel\Notification\DiscordWebhook\DiscordMessage;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordEmbed;
public function toDiscordWebhook(object $notifiable): DiscordMessage
{
return DiscordMessage::create()
->embed(
DiscordEmbed::make(
title: 'INFO',
description: $this->content,
url: route('home'),
)
);
}
使用 DiscordAttachment 可以附带文件发送。content(文件内容)与 filename 为必填项。
use Revolution\Laravel\Notification\DiscordWebhook\DiscordMessage;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordAttachment;
use Illuminate\Support\Facades\Storage;
public function toDiscordWebhook(object $notifiable): DiscordMessage
{
return DiscordMessage::create()
->file(
DiscordAttachment::make(
content: Storage::get('test.png'),
filename: 'test.png',
description: 'test',
filetype: 'image/png'
)
);
}
在 Embed 中引用文件
在 Embed 的 image 或 thumbnail 中,可以通过 attachment://文件名 引用附件。
use Revolution\Laravel\Notification\DiscordWebhook\DiscordMessage;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordAttachment;
use Revolution\Laravel\Notification\DiscordWebhook\DiscordEmbed;
use Illuminate\Support\Facades\Storage;
public function toDiscordWebhook(object $notifiable): DiscordMessage
{
return DiscordMessage::create()
->embed(
DiscordEmbed::make(
title: 'test',
description: $this->content,
image: 'attachment://test.jpg',
thumbnail: 'attachment://test2.jpg',
)
)
->file(DiscordAttachment::make(
content: Storage::get('test.jpg'),
filename: 'test.jpg',
description: 'test',
filetype: 'image/jpg'
))
->file(new DiscordAttachment(
content: Storage::get('test2.jpg'),
filename: 'test2.jpg',
description: 'test2',
filetype: 'image/jpg'
));
}
发送任意 payload
通过 ->with() 可以直接指定 Discord Webhook 的任意参数。
public function toDiscordWebhook(object $notifiable): DiscordMessage
{
return DiscordMessage::create()
->with([
'content' => $this->content,
'embeds' => [[]],
]);
}
Discord Bot 功能强大,但如果只是发送通知,Webhook 要简单得多。使用该包,你可以与 Laravel 通知系统无缝集成,快速地将应用事件通知到团队的 Discord 频道。