Skip to content

Commit 4dd8b91

Browse files
committed
Fixed syntax
1 parent 1abb8f7 commit 4dd8b91

File tree

5 files changed

+91
-92
lines changed

5 files changed

+91
-92
lines changed

app/Actions/Notifications/SendDiscordTestNotification.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace App\Actions\Notifications;
44

55
use Filament\Notifications\Notification;
6-
use Lorisleiva\Actions\Concerns\AsAction;
76
use Illuminate\Support\Facades\Http;
7+
use Lorisleiva\Actions\Concerns\AsAction;
88

99
class SendDiscordTestNotification
1010
{
@@ -25,7 +25,6 @@ public function handle(array $webhooks)
2525
$payload = [
2626
'content' => '👋 Testing the Webhook notification channel.',
2727
];
28-
2928
// Send the request using Laravel's HTTP client
3029
$response = Http::post($webhook['discord_webhook_url'], $payload);
3130
}

app/Filament/Pages/Settings/NotificationPage.php

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace App\Filament\Pages\Settings;
44

55
use App\Actions\Notifications\SendDatabaseTestNotification;
6+
use App\Actions\Notifications\SendDiscordTestNotification;
67
use App\Actions\Notifications\SendMailTestNotification;
78
use App\Actions\Notifications\SendTelegramTestNotification;
89
use App\Actions\Notifications\SendWebhookTestNotification;
9-
use App\Actions\Notifications\SendDiscordTestNotification;
1010
use App\Settings\NotificationSettings;
1111
use Filament\Forms;
1212
use Filament\Forms\Form;
@@ -86,6 +86,48 @@ public function form(Form $form): Form
8686
'md' => 2,
8787
]),
8888

89+
Forms\Components\Section::make('Discord')
90+
->schema([
91+
Forms\Components\Toggle::make('discord_enabled')
92+
->label('Enable Discord webhook notifications')
93+
->reactive()
94+
->columnSpanFull(),
95+
Forms\Components\Grid::make([
96+
'default' => 1,
97+
])
98+
->hidden(fn (Forms\Get $get) => $get('discord_enabled') !== true)
99+
->schema([
100+
Forms\Components\Fieldset::make('Triggers')
101+
->schema([
102+
Forms\Components\Toggle::make('discord_on_speedtest_run')
103+
->label('Notify on every speedtest run')
104+
->columnSpanFull(),
105+
Forms\Components\Toggle::make('discord_on_threshold_failure')
106+
->label('Notify on threshold failures')
107+
->columnSpanFull(),
108+
]),
109+
Forms\Components\Repeater::make('discord_webhooks')
110+
->label('Webhooks')
111+
->schema([
112+
Forms\Components\TextInput::make('discord_webhook_url')
113+
->label('Webhook URL')
114+
->required(),
115+
])
116+
->columnSpanFull(),
117+
Forms\Components\Actions::make([
118+
Forms\Components\Actions\Action::make('test discord')
119+
->label('Test Discord webhook')
120+
->action(fn (Forms\Get $get) => SendDiscordTestNotification::run(webhooks: $get('discord_webhooks')))
121+
->hidden(fn (Forms\Get $get) => ! count($get('discord_webhooks'))),
122+
]),
123+
]),
124+
])
125+
->compact()
126+
->columns([
127+
'default' => 1,
128+
'md' => 2,
129+
]),
130+
89131
Forms\Components\Section::make('Mail')
90132
->schema([
91133
Forms\Components\Toggle::make('mail_enabled')
@@ -176,48 +218,6 @@ public function form(Form $form): Form
176218
'default' => 1,
177219
'md' => 2,
178220
]),
179-
180-
Forms\Components\Section::make('Discord')
181-
->schema([
182-
Forms\Components\Toggle::make('discord_enabled')
183-
->label('Enable Discord webhook notifications')
184-
->reactive()
185-
->columnSpanFull(),
186-
Forms\Components\Grid::make([
187-
'default' => 1,
188-
])
189-
->hidden(fn (Forms\Get $get) => $get('discord_enabled') !== true)
190-
->schema([
191-
Forms\Components\Fieldset::make('Triggers')
192-
->schema([
193-
Forms\Components\Toggle::make('discord_on_speedtest_run')
194-
->label('Notify on every speedtest run')
195-
->columnSpanFull(),
196-
Forms\Components\Toggle::make('discord_on_threshold_failure')
197-
->label('Notify on threshold failures')
198-
->columnSpanFull(),
199-
]),
200-
Forms\Components\Repeater::make('discord_webhooks')
201-
->label('Webhooks')
202-
->schema([
203-
Forms\Components\TextInput::make('discord_webhook_url')
204-
->label('Webhook URL')
205-
->required(),
206-
])
207-
->columnSpanFull(),
208-
Forms\Components\Actions::make([
209-
Forms\Components\Actions\Action::make('test discord')
210-
->label('Test Discord webhook')
211-
->action(fn (Forms\Get $get) => SendDiscordTestNotification::run(webhooks: $get('discord_webhooks')))
212-
->hidden(fn (Forms\Get $get) => !count($get('discord_webhooks'))),
213-
]),
214-
]),
215-
])
216-
->compact()
217-
->columns([
218-
'default' => 1,
219-
'md' => 2,
220-
]),
221221

222222
Forms\Components\Section::make('Webhook')
223223
->schema([

app/Listeners/SpeedtestCompletedListener.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
use App\Settings\NotificationSettings;
99
use App\Telegram\TelegramNotification;
1010
use Filament\Notifications\Notification;
11+
use Illuminate\Support\Facades\Http;
1112
use Illuminate\Support\Facades\Mail;
1213
use Spatie\WebhookServer\WebhookCall;
13-
use Illuminate\Support\Facades\Http;
1414

1515
class SpeedtestCompletedListener
1616
{
@@ -81,17 +81,15 @@ public function handle(ResultCreated $event): void
8181
foreach ($this->notificationSettings->discord_webhooks as $webhook) {
8282
// Construct the payload
8383
$payload = [
84-
'content' => 'There are new speedtest results for your network.' .
85-
"\nResult ID: " . $event->result->id .
86-
"\nSite Name: " . $this->generalSettings->site_name .
87-
"\nPing: " . $event->result->ping . " ms" .
88-
"\nDownload: " . ($event->result->downloadBits / 1000000) . ' (Mbps)' .
89-
"\nUpload: " . ($event->result->uploadBits / 1000000) . ' (Mbps)',
84+
'content' => 'There are new speedtest results for your network.'.
85+
"\nResult ID: ".$event->result->id.
86+
"\nSite Name: ".$this->generalSettings->site_name.
87+
"\nPing: ".$event->result->ping.' ms'.
88+
"\nDownload: ".($event->result->downloadBits / 1000000).' (Mbps)'.
89+
"\nUpload: ".($event->result->uploadBits / 1000000).' (Mbps)',
9090
];
91-
9291
// Send the request using Laravel's HTTP client
9392
$response = Http::post($webhook['discord_webhook_url'], $payload);
94-
9593
}
9694
}
9795
}

app/Listeners/Threshold/AbsoluteListener.php

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function handle(ResultCreated $event): void
6767
if ($this->notificationSettings->discord_enabled == true && $this->notificationSettings->discord_on_threshold_failure == true) {
6868
$this->discordChannel($event);
6969
}
70+
7071
// Webhook notification channel
7172
if ($this->notificationSettings->webhook_enabled == true && $this->notificationSettings->webhook_on_threshold_failure == true) {
7273
$this->webhookChannel($event);
@@ -226,50 +227,51 @@ protected function telegramChannel(ResultCreated $event): void
226227
/**
227228
* Handle Discord notifications.
228229
*/
229-
230230
protected function discordChannel(ResultCreated $event): void
231231
{
232-
if ($this->notificationSettings->discord_enabled) {
233-
$failedThresholds = []; // Initialize an array to keep track of failed thresholds
234-
235-
// Check Download threshold
236-
if ($this->thresholdSettings->absolute_download > 0 && absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->downloadBits)) {
237-
$failedThresholds['Download'] = ($event->result->downloadBits / 1000000) . ' (Mbps)';
238-
}
239-
240-
// Check Upload threshold
241-
if ($this->thresholdSettings->absolute_upload > 0 && absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->uploadBits)) {
242-
$failedThresholds['Upload'] = ($event->result->uploadBits / 1000000) . ' (Mbps)';
243-
}
244-
245-
// Check Ping threshold
246-
if ($this->thresholdSettings->absolute_ping > 0 && absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) {
247-
$failedThresholds['Ping'] = $event->result->ping . " ms";
248-
}
249-
250-
// Proceed with sending notifications only if there are any failed thresholds
251-
if (count($failedThresholds) > 0) {
252-
if ($this->notificationSettings->discord_on_threshold_failure && count($this->notificationSettings->discord_webhooks)) {
253-
foreach ($this->notificationSettings->discord_webhooks as $webhook) {
254-
// Construct the payload with the failed thresholds information
255-
$contentLines = [
256-
"Result ID: " . $event->result->id,
257-
"Site Name: " . $this->generalSettings->site_name
258-
];
259-
foreach ($failedThresholds as $metric => $result) {
260-
$contentLines[] = "{$metric} threshold failed with result: {$result}.";
232+
if ($this->notificationSettings->discord_enabled) {
233+
$failedThresholds = []; // Initialize an array to keep track of failed thresholds
234+
235+
// Check Download threshold
236+
if ($this->thresholdSettings->absolute_download > 0 && absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->downloadBits)) {
237+
$failedThresholds['Download'] = ($event->result->downloadBits / 1000000).' (Mbps)';
238+
}
239+
240+
// Check Upload threshold
241+
if ($this->thresholdSettings->absolute_upload > 0 && absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->uploadBits)) {
242+
$failedThresholds['Upload'] = ($event->result->uploadBits / 1000000).' (Mbps)';
243+
}
244+
245+
// Check Ping threshold
246+
if ($this->thresholdSettings->absolute_ping > 0 && absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) {
247+
$failedThresholds['Ping'] = $event->result->ping.' ms';
248+
}
249+
250+
// Proceed with sending notifications only if there are any failed thresholds
251+
if (count($failedThresholds) > 0) {
252+
if ($this->notificationSettings->discord_on_threshold_failure && count($this->notificationSettings->discord_webhooks)) {
253+
foreach ($this->notificationSettings->discord_webhooks as $webhook) {
254+
// Construct the payload with the failed thresholds information
255+
$contentLines = [
256+
'Result ID: '.$event->result->id,
257+
'Site Name: '.$this->generalSettings->site_name,
258+
];
259+
260+
foreach ($failedThresholds as $metric => $result) {
261+
$contentLines[] = "{$metric} threshold failed with result: {$result}.";
262+
}
263+
264+
$payload = [
265+
'content' => implode("\n", $contentLines),
266+
];
267+
268+
// Send the request using Laravel's HTTP client
269+
$response = Http::post($webhook['discord_webhook_url'], $payload);
261270
}
262-
$payload = [
263-
'content' => implode("\n", $contentLines),
264-
];
265-
266-
// Send the request using Laravel's HTTP client
267-
$response = Http::post($webhook['discord_webhook_url'], $payload);
268271
}
269272
}
270273
}
271274
}
272-
}
273275

274276
/**
275277
* Handle webhook notifications.

database/migrations/2024_02_19_000000_create_discord_notification_settings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ public function up(): void
1414
$this->migrator->add('notification.discord_on_threshold_failure', false);
1515
$this->migrator->add('notification.discord_webhooks', null);
1616
}
17-
};
17+
};

0 commit comments

Comments
 (0)