Skip to content

Commit c535fda

Browse files
committed
add listener and failed notification
1 parent cc0f0d3 commit c535fda

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

app/Actions/Notifications/SendWebhookTestNotification.php

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
use App\Models\Result;
66
use App\Services\SpeedtestFakeResultGenerator;
77
use Filament\Notifications\Notification;
8+
use Illuminate\Support\Facades\Event;
89
use Illuminate\Support\Str;
910
use Lorisleiva\Actions\Concerns\AsAction;
11+
use Spatie\WebhookServer\Events\WebhookCallFailedEvent;
12+
use Spatie\WebhookServer\Events\WebhookCallSucceededEvent;
1013
use Spatie\WebhookServer\WebhookCall;
1114

1215
class SendWebhookTestNotification
@@ -27,9 +30,28 @@ public function handle(array $webhooks)
2730
// Generate a fake Result (NOT saved to database)
2831
$fakeResult = SpeedtestFakeResultGenerator::completed();
2932

33+
$hasFailure = false;
34+
3035
foreach ($webhooks as $webhook) {
36+
$url = $webhook['url'];
37+
$succeeded = false;
38+
39+
// Listen for success/failure events for this specific webhook
40+
Event::listen(WebhookCallSucceededEvent::class, function ($event) use ($url, &$succeeded) {
41+
if ($event->webhookUrl === $url) {
42+
$succeeded = true;
43+
}
44+
});
45+
46+
Event::listen(WebhookCallFailedEvent::class, function ($event) use ($url, &$succeeded) {
47+
if ($event->webhookUrl === $url) {
48+
$succeeded = false;
49+
}
50+
});
51+
52+
// Send webhook synchronously to get immediate result
3153
WebhookCall::create()
32-
->url($webhook['url'])
54+
->url($url)
3355
->payload([
3456
'result_id' => Str::uuid(),
3557
'site_name' => __('settings/notifications.test_notifications.webhook.payload'),
@@ -42,12 +64,25 @@ public function handle(array $webhooks)
4264
'url' => url('/admin/results'),
4365
])
4466
->doNotSign()
45-
->dispatch();
67+
->dispatchSync();
68+
69+
if (! $succeeded) {
70+
$hasFailure = true;
71+
}
4672
}
4773

48-
Notification::make()
49-
->title(__('settings/notifications.test_notifications.webhook.sent'))
50-
->success()
51-
->send();
74+
// Show appropriate notification based on results
75+
if (! $hasFailure) {
76+
Notification::make()
77+
->title(__('settings/notifications.test_notifications.webhook.sent'))
78+
->success()
79+
->send();
80+
} else {
81+
Notification::make()
82+
->title(__('settings/notifications.test_notifications.webhook.failed'))
83+
->body(__('settings/notifications.test_notifications.webhook.failed_body'))
84+
->danger()
85+
->send();
86+
}
5287
}
5388
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use Illuminate\Support\Facades\Log;
6+
use Spatie\WebhookServer\Events\WebhookCallFailedEvent;
7+
8+
class LogWebhookFailure
9+
{
10+
/**
11+
* Handle the event.
12+
*/
13+
public function handle(WebhookCallFailedEvent $event): void
14+
{
15+
Log::error('Webhook notification failed', [
16+
'url' => $event->webhookUrl,
17+
'attempt' => $event->attempt,
18+
'http_verb' => $event->httpVerb,
19+
'error_type' => $event->errorType,
20+
'error_message' => $event->errorMessage,
21+
]);
22+
}
23+
}

lang/en/settings/notifications.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555
],
5656
'webhook' => [
5757
'add' => 'Add webhook URLs!',
58-
'sent' => 'Test webhook notification sent.',
58+
'sent' => 'Test webhook notification sent successfully.',
59+
'failed' => 'Webhook notification failed.',
60+
'failed_body' => 'Check the logs for details.',
5961
'payload' => 'Testing webhook notification',
6062
],
6163
],

0 commit comments

Comments
 (0)