> ## Documentation Index
> Fetch the complete documentation index at: https://kawax.biz/llms.txt
> Use this file to discover all available pages before exploring further.

# Laravel Notification for Discord(Webhook)

> 使用 Discord Webhook 从 Laravel 便捷发送通知的包。无需 Bot、无需 WebSocket，仅需配置 Webhook URL 即可使用。

## 概览

[revolution/laravel-notification-discord-webhook](https://github.com/invokable/laravel-notification-discord-webhook) 是一个通过 Discord Webhook 从 Laravel Notifications 向 Discord 发送通知的包。

Discord 官方 Bot API 需要持续维持 WebSocket 连接，配置较为复杂。而 Webhook 只需获取 URL 即可发送通知。如果你只是想将应用事件通知到团队的 Discord 频道，Webhook 是最合适的选择。

<Info>
  该包要求 PHP 8.3 及以上、Laravel 12.0 及以上。
</Info>

## 安装

```shell theme={null}
composer require revolution/laravel-notification-discord-webhook
```

## 配置

### 获取 Discord 的 Webhook URL

你可以从 Discord 服务器的 **设置 → 整合 → Webhook** 创建 Webhook URL。详情请参见 [Discord 官方指南](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks)。

### config/services.php

```php theme={null}
'discord' => [
    'webhook' => env('DISCORD_WEBHOOK'),
],
```

### .env

```dotenv theme={null}
DISCORD_WEBHOOK=https://discord.com/api/webhooks/...
```

## 基本使用

### 创建 Notification 类

实现 `toDiscordWebhook()` 方法，并在 `via()` 中返回 `DiscordChannel::class`。

```php theme={null}
<?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()`。

```php theme={null}
use Illuminate\Support\Facades\Notification;

Notification::route('discord-webhook', config('services.discord.webhook'))
            ->notify(new DiscordNotification('部署已完成。'));
```

### 从 User 模型发送通知

如果需要按用户使用不同的 Webhook URL，可以定义 `routeNotificationForDiscordWebhook()`。

```php theme={null}
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    public function routeNotificationForDiscordWebhook($notification): string
    {
        return $this->discord_webhook;
    }
}
```

```php theme={null}
$user->notify(new DiscordNotification('测试通知'));
```

## 消息类型

### 文本消息

最简单的发送方式。

```php theme={null}
public function toDiscordWebhook(object $notifiable): DiscordMessage
{
    return DiscordMessage::create(content: $this->content);
}
```

### Embed 消息

使用 `DiscordEmbed` 可以展示包含标题、URL 等的富文本内容。

```php theme={null}
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` 为必填项。

```php theme={null}
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://文件名` 引用附件。

```php theme={null}
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 的任意参数。

```php theme={null}
public function toDiscordWebhook(object $notifiable): DiscordMessage
{
    return DiscordMessage::create()
                          ->with([
                              'content' => $this->content,
                              'embeds' => [[]],
                          ]);
}
```

<Tip>
  关于 `->with()` 可用参数的详情，请参见 [Discord 官方 API 文档](https://discord.com/developers/docs/resources/webhook#execute-webhook)。
</Tip>

## 小结

Discord Bot 功能强大，但如果只是发送通知，Webhook 要简单得多。使用该包，你可以与 Laravel 通知系统无缝集成，快速地将应用事件通知到团队的 Discord 频道。

<Info>
  更多最新信息请参见 [GitHub 仓库](https://github.com/invokable/laravel-notification-discord-webhook)。
</Info>


## Related topics

- [教程 - Laravel Console Starter](/zh/packages/laravel-console-starter/tutorial.md)
- [Socialite for Discord](/zh/packages/socialite-discord.md)
- [LINE SDK for Laravel](/zh/packages/laravel-line-sdk/index.md)
- [通知 Channel - LINE SDK for Laravel](/zh/packages/laravel-line-sdk/notification.md)
- [Webhook / Bot - LINE SDK for Laravel](/zh/packages/laravel-line-sdk/bot.md)
