Zum Hauptinhalt springen

Übersicht

revolution/laravel-notification-discord-webhook ist ein Paket, mit dem Sie über Discord-Webhooks Benachrichtigungen aus Laravel Notifications an Discord senden können. Die offizielle Discord-Bot-API erfordert das Aufrechterhalten einer WebSocket-Verbindung, was die Einrichtung kompliziert macht. Mit Webhooks reicht es aus, eine URL zu erhalten, um Benachrichtigungen zu senden. Wenn Sie nur Anwendungs-Events an einen Discord-Kanal Ihres Teams senden möchten, sind Webhooks die beste Wahl.
Dieses Paket setzt PHP 8.3 oder höher sowie Laravel 12.0 oder höher voraus.

Installation

composer require revolution/laravel-notification-discord-webhook

Konfiguration

Discord-Webhook-URL abrufen

Sie können in Ihrem Discord-Server unter Einstellungen → Integrationen → Webhooks eine Webhook-URL erstellen. Details finden Sie in der offiziellen Discord-Anleitung.

config/services.php

'discord' => [
    'webhook' => env('DISCORD_WEBHOOK'),
],

.env

DISCORD_WEBHOOK=https://discord.com/api/webhooks/...

Grundlegende Verwendung

Notification-Klasse erstellen

Implementieren Sie die Methode toDiscordWebhook() und geben Sie in via() DiscordChannel::class zurück.
<?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);
    }
}

Versandmuster

On-Demand-Benachrichtigungen

Wenn Sie Benachrichtigungen senden möchten, ohne sie an ein bestimmtes User-Modell zu binden, verwenden Sie Notification::route().
use Illuminate\Support\Facades\Notification;

Notification::route('discord-webhook', config('services.discord.webhook'))
            ->notify(new DiscordNotification('Deployment abgeschlossen.'));

Benachrichtigungen vom User-Modell aus

Wenn Sie pro User unterschiedliche Webhook-URLs verwenden möchten, definieren Sie routeNotificationForDiscordWebhook().
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    public function routeNotificationForDiscordWebhook($notification): string
    {
        return $this->discord_webhook;
    }
}
$user->notify(new DiscordNotification('Testbenachrichtigung'));

Nachrichtentypen

Textnachrichten

Die einfachste Versandmethode.
public function toDiscordWebhook(object $notifiable): DiscordMessage
{
    return DiscordMessage::create(content: $this->content);
}

Embed-Nachrichten

Mit DiscordEmbed können Sie eine reichhaltige Darstellung mit Titel und URL erzeugen.
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'),
                              )
                          );
}

Dateianhänge

Mit DiscordAttachment können Sie Dateien anhängen und mitsenden. content (der Dateiinhalt) und filename sind Pflicht.
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'
            )
        );
}

Referenzieren von Dateien in Embeds

Sie können in image oder thumbnail eines Embeds Anhänge über attachment://Dateiname referenzieren.
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'
                          ));
}

Beliebige Payload senden

Mit ->with() können Sie beliebige Parameter des Discord-Webhooks direkt angeben.
public function toDiscordWebhook(object $notifiable): DiscordMessage
{
    return DiscordMessage::create()
                          ->with([
                              'content' => $this->content,
                              'embeds' => [[]],
                          ]);
}
Details zu den mit ->with() übergebbaren Parametern finden Sie in der offiziellen Discord-API-Dokumentation.

Fazit

Discord-Bots sind mächtig, aber wenn es nur um das Senden von Benachrichtigungen geht, sind Webhooks deutlich einfacher. Mit diesem Paket können Sie sich nahtlos in das Laravel-Benachrichtigungssystem integrieren und Anwendungs-Events schnell an einen Discord-Kanal Ihres Teams senden.
Die aktuellsten Informationen finden Sie im GitHub-Repository.
Zuletzt geändert am 13. Juli 2026