implement preset selection

This commit is contained in:
Slavasil 2025-04-20 23:28:04 +03:00
parent b4178761fb
commit ecdaf52e21
3 changed files with 68 additions and 13 deletions

View File

@ -5,6 +5,7 @@ internal class DialogData
public DialogState state;
public string? inputPictureFilename;
public string? inputTitle;
public UserPreset[]? shownPresets;
}
enum DialogState
@ -16,4 +17,5 @@ enum DialogState
ShowingResult,
Settings,
ViewingPresets,
ChoosingPreset,
}

View File

@ -15,6 +15,8 @@ internal static class Interactions
public const string awaitingSubtitleMessage = "Найс. Напиши текст, который будет под заголовком, или точку (.), чтобы не добавлять его.";
public const string chooseActionMessage = "Нажмите на одну из кнопок.";
public const string settingsMessage = "Здесь можно:\n- настроить сохранённые стили";
public const string choosePresetMessage = "Какой пресет сделать активым (цыфра)?";
public const string enterPresetNumberMessage = "Введи номер пресета.";
static readonly string[] helloWords = ["прив","привет","▶️начать","ку","хай","приветик","превед","привки","хаюхай","здравствуй","здравствуйте","здорово","дарова","дороу","здарова","здорова"];
static readonly string[] cancelWords = ["↩️назад", "назад", "выйти", "отмена", "отменить", "отменяй", "галя", "галина", "стоп"];
@ -23,7 +25,7 @@ internal static class Interactions
public static readonly string backButtonText = "↩️Назад";
public static readonly string gotoPresetsButtonText = "🎨Сохранённые стили";
public static readonly string doneButtonText = "✅Готово";
public static readonly string choosePresetButtonText = "☑️Сделать активным";
public static readonly string choosePresetButtonText = "☑️Выбрать активный";
public static readonly IReplyMarkup mainReplyMarkup = new ReplyKeyboardMarkup([[new KeyboardButton("▶️Начать")], [new KeyboardButton("⚙️Настройки")]]);
public static readonly IReplyMarkup backButtonReplyMarkup = new ReplyKeyboardMarkup(new KeyboardButton("↩️Назад"));

View File

@ -7,6 +7,7 @@ using Telegram.Bot.Exceptions;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.ReplyMarkups;
namespace SimpleTGBot;
@ -211,18 +212,7 @@ internal class TelegramBot
if (messageText == Interactions.gotoPresetsButtonText)
{
replied = true;
dialogData.state = DialogState.ViewingPresets;
(UserPreset[] presets, long activePresetId) = await GetUserPresets(user);
int activePresetIndex = 0;
for (int i = 0; i < presets.Length; ++i)
{
if (presets[i].Id == activePresetId)
{
activePresetIndex = i;
break;
}
}
await botClient.SendTextMessageAsync(message.Chat.Id, Interactions.MakePresetListMessage(presets.Select(preset => preset.Name).ToArray(), activePresetIndex), replyMarkup: Interactions.presetsReplyMarkup);
await DialogShowPresets(botClient, user, message.Chat.Id, dialogData);
}
else if (messageText == Interactions.backButtonText)
{
@ -248,6 +238,12 @@ internal class TelegramBot
dialogData.state = DialogState.Settings;
await botClient.SendTextMessageAsync(message.Chat.Id, Interactions.settingsMessage, replyMarkup: Interactions.settingsReplyMarkup);
}
else if (messageText == Interactions.choosePresetButtonText)
{
replied = true;
dialogData.state = DialogState.ChoosingPreset;
await botClient.SendTextMessageAsync(message.Chat.Id, Interactions.choosePresetMessage, replyMarkup: Interactions.backButtonReplyMarkup);
}
}
if (!replied)
{
@ -255,6 +251,35 @@ internal class TelegramBot
}
break;
}
case DialogState.ChoosingPreset:
{
bool replied = false;
if (message.Text is { } messageText)
{
if (messageText == Interactions.backButtonText)
{
replied = true;
await DialogShowPresets(botClient, user, message.Chat.Id, dialogData);
}
else if (int.TryParse(messageText, out int presetIndex))
{
if (presetIndex >= 1 && presetIndex <= dialogData.shownPresets.Count())
{
replied = true;
presetIndex -= 1;
long id = dialogData.shownPresets[presetIndex].Id;
await SetActiveUserPreset(user, id);
await botClient.SendTextMessageAsync(message.Chat.Id, "Выбран пресет \"" + dialogData.shownPresets[presetIndex].Name + "\"", cancellationToken: cancellationToken);
await DialogShowPresets(botClient, user, message.Chat.Id, dialogData);
}
}
}
if (!replied)
{
await botClient.SendTextMessageAsync(message.Chat.Id, Interactions.enterPresetNumberMessage, replyMarkup: Interactions.presetsReplyMarkup);
}
break;
}
}
}
@ -349,6 +374,23 @@ internal class TelegramBot
temp.deleteTemporaryFile(dialogData.inputPictureFilename);
}
async Task DialogShowPresets(ITelegramBotClient botClient, User user, long chatId, DialogData dialogData)
{
dialogData.state = DialogState.ViewingPresets;
(UserPreset[] presets, long activePresetId) = await GetUserPresets(user);
int activePresetIndex = 0;
for (int i = 0; i < presets.Length; ++i)
{
if (presets[i].Id == activePresetId)
{
activePresetIndex = i;
break;
}
}
dialogData.shownPresets = presets;
await botClient.SendTextMessageAsync(chatId, Interactions.MakePresetListMessage(presets.Select(preset => preset.Name).ToArray(), activePresetIndex), replyMarkup: Interactions.presetsReplyMarkup);
}
async Task AddUserToDatabase(User u)
{
var cmd = database.CreateCommand();
@ -486,6 +528,15 @@ internal class TelegramBot
}
}
async Task SetActiveUserPreset(User u, long id)
{
var cmd = database.CreateCommand();
cmd.CommandText = "UPDATE users SET selected_preset = $1 WHERE id = $2";
cmd.Parameters.AddWithValue("$1", id);
cmd.Parameters.AddWithValue("$2", u.Id);
await cmd.ExecuteNonQueryAsync();
}
/// <summary>
/// Обработчик исключений, возникших при работе бота
/// </summary>