From 715d51695233b8c59793df96693d741efb9355de Mon Sep 17 00:00:00 2001 From: mihalin Date: Sat, 19 Feb 2022 20:40:56 +0300 Subject: [PATCH] enable and disable threads --- olgram/commands/bot_actions.py | 5 +++++ olgram/commands/menu.py | 31 +++++++++++++++++++++++++++++++ olgram/models/models.py | 2 ++ server/custom.py | 2 +- 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/olgram/commands/bot_actions.py b/olgram/commands/bot_actions.py index ac9a9e6..ea49bbc 100644 --- a/olgram/commands/bot_actions.py +++ b/olgram/commands/bot_actions.py @@ -69,3 +69,8 @@ async def select_chat(bot: Bot, call: types.CallbackQuery, chat: str): bot.group_chat = chat_obj await bot.save() await call.answer(f"Выбран чат {chat_obj.name}") + + +async def threads(bot: Bot, call: types.CallbackQuery): + bot.enable_threads = not bot.enable_threads + await bot.save(update_fields=["enable_threads"]) diff --git a/olgram/commands/menu.py b/olgram/commands/menu.py index 6d3a004..0c55399 100644 --- a/olgram/commands/menu.py +++ b/olgram/commands/menu.py @@ -116,6 +116,11 @@ async def send_bot_menu(bot: Bot, call: types.CallbackQuery): types.InlineKeyboardButton(text="<< Назад", callback_data=menu_callback.new(level=0, bot_id=empty, operation=empty, chat=empty)) ) + keyboard.insert( + types.InlineKeyboardButton(text="Опции", + callback_data=menu_callback.new(level=2, bot_id=bot.id, operation="settings", + chat=empty)) + ) await edit_or_create(call, dedent(f""" Управление ботом @{bot.name}. @@ -143,6 +148,27 @@ async def send_bot_delete_menu(bot: Bot, call: types.CallbackQuery): """), reply_markup=keyboard) +async def send_bot_settings_menu(bot: Bot, call: types.CallbackQuery): + await call.answer() + keyboard = types.InlineKeyboardMarkup(row_width=1) + keyboard.insert( + types.InlineKeyboardButton(text="Потоки сообщений", + callback_data=menu_callback.new(level=3, bot_id=bot.id, operation="threads", + chat=empty)) + ) + keyboard.insert( + types.InlineKeyboardButton(text="<< Назад", + callback_data=menu_callback.new(level=1, bot_id=bot.id, operation=empty, + chat=empty)) + ) + + thread_turn = "включены" if bot.enable_threads else "выключены" + text = dedent(f""" + Потоки сообщений: {thread_turn} + """) + await edit_or_create(call, text, reply_markup=keyboard, parse_mode="HTML") + + async def send_bot_text_menu(bot: Bot, call: ty.Optional[types.CallbackQuery] = None, chat_id: ty.Optional[int] = None): if call: await call.answer() @@ -360,6 +386,8 @@ async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMCon return await send_bot_delete_menu(bot, call) if operation == "stat": return await send_bot_statistic_menu(bot, call) + if operation == "settings": + return await send_bot_settings_menu(bot, call) if operation == "text": await state.set_state("wait_start_text") async with state.proxy() as proxy: @@ -371,6 +399,9 @@ async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMCon return await bot_actions.delete_bot(bot, call) if operation == "chat": return await bot_actions.select_chat(bot, call, callback_data.get("chat")) + if operation == "threads": + await bot_actions.threads(bot, call) + return await send_bot_settings_menu(bot, call) if operation == "reset_text": await bot_actions.reset_bot_text(bot, call) return await send_bot_text_menu(bot, call) diff --git a/olgram/models/models.py b/olgram/models/models.py index 18cd4e4..ccabf43 100644 --- a/olgram/models/models.py +++ b/olgram/models/models.py @@ -41,6 +41,8 @@ class Bot(Model): incoming_messages_count = fields.BigIntField(default=0) outgoing_messages_count = fields.BigIntField(default=0) + enable_threads = fields.BooleanField(default=False) + def decrypted_token(self): cryptor = DatabaseSettings.cryptor() return cryptor.decrypt(self.token) diff --git a/server/custom.py b/server/custom.py index 9270553..dca7de7 100644 --- a/server/custom.py +++ b/server/custom.py @@ -57,7 +57,7 @@ async def message_handler(message: types.Message, *args, **kwargs): text="Вы заблокированы в этом боте") # Пересылаем сообщение в супер-чат - if is_super_group: + if is_super_group and bot.enable_threads: thread_first_message = await _redis.get(_thread_uniqie_id(bot.pk, message.chat.id)) if thread_first_message: # переслать в супер-чат, отвечая на предыдущее сообщение