pinned channel info

This commit is contained in:
kislovdm
2026-06-12 01:31:01 +03:00
parent aecfe4dedf
commit 2b5260049b
8 changed files with 277 additions and 18 deletions
+19
View File
@@ -12,6 +12,25 @@ def format_tg_author(message: Message) -> tuple[str, str | None]:
return name, user.username
def format_topic_pin_text(
*,
chat_name: str,
is_dm: bool,
max_chat_id: int,
participants_count: int,
max_chat_link: str | None,
) -> str:
chat_type = "PRIVATE" if is_dm else "CHAT"
lines = [
f"{chat_name} · Тип: {chat_type}",
f"id: {max_chat_id}",
f"Участников: {participants_count}",
]
if max_chat_link:
lines.extend(["", f"🔗 {max_chat_link}"])
return "\n".join(lines)
def format_tg_to_max_text(author_name: str, username: str | None, text: str) -> str:
handle = f" (@{username})" if username else ""
return f"{author_name}{handle}:\n{text}"
+96 -1
View File
@@ -1,4 +1,6 @@
import asyncio
from pathlib import Path
from uuid import uuid4
import aiohttp
import structlog
@@ -12,7 +14,13 @@ from aiogram.types import (
)
from app.config import Settings
from app.media_transfer import cleanup_paths, download_media_item, resolve_file_name, tmp_dir
from app.media_transfer import (
cleanup_paths,
download_max_image_url,
download_media_item,
resolve_file_name,
tmp_dir,
)
from app.max_layer.client_holder import MaxClientHolder
from app.max_layer.formatter import format_max_text
from app.models.domain import ChatMapping, MaxIncomingMessage
@@ -25,6 +33,7 @@ from app.models.tasks import (
from app.queue.protocols import QueuePort
from app.storage.protocols import StoragePort
from app.telegram_layer.bot_holder import BotHolder
from app.telegram_layer.formatter import format_topic_pin_text
from app.topic_locks import TopicLockRegistry
logger = structlog.get_logger(__name__)
@@ -98,6 +107,7 @@ class TelegramWorker:
is_dm=task.is_dm,
)
await self._storage.save_mapping(mapping)
await self._send_and_pin_topic_info(bot, topic.message_thread_id, task)
logger.info(
"tg_topic_created",
max_chat_id=task.max_chat_id,
@@ -108,6 +118,86 @@ class TelegramWorker:
)
return mapping
async def _send_and_pin_topic_info(
self,
bot,
thread_id: int,
task: Max2TgTask,
) -> None:
chat_name = task.chat_name or task.chat_title
caption = format_topic_pin_text(
chat_name=chat_name,
is_dm=task.is_dm,
max_chat_id=task.max_chat_id,
participants_count=task.participants_count,
max_chat_link=task.max_chat_link,
)
kwargs: dict = {
"chat_id": self._settings.tg_forum_channel_id,
"message_thread_id": thread_id,
}
downloaded: Path | None = None
icon_path: Path | None = None
try:
icon_path = await self._resolve_topic_icon_path(task)
if icon_path is not None:
sent = await bot.send_photo(
photo=FSInputFile(icon_path),
caption=caption,
**kwargs,
)
if (
not task.chat_icon_local_path
or icon_path.resolve() != Path(task.chat_icon_local_path).resolve()
):
downloaded = icon_path
else:
sent = await bot.send_message(text=caption, **kwargs)
await bot.pin_chat_message(
chat_id=self._settings.tg_forum_channel_id,
message_id=sent.message_id,
disable_notification=True,
)
logger.info(
"tg_topic_info_pinned",
max_chat_id=task.max_chat_id,
thread_id=thread_id,
message_id=sent.message_id,
has_icon=icon_path is not None,
)
except Exception:
logger.exception(
"tg_topic_info_pin_failed",
max_chat_id=task.max_chat_id,
thread_id=thread_id,
)
finally:
if downloaded is not None:
cleanup_paths([downloaded])
async def _resolve_topic_icon_path(self, task: Max2TgTask) -> Path | None:
if task.chat_icon_local_path:
local = Path(task.chat_icon_local_path)
if local.is_file() and local.stat().st_size > 0:
return local
if not task.chat_icon_url:
return None
dest = self._tmp_dir / f"topic_icon_{task.max_chat_id}_{uuid4().hex}.jpg"
async with aiohttp.ClientSession() as session:
if await download_max_image_url(session, task.chat_icon_url, dest):
return dest
logger.warning(
"tg_topic_icon_download_failed",
max_chat_id=task.max_chat_id,
url=task.chat_icon_url,
)
dest.unlink(missing_ok=True)
return None
async def _send_to_tg(self, task: Max2TgTask) -> None:
bot = await self._bot_holder.wait_bot()
logger.debug(
@@ -159,6 +249,11 @@ class TelegramWorker:
sender_name=task.sender_name,
is_dm=task.is_dm,
chat_title=task.chat_title,
chat_name=task.chat_name,
chat_icon_url=task.chat_icon_url,
chat_icon_local_path=task.chat_icon_local_path,
participants_count=task.participants_count,
max_chat_link=task.max_chat_link,
reply_to_max_message_id=None,
media=[],
)