> ## 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 für Discord (Webhook)

> Ein Paket, mit dem Sie einfach Benachrichtigungen von Laravel aus über Discord-Webhooks senden können. Ohne Bot, ohne WebSocket – einfach die Webhook-URL konfigurieren und sofort loslegen.

## Übersicht

[revolution/laravel-notification-discord-webhook](https://github.com/invokable/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.

<Info>
  Dieses Paket setzt PHP 8.3 oder höher sowie Laravel 12.0 oder höher voraus.
</Info>

## Installation

```shell theme={null}
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](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/...
```

## Grundlegende Verwendung

### Notification-Klasse erstellen

Implementieren Sie die Methode `toDiscordWebhook()` und geben Sie in `via()` `DiscordChannel::class` zurück.

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

## Versandmuster

### On-Demand-Benachrichtigungen

Wenn Sie Benachrichtigungen senden möchten, ohne sie an ein bestimmtes User-Modell zu binden, verwenden Sie `Notification::route()`.

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

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

## Nachrichtentypen

### Textnachrichten

Die einfachste Versandmethode.

```php theme={null}
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.

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

### Dateianhänge

Mit `DiscordAttachment` können Sie Dateien anhängen und mitsenden. `content` (der Dateiinhalt) und `filename` sind Pflicht.

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

### Referenzieren von Dateien in Embeds

Sie können in `image` oder `thumbnail` eines Embeds Anhänge über `attachment://Dateiname` referenzieren.

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

### Beliebige Payload senden

Mit `->with()` können Sie beliebige Parameter des Discord-Webhooks direkt angeben.

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

<Tip>
  Details zu den mit `->with()` übergebbaren Parametern finden Sie in der [offiziellen Discord-API-Dokumentation](https://discord.com/developers/docs/resources/webhook#execute-webhook).
</Tip>

## 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.

<Info>
  Die aktuellsten Informationen finden Sie im [GitHub-Repository](https://github.com/invokable/laravel-notification-discord-webhook).
</Info>


## Related topics

- [Notification-Channel - LINE SDK für Laravel](/de/packages/laravel-line-sdk/notification.md)
- [Webhook / Bot - LINE SDK für Laravel](/de/packages/laravel-line-sdk/bot.md)
- [Tutorial - Laravel Console Starter](/de/packages/laravel-console-starter/tutorial.md)
- [LINE SDK für Laravel](/de/packages/laravel-line-sdk/index.md)
- [Socialite für Discord](/de/packages/socialite-discord.md)
