From 1779a5607d2b451d438b0cb5a97a754fcfb7f952 Mon Sep 17 00:00:00 2001 From: mihalin Date: Thu, 17 Mar 2022 08:05:03 +0300 Subject: [PATCH] #11 additional user info --- olgram/commands/bot_actions.py | 5 +++++ olgram/commands/menu.py | 12 +++++++++++- .../migrations/models/11_20220317080443_update.sql | 4 ++++ olgram/models/models.py | 1 + server/custom.py | 12 ++++++++++++ 5 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 olgram/migrations/models/11_20220317080443_update.sql diff --git a/olgram/commands/bot_actions.py b/olgram/commands/bot_actions.py index ea49bbc..7d00b3a 100644 --- a/olgram/commands/bot_actions.py +++ b/olgram/commands/bot_actions.py @@ -74,3 +74,8 @@ async def select_chat(bot: Bot, call: types.CallbackQuery, chat: str): async def threads(bot: Bot, call: types.CallbackQuery): bot.enable_threads = not bot.enable_threads await bot.save(update_fields=["enable_threads"]) + + +async def additional_info(bot: Bot, call: types.CallbackQuery): + bot.enable_additional_info = not bot.enable_additional_info + await bot.save(update_fields=["enable_additional_info"]) diff --git a/olgram/commands/menu.py b/olgram/commands/menu.py index 0c55399..a491f76 100644 --- a/olgram/commands/menu.py +++ b/olgram/commands/menu.py @@ -156,6 +156,11 @@ async def send_bot_settings_menu(bot: Bot, call: types.CallbackQuery): 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=3, bot_id=bot.id, operation="additional_info", + chat=empty)) + ) keyboard.insert( types.InlineKeyboardButton(text="<< Назад", callback_data=menu_callback.new(level=1, bot_id=bot.id, operation=empty, @@ -163,8 +168,10 @@ async def send_bot_settings_menu(bot: Bot, call: types.CallbackQuery): ) thread_turn = "включены" if bot.enable_threads else "выключены" + info_turn = "включена" if bot.enable_additional_info else "выключена" text = dedent(f""" - Потоки сообщений: {thread_turn} + Потоки сообщений: {thread_turn}\n + Дополнительная информация: {info_turn}\n """) await edit_or_create(call, text, reply_markup=keyboard, parse_mode="HTML") @@ -402,6 +409,9 @@ async def callback(call: types.CallbackQuery, callback_data: dict, state: FSMCon if operation == "threads": await bot_actions.threads(bot, call) return await send_bot_settings_menu(bot, call) + if operation == "additional_info": + await bot_actions.additional_info(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/migrations/models/11_20220317080443_update.sql b/olgram/migrations/models/11_20220317080443_update.sql new file mode 100644 index 0000000..09764cf --- /dev/null +++ b/olgram/migrations/models/11_20220317080443_update.sql @@ -0,0 +1,4 @@ +-- upgrade -- +ALTER TABLE "bot" ADD "enable_additional_info" BOOL NOT NULL DEFAULT False; +-- downgrade -- +ALTER TABLE "bot" DROP COLUMN "enable_additional_info"; diff --git a/olgram/models/models.py b/olgram/models/models.py index ccabf43..b63319b 100644 --- a/olgram/models/models.py +++ b/olgram/models/models.py @@ -42,6 +42,7 @@ class Bot(Model): outgoing_messages_count = fields.BigIntField(default=0) enable_threads = fields.BooleanField(default=False) + enable_additional_info = fields.BooleanField(default=False) def decrypted_token(self): cryptor = DatabaseSettings.cryptor() diff --git a/server/custom.py b/server/custom.py index dca7de7..3b8f3b1 100644 --- a/server/custom.py +++ b/server/custom.py @@ -56,6 +56,8 @@ async def message_handler(message: types.Message, *args, **kwargs): return SendMessage(chat_id=message.chat.id, text="Вы заблокированы в этом боте") + in_thread = False + # Пересылаем сообщение в супер-чат if is_super_group and bot.enable_threads: thread_first_message = await _redis.get(_thread_uniqie_id(bot.pk, message.chat.id)) @@ -63,6 +65,7 @@ async def message_handler(message: types.Message, *args, **kwargs): # переслать в супер-чат, отвечая на предыдущее сообщение try: new_message = await message.copy_to(super_chat_id, reply_to_message_id=int(thread_first_message)) + in_thread = True except exceptions.BadRequest: new_message = await message.forward(super_chat_id) await _redis.set(_thread_uniqie_id(bot.pk, message.chat.id), new_message.message_id, @@ -78,6 +81,15 @@ async def message_handler(message: types.Message, *args, **kwargs): await _redis.set(_message_unique_id(bot.pk, new_message.message_id), message.chat.id, pexpire=ServerSettings.redis_timeout_ms()) + if bot.enable_additional_info and not in_thread: + user_info = "От пользователя: " + if message.from_user.full_name: + user_info += message.from_user.full_name + if message.from_user.username: + user_info += " | @" + message.from_user.username + user_info += f" | #{message.from_user.id}" + await message.bot.send_message(super_chat_id, text=user_info, reply_to_message_id=new_message.message_id) + bot.incoming_messages_count = F("incoming_messages_count") + 1 await bot.save(update_fields=["incoming_messages_count"])