Skip to content

Commit 25774dc

Browse files
authored
[Chore] Refactored Telegram notifications to their own listeners (alexjustesen#1270)
1 parent 03178bb commit 25774dc

File tree

11 files changed

+204
-124
lines changed

11 files changed

+204
-124
lines changed

app/Listeners/Mail/SendSpeedtestThresholdNotification.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function absoluteDownloadThreshold(SpeedtestCompleted $event, Threshol
7676
}
7777

7878
/**
79-
* Build webhook notification if absolute upload threshold is breached.
79+
* Build mail notification if absolute upload threshold is breached.
8080
*/
8181
protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array
8282
{
@@ -92,7 +92,7 @@ protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdS
9292
}
9393

9494
/**
95-
* Build webhook notification if absolute ping threshold is breached.
95+
* Build mail notification if absolute ping threshold is breached.
9696
*/
9797
protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array
9898
{

app/Listeners/SpeedtestCompletedListener.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use App\Events\SpeedtestCompleted;
66
use App\Settings\GeneralSettings;
77
use App\Settings\NotificationSettings;
8-
use App\Telegram\TelegramNotification;
98
use Spatie\WebhookServer\WebhookCall;
109

1110
class SpeedtestCompletedListener
@@ -31,29 +30,6 @@ public function __construct()
3130
*/
3231
public function handle(SpeedtestCompleted $event): void
3332
{
34-
if ($this->notificationSettings->telegram_enabled) {
35-
if ($this->notificationSettings->telegram_on_speedtest_run && count($this->notificationSettings->telegram_recipients)) {
36-
foreach ($this->notificationSettings->telegram_recipients as $recipient) {
37-
$download_value = toBits(convertSize($event->result->download), 2).' (Mbps)';
38-
39-
$upload_value = toBits(convertSize($event->result->upload), 2).' (Mbps)';
40-
41-
$ping_value = number_format($event->result->ping, 2).' (ms)';
42-
43-
$message = view('telegram.speedtest-completed', [
44-
'id' => $event->result->id,
45-
'site_name' => $this->generalSettings->site_name,
46-
'ping' => $ping_value,
47-
'download' => $download_value,
48-
'upload' => $upload_value,
49-
])->render();
50-
51-
\Illuminate\Support\Facades\Notification::route('telegram_chat_id', $recipient['telegram_chat_id'])
52-
->notify(new TelegramNotification($message));
53-
}
54-
}
55-
}
56-
5733
if ($this->notificationSettings->discord_enabled) {
5834
if ($this->notificationSettings->discord_on_speedtest_run && count($this->notificationSettings->discord_webhooks)) {
5935
foreach ($this->notificationSettings->discord_webhooks as $webhook) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace App\Listeners\Telegram;
4+
5+
use App\Events\SpeedtestCompleted;
6+
use App\Helpers\Number;
7+
use App\Notifications\Telegram\SpeedtestNotification;
8+
use App\Settings\NotificationSettings;
9+
use Illuminate\Support\Facades\Log;
10+
use Illuminate\Support\Facades\Notification;
11+
use Illuminate\Support\Str;
12+
13+
class SendSpeedtestCompletedNotification
14+
{
15+
/**
16+
* Handle the event.
17+
*/
18+
public function handle(SpeedtestCompleted $event): void
19+
{
20+
$notificationSettings = new NotificationSettings();
21+
22+
if (! $notificationSettings->telegram_enabled) {
23+
return;
24+
}
25+
26+
if (! $notificationSettings->telegram_on_speedtest_run) {
27+
return;
28+
}
29+
30+
if (! count($notificationSettings->telegram_recipients)) {
31+
Log::warning('Telegram recipients not found, check Telegram notification channel settings.');
32+
33+
return;
34+
}
35+
36+
$content = view('telegram.speedtest-completed', [
37+
'id' => $event->result->id,
38+
'service' => Str::title($event->result->service),
39+
'serverName' => $event->result->server_name,
40+
'serverId' => $event->result->server_id,
41+
'ping' => round($event->result->ping).' ms',
42+
'download' => Number::toBitRate(bits: $event->result->download_bits, precision: 2),
43+
'upload' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2),
44+
])->render();
45+
46+
foreach ($notificationSettings->telegram_recipients as $recipient) {
47+
Notification::route('telegram_chat_id', $recipient['telegram_chat_id'])
48+
->notify(new SpeedtestNotification($content, $notificationSettings->telegram_disable_notification));
49+
}
50+
}
51+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
namespace App\Listeners\Telegram;
4+
5+
use App\Events\SpeedtestCompleted;
6+
use App\Helpers\Number;
7+
use App\Notifications\Telegram\SpeedtestNotification;
8+
use App\Settings\NotificationSettings;
9+
use App\Settings\ThresholdSettings;
10+
use Illuminate\Support\Facades\Log;
11+
use Illuminate\Support\Facades\Notification;
12+
use Illuminate\Support\Str;
13+
14+
class SendSpeedtestThresholdNotification
15+
{
16+
/**
17+
* Handle the event.
18+
*/
19+
public function handle(SpeedtestCompleted $event): void
20+
{
21+
$notificationSettings = new NotificationSettings();
22+
23+
if (! $notificationSettings->telegram_enabled) {
24+
return;
25+
}
26+
27+
if (! $notificationSettings->telegram_on_threshold_failure) {
28+
return;
29+
}
30+
31+
if (! count($notificationSettings->telegram_recipients) > 0) {
32+
Log::warning('Telegram recipients not found, check Telegram notification channel settings.');
33+
34+
return;
35+
}
36+
37+
$thresholdSettings = new ThresholdSettings();
38+
39+
$failed = [];
40+
41+
if ($thresholdSettings->absolute_download > 0) {
42+
array_push($failed, $this->absoluteDownloadThreshold(event: $event, thresholdSettings: $thresholdSettings));
43+
}
44+
45+
if ($thresholdSettings->absolute_upload > 0) {
46+
array_push($failed, $this->absoluteUploadThreshold(event: $event, thresholdSettings: $thresholdSettings));
47+
}
48+
49+
if ($thresholdSettings->absolute_ping > 0) {
50+
array_push($failed, $this->absolutePingThreshold(event: $event, thresholdSettings: $thresholdSettings));
51+
}
52+
53+
if (! count($failed)) {
54+
return;
55+
}
56+
57+
$content = view('telegram.speedtest-threshold', [
58+
'id' => $event->result->id,
59+
'service' => Str::title($event->result->service),
60+
'serverName' => $event->result->server_name,
61+
'serverId' => $event->result->server_id,
62+
'metrics' => $failed,
63+
])->render();
64+
65+
foreach ($notificationSettings->telegram_recipients as $recipient) {
66+
Notification::route('telegram_chat_id', $recipient['telegram_chat_id'])
67+
->notify(new SpeedtestNotification($content, $notificationSettings->telegram_disable_notification));
68+
}
69+
}
70+
71+
/**
72+
* Build Telegram notification if absolute download threshold is breached.
73+
*/
74+
protected function absoluteDownloadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array
75+
{
76+
if (! absoluteDownloadThresholdFailed($thresholdSettings->absolute_download, $event->result->download)) {
77+
return [];
78+
}
79+
80+
return [
81+
'name' => 'Download',
82+
'threshold' => $thresholdSettings->absolute_download.' Mbps',
83+
'value' => Number::toBitRate(bits: $event->result->download_bits, precision: 2),
84+
];
85+
}
86+
87+
/**
88+
* Build Telegram notification if absolute upload threshold is breached.
89+
*/
90+
protected function absoluteUploadThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array
91+
{
92+
if (! absoluteUploadThresholdFailed($thresholdSettings->absolute_upload, $event->result->upload)) {
93+
return [];
94+
}
95+
96+
return [
97+
'name' => 'Upload',
98+
'threshold' => $thresholdSettings->absolute_upload.' Mbps',
99+
'value' => Number::toBitRate(bits: $event->result->upload_bits, precision: 2),
100+
];
101+
}
102+
103+
/**
104+
* Build Telegram notification if absolute ping threshold is breached.
105+
*/
106+
protected function absolutePingThreshold(SpeedtestCompleted $event, ThresholdSettings $thresholdSettings): array
107+
{
108+
if (! absolutePingThresholdFailed($thresholdSettings->absolute_ping, $event->result->ping)) {
109+
return [];
110+
}
111+
112+
return [
113+
'name' => 'Ping',
114+
'threshold' => $thresholdSettings->absolute_ping.' ms',
115+
'value' => round($event->result->ping, 2).' ms',
116+
];
117+
}
118+
}

app/Listeners/Threshold/AbsoluteListener.php

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
use App\Settings\GeneralSettings;
77
use App\Settings\NotificationSettings;
88
use App\Settings\ThresholdSettings;
9-
use App\Telegram\TelegramNotification;
109
use Illuminate\Contracts\Queue\ShouldQueue;
11-
use Illuminate\Support\Facades\Log;
1210
use Spatie\WebhookServer\WebhookCall;
1311

1412
class AbsoluteListener implements ShouldQueue
@@ -42,76 +40,12 @@ public function handle(SpeedtestCompleted $event): void
4240
return;
4341
}
4442

45-
// Telegram notification channel
46-
if ($this->notificationSettings->telegram_enabled == true && $this->notificationSettings->telegram_on_threshold_failure == true) {
47-
$this->telegramChannel($event);
48-
}
49-
5043
// Discord notification channel
5144
if ($this->notificationSettings->discord_enabled == true && $this->notificationSettings->discord_on_threshold_failure == true) {
5245
$this->discordChannel($event);
5346
}
5447
}
5548

56-
/**
57-
* Handle telegram notifications.
58-
*/
59-
protected function telegramChannel(SpeedtestCompleted $event): void
60-
{
61-
$failedThresholds = [];
62-
63-
if (! count($this->notificationSettings->telegram_recipients) > 0) {
64-
Log::info('Skipping sending telegram notification, no recipients.');
65-
}
66-
67-
// Download threshold
68-
if ($this->thresholdSettings->absolute_download > 0) {
69-
if (absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) {
70-
array_push($failedThresholds, [
71-
'name' => 'Download',
72-
'threshold' => $this->thresholdSettings->absolute_download.' Mbps',
73-
'value' => toBits(convertSize($event->result->download), 2).'Mbps',
74-
]);
75-
}
76-
}
77-
78-
// Upload threshold
79-
if ($this->thresholdSettings->absolute_upload > 0) {
80-
if (absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) {
81-
array_push($failedThresholds, [
82-
'name' => 'Upload',
83-
'threshold' => $this->thresholdSettings->absolute_upload.' Mbps',
84-
'value' => toBits(convertSize($event->result->upload), 2).'Mbps',
85-
]);
86-
}
87-
}
88-
89-
// Ping threshold
90-
if ($this->thresholdSettings->absolute_ping > 0) {
91-
if (absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) {
92-
array_push($failedThresholds, [
93-
'name' => 'Ping',
94-
'threshold' => $this->thresholdSettings->absolute_ping.' ms',
95-
'value' => round($event->result->ping, 2).' ms',
96-
]);
97-
}
98-
}
99-
100-
if (count($failedThresholds)) {
101-
foreach ($this->notificationSettings->telegram_recipients as $recipient) {
102-
$message = view('telegram.threshold.absolute', [
103-
'id' => $event->result->id,
104-
'url' => url('/admin/results'),
105-
'site_name' => $this->generalSettings->site_name,
106-
'metrics' => $failedThresholds,
107-
])->render();
108-
109-
\Illuminate\Support\Facades\Notification::route('telegram_chat_id', $recipient['telegram_chat_id'])
110-
->notify(new TelegramNotification($message));
111-
}
112-
}
113-
}
114-
11549
/**
11650
* Handle Discord notifications.
11751
*/
Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,31 @@
11
<?php
22

3-
namespace App\Telegram;
3+
namespace App\Notifications\Telegram;
44

5-
use App\Settings\NotificationSettings;
65
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
77
use Illuminate\Notifications\Notification;
88
use NotificationChannels\Telegram\TelegramMessage;
99

10-
class TelegramNotification extends Notification
10+
class SpeedtestNotification extends Notification implements ShouldQueue
1111
{
1212
use Queueable;
1313

14-
protected $message;
15-
16-
protected $settings;
17-
1814
/**
1915
* Create a new notification instance.
20-
*
21-
* @return void
2216
*/
23-
public function __construct($message)
24-
{
25-
$this->message = $message;
26-
27-
$this->settings = new NotificationSettings();
17+
public function __construct(
18+
public string $content,
19+
public bool $disableNotification = false,
20+
) {
2821
}
2922

3023
/**
3124
* Get the notification's delivery channels.
3225
*
33-
* @param mixed $notifiable
26+
* @return array<int, string>
3427
*/
35-
public function via($notifiable): array
28+
public function via(object $notifiable): array
3629
{
3730
return ['telegram'];
3831
}
@@ -46,7 +39,7 @@ public function toTelegram($notifiable): TelegramMessage
4639
{
4740
return TelegramMessage::create()
4841
->to($notifiable->routes['telegram_chat_id'])
49-
->disableNotification($this->settings->telegram_disable_notification)
50-
->content($this->message);
42+
->content($this->content)
43+
->disableNotification($this->disableNotification);
5144
}
5245
}

app/Providers/EventServiceProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use App\Listeners\Mail\SendSpeedtestCompletedNotification as MailSendSpeedtestCompletedNotification;
1313
use App\Listeners\Mail\SendSpeedtestThresholdNotification as MailSendSpeedtestThresholdNotification;
1414
use App\Listeners\SpeedtestCompletedListener;
15+
use App\Listeners\Telegram\SendSpeedtestCompletedNotification as TelegramSendSpeedtestCompletedNotification;
16+
use App\Listeners\Telegram\SendSpeedtestThresholdNotification as TelegramSendSpeedtestThresholdNotification;
1517
use App\Listeners\Threshold\AbsoluteListener;
1618
use App\Listeners\Webhook\SendSpeedtestCompletedNotification as WebhookSendSpeedtestCompletedNotification;
1719
use App\Listeners\Webhook\SendSpeedtestThresholdNotification as WebhookSendSpeedtestThresholdNotification;
@@ -51,6 +53,10 @@ class EventServiceProvider extends ServiceProvider
5153
MailSendSpeedtestCompletedNotification::class,
5254
MailSendSpeedtestThresholdNotification::class,
5355

56+
// Telegram notification listeners
57+
TelegramSendSpeedtestCompletedNotification::class,
58+
TelegramSendSpeedtestThresholdNotification::class,
59+
5460
// Webhook notification listeners
5561
WebhookSendSpeedtestCompletedNotification::class,
5662
WebhookSendSpeedtestThresholdNotification::class,

resources/views/emails/speedtest-threshold.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<x-mail::message>
2-
# Speedtest Thresholds Breached - #{{ $id }}
2+
# Speedtest Threshold Breached - #{{ $id }}
33

44
A new speedtest was completed using **{{ $service }}** but a threshold was breached.
55

0 commit comments

Comments
 (0)