diff --git a/app/Actions/Notifications/SendGotifyTestNotification.php b/app/Actions/Notifications/SendGotifyTestNotification.php new file mode 100644 index 000000000..fdd6405de --- /dev/null +++ b/app/Actions/Notifications/SendGotifyTestNotification.php @@ -0,0 +1,37 @@ +title('You need to add Gotify urls!') + ->warning() + ->send(); + + return; + } + + foreach ($webhooks as $webhook) { + WebhookCall::create() + ->url($webhook['url']) + ->payload(['message' => '👋 Testing the Gotify notification channel.']) + ->doNotSign() + ->dispatch(); + } + + Notification::make() + ->title('Test Gotify notification sent.') + ->success() + ->send(); + } +} diff --git a/app/Filament/Pages/Settings/NotificationPage.php b/app/Filament/Pages/Settings/NotificationPage.php index 312e45ec8..02cb9230c 100755 --- a/app/Filament/Pages/Settings/NotificationPage.php +++ b/app/Filament/Pages/Settings/NotificationPage.php @@ -4,6 +4,7 @@ use App\Actions\Notifications\SendDatabaseTestNotification; use App\Actions\Notifications\SendDiscordTestNotification; +use App\Actions\Notifications\SendGotifyTestNotification; use App\Actions\Notifications\SendMailTestNotification; use App\Actions\Notifications\SendTelegramTestNotification; use App\Actions\Notifications\SendWebhookTestNotification; @@ -128,6 +129,50 @@ public function form(Form $form): Form 'md' => 2, ]), + Forms\Components\Section::make('Gotify') + ->schema([ + Forms\Components\Toggle::make('gotify_enabled') + ->label('Enable Gotify webhook notifications') + ->reactive() + ->columnSpanFull(), + Forms\Components\Grid::make([ + 'default' => 1, + ]) + ->hidden(fn (Forms\Get $get) => $get('gotify_enabled') !== true) + ->schema([ + Forms\Components\Fieldset::make('Triggers') + ->schema([ + Forms\Components\Toggle::make('gotify_on_speedtest_run') + ->label('Notify on every speedtest run') + ->columnSpanFull(), + Forms\Components\Toggle::make('gotify_on_threshold_failure') + ->label('Notify on threshold failures') + ->columnSpanFull(), + ]), + Forms\Components\Repeater::make('gotify_webhooks') + ->label('Webhooks') + ->schema([ + Forms\Components\TextInput::make('url') + ->placeholder('https://example.com/message?token=') + ->maxLength(2000) + ->required() + ->url(), + ]) + ->columnSpanFull(), + Forms\Components\Actions::make([ + Forms\Components\Actions\Action::make('test gotify') + ->label('Test Gotify webhook') + ->action(fn (Forms\Get $get) => SendgotifyTestNotification::run(webhooks: $get('gotify_webhooks'))) + ->hidden(fn (Forms\Get $get) => ! count($get('gotify_webhooks'))), + ]), + ]), + ]) + ->compact() + ->columns([ + 'default' => 1, + 'md' => 2, + ]), + Forms\Components\Section::make('Mail') ->schema([ Forms\Components\Toggle::make('mail_enabled') diff --git a/app/Listeners/Gotify/SendSpeedtestCompletedNotification.php b/app/Listeners/Gotify/SendSpeedtestCompletedNotification.php new file mode 100644 index 000000000..05b93d976 --- /dev/null +++ b/app/Listeners/Gotify/SendSpeedtestCompletedNotification.php @@ -0,0 +1,58 @@ +gotify_enabled) { + return; + } + + if (! $notificationSettings->gotify_on_speedtest_run) { + return; + } + + if (! count($notificationSettings->gotify_webhooks)) { + Log::warning('Gotify urls not found, check Gotify notification channel settings.'); + + return; + } + + $payload = [ + 'message' => view('gotify.speedtest-completed', [ + 'id' => $event->result->id, + 'service' => Str::title($event->result->service), + 'serverName' => $event->result->server_name, + 'serverId' => $event->result->server_id, + 'isp' => $event->result->isp, + 'ping' => round($event->result->ping).' ms', + 'download' => Number::toBitRate(bits: $event->result->download_bits, precision: 2), + 'upload' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2), + 'packetLoss' => $event->result->packet_loss, + 'url' => url('/admin/results'), + ])->render(), + ]; + + foreach ($notificationSettings->gotify_webhooks as $url) { + WebhookCall::create() + ->url($url['url']) + ->payload($payload) + ->doNotSign() + ->dispatch(); + } + } +} diff --git a/app/Listeners/Gotify/SendSpeedtestThresholdNotification.php b/app/Listeners/Gotify/SendSpeedtestThresholdNotification.php new file mode 100644 index 000000000..1ca0b14bf --- /dev/null +++ b/app/Listeners/Gotify/SendSpeedtestThresholdNotification.php @@ -0,0 +1,132 @@ +gotify_enabled) { + return; + } + + if (! $notificationSettings->gotify_on_threshold_failure) { + return; + } + + if (! count($notificationSettings->gotify_webhooks)) { + Log::warning('Gotify urls not found, check gotify notification channel settings.'); + + return; + } + + $thresholdSettings = new ThresholdSettings(); + + if (! $thresholdSettings->absolute_enabled) { + return; + } + + $failed = []; + + if ($thresholdSettings->absolute_download > 0) { + array_push($failed, $this->absoluteDownloadThreshold(event: $event, thresholdSettings: $thresholdSettings)); + } + + if ($thresholdSettings->absolute_upload > 0) { + array_push($failed, $this->absoluteUploadThreshold(event: $event, thresholdSettings: $thresholdSettings)); + } + + if ($thresholdSettings->absolute_ping > 0) { + array_push($failed, $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings)); + } + + $failed = array_filter($failed); + + if (! count($failed)) { + Log::warning('Failed Gotify thresholds not found, won\'t send notification.'); + + return; + } + + $payload = [ + 'message' => view('gotify.speedtest-threshold', [ + 'id' => $event->result->id, + 'service' => Str::title($event->result->service), + 'serverName' => $event->result->server_name, + 'serverId' => $event->result->server_id, + 'isp' => $event->result->isp, + 'metrics' => $failed, + 'url' => url('/admin/results'), + ])->render(), + ]; + + foreach ($notificationSettings->gotify_webhooks as $url) { + WebhookCall::create() + ->url($url['url']) + ->payload($payload) + ->doNotSign() + ->dispatch(); + } + } + + /** + * Build gotify notification if absolute download threshold is breached. + */ + protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array + { + if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) { + return false; + } + + return [ + 'name' => 'Download', + 'threshold' => $thresholdSettings->absolute_download.' Mbps', + 'value' => Number::toBitRate(bits: $event->result->download_bits, precision: 2), + ]; + } + + /** + * Build gotify notification if absolute upload threshold is breached. + */ + protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array + { + if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) { + return false; + } + + return [ + 'name' => 'Upload', + 'threshold' => $thresholdSettings->absolute_upload.' Mbps', + 'value' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2), + ]; + } + + /** + * Build gotify notification if absolute ping threshold is breached. + */ + protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): bool|array + { + if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) { + return false; + } + + return [ + 'name' => 'Ping', + 'threshold' => $thresholdSettings->absolute_ping.' ms', + 'value' => round($event->result->ping, 2).' ms', + ]; + } +} diff --git a/app/Settings/NotificationSettings.php b/app/Settings/NotificationSettings.php index ff2d4d22a..dbca6d827 100644 --- a/app/Settings/NotificationSettings.php +++ b/app/Settings/NotificationSettings.php @@ -46,6 +46,14 @@ class NotificationSettings extends Settings public ?array $discord_webhooks; + public bool $gotify_enabled; + + public bool $gotify_on_speedtest_run; + + public bool $gotify_on_threshold_failure; + + public ?array $gotify_webhooks; + public static function group(): string { return 'notification'; diff --git a/database/settings/2024_02_22_144654_create_gotify_notification_settings.php b/database/settings/2024_02_22_144654_create_gotify_notification_settings.php new file mode 100644 index 000000000..af0d818d7 --- /dev/null +++ b/database/settings/2024_02_22_144654_create_gotify_notification_settings.php @@ -0,0 +1,14 @@ +migrator->add('notification.gotify_enabled', false); + $this->migrator->add('notification.gotify_on_speedtest_run', false); + $this->migrator->add('notification.gotify_on_threshold_failure', false); + $this->migrator->add('notification.gotify_webhooks', null); + } +}; diff --git a/resources/views/gotify/speedtest-completed.blade.php b/resources/views/gotify/speedtest-completed.blade.php new file mode 100644 index 000000000..2bd6a8581 --- /dev/null +++ b/resources/views/gotify/speedtest-completed.blade.php @@ -0,0 +1,12 @@ +Speedtest Completed - #{{ $id }} + +A new speedtest was completed using {{ $service }}. + +- Server name: {{ $serverName }} +- Server ID: {{ $serverId }} +- ISP: {{ $isp }} +- Ping: {{ $ping }} +- Download: {{ $download }} +- Upload: {{ $upload }} +- Packet Loss: {{ $packetLoss }} % +- URL: {{ $url }} diff --git a/resources/views/gotify/speedtest-threshold.blade.php b/resources/views/gotify/speedtest-threshold.blade.php new file mode 100644 index 000000000..3bc1830ab --- /dev/null +++ b/resources/views/gotify/speedtest-threshold.blade.php @@ -0,0 +1,8 @@ +Speedtest Threshold Breached - #{{ $id }} + +A new speedtest was completed using {{ $service }} on {{ $isp }} but a threshold was breached. + +@foreach ($metrics as $item) +- {{ $item['name'] }} {{ $item['threshold'] }}: {{ $item['value'] }} +@endforeach +- URL: {{ $url }}