reverse bridge
This commit is contained in:
+38
-4
@@ -70,12 +70,22 @@ class MaxToTelegramBridge:
|
||||
total_media = len(parsed.image_urls) + len(parsed.video_urls)
|
||||
if total_media > 1:
|
||||
# Отправляем одним альбомом в Telegram (единое сообщение).
|
||||
await self._telegram.send_media_group(
|
||||
sent_messages = await self._telegram.send_media_group(
|
||||
chat_id=target_chat_id,
|
||||
image_urls=parsed.image_urls,
|
||||
video_urls=parsed.video_urls,
|
||||
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)
|
||||
logger.info(
|
||||
"Forwarded media group %s/%s (images=%s, videos=%s)",
|
||||
@@ -88,17 +98,41 @@ class MaxToTelegramBridge:
|
||||
|
||||
sent_any = False
|
||||
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
|
||||
|
||||
for index, image_url in enumerate(parsed.image_urls):
|
||||
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
|
||||
|
||||
for index, video_url in enumerate(parsed.video_urls):
|
||||
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
|
||||
|
||||
self._storage.mark_forwarded(parsed.message_id, parsed.chat_id)
|
||||
|
||||
+13
-9
@@ -23,8 +23,8 @@ class TelegramClient:
|
||||
return chat_id, True
|
||||
return self._fallback_user_id, False
|
||||
|
||||
async def send_text(self, chat_id: str, text: str) -> None:
|
||||
await self._request(
|
||||
async def send_text(self, chat_id: str, text: str) -> dict[str, Any]:
|
||||
return await self._request(
|
||||
"sendMessage",
|
||||
{
|
||||
"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] = {
|
||||
"chat_id": chat_id,
|
||||
"photo": photo_url,
|
||||
}
|
||||
if 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] = {
|
||||
"chat_id": chat_id,
|
||||
"video": video_url,
|
||||
@@ -50,7 +50,7 @@ class TelegramClient:
|
||||
}
|
||||
if caption:
|
||||
payload["caption"] = caption
|
||||
await self._request("sendVideo", payload)
|
||||
return await self._request("sendVideo", payload)
|
||||
|
||||
async def send_media_group(
|
||||
self,
|
||||
@@ -58,7 +58,7 @@ class TelegramClient:
|
||||
image_urls: list[str],
|
||||
video_urls: list[str],
|
||||
caption: str | None = None,
|
||||
) -> None:
|
||||
) -> list[dict[str, Any]]:
|
||||
media: list[dict[str, Any]] = []
|
||||
|
||||
for url in image_urls:
|
||||
@@ -68,18 +68,22 @@ class TelegramClient:
|
||||
media.append({"type": "video", "media": url, "supports_streaming": True})
|
||||
|
||||
if not media:
|
||||
return
|
||||
return []
|
||||
|
||||
if caption:
|
||||
media[0]["caption"] = caption
|
||||
|
||||
await self._request(
|
||||
data = await self._request(
|
||||
"sendMediaGroup",
|
||||
{
|
||||
"chat_id": chat_id,
|
||||
"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]:
|
||||
if self._me is not None:
|
||||
|
||||
Reference in New Issue
Block a user