From 4d23835119be38f41cdada1cc2dc99fe296eb5fb Mon Sep 17 00:00:00 2001 From: peaklabs-dev <122374094+peaklabs-dev@users.noreply.github.com> Date: Mon, 9 Dec 2024 17:37:39 +0100 Subject: [PATCH] feat: use new place for settings and topic IDs for telegram --- .../Channels/TelegramChannel.php | 46 ++++++++----------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/app/Notifications/Channels/TelegramChannel.php b/app/Notifications/Channels/TelegramChannel.php index 958c46c21..17d76181a 100644 --- a/app/Notifications/Channels/TelegramChannel.php +++ b/app/Notifications/Channels/TelegramChannel.php @@ -9,38 +9,32 @@ class TelegramChannel public function send($notifiable, $notification): void { $data = $notification->toTelegram($notifiable); - $telegramData = $notifiable->routeNotificationForTelegram(); + $settings = $notifiable->telegramNotificationSettings; + $message = data_get($data, 'message'); $buttons = data_get($data, 'buttons', []); - $telegramToken = data_get($telegramData, 'token'); - $chatId = data_get($telegramData, 'chat_id'); - $topicId = null; - $topicsInstance = get_class($notification); + $telegramToken = $settings->telegram_token; + $chatId = $settings->telegram_chat_id; + + $topicId = match (get_class($notification)) { + \App\Notifications\Test::class => $settings->telegram_notifications_test_topic_id, + \App\Notifications\Application\StatusChanged::class, + \App\Notifications\Container\ContainerRestarted::class, + \App\Notifications\Container\ContainerStopped::class => $settings->telegram_notifications_status_change_topic_id, + \App\Notifications\Application\DeploymentSuccess::class => $settings->telegram_notifications_deployment_success_topic_id, + \App\Notifications\Application\DeploymentFailed::class => $settings->telegram_notifications_deployment_failure_topic_id, + \App\Notifications\Database\BackupSuccess::class => $settings->telegram_notifications_backup_success_topic_id, + \App\Notifications\Database\BackupFailed::class => $settings->telegram_notifications_backup_failure_topic_id, + \App\Notifications\ScheduledTask\TaskFailed::class => $settings->telegram_notifications_scheduled_task_failure_topic_id, + \App\Notifications\Server\Unreachable::class => $settings->telegram_notifications_server_unreachable_topic_id, + \App\Notifications\Server\Reachable::class => $settings->telegram_notifications_server_reachable_topic_id, + default => null, + }; - switch ($topicsInstance) { - case \App\Notifications\Test::class: - $topicId = data_get($notifiable, 'telegram_notifications_test_message_thread_id'); - break; - case \App\Notifications\Application\StatusChanged::class: - case \App\Notifications\Container\ContainerRestarted::class: - case \App\Notifications\Container\ContainerStopped::class: - $topicId = data_get($notifiable, 'telegram_notifications_status_changes_message_thread_id'); - break; - case \App\Notifications\Application\DeploymentSuccess::class: - case \App\Notifications\Application\DeploymentFailed::class: - $topicId = data_get($notifiable, 'telegram_notifications_deployments_message_thread_id'); - break; - case \App\Notifications\Database\BackupSuccess::class: - case \App\Notifications\Database\BackupFailed::class: - $topicId = data_get($notifiable, 'telegram_notifications_database_backups_message_thread_id'); - break; - case \App\Notifications\ScheduledTask\TaskFailed::class: - $topicId = data_get($notifiable, 'telegram_notifications_scheduled_tasks_thread_id'); - break; - } if (! $telegramToken || ! $chatId || ! $message) { return; } + SendMessageToTelegramJob::dispatch($message, $buttons, $telegramToken, $chatId, $topicId); } }