diff --git a/session_bot/bot.py b/session_bot/bot.py index 83d5891..238806f 100644 --- a/session_bot/bot.py +++ b/session_bot/bot.py @@ -2,6 +2,7 @@ from __future__ import annotations import asyncio import logging +import re from typing import Any from aiogram import Bot, Dispatcher, F, Router @@ -105,12 +106,29 @@ def get_actor_runtime_state(actor: dict[str, Any], state_storage: JsonStateStora def extract_template_text(message: Message) -> str | None: + if message.text and message.text.startswith("/post "): + return message.text.split(maxsplit=1)[1] + source = message.reply_to_message or message if source.text: return source.md_text return None +def normalize_template_placeholders(template: str) -> str: + normalized = template + normalized = re.sub(r"\\\{\\\{\s*actors\s*\\\}\\\}", "{{actors}}", normalized, flags=re.IGNORECASE) + normalized = re.sub(r"\{\{\s*actors\s*\}\}", "{{actors}}", normalized, flags=re.IGNORECASE) + normalized = re.sub( + r"\\\{\\\{\s*hidden_link\s*\\\}\\\}", + "{{hidden_link}}", + normalized, + flags=re.IGNORECASE, + ) + normalized = re.sub(r"\{\{\s*hidden_link\s*\}\}", "{{hidden_link}}", normalized, flags=re.IGNORECASE) + return normalized + + async def safe_edit_message(callback: CallbackQuery, text: str, reply_markup=None) -> None: try: await callback.message.edit_text( @@ -189,7 +207,7 @@ async def apply_status_update( def save_post_template(state_storage: JsonStateStorage, template: str) -> None: payload = state_storage.load() - payload["template"] = {"text": template} + payload["template"] = {"text": normalize_template_placeholders(template)} state_storage.save(payload) @@ -254,10 +272,14 @@ async def post_handler( template = extract_template_text(message) if template is not None: - save_post_template(state_storage, template) + normalized = normalize_template_placeholders(template) + save_post_template(state_storage, normalized) await state.clear() - if "{{actors}}" not in template: - await message.answer("Шаблон сохранен, но в нем нет {{actors}}.") + if "{{actors}}" not in normalized: + await message.answer( + "Шаблон сохранен, но в нем нет {{actors}}.\n" + "Плейсхолдер должен быть именно {{actors}} в любом регистре." + ) return await message.answer("Шаблон поста сохранен.") return @@ -465,11 +487,15 @@ async def post_template_handler( await message.answer("Нужен текстовый шаблон. Перешлите текстовый пост или отправьте текст.") return - save_post_template(state_storage, template) + normalized = normalize_template_placeholders(template) + save_post_template(state_storage, normalized) await state.clear() - if "{{actors}}" not in template: - await message.answer("Шаблон сохранен, но в нем нет {{actors}}.") + if "{{actors}}" not in normalized: + await message.answer( + "Шаблон сохранен, но в нем нет {{actors}}.\n" + "Плейсхолдер должен быть именно {{actors}} в любом регистре." + ) return await message.answer("Шаблон поста сохранен.")