Skip to content

Commit 50f8c6f

Browse files
committed
Got test discord notifications working
1 parent 5b17328 commit 50f8c6f

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace App\Actions\Notifications;
4+
5+
use Filament\Notifications\Notification;
6+
use Lorisleiva\Actions\Concerns\AsAction;
7+
use Illuminate\Support\Facades\Http;
8+
9+
class SendDiscordTestNotification
10+
{
11+
use AsAction;
12+
13+
public function handle(array $webhooks)
14+
{
15+
if (! count($webhooks)) {
16+
Notification::make()
17+
->title('You need to add webhook urls!')
18+
->warning()
19+
->send();
20+
21+
return;
22+
}
23+
24+
foreach ($webhooks as $webhook) {
25+
$payload = [
26+
'content' => '👋 Testing the Webhook notification channel.',
27+
];
28+
29+
// Send the request using Laravel's HTTP client
30+
$response = Http::post($webhook['discord_webhook_url'], $payload);
31+
}
32+
33+
Notification::make()
34+
->title('Test webhook notification sent.')
35+
->success()
36+
->send();
37+
}
38+
}

app/Filament/Pages/Settings/NotificationPage.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Actions\Notifications\SendMailTestNotification;
77
use App\Actions\Notifications\SendTelegramTestNotification;
88
use App\Actions\Notifications\SendWebhookTestNotification;
9+
use App\Actions\Notifications\SendDiscordTestNotification;
910
use App\Settings\NotificationSettings;
1011
use Filament\Forms;
1112
use Filament\Forms\Form;
@@ -175,6 +176,48 @@ public function form(Form $form): Form
175176
'default' => 1,
176177
'md' => 2,
177178
]),
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+
]),
178221

179222
Forms\Components\Section::make('Webhook')
180223
->schema([

app/Settings/NotificationSettings.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ class NotificationSettings extends Settings
3838

3939
public ?array $webhook_urls;
4040

41+
public bool $discord_enabled;
42+
43+
public bool $discord_on_speedtest_run;
44+
45+
public bool $discord_on_threshold_failure;
46+
47+
public ?array $discord_webhooks;
48+
4149
public static function group(): string
4250
{
4351
return 'notification';
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Spatie\LaravelSettings\Migrations\SettingsMigration;
4+
5+
return new class extends SettingsMigration
6+
{
7+
/**
8+
* Run the migrations.
9+
*/
10+
public function up(): void
11+
{
12+
$this->migrator->add('notification.discord_enabled', false);
13+
$this->migrator->add('notification.discord_on_speedtest_run', false);
14+
$this->migrator->add('notification.discord_on_threshold_failure', false);
15+
$this->migrator->add('notification.discord_webhooks', null);
16+
}
17+
};

0 commit comments

Comments
 (0)