spatie / laravel-backup

A package to backup your Laravel app
https://spatie.be/docs/laravel-backup
MIT License
5.58k stars 754 forks source link

Discord Webhook not working if username is an empty string #1815

Closed pratikkuikel closed 1 month ago

pratikkuikel commented 1 month ago

Steps to reproduce :

  1. Create a new webhook for a discord channel.
  2. Place the webhook url in backup.php config file.

        'discord' => [
            'webhook_url' => 'https://discord.com/api/webhooks/12344',
    
            /*
             * If this is an empty string, the name field on the webhook will be used.
             */
            'username' => '',
    
            /*
             * If this is an empty string, the avatar on the webhook will be used.
             */
            'avatar_url' => '',
        ],
  3. Change backup's notifications channel from mail to discord
        'notifications' => [
            \Spatie\Backup\Notifications\Notifications\BackupHasFailedNotification::class => ['discord'],
            \Spatie\Backup\Notifications\Notifications\UnhealthyBackupWasFoundNotification::class => ['discord'],
            \Spatie\Backup\Notifications\Notifications\CleanupHasFailedNotification::class => ['discord'],
            \Spatie\Backup\Notifications\Notifications\BackupWasSuccessfulNotification::class => ['discord'],
            \Spatie\Backup\Notifications\Notifications\HealthyBackupWasFoundNotification::class => ['discord'],
            \Spatie\Backup\Notifications\Notifications\CleanupWasSuccessfulNotification::class => ['discord'],
        ],
  4. Run Php artisan backup:run
  5. See the console info 'Backup completed!'
  6. Take a look at discord channel and it has no message notification.
  7. Log response on this file : vendor\spatie\laravel-backup\src\Notifications\Channels\Discord\DiscordChannel.php

    public function send($notifiable, Notification $notification): void
    {
        $discordMessage = $notification->toDiscord(); // @phpstan-ignore-line
    
        $discordWebhook = $notifiable->routeNotificationForDiscord();
    
        $response = Http::post($discordWebhook, $discordMessage->toArray());
        info($response);
    }
  8. Run the backup command again and check laravel.log
    [2024-07-09 14:48:58] local.INFO: {"username": ["Must be between 1 and 80 in length.", "Username cannot be \"\""]}  
  9. Set config file's discord username to any string and run the backup command again. Now the discord channel gets the notification.
RVxLab commented 1 month ago

According to the documentation, username is an optional field. So theoretically if that field (and avatar_url if the same rules apply) gets wrapped with a !empty or filled check, it should solve the issue.