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 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 모델에서의 알림
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'
));
}
임의의 페이로드 전송
->with()를 사용하면 Discord Webhook의 임의 파라미터를 직접 지정할 수 있습니다.
public function toDiscordWebhook(object $notifiable): DiscordMessage
{
return DiscordMessage::create()
->with([
'content' => $this->content,
'embeds' => [[]],
]);
}
Discord Bot은 강력하지만, 단순히 알림을 보내는 것이라면 Webhook이 훨씬 심플합니다. 이 패키지를 사용하면 Laravel의 알림 시스템과 매끄럽게 통합하여 팀의 Discord 채널에 앱의 이벤트를 빠르게 알릴 수 있습니다.