@@ -219,6 +219,55 @@ protected function telegramChannel(ResultCreated $event): void
219219 }
220220 }
221221
222+ /**
223+ * Handle Discord notifications.
224+ */
225+
226+ protected function sendDiscordNotification (ResultCreated $ event ): void
227+ {
228+ if ($ this ->notificationSettings ->discord_enabled ) {
229+ $ failedThresholds = []; // Initialize an array to keep track of failed thresholds
230+
231+ // Check Download threshold
232+ if ($ this ->thresholdSettings ->absolute_download > 0 && absoluteDownloadThresholdFailed ($ this ->thresholdSettings ->absolute_download , $ event ->result ->downloadBits )) {
233+ $ failedThresholds [] = 'Download ' ;
234+ }
235+
236+ // Check Upload threshold
237+ if ($ this ->thresholdSettings ->absolute_upload > 0 && absoluteUploadThresholdFailed ($ this ->thresholdSettings ->absolute_upload , $ event ->result ->uploadBits )) {
238+ $ failedThresholds [] = 'Upload ' ;
239+ }
240+
241+ // Check Ping threshold
242+ if ($ this ->thresholdSettings ->absolute_ping > 0 && absolutePingThresholdFailed ($ this ->thresholdSettings ->absolute_ping , $ event ->result ->ping )) {
243+ $ failedThresholds [] = 'Ping ' ;
244+ }
245+
246+ // Proceed with sending notifications only if there are any failed thresholds
247+ if (count ($ failedThresholds ) > 0 ) {
248+ if ($ this ->notificationSettings ->discord_on_threshold_failure && count ($ this ->notificationSettings ->discord_webhooks )) {
249+ foreach ($ this ->notificationSettings ->discord_webhooks as $ webhook ) {
250+ // Construct the payload with the failed thresholds information
251+ $ contentLines = [
252+ 'There are new speedtest results for your network. ' ,
253+ "Result ID: " . $ event ->result ->id ,
254+ "Site Name: " . $ this ->generalSettings ->site_name
255+ ];
256+ foreach ($ failedThresholds as $ metric ) {
257+ $ contentLines [] = "{$ metric } threshold failed. " ;
258+ }
259+ $ payload = [
260+ 'content ' => 'Testing Threshold Run ' ,
261+ ];
262+
263+ // Send the request using Laravel's HTTP client
264+ $ response = Http::post ($ webhook ['discord_webhook_url ' ], $ payload );
265+ }
266+ }
267+ }
268+ }
269+ }
270+
222271 /**
223272 * Handle webhook notifications.
224273 *
0 commit comments