diff --git a/Program.cs b/Program.cs index 7c5bdfd..287244b 100644 --- a/Program.cs +++ b/Program.cs @@ -40,7 +40,6 @@ class Program throw; } - Log.Logger = new LoggerConfiguration() .WriteTo.Console() .WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day) @@ -50,7 +49,6 @@ class Program try { - _botClient = new TelegramBotClient(_botToken); var me = await _botClient.GetMe(); Log.Information($"Бот {me.FirstName} запущен! ID: {me.Id}"); @@ -58,7 +56,6 @@ class Program catch (Exception ex) { Log.Error($"Ошибка при подключении к Telegram API: {ex.Message}"); - throw; } @@ -93,6 +90,49 @@ class Program cts.Token.WaitHandle.WaitOne(); } + private static async Task MonitorReportStatus(long reportId, CancellationToken token) + { + while (!token.IsCancellationRequested) + { + try + { + using (var connection = new SqliteConnection("Data Source=bot.db")) + { + await connection.OpenAsync(); + var command = connection.CreateCommand(); + command.CommandText = @" + SELECT Status + FROM Reports + WHERE Id = @id"; + command.Parameters.AddWithValue("@id", reportId); + + var status = (string?)await command.ExecuteScalarAsync(); + + if (status == "в работе" || status == "закрыта") + { + // Если статус изменился на "в работе" или "закрыта", прекращаем мониторинг + return; + } + + foreach (var adminId in admins) + { + await _botClient.SendMessage( + chatId: adminId, + text: $"⚠️ Заявка #{reportId} остается в статусе 'Ожидает'." + ); + } + } + } + catch (Exception ex) + { + Log.Error($"Ошибка при мониторинге статуса заявки #{reportId}: {ex.Message}"); + } + + // Ждем 15 секунд перед следующей проверкой + await Task.Delay(15000, token); + } + } + private static async Task DeletePreviousMessage(ITelegramBotClient botClient, long chatId, int messageId) { try @@ -1450,6 +1490,10 @@ class Program // Уведомляем администраторов о новой заявке await NotifyAdminsAboutNewReport(reportId, report); + + // Запускаем мониторинг статуса заявки + var cts = new CancellationTokenSource(); + _ = Task.Run(() => MonitorReportStatus(reportId, cts.Token)); } } catch (Exception ex) @@ -1460,6 +1504,7 @@ class Program + // Обновляем метод CreateDatabaseIfNotExists private static async Task CreateDatabaseIfNotExists() {