PHP code example of revolution / laravel-notification-discord-webhook

1. Go to this page and download the library: Download revolution/laravel-notification-discord-webhook library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

revolution / laravel-notification-discord-webhook example snippets


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



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

use Illuminate\Support\Facades\Notification;

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

use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    public function routeNotificationForDiscordWebhook($notification): string
    {
        return $this->discord_webhook;
    }
}

$user->notify(new DiscordNotification('test'));

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

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

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

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