feat: use new place for settings and topic IDs for telegram

This commit is contained in:
peaklabs-dev
2024-12-09 17:37:39 +01:00
parent adaef2d341
commit 4d23835119

View File

@@ -9,38 +9,32 @@ class TelegramChannel
public function send($notifiable, $notification): void public function send($notifiable, $notification): void
{ {
$data = $notification->toTelegram($notifiable); $data = $notification->toTelegram($notifiable);
$telegramData = $notifiable->routeNotificationForTelegram(); $settings = $notifiable->telegramNotificationSettings;
$message = data_get($data, 'message'); $message = data_get($data, 'message');
$buttons = data_get($data, 'buttons', []); $buttons = data_get($data, 'buttons', []);
$telegramToken = data_get($telegramData, 'token'); $telegramToken = $settings->telegram_token;
$chatId = data_get($telegramData, 'chat_id'); $chatId = $settings->telegram_chat_id;
$topicId = null;
$topicsInstance = get_class($notification); $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) { if (! $telegramToken || ! $chatId || ! $message) {
return; return;
} }
SendMessageToTelegramJob::dispatch($message, $buttons, $telegramToken, $chatId, $topicId); SendMessageToTelegramJob::dispatch($message, $buttons, $telegramToken, $chatId, $topicId);
} }
} }