Improve Telegram forum onboarding and topic safety

This commit is contained in:
Mikhail Putilovskij 2026-04-01 01:49:45 +03:00
parent 2b56b98697
commit a1b7a14138
13 changed files with 1101 additions and 376 deletions

View file

@ -5,12 +5,35 @@ from aiogram import F, Router
from aiogram.fsm.context import FSMContext
from aiogram.types import CallbackQuery
from adapter.telegram import db
from adapter.telegram.converter import format_outgoing, is_forum_message, resolve_forum_chat_id
from core.handler import EventDispatcher
from core.protocol import IncomingCallback
from core.protocol import IncomingCallback, OutgoingMessage, OutgoingUI
router = Router(name="confirm")
async def _send_reply(
callback: CallbackQuery,
text: str,
*,
thread_id: int | None = None,
) -> None:
if callback.message is None:
await callback.answer()
return
if thread_id is None:
await callback.message.answer(text)
return
await callback.message.bot.send_message(
callback.message.chat.id,
text,
message_thread_id=thread_id,
)
@router.callback_query(F.data.startswith("confirm:"))
async def handle_confirm(
callback: CallbackQuery,
@ -23,12 +46,21 @@ async def handle_confirm(
data = await state.get_data()
chat_id = data.get("active_chat_id", "")
chat_name = data.get("active_chat_name", "Чат")
thread_id = getattr(callback.message, "message_thread_id", None)
forum_mode = callback.message is not None and is_forum_message(callback.message)
from adapter.telegram import db as tgdb
tg_id = callback.from_user.id
tg_user = tgdb.get_or_create_tg_user(tg_id, str(tg_id), callback.from_user.full_name)
tg_user = db.get_or_create_tg_user(tg_id, str(tg_id), callback.from_user.full_name)
platform_user_id = tg_user.get("platform_user_id", str(tg_id))
if forum_mode and callback.message is not None:
resolved_chat_id = resolve_forum_chat_id(callback.message, tg_id)
if resolved_chat_id:
chat_id = resolved_chat_id
chat = db.get_chat_by_id(chat_id)
if chat:
chat_name = chat["name"]
incoming = IncomingCallback(
user_id=platform_user_id,
platform="telegram",
@ -38,12 +70,12 @@ async def handle_confirm(
)
events = await dispatcher.dispatch(incoming)
await callback.message.edit_reply_markup(reply_markup=None)
if callback.message is not None:
await callback.message.edit_reply_markup(reply_markup=None)
for event in events:
from core.protocol import OutgoingMessage, OutgoingUI
from adapter.telegram.converter import format_outgoing
if isinstance(event, (OutgoingMessage, OutgoingUI)):
await callback.message.answer(format_outgoing(chat_name, event))
rendered = format_outgoing(chat_name, event, prefix=not forum_mode)
await _send_reply(callback, rendered, thread_id=thread_id if forum_mode else None)
await callback.answer()