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

> Un package pour envoyer facilement des notifications depuis Laravel via un Webhook Discord. Sans bot, sans WebSocket : il suffit de configurer l'URL du Webhook et c'est prêt.

## Vue d'ensemble

[revolution/laravel-notification-discord-webhook](https://github.com/invokable/laravel-notification-discord-webhook) est un package qui envoie des notifications vers Discord depuis Laravel Notifications via un Webhook Discord.

L'API Bot officielle de Discord nécessite le maintien d'une connexion WebSocket et sa mise en place est complexe. Avec un Webhook, il suffit d'obtenir une URL pour envoyer des notifications. Si votre seul besoin est d'envoyer des événements applicatifs vers un canal Discord d'équipe, le Webhook est le meilleur choix.

<Info>
  Ce package nécessite PHP 8.3 ou supérieur et Laravel 12.0 ou supérieur.
</Info>

## Installation

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

## Configuration

### Obtenir l'URL du Webhook Discord

Vous pouvez créer une URL de Webhook depuis votre serveur Discord via **Paramètres → Intégrations → Webhooks**. Pour les détails, consultez le [guide officiel de 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/...
```

## Utilisation de base

### Créer la classe Notification

Implémentez la méthode `toDiscordWebhook()` et retournez `DiscordChannel::class` depuis `via()`.

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

## Modes d'envoi

### Notification à la demande (on-demand)

Pour envoyer une notification sans passer par un modèle utilisateur, utilisez `Notification::route()`.

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

Notification::route('discord-webhook', config('services.discord.webhook'))
            ->notify(new DiscordNotification('Le déploiement est terminé.'));
```

### Notification depuis un modèle User

Pour utiliser une URL de Webhook différente selon l'utilisateur, définissez `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('Notification de test'));
```

## Types de messages

### Message texte

La méthode d'envoi la plus simple.

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

### Message Embed

`DiscordEmbed` permet un affichage enrichi avec un titre, une URL, etc.

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

### Pièces jointes

`DiscordAttachment` permet d'envoyer une pièce jointe. `content` (le contenu du fichier) et `filename` sont obligatoires.

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

### Référencer une pièce jointe dans un Embed

Vous pouvez référencer une pièce jointe dans `image` ou `thumbnail` d'un Embed via `attachment://nom-du-fichier`.

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

### Envoyer un payload personnalisé

`->with()` permet de spécifier directement n'importe quel paramètre du Webhook Discord.

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

<Tip>
  Pour le détail des paramètres utilisables avec `->with()`, consultez la [documentation officielle de l'API Discord](https://discord.com/developers/docs/resources/webhook#execute-webhook).
</Tip>

## Conclusion

Les bots Discord sont puissants, mais si vous souhaitez simplement envoyer des notifications, le Webhook est bien plus simple. Avec ce package, vous pouvez intégrer de manière transparente le système de notifications de Laravel et transmettre rapidement les événements de votre application dans un canal Discord d'équipe.

<Info>
  Pour les dernières informations, consultez le [dépôt GitHub](https://github.com/invokable/laravel-notification-discord-webhook).
</Info>


## Related topics

- [Tutoriel - Laravel Console Starter](/fr/packages/laravel-console-starter/tutorial.md)
- [Socialite for Discord](/fr/packages/socialite-discord.md)
- [Canal de notification - LINE SDK for Laravel](/fr/packages/laravel-line-sdk/notification.md)
- [Laravel Console Starter](/fr/packages/laravel-console-starter/index.md)
- [Notifications](/fr/notifications.md)
