reverse bridge

This commit is contained in:
kislovdm
2026-04-09 12:29:41 +03:00
parent 52846d341a
commit 6f5a15cd49
2 changed files with 51 additions and 13 deletions
+38 -4
View File
@@ -70,12 +70,22 @@ class MaxToTelegramBridge:
total_media = len(parsed.image_urls) + len(parsed.video_urls) total_media = len(parsed.image_urls) + len(parsed.video_urls)
if total_media > 1: if total_media > 1:
# Отправляем одним альбомом в Telegram (единое сообщение). # Отправляем одним альбомом в Telegram (единое сообщение).
await self._telegram.send_media_group( sent_messages = await self._telegram.send_media_group(
chat_id=target_chat_id, chat_id=target_chat_id,
image_urls=parsed.image_urls, image_urls=parsed.image_urls,
video_urls=parsed.video_urls, video_urls=parsed.video_urls,
caption=text, caption=text,
) )
for sent in sent_messages:
mid = sent.get("message_id")
if mid is None:
continue
self._storage.save_mapping(
telegram_chat_id=str(target_chat_id),
telegram_message_id=str(mid),
max_chat_id=str(parsed.chat_id),
max_message_id=str(parsed.message_id),
)
self._storage.mark_forwarded(parsed.message_id, parsed.chat_id) self._storage.mark_forwarded(parsed.message_id, parsed.chat_id)
logger.info( logger.info(
"Forwarded media group %s/%s (images=%s, videos=%s)", "Forwarded media group %s/%s (images=%s, videos=%s)",
@@ -88,17 +98,41 @@ class MaxToTelegramBridge:
sent_any = False sent_any = False
if parsed.text.strip() and total_media == 0: if parsed.text.strip() and total_media == 0:
await self._telegram.send_text(target_chat_id, text) sent = await self._telegram.send_text(target_chat_id, text)
mid = sent.get("result", {}).get("message_id") if isinstance(sent.get("result"), dict) else None
if mid is not None:
self._storage.save_mapping(
telegram_chat_id=str(target_chat_id),
telegram_message_id=str(mid),
max_chat_id=str(parsed.chat_id),
max_message_id=str(parsed.message_id),
)
sent_any = True sent_any = True
for index, image_url in enumerate(parsed.image_urls): for index, image_url in enumerate(parsed.image_urls):
caption = text if not sent_any and index == 0 else None caption = text if not sent_any and index == 0 else None
await self._telegram.send_photo(target_chat_id, image_url, caption=caption) sent = await self._telegram.send_photo(target_chat_id, image_url, caption=caption)
mid = sent.get("result", {}).get("message_id") if isinstance(sent.get("result"), dict) else None
if mid is not None:
self._storage.save_mapping(
telegram_chat_id=str(target_chat_id),
telegram_message_id=str(mid),
max_chat_id=str(parsed.chat_id),
max_message_id=str(parsed.message_id),
)
sent_any = True sent_any = True
for index, video_url in enumerate(parsed.video_urls): for index, video_url in enumerate(parsed.video_urls):
caption = text if not sent_any and index == 0 else None caption = text if not sent_any and index == 0 else None
await self._telegram.send_video(target_chat_id, video_url, caption=caption) sent = await self._telegram.send_video(target_chat_id, video_url, caption=caption)
mid = sent.get("result", {}).get("message_id") if isinstance(sent.get("result"), dict) else None
if mid is not None:
self._storage.save_mapping(
telegram_chat_id=str(target_chat_id),
telegram_message_id=str(mid),
max_chat_id=str(parsed.chat_id),
max_message_id=str(parsed.message_id),
)
sent_any = True sent_any = True
self._storage.mark_forwarded(parsed.message_id, parsed.chat_id) self._storage.mark_forwarded(parsed.message_id, parsed.chat_id)
+13 -9
View File
@@ -23,8 +23,8 @@ class TelegramClient:
return chat_id, True return chat_id, True
return self._fallback_user_id, False return self._fallback_user_id, False
async def send_text(self, chat_id: str, text: str) -> None: async def send_text(self, chat_id: str, text: str) -> dict[str, Any]:
await self._request( return await self._request(
"sendMessage", "sendMessage",
{ {
"chat_id": chat_id, "chat_id": chat_id,
@@ -33,16 +33,16 @@ class TelegramClient:
}, },
) )
async def send_photo(self, chat_id: str, photo_url: str, caption: str | None = None) -> None: async def send_photo(self, chat_id: str, photo_url: str, caption: str | None = None) -> dict[str, Any]:
payload: dict[str, Any] = { payload: dict[str, Any] = {
"chat_id": chat_id, "chat_id": chat_id,
"photo": photo_url, "photo": photo_url,
} }
if caption: if caption:
payload["caption"] = caption payload["caption"] = caption
await self._request("sendPhoto", payload) return await self._request("sendPhoto", payload)
async def send_video(self, chat_id: str, video_url: str, caption: str | None = None) -> None: async def send_video(self, chat_id: str, video_url: str, caption: str | None = None) -> dict[str, Any]:
payload: dict[str, Any] = { payload: dict[str, Any] = {
"chat_id": chat_id, "chat_id": chat_id,
"video": video_url, "video": video_url,
@@ -50,7 +50,7 @@ class TelegramClient:
} }
if caption: if caption:
payload["caption"] = caption payload["caption"] = caption
await self._request("sendVideo", payload) return await self._request("sendVideo", payload)
async def send_media_group( async def send_media_group(
self, self,
@@ -58,7 +58,7 @@ class TelegramClient:
image_urls: list[str], image_urls: list[str],
video_urls: list[str], video_urls: list[str],
caption: str | None = None, caption: str | None = None,
) -> None: ) -> list[dict[str, Any]]:
media: list[dict[str, Any]] = [] media: list[dict[str, Any]] = []
for url in image_urls: for url in image_urls:
@@ -68,18 +68,22 @@ class TelegramClient:
media.append({"type": "video", "media": url, "supports_streaming": True}) media.append({"type": "video", "media": url, "supports_streaming": True})
if not media: if not media:
return return []
if caption: if caption:
media[0]["caption"] = caption media[0]["caption"] = caption
await self._request( data = await self._request(
"sendMediaGroup", "sendMediaGroup",
{ {
"chat_id": chat_id, "chat_id": chat_id,
"media": media, "media": media,
}, },
) )
result = data.get("result", [])
if not isinstance(result, list):
return []
return [m for m in result if isinstance(m, dict)]
async def get_me(self) -> dict[str, Any]: async def get_me(self) -> dict[str, Any]:
if self._me is not None: if self._me is not None: