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:
parent
d4c50432d7
commit
d5d87a8f3b
@ -54,9 +54,6 @@ def create_bot(token: str, engine: Engine) -> AsyncTeleBot:
|
||||
functools.partial(message_handlers.gen_password, bot),
|
||||
commands=["gen_password"],
|
||||
)
|
||||
bot.register_message_handler(
|
||||
functools.partial(message_handlers.cancel, bot), commands=["cancel"]
|
||||
)
|
||||
bot.register_message_handler(
|
||||
functools.partial(message_handlers.message_handler, bot),
|
||||
content_types=[
|
||||
|
@ -7,6 +7,11 @@ from . import markups
|
||||
|
||||
Message = telebot.types.Message
|
||||
Handler = Callable[[Message], Awaitable[Any]]
|
||||
Markups = (
|
||||
telebot.types.ReplyKeyboardMarkup
|
||||
| telebot.types.InlineKeyboardMarkup
|
||||
| telebot.types.ReplyKeyboardRemove
|
||||
)
|
||||
states: dict[tuple[int, int], Handler] = {}
|
||||
|
||||
|
||||
@ -48,9 +53,13 @@ async def send_tmp_message(
|
||||
bot: AsyncTeleBot,
|
||||
chat_id: telebot.types.Message,
|
||||
text: str,
|
||||
*,
|
||||
sleep_time: int = 5,
|
||||
markup: Markups | 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)
|
||||
|
||||
|
||||
|
@ -1,4 +1,8 @@
|
||||
from telebot.types import InlineKeyboardButton, InlineKeyboardMarkup
|
||||
from telebot.types import (
|
||||
InlineKeyboardButton,
|
||||
InlineKeyboardMarkup,
|
||||
ReplyKeyboardMarkup,
|
||||
)
|
||||
|
||||
|
||||
def deletion_markup() -> InlineKeyboardMarkup:
|
||||
@ -6,3 +10,9 @@ def deletion_markup() -> InlineKeyboardMarkup:
|
||||
button = InlineKeyboardButton("Удалить сообщение", callback_data="DELETE")
|
||||
markup.add(button)
|
||||
return markup
|
||||
|
||||
|
||||
def account_markup(account_names: list[str]) -> ReplyKeyboardMarkup:
|
||||
markup = ReplyKeyboardMarkup(resize_keyboard=True, row_width=2)
|
||||
markup.add(*account_names)
|
||||
return markup
|
||||
|
@ -333,9 +333,14 @@ async def get_account(bot: AsyncTeleBot, engine: Engine, mes: Message) -> None:
|
||||
if master_pass is None:
|
||||
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(
|
||||
mes.chat.id,
|
||||
"Отправьте название аккаунта",
|
||||
mes.chat.id, "Отправьте название аккаунта", reply_markup=markup
|
||||
)
|
||||
register_state(
|
||||
mes,
|
||||
@ -483,11 +488,6 @@ async def help_command(bot: AsyncTeleBot, mes: Message) -> None:
|
||||
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:
|
||||
await base_handler(bot, mes)
|
||||
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Нажмите на пароль, чтобы его скопировать"
|
||||
)
|
||||
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:
|
||||
handler = get_state(mes)
|
||||
if handler is None:
|
||||
await delete_message(bot, mes)
|
||||
if mes.text.strip() == "/cancel":
|
||||
await send_tmp_message(bot, mes.chat.id, "Нет активного действия")
|
||||
await send_tmp_message(
|
||||
bot,
|
||||
mes.chat.id,
|
||||
|
Reference in New Issue
Block a user