Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pratikkuikel opened this issue Jul 9, 2024 · 1 comment · Fixed by #1821
Closed

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

pratikkuikel opened this issue Jul 9, 2024 · 1 comment · Fixed by #1821

Comments

@pratikkuikel
Copy link

pratikkuikel commented Jul 9, 2024

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' => '',
        ],
  1. 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'],
        ],
  1. Run Php artisan backup:run
  2. See the console info 'Backup completed!'
  3. Take a look at discord channel and it has no message notification.
  4. 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);
    }
  1. 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 \"\""]}  
  1. Set config file's discord username to any string and run the backup command again. Now the discord channel gets the notification.
@RVxLab
Copy link
Contributor

RVxLab commented Jul 9, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants