diff --git a/core/handlers/message.py b/core/handlers/message.py index d9f91cd..876754c 100644 --- a/core/handlers/message.py +++ b/core/handlers/message.py @@ -1,7 +1,35 @@ # core/handlers/message.py from __future__ import annotations -from core.protocol import IncomingMessage, OutgoingMessage, OutgoingTyping +from core.protocol import Attachment, IncomingMessage, OutgoingMessage, OutgoingTyping + + +def _infer_attachment_type(mime_type: str | None) -> str: + if not mime_type: + return "document" + if mime_type.startswith("image/"): + return "image" + if mime_type.startswith("audio/"): + return "audio" + if mime_type.startswith("video/"): + return "video" + return "document" + + +def _to_core_attachments(raw: list) -> list[Attachment]: + result = [] + for a in raw: + if isinstance(a, Attachment): + result.append(a) + else: + result.append(Attachment( + type=getattr(a, "type", None) or _infer_attachment_type(getattr(a, "mime_type", None)), + url=getattr(a, "url", None), + filename=getattr(a, "filename", None), + mime_type=getattr(a, "mime_type", None), + workspace_path=getattr(a, "workspace_path", None), + )) + return result def _start_command(platform: str) -> str: @@ -38,6 +66,6 @@ async def handle_message(event: IncomingMessage, auth_mgr, platform, chat_mgr, s chat_id=event.chat_id, text=response.response, parse_mode="markdown", - attachments=list(getattr(response, "attachments", [])), + attachments=_to_core_attachments(getattr(response, "attachments", [])), ), ]