From 74540d9d83771fa8b55a0bd92e55514145c98c74 Mon Sep 17 00:00:00 2001 From: garanda21 Date: Wed, 11 Jan 2023 15:29:41 +0100 Subject: [PATCH 1/3] Telegram channel notification --- .../Pages/Settings/NotificationPage.php | 62 ++++++++++++++++ .../Components/TestTelegramNotification.php | 10 +++ app/Listeners/SpeedtestCompletedListener.php | 21 ++++++ app/Listeners/Threshold/AbsoluteListener.php | 67 +++++++++++++++++ app/Settings/NotificationSettings.php | 8 ++ app/Telegram/TelegramNotification.php | 61 ++++++++++++++++ composer.json | 1 + composer.lock | 73 +++++++++++++++++++ config/services.php | 4 + ..._create_telegram_notification_settings.php | 14 ++++ .../test-telegram-notification.blade.php | 5 ++ .../telegram/speedtest-completed.blade.php | 4 + .../telegram/threshold/absolute.blade.php | 5 ++ 13 files changed, 335 insertions(+) create mode 100644 app/Forms/Components/TestTelegramNotification.php create mode 100644 app/Telegram/TelegramNotification.php create mode 100644 database/settings/2022_12_22_125055_create_telegram_notification_settings.php create mode 100644 resources/views/forms/components/test-telegram-notification.blade.php create mode 100644 resources/views/telegram/speedtest-completed.blade.php create mode 100644 resources/views/telegram/threshold/absolute.blade.php diff --git a/app/Filament/Pages/Settings/NotificationPage.php b/app/Filament/Pages/Settings/NotificationPage.php index c70d55ee5..a9690be84 100644 --- a/app/Filament/Pages/Settings/NotificationPage.php +++ b/app/Filament/Pages/Settings/NotificationPage.php @@ -4,8 +4,10 @@ use App\Forms\Components\TestDatabaseNotification; use App\Forms\Components\TestMailNotification; +use App\Forms\Components\TestTelegramNotification; use App\Mail\Test; use App\Settings\NotificationSettings; +use App\Telegram\TelegramNotification; use Closure; use Filament\Forms\Components\Card; use Filament\Forms\Components\Fieldset; @@ -113,6 +115,44 @@ protected function getFormSchema(): array '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 speetest 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, @@ -169,4 +209,26 @@ public function sendTestMailNotification() ->send(); } } + + public function sendTestTelegramNotification() + { + $notificationSettings = new (NotificationSettings::class); + + if (count($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(); + } + } } diff --git a/app/Forms/Components/TestTelegramNotification.php b/app/Forms/Components/TestTelegramNotification.php new file mode 100644 index 000000000..3d871543e --- /dev/null +++ b/app/Forms/Components/TestTelegramNotification.php @@ -0,0 +1,10 @@ +notificationSettings->telegram_enabled) { + if ($this->notificationSettings->telegram_on_speedtest_run && count($this->notificationSettings->telegram_recipients)) { + foreach ($this->notificationSettings->telegram_recipients as $recipient) { + $download_value = formatBits(formatBytesToBits($event->result->download)).'ps'; + $upload_value = formatBits(formatBytesToBits($event->result->upload)).'ps'; + $ping_value = round($event->result->ping, 2); + + $message = view('telegram.speedtest-completed', [ + 'id' => $event->result->id, + 'ping' => $ping_value, + 'download' => $download_value, + 'upload' => $upload_value, + ])->render(); + + \Illuminate\Support\Facades\Notification::route('telegram_chat_id', $recipient['telegram_chat_id']) + ->notify(new TelegramNotification($message)); + } + } + } } } diff --git a/app/Listeners/Threshold/AbsoluteListener.php b/app/Listeners/Threshold/AbsoluteListener.php index 95cef7f4f..c3324a872 100644 --- a/app/Listeners/Threshold/AbsoluteListener.php +++ b/app/Listeners/Threshold/AbsoluteListener.php @@ -6,6 +6,7 @@ use App\Mail\Threshold\AbsoluteMail; use App\Settings\NotificationSettings; use App\Settings\ThresholdSettings; +use App\Telegram\TelegramNotification; use Filament\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Support\Facades\Log; @@ -52,6 +53,11 @@ public function handle(ResultCreated $event) if ($this->notificationSettings->mail_enabled == true && $this->notificationSettings->mail_on_threshold_failure == true) { $this->mailChannel($event); } + + // Telegram notification channel + if ($this->notificationSettings->telegram_enabled == true && $this->notificationSettings->telegram_on_threshold_failure == true) { + $this->telegramChannel($event); + } } /** @@ -150,4 +156,65 @@ protected function mailChannel(ResultCreated $event) } } } + + /** + * Handle telegram notifications. + * + * @param \App\Events\ResultCreated $event + * @return void + */ + protected function telegramChannel(ResultCreated $event) + { + $failedThresholds = []; + + if (! count($this->notificationSettings->telegram_recipients) > 0) { + Log::info('Skipping sending telegram notification, no recipients.'); + } + + // Download threshold + if ($this->thresholdSettings->absolute_download > 0) { + if (absoluteDownloadThresholdFailed($this->thresholdSettings->absolute_download, $event->result->download)) { + array_push($failedThresholds, [ + 'name' => 'Download', + 'threshold' => $this->thresholdSettings->absolute_download.' Mbps', + 'value' => formatBits(formatBytesToBits($event->result->download)).'ps', + ]); + } + } + + // Upload threshold + if ($this->thresholdSettings->absolute_upload > 0) { + if (absoluteUploadThresholdFailed($this->thresholdSettings->absolute_upload, $event->result->upload)) { + array_push($failedThresholds, [ + 'name' => 'Upload', + 'threshold' => $this->thresholdSettings->absolute_upload.' Mbps', + 'value' => formatBits(formatBytesToBits($event->result->upload)).'ps', + ]); + } + } + + // Ping threshold + if ($this->thresholdSettings->absolute_ping > 0) { + if (absolutePingThresholdFailed($this->thresholdSettings->absolute_ping, $event->result->ping)) { + array_push($failedThresholds, [ + 'name' => 'Ping', + 'threshold' => $this->thresholdSettings->absolute_ping.' Ms', + 'value' => round($event->result->ping, 2).' Ms', + ]); + } + } + + if (count($failedThresholds)) { + foreach ($this->notificationSettings->telegram_recipients as $recipient) { + $message = view('telegram.threshold.absolute', [ + 'id' => $event->result->id, + 'url' => url('/admin/results'), + 'metrics' => $failedThresholds, + ])->render(); + + \Illuminate\Support\Facades\Notification::route('telegram_chat_id', $recipient['telegram_chat_id']) + ->notify(new TelegramNotification($message)); + } + } + } } diff --git a/app/Settings/NotificationSettings.php b/app/Settings/NotificationSettings.php index cd2dd42ad..182794dc8 100644 --- a/app/Settings/NotificationSettings.php +++ b/app/Settings/NotificationSettings.php @@ -20,6 +20,14 @@ class NotificationSettings extends Settings public ?array $mail_recipients; + public bool $telegram_enabled; + + public bool $telegram_on_speedtest_run; + + public bool $telegram_on_threshold_failure; + + public ?array $telegram_recipients; + public static function group(): string { return 'notification'; diff --git a/app/Telegram/TelegramNotification.php b/app/Telegram/TelegramNotification.php new file mode 100644 index 000000000..8b8bc52d4 --- /dev/null +++ b/app/Telegram/TelegramNotification.php @@ -0,0 +1,61 @@ +message = $message; + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * @return array + */ + public function via($notifiable) + { + return [TelegramChannel::class]; + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * @return \Illuminate\Notifications\Messages\MailMessage + */ + public function toTelegram($notifiable) + { + return TelegramMessage::create() + ->to($notifiable->routes['telegram_chat_id']) + ->content($this->message); + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } +} diff --git a/composer.json b/composer.json index ac0ca527d..61f32ee46 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "filament/spatie-laravel-settings-plugin": "^2.0", "guzzlehttp/guzzle": "^7.2", "influxdata/influxdb-client-php": "^2.9", + "laravel-notification-channels/telegram": "^3.0", "laravel/framework": "^9.19", "laravel/sanctum": "^3.0", "laravel/tinker": "^2.7", diff --git a/composer.lock b/composer.lock index 74f236832..014e69c97 100644 --- a/composer.lock +++ b/composer.lock @@ -1990,6 +1990,79 @@ }, "time": "2022-07-29T17:08:15+00:00" }, + { + "name": "laravel-notification-channels/telegram", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/laravel-notification-channels/telegram.git", + "reference": "8609d67d2f0b6ae5adfba85021722443dd332fb8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/laravel-notification-channels/telegram/zipball/8609d67d2f0b6ae5adfba85021722443dd332fb8", + "reference": "8609d67d2f0b6ae5adfba85021722443dd332fb8", + "shasum": "" + }, + "require": { + "ext-json": "*", + "guzzlehttp/guzzle": "^7.0", + "illuminate/contracts": "^9.0", + "illuminate/notifications": "^9.0", + "illuminate/support": "^9.0", + "php": "^8.0" + }, + "require-dev": { + "mockery/mockery": "^1.4", + "nunomaduro/larastan": "^2.0", + "orchestra/testbench": "^7.0", + "pestphp/pest": "^1.21", + "pestphp/pest-plugin-laravel": "^1.1", + "phpstan/extension-installer": "^1.1", + "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-phpunit": "^1.0", + "phpunit/phpunit": "^9.5" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "NotificationChannels\\Telegram\\TelegramServiceProvider" + ] + } + }, + "autoload": { + "psr-4": { + "NotificationChannels\\Telegram\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Irfaq Syed", + "email": "github@lukonet.net", + "homepage": "https://lukonet.com", + "role": "Developer" + } + ], + "description": "Telegram Notifications Channel for Laravel", + "homepage": "https://github.com/laravel-notification-channels/telegram", + "keywords": [ + "laravel", + "notification", + "telegram", + "telegram notification", + "telegram notifications channel" + ], + "support": { + "issues": "https://github.com/laravel-notification-channels/telegram/issues", + "source": "https://github.com/laravel-notification-channels/telegram/tree/3.0.0" + }, + "time": "2022-10-15T01:56:19+00:00" + }, { "name": "laravel/framework", "version": "v9.47.0", diff --git a/config/services.php b/config/services.php index 0ace530e8..81627cb76 100644 --- a/config/services.php +++ b/config/services.php @@ -31,4 +31,8 @@ 'region' => env('AWS_DEFAULT_REGION', 'us-east-1'), ], + 'telegram-bot-api' => [ + 'token' => env('TELEGRAM_BOT_TOKEN', 'YOUR BOT TOKEN HERE'), + ], + ]; diff --git a/database/settings/2022_12_22_125055_create_telegram_notification_settings.php b/database/settings/2022_12_22_125055_create_telegram_notification_settings.php new file mode 100644 index 000000000..62ccc37be --- /dev/null +++ b/database/settings/2022_12_22_125055_create_telegram_notification_settings.php @@ -0,0 +1,14 @@ +migrator->add('notification.telegram_enabled', false); + $this->migrator->add('notification.telegram_on_speedtest_run', false); + $this->migrator->add('notification.telegram_on_threshold_failure', false); + $this->migrator->add('notification.telegram_recipients', null); + } +} diff --git a/resources/views/forms/components/test-telegram-notification.blade.php b/resources/views/forms/components/test-telegram-notification.blade.php new file mode 100644 index 000000000..e6778ab3a --- /dev/null +++ b/resources/views/forms/components/test-telegram-notification.blade.php @@ -0,0 +1,5 @@ +
+ + Test telegram channel + +
diff --git a/resources/views/telegram/speedtest-completed.blade.php b/resources/views/telegram/speedtest-completed.blade.php new file mode 100644 index 000000000..481b35363 --- /dev/null +++ b/resources/views/telegram/speedtest-completed.blade.php @@ -0,0 +1,4 @@ +Speedtest #{{ $id }} +Ping: *{{ $ping }}* +Download: *{{ $download }}* +Upload: *{{ $upload }}* \ No newline at end of file diff --git a/resources/views/telegram/threshold/absolute.blade.php b/resources/views/telegram/threshold/absolute.blade.php new file mode 100644 index 000000000..8d8e95c56 --- /dev/null +++ b/resources/views/telegram/threshold/absolute.blade.php @@ -0,0 +1,5 @@ +Speedtest Result *#{{ $id }}* +*Absolute Threshold Failed* +@foreach ($metrics as $item) +Threshold *{{ $item['name'] }}* {{ $item['threshold'] }}: *{{ $item['value'] }}* +@endforeach From f81c5558e7ef471a2d21ff688104342a37afafb6 Mon Sep 17 00:00:00 2001 From: Giovanny Aranda Date: Wed, 11 Jan 2023 18:43:03 +0100 Subject: [PATCH 2/3] Update telegram driver --- app/Telegram/TelegramNotification.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Telegram/TelegramNotification.php b/app/Telegram/TelegramNotification.php index 8b8bc52d4..cf4fcd47f 100644 --- a/app/Telegram/TelegramNotification.php +++ b/app/Telegram/TelegramNotification.php @@ -30,7 +30,7 @@ public function __construct($message) */ public function via($notifiable) { - return [TelegramChannel::class]; + return ['telegram']; } /** From 14fbd7b6fb6c077bb42d05e84b1fe9307a8ae9d3 Mon Sep 17 00:00:00 2001 From: garanda21 Date: Wed, 11 Jan 2023 19:01:11 +0100 Subject: [PATCH 3/3] Removed unused code --- app/Telegram/TelegramNotification.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/app/Telegram/TelegramNotification.php b/app/Telegram/TelegramNotification.php index cf4fcd47f..c03ca6566 100644 --- a/app/Telegram/TelegramNotification.php +++ b/app/Telegram/TelegramNotification.php @@ -45,17 +45,4 @@ public function toTelegram($notifiable) ->to($notifiable->routes['telegram_chat_id']) ->content($this->message); } - - /** - * Get the array representation of the notification. - * - * @param mixed $notifiable - * @return array - */ - public function toArray($notifiable) - { - return [ - // - ]; - } }