Skip to content

Commit 3b0095b

Browse files
committed
Finished discord threshold notifications
1 parent f443e09 commit 3b0095b

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

app/Listeners/Threshold/AbsoluteListener.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ public function handle(ResultCreated $event): void
6363
$this->telegramChannel($event);
6464
}
6565

66+
// Discord notification channel
67+
if ($this->notificationSettings->discord_enabled == true && $this->notificationSettings->discord_on_threshold_failure == true) {
68+
$this->discordChannel($event);
69+
}
6670
// Webhook notification channel
6771
if ($this->notificationSettings->webhook_enabled == true && $this->notificationSettings->webhook_on_threshold_failure == true) {
6872
$this->webhookChannel($event);
@@ -223,24 +227,24 @@ protected function telegramChannel(ResultCreated $event): void
223227
* Handle Discord notifications.
224228
*/
225229

226-
protected function sendDiscordNotification(ResultCreated $event): void
230+
protected function discordChannel(ResultCreated $event): void
227231
{
228232
if ($this->notificationSettings->discord_enabled) {
229233
$failedThresholds = []; // Initialize an array to keep track of failed thresholds
230234

231235
// Check Download threshold
232236
if ($this->thresholdSettings->absolute_download > 0 && absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->downloadBits)) {
233-
$failedThresholds[] = 'Download';
237+
$failedThresholds['Download'] = ($event->result->downloadBits / 1000000) . " Mbits";
234238
}
235239

236240
// Check Upload threshold
237241
if ($this->thresholdSettings->absolute_upload > 0 && absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->uploadBits)) {
238-
$failedThresholds[] = 'Upload';
242+
$failedThresholds['Upload'] = ($event->result->uploadBits / 1000000) . " Mbits";
239243
}
240244

241245
// Check Ping threshold
242246
if ($this->thresholdSettings->absolute_ping > 0 && absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) {
243-
$failedThresholds[] = 'Ping';
247+
$failedThresholds['Ping'] = $event->result->ping . " ms";
244248
}
245249

246250
// Proceed with sending notifications only if there are any failed thresholds
@@ -249,15 +253,14 @@ protected function sendDiscordNotification(ResultCreated $event): void
249253
foreach ($this->notificationSettings->discord_webhooks as $webhook) {
250254
// Construct the payload with the failed thresholds information
251255
$contentLines = [
252-
'There are new speedtest results for your network.',
253256
"Result ID: " . $event->result->id,
254257
"Site Name: " . $this->generalSettings->site_name
255258
];
256-
foreach ($failedThresholds as $metric) {
257-
$contentLines[] = "{$metric} threshold failed.";
259+
foreach ($failedThresholds as $metric => $result) {
260+
$contentLines[] = "{$metric} threshold failed with result: {$result}.";
258261
}
259262
$payload = [
260-
'content' => 'Testing Threshold Run',
263+
'content' => implode("\n", $contentLines),
261264
];
262265

263266
// Send the request using Laravel's HTTP client

0 commit comments

Comments
 (0)