Quality of life changes

Changed default action of /cancel to be handled at message_handler
Added keyboard markup to get_account for easier account selection
This commit is contained in:
StNicolay 2022-12-29 15:38:38 +03:00
parent d4c50432d7
commit d5d87a8f3b
4 changed files with 31 additions and 13 deletions

View File

@ -54,9 +54,6 @@ def create_bot(token: str, engine: Engine) -> AsyncTeleBot:
functools.partial(message_handlers.gen_password, bot), functools.partial(message_handlers.gen_password, bot),
commands=["gen_password"], commands=["gen_password"],
) )
bot.register_message_handler(
functools.partial(message_handlers.cancel, bot), commands=["cancel"]
)
bot.register_message_handler( bot.register_message_handler(
functools.partial(message_handlers.message_handler, bot), functools.partial(message_handlers.message_handler, bot),
content_types=[ content_types=[

View File

@ -7,6 +7,11 @@ from . import markups
Message = telebot.types.Message Message = telebot.types.Message
Handler = Callable[[Message], Awaitable[Any]] Handler = Callable[[Message], Awaitable[Any]]
Markups = (
telebot.types.ReplyKeyboardMarkup
| telebot.types.InlineKeyboardMarkup
| telebot.types.ReplyKeyboardRemove
)
states: dict[tuple[int, int], Handler] = {} states: dict[tuple[int, int], Handler] = {}
@ -48,9 +53,13 @@ async def send_tmp_message(
bot: AsyncTeleBot, bot: AsyncTeleBot,
chat_id: telebot.types.Message, chat_id: telebot.types.Message,
text: str, text: str,
*,
sleep_time: int = 5, sleep_time: int = 5,
markup: Markups | None = None,
) -> None: ) -> None:
bot_mes = await bot.send_message(chat_id, text, parse_mode="MarkdownV2") bot_mes = await bot.send_message(
chat_id, text, parse_mode="MarkdownV2", reply_markup=markup
)
await delete_message(bot, bot_mes, sleep_time=sleep_time) await delete_message(bot, bot_mes, sleep_time=sleep_time)

View File

@ -1,4 +1,8 @@
from telebot.types import InlineKeyboardButton, InlineKeyboardMarkup from telebot.types import (
InlineKeyboardButton,
InlineKeyboardMarkup,
ReplyKeyboardMarkup,
)
def deletion_markup() -> InlineKeyboardMarkup: def deletion_markup() -> InlineKeyboardMarkup:
@ -6,3 +10,9 @@ def deletion_markup() -> InlineKeyboardMarkup:
button = InlineKeyboardButton("Удалить сообщение", callback_data="DELETE") button = InlineKeyboardButton("Удалить сообщение", callback_data="DELETE")
markup.add(button) markup.add(button)
return markup return markup
def account_markup(account_names: list[str]) -> ReplyKeyboardMarkup:
markup = ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
markup.add(*account_names)
return markup

View File

@ -333,9 +333,14 @@ async def get_account(bot: AsyncTeleBot, engine: Engine, mes: Message) -> None:
if master_pass is None: if master_pass is None:
return await send_tmp_message(bot, mes.chat.id, "Нет мастер пароля") return await send_tmp_message(bot, mes.chat.id, "Нет мастер пароля")
accounts = database.get.get_accounts(
engine,
mes.from_user.id,
to_sort=True,
)
markup = markups.account_markup(accounts)
bot_mes = await bot.send_message( bot_mes = await bot.send_message(
mes.chat.id, mes.chat.id, "Отправьте название аккаунта", reply_markup=markup
"Отправьте название аккаунта",
) )
register_state( register_state(
mes, mes,
@ -483,11 +488,6 @@ async def help_command(bot: AsyncTeleBot, mes: Message) -> None:
await bot.send_message(mes.chat.id, message) await bot.send_message(mes.chat.id, message)
async def cancel(bot: AsyncTeleBot, mes: Message) -> None:
await base_handler(bot, mes)
await send_tmp_message(bot, mes.chat.id, "Нет активного действия")
async def export(bot: AsyncTeleBot, engine: Engine, mes: Message) -> None: async def export(bot: AsyncTeleBot, engine: Engine, mes: Message) -> None:
await base_handler(bot, mes) await base_handler(bot, mes)
master_password_from_db = database.get.get_master_pass( master_password_from_db = database.get.get_master_pass(
@ -657,13 +657,15 @@ async def gen_password(bot: AsyncTeleBot, mes: Message) -> None:
+ "\n".join(passwords) + "\n".join(passwords)
+ "\nНажмите на пароль, чтобы его скопировать" + "\nНажмите на пароль, чтобы его скопировать"
) )
await send_tmp_message(bot, mes.chat.id, text, 15) await send_deleteable_message(bot, mes.chat.id, text)
async def message_handler(bot: AsyncTeleBot, mes: Message) -> None: async def message_handler(bot: AsyncTeleBot, mes: Message) -> None:
handler = get_state(mes) handler = get_state(mes)
if handler is None: if handler is None:
await delete_message(bot, mes) await delete_message(bot, mes)
if mes.text.strip() == "/cancel":
await send_tmp_message(bot, mes.chat.id, "Нет активного действия")
await send_tmp_message( await send_tmp_message(
bot, bot,
mes.chat.id, mes.chat.id,