From bab8ec6e180bec17cf28adc7e98aff810cb4001b Mon Sep 17 00:00:00 2001 From: JoanFo <161775222+JoanFo1456@users.noreply.github.com> Date: Thu, 31 Jul 2025 23:47:46 +0200 Subject: [PATCH] Fixed not working variables on DiscordWebhooks and headers. (#1516) Co-authored-by: notCharles --- .../Admin/Resources/WebhookResource.php | 30 +++++++++---------- .../Pages/CreateWebhookConfiguration.php | 11 +++++++ .../Pages/EditWebhookConfiguration.php | 6 ++++ app/Jobs/ProcessWebhook.php | 17 ++++++----- lang/en/admin/webhook.php | 1 + 5 files changed, 42 insertions(+), 23 deletions(-) diff --git a/app/Filament/Admin/Resources/WebhookResource.php b/app/Filament/Admin/Resources/WebhookResource.php index 85f0f26b2..f11a854ba 100644 --- a/app/Filament/Admin/Resources/WebhookResource.php +++ b/app/Filament/Admin/Resources/WebhookResource.php @@ -19,6 +19,7 @@ use Filament\Forms\Components\Section; use Filament\Forms\Components\Textarea; use Filament\Forms\Components\TextInput; use Filament\Forms\Components\ToggleButtons; +use Filament\Forms\Components\Actions\Action; use Filament\Forms\Form; use Filament\Resources\Pages\PageRegistration; use Filament\Forms\Get; @@ -129,23 +130,12 @@ class WebhookResource extends Resource ->live() ->inline() ->options(WebhookType::class) - ->default(WebhookType::Regular->value) - ->afterStateHydrated(function (string $state) { - if ($state === WebhookType::Discord->value) { - self::sendHelpBanner(); - } - }) - ->afterStateUpdated(function (string $state) { - if ($state === WebhookType::Discord->value) { - self::sendHelpBanner(); - } - }), + ->default(WebhookType::Regular->value), TextInput::make('description') ->label(trans('admin/webhook.description')) ->required(), TextInput::make('endpoint') ->label(trans('admin/webhook.endpoint')) - ->activeUrl() ->required() ->columnSpanFull() ->afterStateUpdated(fn (string $state, Set $set) => $set('type', str($state)->contains('discord.com') ? WebhookType::Discord->value : WebhookType::Regular->value)), @@ -153,6 +143,15 @@ class WebhookResource extends Resource ->hidden(fn (Get $get) => $get('type') === WebhookType::Discord->value) ->dehydratedWhenHidden() ->schema(fn () => self::getRegularFields()) + ->headerActions([ + Action::make('reset_headers') + ->label(trans('admin/webhook.reset_headers')) + ->color('danger') + ->icon('heroicon-o-trash') + ->action(fn (Get $get, Set $set) => $set('headers', [ + 'X-Webhook-Event' => '{{event}}', + ])), + ]) ->formBefore(), Section::make(trans('admin/webhook.discord')) ->hidden(fn (Get $get) => $get('type') === WebhookType::Regular->value) @@ -163,8 +162,6 @@ class WebhookResource extends Resource ->aside() ->formBefore(), Section::make(trans('admin/webhook.events')) - ->collapsible() - ->collapsed(fn (Get $get) => count($get('events') ?? [])) ->schema([ CheckboxList::make('events') ->live() @@ -183,7 +180,10 @@ class WebhookResource extends Resource { return [ KeyValue::make('headers') - ->label(trans('admin/webhook.headers')), + ->label(trans('admin/webhook.headers')) + ->default(fn () => [ + 'X-Webhook-Event' => '{{event}}', + ]), ]; } diff --git a/app/Filament/Admin/Resources/WebhookResource/Pages/CreateWebhookConfiguration.php b/app/Filament/Admin/Resources/WebhookResource/Pages/CreateWebhookConfiguration.php index 68ae9a22f..03996df8c 100644 --- a/app/Filament/Admin/Resources/WebhookResource/Pages/CreateWebhookConfiguration.php +++ b/app/Filament/Admin/Resources/WebhookResource/Pages/CreateWebhookConfiguration.php @@ -63,4 +63,15 @@ class CreateWebhookConfiguration extends CreateRecord return $data; } + + protected function getRedirectUrl(): string + { + return EditWebhookConfiguration::getUrl(['record' => $this->getRecord()]); + } + + public function mount(): void + { + parent::mount(); + WebhookResource::sendHelpBanner(); + } } diff --git a/app/Filament/Admin/Resources/WebhookResource/Pages/EditWebhookConfiguration.php b/app/Filament/Admin/Resources/WebhookResource/Pages/EditWebhookConfiguration.php index 6cc49df23..1da3baa0a 100644 --- a/app/Filament/Admin/Resources/WebhookResource/Pages/EditWebhookConfiguration.php +++ b/app/Filament/Admin/Resources/WebhookResource/Pages/EditWebhookConfiguration.php @@ -123,4 +123,10 @@ class EditWebhookConfiguration extends EditRecord { $this->dispatch('refresh-widget'); } + + public function mount(int|string $record): void + { + parent::mount($record); + WebhookResource::sendHelpBanner(); + } } diff --git a/app/Jobs/ProcessWebhook.php b/app/Jobs/ProcessWebhook.php index f499518a8..5efac6c84 100644 --- a/app/Jobs/ProcessWebhook.php +++ b/app/Jobs/ProcessWebhook.php @@ -28,14 +28,14 @@ class ProcessWebhook implements ShouldQueue public function handle(): void { - $data = $this->data[0]; + $data = $this->data[0] ?? []; + if (count($data) === 1) { + $data = reset($data); + } + $data = is_array($data) ? $data : (json_decode($data, true) ?? []); + $data['event'] = $this->webhookConfiguration->transformClassName($this->eventName); if ($this->webhookConfiguration->type === WebhookType::Discord) { - $data = array_merge( - is_array($data) ? $data : json_decode($data, true), - ['event' => $this->webhookConfiguration->transformClassName($this->eventName)] - ); - $payload = json_encode($this->webhookConfiguration->payload); $tmp = $this->webhookConfiguration->replaceVars($data, $payload); $data = json_decode($tmp, true); @@ -53,9 +53,10 @@ class ProcessWebhook implements ShouldQueue } try { + $customHeaders = $this->webhookConfiguration->headers; $headers = []; - if ($this->webhookConfiguration->type === WebhookType::Regular && $customHeaders = $this->webhookConfiguration->headers) { - $headers = array_merge(['X-Webhook-Event', $this->eventName], $customHeaders); + foreach ($customHeaders as $key => $value) { + $headers[$key] = $this->webhookConfiguration->replaceVars($data, $value); } Http::withHeaders($headers)->post($this->webhookConfiguration->endpoint, $data)->throw(); diff --git a/lang/en/admin/webhook.php b/lang/en/admin/webhook.php index 691ad8501..9af58d75c 100644 --- a/lang/en/admin/webhook.php +++ b/lang/en/admin/webhook.php @@ -19,6 +19,7 @@ return [ 'headers' => 'Headers', 'events' => 'Events', 'regular' => 'Regular', + 'reset_headers' => 'Reset Headers', 'discord' => 'Discord', 'discord_message' => [ 'profile' => 'Profile',