|
6 | 6 | use App\Mail\Threshold\AbsoluteMail; |
7 | 7 | use App\Settings\NotificationSettings; |
8 | 8 | use App\Settings\ThresholdSettings; |
| 9 | +use App\Telegram\TelegramNotification; |
9 | 10 | use Filament\Notifications\Notification; |
10 | 11 | use Illuminate\Contracts\Queue\ShouldQueue; |
11 | 12 | use Illuminate\Support\Facades\Log; |
@@ -52,6 +53,11 @@ public function handle(ResultCreated $event) |
52 | 53 | if ($this->notificationSettings->mail_enabled == true && $this->notificationSettings->mail_on_threshold_failure == true) { |
53 | 54 | $this->mailChannel($event); |
54 | 55 | } |
| 56 | + |
| 57 | + // Telegram notification channel |
| 58 | + if ($this->notificationSettings->telegram_enabled == true && $this->notificationSettings->telegram_on_threshold_failure == true) { |
| 59 | + $this->telegramChannel($event); |
| 60 | + } |
55 | 61 | } |
56 | 62 |
|
57 | 63 | /** |
@@ -150,4 +156,65 @@ protected function mailChannel(ResultCreated $event) |
150 | 156 | } |
151 | 157 | } |
152 | 158 | } |
| 159 | + |
| 160 | + /** |
| 161 | + * Handle telegram notifications. |
| 162 | + * |
| 163 | + * @param \App\Events\ResultCreated $event |
| 164 | + * @return void |
| 165 | + */ |
| 166 | + protected function telegramChannel(ResultCreated $event) |
| 167 | + { |
| 168 | + $failedThresholds = []; |
| 169 | + |
| 170 | + if (! count($this->notificationSettings->telegram_recipients) > 0) { |
| 171 | + Log::info('Skipping sending telegram notification, no recipients.'); |
| 172 | + } |
| 173 | + |
| 174 | + // Download threshold |
| 175 | + if ($this->thresholdSettings->absolute_download > 0) { |
| 176 | + if (absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) { |
| 177 | + array_push($failedThresholds, [ |
| 178 | + 'name' => 'Download', |
| 179 | + 'threshold' => $this->thresholdSettings->absolute_download.' Mbps', |
| 180 | + 'value' => formatBits(formatBytesToBits($event->result->download)).'ps', |
| 181 | + ]); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + // Upload threshold |
| 186 | + if ($this->thresholdSettings->absolute_upload > 0) { |
| 187 | + if (absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) { |
| 188 | + array_push($failedThresholds, [ |
| 189 | + 'name' => 'Upload', |
| 190 | + 'threshold' => $this->thresholdSettings->absolute_upload.' Mbps', |
| 191 | + 'value' => formatBits(formatBytesToBits($event->result->upload)).'ps', |
| 192 | + ]); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + // Ping threshold |
| 197 | + if ($this->thresholdSettings->absolute_ping > 0) { |
| 198 | + if (absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) { |
| 199 | + array_push($failedThresholds, [ |
| 200 | + 'name' => 'Ping', |
| 201 | + 'threshold' => $this->thresholdSettings->absolute_ping.' Ms', |
| 202 | + 'value' => round($event->result->ping, 2).' Ms', |
| 203 | + ]); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + if (count($failedThresholds)) { |
| 208 | + foreach ($this->notificationSettings->telegram_recipients as $recipient) { |
| 209 | + $message = view('telegram.threshold.absolute', [ |
| 210 | + 'id' => $event->result->id, |
| 211 | + 'url' => url('/admin/results'), |
| 212 | + 'metrics' => $failedThresholds, |
| 213 | + ])->render(); |
| 214 | + |
| 215 | + \Illuminate\Support\Facades\Notification::route('telegram_chat_id', $recipient['telegram_chat_id']) |
| 216 | + ->notify(new TelegramNotification($message)); |
| 217 | + } |
| 218 | + } |
| 219 | + } |
153 | 220 | } |
0 commit comments