> ## 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 pacchetto per inviare facilmente notifiche da Laravel utilizzando i Webhook di Discord. Nessun bot, nessun WebSocket: basta impostare l'URL del Webhook per iniziare subito.

## Panoramica

[revolution/laravel-notification-discord-webhook](https://github.com/invokable/laravel-notification-discord-webhook) è un pacchetto per inviare notifiche a Discord tramite Laravel Notifications utilizzando i Webhook di Discord.

L'API Bot ufficiale di Discord richiede il mantenimento di una connessione WebSocket ed è complessa da configurare. Con i Webhook basta ottenere un URL per poter inviare notifiche. Se vuoi solo notificare gli eventi dell'app sul canale Discord del tuo team, il Webhook è la scelta ideale.

<Info>
  Questo pacchetto richiede PHP 8.3 o superiore e Laravel 12.0 o superiore.
</Info>

## Installazione

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

## Configurazione

### Ottieni l'URL del Webhook di Discord

Puoi creare un URL del Webhook dalle **Impostazioni del server Discord → Integrazioni → Webhook**. Per i dettagli, consulta la [guida ufficiale di 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/...
```

## Utilizzo di base

### Crea una classe Notification

Implementa il metodo `toDiscordWebhook()` e restituisci `DiscordChannel::class` da `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);
    }
}
```

## Modalità di invio

### Notifica on-demand

Per inviare notifiche senza associarle a un modello utente specifico, utilizza `Notification::route()`.

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

Notification::route('discord-webhook', config('services.discord.webhook'))
            ->notify(new DiscordNotification('Deploy completato.'));
```

### Notifiche dal modello User

Per utilizzare un URL Webhook diverso per ogni utente, definisci `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('Notifica di test'));
```

## Tipi di messaggio

### Messaggio di testo

Il metodo di invio più semplice.

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

### Messaggio Embed

Con `DiscordEmbed` puoi ottenere una visualizzazione ricca che include titolo e 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'),
                              )
                          );
}
```

### Allegati

Con `DiscordAttachment` puoi allegare e inviare file. `content` (il contenuto del file) e `filename` sono obbligatori.

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

### Riferimento a file all'interno di un Embed

Puoi fare riferimento a un allegato in `image` o `thumbnail` di un Embed usando `attachment://nome_file`.

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

### Invio di payload arbitrari

Con `->with()` puoi specificare direttamente parametri arbitrari del Webhook di Discord.

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

<Tip>
  Per i dettagli sui parametri specificabili con `->with()`, consulta la [documentazione ufficiale API di Discord](https://discord.com/developers/docs/resources/webhook#execute-webhook).
</Tip>

## Riepilogo

I bot Discord sono potenti, ma se vuoi semplicemente inviare notifiche, il Webhook è molto più semplice. Con questo pacchetto puoi integrarti perfettamente con il sistema di notifiche di Laravel e notificare rapidamente gli eventi dell'app sul canale Discord del tuo team.

<Info>
  Per le informazioni più aggiornate consulta il [repository GitHub](https://github.com/invokable/laravel-notification-discord-webhook).
</Info>


## Related topics

- [Tutorial - Laravel Console Starter](/it/packages/laravel-console-starter/tutorial.md)
- [Socialite for Discord](/it/packages/socialite-discord.md)
- [LINE SDK for Laravel](/it/packages/laravel-line-sdk/index.md)
- [Canale di notifica - LINE SDK for Laravel](/it/packages/laravel-line-sdk/notification.md)
- [Webhook / Bot - LINE SDK for Laravel](/it/packages/laravel-line-sdk/bot.md)
