Vai al contenuto principale

Panoramica

revolution/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.
Questo pacchetto richiede PHP 8.3 o superiore e Laravel 12.0 o superiore.

Installazione

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.

config/services.php

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

.env

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

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().
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().
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

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

Tipi di messaggio

Messaggio di testo

Il metodo di invio più semplice.
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.
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.
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.
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.
public function toDiscordWebhook(object $notifiable): DiscordMessage
{
    return DiscordMessage::create()
                          ->with([
                              'content' => $this->content,
                              'embeds' => [[]],
                          ]);
}
Per i dettagli sui parametri specificabili con ->with(), consulta la documentazione ufficiale API di Discord.
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.
Per le informazioni più aggiornate consulta il repository GitHub.
Ultima modifica il 13 luglio 2026