forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendAppriseTestNotification.php
More file actions
100 lines (80 loc) · 3.07 KB
/
SendAppriseTestNotification.php
File metadata and controls
100 lines (80 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace App\Actions\Notifications;
use App\Notifications\Apprise\TestNotification;
use App\Settings\NotificationSettings;
use Filament\Notifications\Notification;
use Illuminate\Support\Facades\Notification as FacadesNotification;
use Lorisleiva\Actions\Concerns\AsAction;
use Throwable;
class SendAppriseTestNotification
{
use AsAction;
public function handle(array $channel_urls): void
{
if (! count($channel_urls)) {
Notification::make()
->title('You need to add Apprise channel URLs!')
->warning()
->send();
return;
}
$settings = app(NotificationSettings::class);
$appriseUrl = rtrim($settings->apprise_server_url ?? '', '/');
if (empty($appriseUrl)) {
Notification::make()
->title('Apprise Server URL is not configured')
->body('Please configure the Apprise Server URL in the settings above.')
->danger()
->send();
return;
}
try {
foreach ($channel_urls as $row) {
$channelUrl = $row['channel_url'] ?? null;
if (! $channelUrl) {
continue;
}
// Use notifyNow() to send synchronously even though notification implements ShouldQueue
// This allows us to catch exceptions and show them in the UI immediately
FacadesNotification::route('apprise_urls', $channelUrl)
->notifyNow(new TestNotification);
}
} catch (Throwable $e) {
$errorMessage = $this->cleanErrorMessage($e);
Notification::make()
->title('Failed to send Apprise test notification')
->body($errorMessage)
->danger()
->send();
return;
}
Notification::make()
->title('Test Apprise notification sent.')
->success()
->send();
}
/**
* Clean up error message for display in UI.
*/
protected function cleanErrorMessage(Throwable $e): string
{
$message = $e->getMessage();
// Get the full Apprise server URL for error messages
$settings = app(NotificationSettings::class);
$appriseUrl = rtrim($settings->apprise_server_url ?? '', '/');
// Handle connection errors - extract just the important part
if (str_contains($message, 'cURL error')) {
if (str_contains($message, 'Could not resolve host')) {
return "Could not connect to Apprise server at {$appriseUrl}";
}
if (str_contains($message, 'Connection refused')) {
return "Connection refused by Apprise server at {$appriseUrl}";
}
if (str_contains($message, 'Operation timed out')) {
return "Connection to Apprise server at {$appriseUrl} timed out";
}
return "Failed to connect to Apprise server at {$appriseUrl}";
}
return $message;
}
}