mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
fix(line): normalize inbound media types and cache routing
This commit is contained in:
parent
7ee111ff9b
commit
73e193c03d
2 changed files with 117 additions and 14 deletions
|
|
@ -92,7 +92,10 @@ from gateway.platforms.base import (
|
|||
MessageEvent,
|
||||
MessageType,
|
||||
SendResult,
|
||||
cache_audio_from_bytes,
|
||||
cache_document_from_bytes,
|
||||
cache_image_from_bytes,
|
||||
cache_video_from_bytes,
|
||||
)
|
||||
from gateway.config import Platform
|
||||
|
||||
|
|
@ -955,11 +958,15 @@ class LineAdapter(BasePlatformAdapter):
|
|||
|
||||
if msg_type == "text":
|
||||
text = msg.get("text", "") or ""
|
||||
elif msg_type in {"image", "audio", "video", "file"}:
|
||||
local_path = await self._download_media(message_id, msg_type)
|
||||
elif msg_type in ("image", "audio", "video", "file"):
|
||||
local_path, media_type = await self._download_media(
|
||||
message_id,
|
||||
msg_type,
|
||||
filename=msg.get("fileName") or msg.get("file_name"),
|
||||
)
|
||||
if local_path:
|
||||
media_urls.append(local_path)
|
||||
media_types.append(msg_type)
|
||||
media_types.append(media_type)
|
||||
text = f"[{msg_type}]"
|
||||
elif msg_type == "sticker":
|
||||
keywords = msg.get("keywords") or []
|
||||
|
|
@ -1054,14 +1061,20 @@ class LineAdapter(BasePlatformAdapter):
|
|||
except Exception:
|
||||
pass
|
||||
|
||||
async def _download_media(self, message_id: str, msg_type: str) -> Optional[str]:
|
||||
async def _download_media(
|
||||
self,
|
||||
message_id: str,
|
||||
msg_type: str,
|
||||
*,
|
||||
filename: Optional[str] = None,
|
||||
) -> Tuple[Optional[str], str]:
|
||||
if not self._client or not message_id:
|
||||
return None
|
||||
return None, ""
|
||||
try:
|
||||
data = await self._client.fetch_content(message_id)
|
||||
except Exception as exc:
|
||||
logger.warning("LINE: failed to fetch %s content for %s: %s", msg_type, message_id, exc)
|
||||
return None
|
||||
return None, ""
|
||||
ext = {
|
||||
"image": ".jpg",
|
||||
"audio": ".m4a",
|
||||
|
|
@ -1069,10 +1082,22 @@ class LineAdapter(BasePlatformAdapter):
|
|||
"file": ".bin",
|
||||
}.get(msg_type, ".bin")
|
||||
try:
|
||||
return cache_image_from_bytes(data, ext=ext)
|
||||
if msg_type == "image":
|
||||
return cache_image_from_bytes(data, ext=ext), "image/jpeg"
|
||||
if msg_type == "audio":
|
||||
media_type = mimetypes.guess_type(f"audio{ext}")[0] or "audio/mp4"
|
||||
return cache_audio_from_bytes(data, ext=ext), media_type
|
||||
if msg_type == "video":
|
||||
media_type = mimetypes.guess_type(f"video{ext}")[0] or "video/mp4"
|
||||
return cache_video_from_bytes(data, ext=ext), media_type
|
||||
document_name = filename or f"line_file{ext}"
|
||||
return (
|
||||
cache_document_from_bytes(data, document_name),
|
||||
mimetypes.guess_type(document_name)[0] or "application/octet-stream",
|
||||
)
|
||||
except Exception as exc:
|
||||
logger.warning("LINE: failed to cache %s payload: %s", msg_type, exc)
|
||||
return None
|
||||
return None, ""
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Outbound send (text)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue