implement preset selection
This commit is contained in:
parent
b4178761fb
commit
ecdaf52e21
@ -5,6 +5,7 @@ internal class DialogData
|
|||||||
public DialogState state;
|
public DialogState state;
|
||||||
public string? inputPictureFilename;
|
public string? inputPictureFilename;
|
||||||
public string? inputTitle;
|
public string? inputTitle;
|
||||||
|
public UserPreset[]? shownPresets;
|
||||||
}
|
}
|
||||||
|
|
||||||
enum DialogState
|
enum DialogState
|
||||||
@ -16,4 +17,5 @@ enum DialogState
|
|||||||
ShowingResult,
|
ShowingResult,
|
||||||
Settings,
|
Settings,
|
||||||
ViewingPresets,
|
ViewingPresets,
|
||||||
|
ChoosingPreset,
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,8 @@ internal static class Interactions
|
|||||||
public const string awaitingSubtitleMessage = "Найс. Напиши текст, который будет под заголовком, или точку (.), чтобы не добавлять его.";
|
public const string awaitingSubtitleMessage = "Найс. Напиши текст, который будет под заголовком, или точку (.), чтобы не добавлять его.";
|
||||||
public const string chooseActionMessage = "Нажмите на одну из кнопок.";
|
public const string chooseActionMessage = "Нажмите на одну из кнопок.";
|
||||||
public const string settingsMessage = "Здесь можно:\n- настроить сохранённые стили";
|
public const string settingsMessage = "Здесь можно:\n- настроить сохранённые стили";
|
||||||
|
public const string choosePresetMessage = "Какой пресет сделать активым (цыфра)?";
|
||||||
|
public const string enterPresetNumberMessage = "Введи номер пресета.";
|
||||||
|
|
||||||
static readonly string[] helloWords = ["прив","привет","▶️начать","ку","хай","приветик","превед","привки","хаюхай","здравствуй","здравствуйте","здорово","дарова","дороу","здарова","здорова"];
|
static readonly string[] helloWords = ["прив","привет","▶️начать","ку","хай","приветик","превед","привки","хаюхай","здравствуй","здравствуйте","здорово","дарова","дороу","здарова","здорова"];
|
||||||
static readonly string[] cancelWords = ["↩️назад", "назад", "выйти", "отмена", "отменить", "отменяй", "галя", "галина", "стоп"];
|
static readonly string[] cancelWords = ["↩️назад", "назад", "выйти", "отмена", "отменить", "отменяй", "галя", "галина", "стоп"];
|
||||||
@ -23,7 +25,7 @@ internal static class Interactions
|
|||||||
public static readonly string backButtonText = "↩️Назад";
|
public static readonly string backButtonText = "↩️Назад";
|
||||||
public static readonly string gotoPresetsButtonText = "🎨Сохранённые стили";
|
public static readonly string gotoPresetsButtonText = "🎨Сохранённые стили";
|
||||||
public static readonly string doneButtonText = "✅Готово";
|
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 mainReplyMarkup = new ReplyKeyboardMarkup([[new KeyboardButton("▶️Начать")], [new KeyboardButton("⚙️Настройки")]]);
|
||||||
public static readonly IReplyMarkup backButtonReplyMarkup = new ReplyKeyboardMarkup(new KeyboardButton("↩️Назад"));
|
public static readonly IReplyMarkup backButtonReplyMarkup = new ReplyKeyboardMarkup(new KeyboardButton("↩️Назад"));
|
||||||
|
@ -7,6 +7,7 @@ using Telegram.Bot.Exceptions;
|
|||||||
using Telegram.Bot.Polling;
|
using Telegram.Bot.Polling;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
using Telegram.Bot.Types.Enums;
|
using Telegram.Bot.Types.Enums;
|
||||||
|
using Telegram.Bot.Types.ReplyMarkups;
|
||||||
|
|
||||||
namespace SimpleTGBot;
|
namespace SimpleTGBot;
|
||||||
|
|
||||||
@ -211,18 +212,7 @@ internal class TelegramBot
|
|||||||
if (messageText == Interactions.gotoPresetsButtonText)
|
if (messageText == Interactions.gotoPresetsButtonText)
|
||||||
{
|
{
|
||||||
replied = true;
|
replied = true;
|
||||||
dialogData.state = DialogState.ViewingPresets;
|
await DialogShowPresets(botClient, user, message.Chat.Id, dialogData);
|
||||||
(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);
|
|
||||||
}
|
}
|
||||||
else if (messageText == Interactions.backButtonText)
|
else if (messageText == Interactions.backButtonText)
|
||||||
{
|
{
|
||||||
@ -248,6 +238,12 @@ internal class TelegramBot
|
|||||||
dialogData.state = DialogState.Settings;
|
dialogData.state = DialogState.Settings;
|
||||||
await botClient.SendTextMessageAsync(message.Chat.Id, Interactions.settingsMessage, replyMarkup: Interactions.settingsReplyMarkup);
|
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)
|
if (!replied)
|
||||||
{
|
{
|
||||||
@ -255,6 +251,35 @@ internal class TelegramBot
|
|||||||
}
|
}
|
||||||
break;
|
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);
|
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)
|
async Task AddUserToDatabase(User u)
|
||||||
{
|
{
|
||||||
var cmd = database.CreateCommand();
|
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>
|
||||||
/// Обработчик исключений, возникших при работе бота
|
/// Обработчик исключений, возникших при работе бота
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user