> ## 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 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 모델에서의 알림

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'
                          ));
}
```

### 임의의 페이로드 전송

`->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](/ko/packages/laravel-console-starter/tutorial.md)
- [Socialite for Discord](/ko/packages/socialite-discord.md)
- [LINE SDK for Laravel](/ko/packages/laravel-line-sdk/index.md)
- [알림 채널 - LINE SDK for Laravel](/ko/packages/laravel-line-sdk/notification.md)
- [Webhook / Bot - LINE SDK for Laravel](/ko/packages/laravel-line-sdk/bot.md)
