diff --git a/app/Actions/Notifications/SendTeamsTestNotification.php b/app/Actions/Notifications/SendTeamsTestNotification.php new file mode 100644 index 000000000..f0ffc3d62 --- /dev/null +++ b/app/Actions/Notifications/SendTeamsTestNotification.php @@ -0,0 +1,37 @@ +title('You need to add Teams urls!') + ->warning() + ->send(); + + return; + } + + foreach ($webhooks as $webhook) { + WebhookCall::create() + ->url($webhook['url']) + ->payload(['text' => '👋 Testing the Teams notification channel.']) + ->doNotSign() + ->dispatch(); + } + + Notification::make() + ->title('Test Teams notification sent.') + ->success() + ->send(); + } +} diff --git a/app/Filament/Pages/Settings/NotificationPage.php b/app/Filament/Pages/Settings/NotificationPage.php index f3faa9687..bac12b7b3 100755 --- a/app/Filament/Pages/Settings/NotificationPage.php +++ b/app/Filament/Pages/Settings/NotificationPage.php @@ -9,6 +9,7 @@ use App\Actions\Notifications\SendMailTestNotification; use App\Actions\Notifications\SendPushoverTestNotification; use App\Actions\Notifications\SendSlackTestNotification; +use App\Actions\Notifications\SendTeamsTestNotification; use App\Actions\Notifications\SendTelegramTestNotification; use App\Actions\Notifications\SendWebhookTestNotification; use App\Settings\NotificationSettings; @@ -83,6 +84,51 @@ public function form(Form $form): Form ]), ]) ->compact() + ->columns([ + 'default' => 1, + 'md' => 2, + + ]), + + Forms\Components\Section::make('Microsoft Teams') + ->schema([ + Forms\Components\Toggle::make('teams_enabled') + ->label('Enable Teams webhook notifications') + ->reactive() + ->columnSpanFull(), + Forms\Components\Grid::make([ + 'default' => 1, + ]) + ->hidden(fn (Forms\Get $get) => $get('teams_enabled') !== true) + ->schema([ + Forms\Components\Fieldset::make('Triggers') + ->schema([ + Forms\Components\Toggle::make('teams_on_speedtest_run') + ->label('Notify on every speedtest run') + ->columnSpanFull(), + Forms\Components\Toggle::make('teams_on_threshold_failure') + ->label('Notify on threshold failures') + ->columnSpanFull(), + ]), + Forms\Components\Repeater::make('teams_webhooks') + ->label('Webhooks') + ->schema([ + Forms\Components\TextInput::make('url') + ->maxLength(2000) + ->placeholder('https://webhook.office.com/webhookb2/') + ->required() + ->url(), + ]) + ->columnSpanFull(), + Forms\Components\Actions::make([ + Forms\Components\Actions\Action::make('test teams') + ->label('Test Teams webhook') + ->action(fn (Forms\Get $get) => SendTeamsTestNotification::run(webhooks: $get('teams_webhooks'))) + ->hidden(fn (Forms\Get $get) => ! count($get('teams_webhooks'))), + ]), + ]), + ]) + ->compact() ->columns([ 'default' => 1, 'md' => 2, diff --git a/app/Listeners/Teams/SendSpeedtestCompletedNotification.php b/app/Listeners/Teams/SendSpeedtestCompletedNotification.php new file mode 100644 index 000000000..3b22c9137 --- /dev/null +++ b/app/Listeners/Teams/SendSpeedtestCompletedNotification.php @@ -0,0 +1,58 @@ +teams_enabled) { + return; + } + + if (! $notificationSettings->teams_on_speedtest_run) { + return; + } + + if (! count($notificationSettings->teams_webhooks)) { + Log::warning('Teams urls not found, check Teams notification channel settings.'); + + return; + } + + $payload = [ + 'text' => view('teams.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->teams_webhooks as $url) { + WebhookCall::create() + ->url($url['url']) + ->payload($payload) + ->doNotSign() + ->dispatch(); + } + } +} diff --git a/app/Listeners/Teams/SendSpeedtestThresholdNotification.php b/app/Listeners/Teams/SendSpeedtestThresholdNotification.php new file mode 100644 index 000000000..3f1d5ac92 --- /dev/null +++ b/app/Listeners/Teams/SendSpeedtestThresholdNotification.php @@ -0,0 +1,132 @@ +teams_enabled) { + return; + } + + if (! $notificationSettings->teams_on_threshold_failure) { + return; + } + + if (! count($notificationSettings->teams_webhooks)) { + Log::warning('Teams urls not found, check Teams 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 Teams thresholds not found, won\'t send notification.'); + + return; + } + + $payload = [ + 'text' => view('teams.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->teams_webhooks as $url) { + WebhookCall::create() + ->url($url['url']) + ->payload($payload) + ->doNotSign() + ->dispatch(); + } + } + + /** + * Build Teams 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 Teams 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 Teams 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 f41f962e6..502325b11 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 $teams_enabled; + + public bool $teams_on_speedtest_run; + + public bool $teams_on_threshold_failure; + + public ?array $teams_webhooks; + public bool $pushover_enabled; public bool $pushover_on_speedtest_run; diff --git a/database/settings/2024_02_22_144650_create_teams_notification_settings.php b/database/settings/2024_02_22_144650_create_teams_notification_settings.php new file mode 100644 index 000000000..41578df05 --- /dev/null +++ b/database/settings/2024_02_22_144650_create_teams_notification_settings.php @@ -0,0 +1,14 @@ +migrator->add('notification.teams_enabled', false); + $this->migrator->add('notification.teams_on_speedtest_run', false); + $this->migrator->add('notification.teams_on_threshold_failure', false); + $this->migrator->add('notification.teams_webhooks', null); + } +}; diff --git a/resources/views/teams/speedtest-completed.blade.php b/resources/views/teams/speedtest-completed.blade.php new file mode 100644 index 000000000..dda7a8782 --- /dev/null +++ b/resources/views/teams/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/teams/speedtest-threshold.blade.php b/resources/views/teams/speedtest-threshold.blade.php new file mode 100644 index 000000000..2cb708643 --- /dev/null +++ b/resources/views/teams/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 }}