фикс переменных
This commit is contained in:
@@ -2,6 +2,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
import re
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from aiogram import Bot, Dispatcher, F, Router
|
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:
|
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
|
source = message.reply_to_message or message
|
||||||
if source.text:
|
if source.text:
|
||||||
return source.md_text
|
return source.md_text
|
||||||
return None
|
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:
|
async def safe_edit_message(callback: CallbackQuery, text: str, reply_markup=None) -> None:
|
||||||
try:
|
try:
|
||||||
await callback.message.edit_text(
|
await callback.message.edit_text(
|
||||||
@@ -189,7 +207,7 @@ async def apply_status_update(
|
|||||||
|
|
||||||
def save_post_template(state_storage: JsonStateStorage, template: str) -> None:
|
def save_post_template(state_storage: JsonStateStorage, template: str) -> None:
|
||||||
payload = state_storage.load()
|
payload = state_storage.load()
|
||||||
payload["template"] = {"text": template}
|
payload["template"] = {"text": normalize_template_placeholders(template)}
|
||||||
state_storage.save(payload)
|
state_storage.save(payload)
|
||||||
|
|
||||||
|
|
||||||
@@ -254,10 +272,14 @@ async def post_handler(
|
|||||||
|
|
||||||
template = extract_template_text(message)
|
template = extract_template_text(message)
|
||||||
if template is not None:
|
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()
|
await state.clear()
|
||||||
if "{{actors}}" not in template:
|
if "{{actors}}" not in normalized:
|
||||||
await message.answer("Шаблон сохранен, но в нем нет {{actors}}.")
|
await message.answer(
|
||||||
|
"Шаблон сохранен, но в нем нет {{actors}}.\n"
|
||||||
|
"Плейсхолдер должен быть именно {{actors}} в любом регистре."
|
||||||
|
)
|
||||||
return
|
return
|
||||||
await message.answer("Шаблон поста сохранен.")
|
await message.answer("Шаблон поста сохранен.")
|
||||||
return
|
return
|
||||||
@@ -465,11 +487,15 @@ async def post_template_handler(
|
|||||||
await message.answer("Нужен текстовый шаблон. Перешлите текстовый пост или отправьте текст.")
|
await message.answer("Нужен текстовый шаблон. Перешлите текстовый пост или отправьте текст.")
|
||||||
return
|
return
|
||||||
|
|
||||||
save_post_template(state_storage, template)
|
normalized = normalize_template_placeholders(template)
|
||||||
|
save_post_template(state_storage, normalized)
|
||||||
await state.clear()
|
await state.clear()
|
||||||
|
|
||||||
if "{{actors}}" not in template:
|
if "{{actors}}" not in normalized:
|
||||||
await message.answer("Шаблон сохранен, но в нем нет {{actors}}.")
|
await message.answer(
|
||||||
|
"Шаблон сохранен, но в нем нет {{actors}}.\n"
|
||||||
|
"Плейсхолдер должен быть именно {{actors}} в любом регистре."
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
await message.answer("Шаблон поста сохранен.")
|
await message.answer("Шаблон поста сохранен.")
|
||||||
|
|||||||
Reference in New Issue
Block a user