From 944c5ce002442d05c98a0acbe2b70d8152967be5 Mon Sep 17 00:00:00 2001 From: mihalin Date: Fri, 24 Jun 2022 23:58:59 +0300 Subject: [PATCH] html support in /start message --- olgram/commands/menu.py | 4 ++-- olgram/migrations/custom.py | 32 +++++++++++++++++++++++++++++++- server/custom.py | 4 ++-- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/olgram/commands/menu.py b/olgram/commands/menu.py index c359e10..079c239 100644 --- a/olgram/commands/menu.py +++ b/olgram/commands/menu.py @@ -335,7 +335,7 @@ async def start_text_received(message: types.Message, state: FSMContext): async with state.proxy() as proxy: bot_id = proxy.get("bot_id") bot = await Bot.get_or_none(pk=bot_id) - bot.start_text = message.text + bot.start_text = message.html_text await bot.save() await send_bot_text_menu(bot, chat_id=message.chat.id) @@ -345,7 +345,7 @@ async def second_text_received(message: types.Message, state: FSMContext): async with state.proxy() as proxy: bot_id = proxy.get("bot_id") bot = await Bot.get_or_none(pk=bot_id) - bot.second_text = message.text + bot.second_text = message.html_text await bot.save() await send_bot_second_text_menu(bot, chat_id=message.chat.id) diff --git a/olgram/migrations/custom.py b/olgram/migrations/custom.py index 87a8a20..b7fd4a7 100644 --- a/olgram/migrations/custom.py +++ b/olgram/migrations/custom.py @@ -26,6 +26,10 @@ async def upgrade_1(): async def upgrade_2(): """Отменяем малый TTL для старых сообщений""" + meta_info = await MetaInfo.first() + if meta_info.version != 1: + logging.info("skip") + return con = await aioredis.create_connection(ServerSettings.redis_path()) client = aioredis.Redis(con) @@ -35,9 +39,35 @@ async def upgrade_2(): if not key.startswith(b"thread"): await client.pexpire(key, ServerSettings.redis_timeout_ms()) + meta_info.version = 2 + await meta_info.save() + logging.info("done") + + +async def upgrade_3(): + """start_text и second_text должны быть валидными HTML""" + import html + + meta_info = await MetaInfo.first() + if meta_info.version != 2: + logging.info("skip") + return + + async with transactions.in_transaction(): + bots = await Bot.all() + for bot in bots: + if bot.start_text: + bot.start_text = html.escape(bot.start_text) + if bot.second_text: + bot.second_text = html.escape(bot.second_text) + await bot.save(update_fields=["start_text", "second_text"]) + meta_info.version = 3 + await meta_info.save() + logging.info("done") + # Не забудь добавить миграцию в этот лист! -_migrations = [upgrade_1, upgrade_2] +_migrations = [upgrade_1, upgrade_2, upgrade_3] async def migrate(): diff --git a/server/custom.py b/server/custom.py index 6cda22a..9083955 100644 --- a/server/custom.py +++ b/server/custom.py @@ -137,7 +137,7 @@ async def handle_user_message(message: types.Message, super_chat_id: int, bot): # И отправить пользователю специальный текст, если он указан if bot.second_text: - return SendMessage(chat_id=message.chat.id, text=bot.second_text) + return SendMessage(chat_id=message.chat.id, text=bot.second_text, parse_mode="HTML") async def handle_operator_message(message: types.Message, super_chat_id: int, bot): @@ -200,7 +200,7 @@ async def message_handler(message: types.Message, *args, **kwargs): text = bot.start_text if bot.enable_olgram_text: text += _(ServerSettings.append_text()) - return SendMessage(chat_id=message.chat.id, text=text) + return SendMessage(chat_id=message.chat.id, text=text, parse_mode="HTML") if message.text and message.text == "/security_policy": # На команду security_policy нужно ответить, не пересылая сообщение никуда