bot rafactor and bugfix

This commit is contained in:
2025-08-19 04:45:16 +09:00
parent 43dda889f8
commit a8d860ed87
31 changed files with 4396 additions and 613 deletions

View File

@@ -0,0 +1,49 @@
"""Модуль для работы с шаблонами."""
from __future__ import annotations
from typing import Dict, Any
from app.db.session import async_session_maker
from app.models.templates import Template
async def render_template_by_name(
name: str,
template_vars: Dict[str, Any],
context: Dict[str, Any],
) -> Dict[str, Any]:
"""Рендеринг шаблона по имени.
Args:
name: Имя шаблона
template_vars: Переменные для подстановки
context: Дополнительный контекст
Returns:
Отрендеренные данные для поста
Raises:
ValueError: Если шаблон не найден
"""
async with async_session_maker() as session:
stmt = Template.__table__.select().where(Template.__table__.c.name == name)
result = await session.execute(stmt)
template = result.scalar_one_or_none()
if not template:
raise ValueError(f"Шаблон {name} не найден")
text = template.content
keyboard = template.keyboard_tpl
# Подстановка переменных
for key, value in template_vars.items():
text = text.replace(f"{{${key}}}", str(value))
# Подготовка данных для отправки
return {
"type": template.type,
"text": text,
"keyboard": keyboard,
"parse_mode": template.parse_mode
}