diff --git a/Program.cs b/Program.cs index e597ff4..d273b52 100644 --- a/Program.cs +++ b/Program.cs @@ -127,7 +127,9 @@ class Program if (data == "report") { usersWaitingForReport[chatId] = true; - await botClient.SendMessage(chatId, "Пожалуйста, отправьте описание проблемы."); + userReportSteps[chatId] = 1; + userReports[chatId] = new Report(); + await botClient.SendMessage(chatId, "Пожалуйста, укажите приоритет (низкий, средний, высокий)."); Log.Information($"Пользователь {chatId} начал создание заявки"); } else if (data == "admin_panel") @@ -286,29 +288,43 @@ class Program } else if (usersWaitingForReport.TryGetValue(message.Chat.Id, out bool isWaiting) && isWaiting) { - string problemDescription = message.Text; - await SaveReportToDatabase(message.Chat.Id, problemDescription); - - // Создаем клавиатуру с кнопкой возврата - var keyboard = new InlineKeyboardMarkup(new[] + if (userReportSteps.TryGetValue(message.Chat.Id, out int step)) { - new[] - { - InlineKeyboardButton.WithCallbackData("Главное меню", "main_menu") + switch (step) + { + case 1: + userReports[message.Chat.Id].Priority = message.Text; + userReportSteps[message.Chat.Id] = 2; + await botClient.SendMessage(message.Chat.Id, "Пожалуйста, укажите кабинет."); + break; + case 2: + userReports[message.Chat.Id].Room = message.Text; + userReportSteps[message.Chat.Id] = 3; + await botClient.SendMessage(message.Chat.Id, "Пожалуйста, опишите проблему."); + break; + case 3: + userReports[message.Chat.Id].Description = message.Text; + userReportSteps[message.Chat.Id] = 4; + await botClient.SendMessage(message.Chat.Id, "Пожалуйста, укажите ваше ФИО."); + break; + case 4: + userReports[message.Chat.Id].ReporterName = message.Text; + await SaveReportToDatabase(message.Chat.Id, userReports[message.Chat.Id]); + await botClient.SendMessage( + message.Chat.Id, + "✅ Спасибо за заявку! Мы обработаем её в ближайшее время." + ); + usersWaitingForReport[message.Chat.Id] = false; + userReportSteps.Remove(message.Chat.Id); + userReports.Remove(message.Chat.Id); + Log.Information($"Заявка пользователя {message.Chat.Id} сохранена в базе данных."); + break; + } } - }); - - await botClient.SendMessage( - chatId: message.Chat.Id, - text: "✅ Спасибо за заявку! Мы обработаем её в ближайшее время.", - replyMarkup: keyboard); - - usersWaitingForReport[message.Chat.Id] = false; - Log.Information($"Заявка пользователя {message.Chat.Id} сохранена в базе данных."); } else { - await botClient.SendMessage(chatId: message.Chat.Id, text: "ℹ️ Используйте команду /start для начала работы с ботом."); + await botClient.SendMessage(message.Chat.Id, "ℹ️ Используйте команду /start для начала работы с ботом."); } } } @@ -320,6 +336,7 @@ class Program } + private static async Task DeleteReport(ITelegramBotClient botClient, long chatId, long reportId) { try @@ -580,7 +597,7 @@ class Program } } - private static async Task SaveReportToDatabase(long chatId, string description) + private static async Task SaveReportToDatabase(long chatId, Report report) { string connectionString = "Data Source=bot.db"; // Используем SQLite @@ -593,11 +610,14 @@ class Program var insertCommand = connection.CreateCommand(); insertCommand.CommandText = @" - INSERT INTO Reports (ChatId, Description, Status) - VALUES (@ChatId, @Description, 'ожидает'); - "; + INSERT INTO Reports (ChatId, Priority, Room, Description, ReporterName, Status) + VALUES (@ChatId, @Priority, @Room, @Description, @ReporterName, 'ожидает'); + "; insertCommand.Parameters.AddWithValue("@ChatId", chatId); - insertCommand.Parameters.AddWithValue("@Description", description); + insertCommand.Parameters.AddWithValue("@Priority", report.Priority); + insertCommand.Parameters.AddWithValue("@Room", report.Room); + insertCommand.Parameters.AddWithValue("@Description", report.Description); + insertCommand.Parameters.AddWithValue("@ReporterName", report.ReporterName); await insertCommand.ExecuteNonQueryAsync(); Log.Information($"Заявка от пользователя {chatId} успешно сохранена."); @@ -610,6 +630,7 @@ class Program } + private static async Task CreateDatabaseIfNotExists() { string connectionString = "Data Source=bot.db"; // Путь к вашей базе данных @@ -623,14 +644,17 @@ class Program var createTableCommand = connection.CreateCommand(); createTableCommand.CommandText = @" - CREATE TABLE IF NOT EXISTS Reports ( - Id INTEGER PRIMARY KEY AUTOINCREMENT, - ChatId INTEGER NOT NULL, - Description TEXT NOT NULL, - DateCreated DATETIME DEFAULT CURRENT_TIMESTAMP, - Status TEXT DEFAULT 'В процессе' - ); - "; + CREATE TABLE IF NOT EXISTS Reports ( + Id INTEGER PRIMARY KEY AUTOINCREMENT, + ChatId INTEGER NOT NULL, + Priority TEXT NOT NULL, + Room TEXT NOT NULL, + Description TEXT NOT NULL, + ReporterName TEXT NOT NULL, + DateCreated DATETIME DEFAULT CURRENT_TIMESTAMP, + Status TEXT DEFAULT 'В процессе' + ); + "; await createTableCommand.ExecuteNonQueryAsync(); Log.Information("Таблица Reports успешно создана (если её не было)."); @@ -642,6 +666,18 @@ class Program } } + private static Dictionary userReportSteps = new Dictionary(); + private static Dictionary userReports = new Dictionary(); + + private class Report + { + public string Priority { get; set; } = string.Empty; + public string Room { get; set; } = string.Empty; + public string Description { get; set; } = string.Empty; + public string ReporterName { get; set; } = string.Empty; + } + + private static Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken) { Log.Error($"Ошибка в процессе работы с ботом: {exception.Message}");