forked from alexjustesen/speedtest-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationPage.php
More file actions
executable file
·252 lines (227 loc) · 11 KB
/
NotificationPage.php
File metadata and controls
executable file
·252 lines (227 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
namespace App\Filament\Pages\Settings;
use App\Forms\Components\TestDatabaseNotification;
use App\Forms\Components\TestMailNotification;
use App\Forms\Components\TestTelegramNotification;
use App\Mail\Test;
use App\Settings\GeneralSettings;
use App\Settings\NotificationSettings;
use App\Telegram\TelegramNotification;
use Closure;
use Filament\Forms\Components\Card;
use Filament\Forms\Components\Fieldset;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Components\View;
use Filament\Notifications\Notification;
use Filament\Pages\SettingsPage;
use Illuminate\Support\Facades\Mail;
class NotificationPage extends SettingsPage
{
protected static ?string $navigationIcon = 'heroicon-o-bell';
protected static ?string $navigationGroup = 'Settings';
protected static ?int $navigationSort = 3;
protected static ?string $title = 'Notifications';
protected static ?string $navigationLabel = 'Notifications';
protected static string $settings = NotificationSettings::class;
protected function getMaxContentWidth(): string
{
$settings = new GeneralSettings();
return $settings->content_width;
}
protected function getFormSchema(): array
{
return [
Grid::make([
'default' => 1,
'md' => 3,
])
->schema([
Grid::make([
'default' => 1,
])
->schema([
Section::make('Database')
->description('Notifications sent to this channel will show up under the 🔔 icon in the header.')
->schema([
Toggle::make('database_enabled')
->label('Enable database notifications')
->reactive()
->columnSpan(2),
Grid::make([
'default' => 1,
])
->hidden(fn (Closure $get) => $get('database_enabled') !== true)
->schema([
Fieldset::make('Triggers')
->schema([
Toggle::make('database_on_speedtest_run')
->label('Notify on every speedtest run')
->columnSpan(2),
Toggle::make('database_on_threshold_failure')
->label('Notify on threshold failures')
->columnSpan(2),
]),
TestDatabaseNotification::make('test channel'),
]),
])
->compact()
->columns([
'default' => 1,
'md' => 2,
]),
Section::make('Mail')
->schema([
Toggle::make('mail_enabled')
->label('Enable mail notifications')
->reactive()
->columnSpan(2),
Grid::make([
'default' => 1,
])
->schema([
Fieldset::make('Triggers')
->schema([
Toggle::make('mail_on_speedtest_run')
->label('Notify on every speedtest run')
->columnSpan(2),
Toggle::make('mail_on_threshold_failure')
->label('Notify on threshold failures')
->columnSpan(2),
]),
])
->hidden(fn (Closure $get) => $get('mail_enabled') !== true),
Repeater::make('mail_recipients')
->label('Recipients')
->schema([
TextInput::make('email_address')
->email()
->required(),
])
->hidden(fn (Closure $get) => $get('mail_enabled') !== true)
->columnSpan(['md' => 2]),
TestMailNotification::make('test channel')
->hidden(fn (Closure $get) => $get('mail_enabled') !== true),
])
->compact()
->columns([
'default' => 1,
'md' => 2,
]),
Section::make('Telegram')
->schema([
Toggle::make('telegram_enabled')
->label('Enable telegram notifications')
->reactive()
->columnSpan(2),
Grid::make([
'default' => 1, ])
->hidden(fn (Closure $get) => $get('telegram_enabled') !== true)
->schema([
Fieldset::make('Triggers')
->schema([
Toggle::make('telegram_on_speedtest_run')
->label('Notify on every speedtest run')
->columnSpan(2),
Toggle::make('telegram_on_threshold_failure')
->label('Notify on threshold failures')
->columnSpan(2),
]),
]),
Repeater::make('telegram_recipients')
->label('Recipients')
->schema([
TextInput::make('telegram_chat_id')
->maxLength(50)
->required()
->columnSpan(['md' => 2]),
])
->hidden(fn (Closure $get) => $get('telegram_enabled') !== true)
->columnSpan(['md' => 2]),
TestTelegramNotification::make('test channel')
->hidden(fn (Closure $get) => $get('telegram_enabled') !== true),
])
->compact()
->columns([
'default' => 1,
'md' => 2,
]),
])
->columnSpan([
'md' => 2,
]),
Card::make()
->schema([
View::make('filament.forms.notifications-helptext'),
])
->columnSpan([
'md' => 1,
]),
]),
];
}
public function sendTestDatabaseNotification()
{
$recipient = auth()->user();
$recipient->notify(
Notification::make()
->title('Test database notification received!')
->body('You say pong')
->success()
->toDatabase(),
);
Notification::make()
->title('Test database notification sent.')
->body('I say ping')
->success()
->send();
}
public function sendTestMailNotification()
{
$notificationSettings = new (NotificationSettings::class);
if (! empty($notificationSettings->mail_recipients)) {
foreach ($notificationSettings->mail_recipients as $recipient) {
Mail::to($recipient)
->send(new Test());
}
Notification::make()
->title('Test mail notification sent.')
->success()
->send();
} else {
Notification::make()
->title('You need to add recipients to receive mail notifications.')
->warning()
->send();
}
}
public function sendTestTelegramNotification()
{
$notificationSettings = new (NotificationSettings::class);
$bot = config('telegram.bot');
if (blank($bot)) {
Notification::make()
->title('First you need to add \'TELEGRAM_BOT_TOKEN\' on your .env file or add it as environment variable')
->warning()
->send();
}
if (! empty($notificationSettings->telegram_recipients)) {
foreach ($notificationSettings->telegram_recipients as $recipient) {
\Illuminate\Support\Facades\Notification::route('telegram_chat_id', $recipient['telegram_chat_id'])
->notify(new TelegramNotification('Test notification channel *telegram*'));
}
Notification::make()
->title('Test Telegram notification sent.')
->success()
->send();
} else {
Notification::make()
->title('You need to add recipients to receive Telegram notifications.')
->warning()
->send();
}
}
}