статусы и навигация в списку заявок
This commit is contained in:
parent
9b39ea4440
commit
c15631e4a4
198
Program.cs
198
Program.cs
@ -96,6 +96,7 @@ class Program
|
||||
var callbackQuery = update.CallbackQuery;
|
||||
long chatId = callbackQuery.From.Id;
|
||||
string data = callbackQuery.Data;
|
||||
int messageId = callbackQuery.Message.MessageId;
|
||||
|
||||
await botClient.AnswerCallbackQueryAsync(callbackQuery.Id); // Убираем "часики" у кнопки
|
||||
|
||||
@ -117,6 +118,27 @@ class Program
|
||||
Log.Information($"Неавторизованный доступ к заявкам от {chatId}");
|
||||
}
|
||||
}
|
||||
else if (data.StartsWith("report_"))
|
||||
{
|
||||
long reportId = long.Parse(data.Substring(7));
|
||||
await ShowReportDetails(botClient, chatId, reportId, messageId);
|
||||
}
|
||||
else if (data.StartsWith("status_"))
|
||||
{
|
||||
string[] parts = data.Split('_');
|
||||
long reportId = long.Parse(parts[1]);
|
||||
string newStatus = parts[2];
|
||||
await UpdateReportStatus(reportId, newStatus);
|
||||
await ShowReportDetails(botClient, chatId, reportId, messageId);
|
||||
}
|
||||
else if (data == "back_to_list")
|
||||
{
|
||||
await ViewReports(botClient, chatId);
|
||||
}
|
||||
else if (data == "main_menu")
|
||||
{
|
||||
await SendMainMenu(botClient, chatId);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@ -182,6 +204,147 @@ class Program
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task SendMainMenu(ITelegramBotClient botClient, long chatId)
|
||||
{
|
||||
var keyboard = new InlineKeyboardMarkup(new[]
|
||||
{
|
||||
new[]
|
||||
{
|
||||
InlineKeyboardButton.WithCallbackData("Подать заявку", "report"),
|
||||
InlineKeyboardButton.WithCallbackData("Просмотр заявок", "view_reports")
|
||||
}
|
||||
});
|
||||
|
||||
await botClient.SendMessage(
|
||||
chatId: chatId,
|
||||
text: "Главное меню:",
|
||||
replyMarkup: keyboard
|
||||
);
|
||||
}
|
||||
|
||||
private static async Task ViewReports(ITelegramBotClient botClient, long chatId)
|
||||
{
|
||||
string connectionString = "Data Source=bot.db";
|
||||
|
||||
try
|
||||
{
|
||||
using (var connection = new SqliteConnection(connectionString))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = "SELECT Id, Description, Status FROM Reports";
|
||||
|
||||
var buttons = new List<InlineKeyboardButton[]>();
|
||||
|
||||
using (var reader = await command.ExecuteReaderAsync())
|
||||
{
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
long id = reader.GetInt64(0);
|
||||
string description = reader.GetString(1).Substring(0, Math.Min(20, reader.GetString(1).Length));
|
||||
string status = reader.GetString(2);
|
||||
|
||||
buttons.Add(new[]
|
||||
{
|
||||
InlineKeyboardButton.WithCallbackData(
|
||||
$"#{id} - {status} - {description}...",
|
||||
$"report_{id}")
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Добавляем кнопки навигации
|
||||
buttons.Add(new[]
|
||||
{
|
||||
InlineKeyboardButton.WithCallbackData("Главное меню", "main_menu")
|
||||
});
|
||||
|
||||
await botClient.SendMessage(
|
||||
chatId: chatId,
|
||||
text: "Список заявок:",
|
||||
replyMarkup: new InlineKeyboardMarkup(buttons)
|
||||
);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error($"Ошибка: {ex.Message}");
|
||||
await botClient.SendMessage(chatId, "Ошибка при получении заявок");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task ShowReportDetails(ITelegramBotClient botClient, long chatId, long reportId, int messageId)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var connection = new SqliteConnection("Data Source=bot.db"))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = "SELECT Description, Status FROM Reports WHERE Id = @id";
|
||||
command.Parameters.AddWithValue("@id", reportId);
|
||||
|
||||
using (var reader = await command.ExecuteReaderAsync())
|
||||
{
|
||||
if (await reader.ReadAsync())
|
||||
{
|
||||
string description = reader.GetString(0);
|
||||
string status = reader.GetString(1);
|
||||
|
||||
var statusButtons = new InlineKeyboardMarkup(new[]
|
||||
{
|
||||
new[]
|
||||
{
|
||||
InlineKeyboardButton.WithCallbackData("🟡 Ожидает", $"status_{reportId}_ожидает"),
|
||||
InlineKeyboardButton.WithCallbackData("🔵 В работе", $"status_{reportId}_в работе")
|
||||
},
|
||||
new[]
|
||||
{
|
||||
InlineKeyboardButton.WithCallbackData("🟢 Закрыта", $"status_{reportId}_закрыта")
|
||||
},
|
||||
new[]
|
||||
{
|
||||
InlineKeyboardButton.WithCallbackData("Назад", "back_to_list"),
|
||||
InlineKeyboardButton.WithCallbackData("Главное меню", "main_menu")
|
||||
}
|
||||
});
|
||||
|
||||
await botClient.EditMessageTextAsync(
|
||||
chatId: chatId,
|
||||
messageId: messageId,
|
||||
text: $"Заявка #{reportId}\n\nОписание: {description}\nСтатус: {status}",
|
||||
replyMarkup: statusButtons
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error($"Ошибка: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task UpdateReportStatus(long reportId, string newStatus)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var connection = new SqliteConnection("Data Source=bot.db"))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = "UPDATE Reports SET Status = @status WHERE Id = @id";
|
||||
command.Parameters.AddWithValue("@status", newStatus);
|
||||
command.Parameters.AddWithValue("@id", reportId);
|
||||
await command.ExecuteNonQueryAsync();
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error($"Ошибка: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task SaveReportToDatabase(long chatId, string description)
|
||||
{
|
||||
string connectionString = "Data Source=bot.db"; // Используем SQLite
|
||||
@ -243,41 +406,6 @@ class Program
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task ViewReports(ITelegramBotClient botClient, long chatId)
|
||||
{
|
||||
string connectionString = "Data Source=bot.db"; // Путь к базе данных
|
||||
|
||||
try
|
||||
{
|
||||
using (var connection = new SqliteConnection(connectionString))
|
||||
{
|
||||
await connection.OpenAsync();
|
||||
var command = connection.CreateCommand();
|
||||
command.CommandText = "SELECT * FROM Reports";
|
||||
|
||||
using (var reader = await command.ExecuteReaderAsync())
|
||||
{
|
||||
string reportsText = "Список заявок:\n";
|
||||
while (await reader.ReadAsync())
|
||||
{
|
||||
long id = reader.GetInt64(0);
|
||||
long reportChatId = reader.GetInt64(1);
|
||||
string description = reader.GetString(2);
|
||||
string status = reader.GetString(4);
|
||||
|
||||
reportsText += $"ID: {id}, ChatId: {reportChatId}, Описание: {description}, Статус: {status}\n";
|
||||
}
|
||||
await botClient.SendMessage(chatId: chatId, text: reportsText);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Error($"Ошибка при просмотре заявок: {ex.Message}");
|
||||
await botClient.SendMessage(chatId: chatId, text: "Ошибка при получении заявок.");
|
||||
}
|
||||
}
|
||||
|
||||
private static Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
|
||||
{
|
||||
Log.Error($"Ошибка в процессе работы с ботом: {exception.Message}");
|
||||
|
Loading…
x
Reference in New Issue
Block a user