inbound.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. """Pure parsers for inbound DingTalk ``ChatbotMessage`` payloads (no I/O, no adapter state)."""
  2. import json
  3. from typing import Any, List, Optional, Tuple
  4. from gateway.platforms.event import MessageType
  5. # DingTalk rich-text item type → runtime content type
  6. DINGTALK_TYPE_MAPPING = {"picture": "image", "voice": "audio"}
  7. # File extension → MIME type for DingTalk file/image messages. image/* MIMEs
  8. # make ``extract_media`` classify msgtype='image'/'file' payloads as PHOTO.
  9. EXT_MAP = {
  10. "pdf": "application/pdf", "doc": "application/msword", "xls": "application/vnd.ms-excel",
  11. "docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  12. "xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  13. "png": "image/png", "jpg": "image/jpeg", "jpeg": "image/jpeg", "gif": "image/gif", "webp": "image/webp",
  14. "md": "text/markdown", "txt": "text/plain", "csv": "text/csv", "zip": "application/zip", "mp4": "video/mp4",
  15. }
  16. # rich-text runtime type → (media_types entry, MessageType promotion when still TEXT)
  17. _RICH_MEDIA = {"image": ("image", MessageType.PHOTO), "video": ("video", MessageType.VIDEO), "file": ("application/octet-stream", MessageType.DOCUMENT)}
  18. def _extensions(message: Any) -> Any:
  19. return getattr(message, "extensions", {}) or {}
  20. def _ext_content(message: Any) -> Optional[dict]:
  21. """``extensions['content']`` when it is a dict, else None."""
  22. content = _extensions(message).get("content", {})
  23. return content if isinstance(content, dict) else None
  24. def _rich_list(message: Any) -> Optional[list]:
  25. """Rich-text item list from either SDK shape (``rich_text_content.rich_text_list`` or legacy ``rich_text``)."""
  26. rich_text = getattr(message, "rich_text_content", None) or getattr(message, "rich_text", None)
  27. if not rich_text:
  28. return None
  29. rich_list = getattr(rich_text, "rich_text_list", None) or rich_text
  30. return rich_list if isinstance(rich_list, list) else None
  31. def _url_of(raw: Any) -> str:
  32. return (raw.get("url", "") or raw.get("docUrl", "")) if isinstance(raw, dict) else ""
  33. def _card_text(message: Any) -> str:
  34. """msgtype='card' (钉钉文档分享卡片 / link card): title + doc URL from ``extensions['card']``."""
  35. extensions = _extensions(message)
  36. card = extensions.get("card", {})
  37. content = ""
  38. if isinstance(card, dict):
  39. title, raw_content = card.get("title", ""), card.get("content", "")
  40. doc_url = _url_of(raw_content)
  41. if isinstance(raw_content, str) and raw_content.strip():
  42. try:
  43. doc_url = _url_of(json.loads(raw_content.strip()))
  44. except (ValueError, TypeError):
  45. doc_url = raw_content
  46. content = " ".join(([f"[文档] {title}"] if title else []) + ([doc_url] if doc_url else []))
  47. ext_text = extensions.get("text", {}) # last-resort: raw text field from extensions
  48. return content or ((ext_text.get("content", "") or "").strip() if isinstance(ext_text, dict) else "")
  49. def _interactive_card_text(message: Any) -> str:
  50. """msgtype='interactiveCard': ``extensions['content']`` carries title + biz_custom_action_url."""
  51. ext_content = _ext_content(message) or {}
  52. doc_url, title = ext_content.get("biz_custom_action_url", ""), ext_content.get("title", "")
  53. return " ".join([f"[文档卡片] {title}" if title else "[文档卡片]"] + ([doc_url] if doc_url else [])) if (doc_url or title) else ""
  54. def _ext_field(message: Any, field: str) -> Any:
  55. ext_content = _ext_content(message)
  56. return ext_content.get(field, "") if ext_content else ""
  57. # Fallbacks by msgtype when no plain/rich text was found (types are exclusive): audio -> DingTalk speech
  58. # recognition text; file -> fileName; card / interactiveCard -> title + doc URL.
  59. _EMPTY_TEXT_FALLBACKS = {
  60. "audio": lambda m: (_ext_field(m, "recognition") or "").strip(),
  61. "file": lambda m: f"[文件] {_ext_field(m, 'fileName')}" if _ext_field(m, "fileName") else "",
  62. "card": _card_text,
  63. "interactiveCard": _interactive_card_text,
  64. }
  65. def _rich_item_text(item: Any) -> str:
  66. if isinstance(item, dict):
  67. return item.get("text") or item.get("content") or ""
  68. return getattr(item, "text", "") or ""
  69. def extract_text(message: Any) -> str:
  70. """Extract plain text from a DingTalk chatbot message. Handles both SDK shapes: legacy ``message.text`` dict ``{"content": ...}`` and >= 0.20 ``TextContent``
  71. (whose ``__str__`` is ``"TextContent(content=...)"`` — always read ``.content`` first)."""
  72. text = getattr(message, "text", None) or ""
  73. content = (text.content or "" if hasattr(text, "content") else text.get("content", "") if isinstance(text, dict) else str(text)).strip()
  74. rich_list = _rich_list(message) if not content else None
  75. if rich_list is not None:
  76. content = " ".join(t for t in map(_rich_item_text, rich_list) if t).strip()
  77. fallback = _EMPTY_TEXT_FALLBACKS.get(getattr(message, "message_type", "")) if not content else None
  78. # Do NOT strip "@bot": the mention is routed structurally (callback ``isInAtList``), and
  79. # regex-stripping @handles would damage e-mails, SSH URLs and literal "@openai" references.
  80. return fallback(message) if fallback else content
  81. def extract_media(message: Any) -> tuple[MessageType, list[str], list[str]]:
  82. """Return ``(MessageType, [download codes/urls], [mime types])`` for a message."""
  83. msg_type = MessageType.TEXT
  84. media_urls: list[str] = []
  85. media_types: list[str] = []
  86. image_content = getattr(message, "image_content", None)
  87. if download_code := (getattr(image_content, "download_code", None) if image_content else None):
  88. media_urls.append(download_code)
  89. media_types.append("image")
  90. msg_type = MessageType.PHOTO
  91. for item in _rich_list(message) or ():
  92. if not isinstance(item, dict):
  93. continue
  94. dl_code = item.get("downloadCode") or item.get("download_code") or ""
  95. if not dl_code:
  96. continue
  97. item_type = item.get("type", "")
  98. mapped = DINGTALK_TYPE_MAPPING.get(item_type, "file")
  99. # "voice" items are native voice notes → STT (VOICE); "audio" file uploads stay AUDIO.
  100. mime, promoted = ("audio", MessageType.VOICE if item_type == "voice" else MessageType.AUDIO) if mapped == "audio" else _RICH_MEDIA[mapped]
  101. media_urls.append(dl_code)
  102. media_types.append(mime)
  103. if msg_type == MessageType.TEXT:
  104. msg_type = promoted
  105. msg_type_str = getattr(message, "message_type", "") or ""
  106. if msg_type_str == "picture" and not media_urls:
  107. msg_type = MessageType.PHOTO
  108. elif msg_type_str == "richText":
  109. # Only re-derive when the scan above left TEXT — resetting a VOICE/AUDIO/VIDEO/DOCUMENT
  110. # promotion here dropped native voice notes back to TEXT and skipped STT.
  111. if msg_type == MessageType.TEXT and any("image" in t for t in media_types):
  112. msg_type = MessageType.PHOTO
  113. elif msg_type_str == "audio":
  114. # Voice message: recognition text is already in the text. Do NOT add media_urls, or
  115. # run.py's transcription enrichment overwrites it with a failed STT attempt.
  116. if msg_type == MessageType.TEXT:
  117. msg_type = MessageType.VOICE
  118. elif msg_type_str in ("file", "image"):
  119. ext_content = _ext_content(message) or {}
  120. if dl_code := ext_content.get("downloadCode") or "":
  121. fname = ext_content.get("fileName", "")
  122. mime = EXT_MAP.get(fname.rsplit(".", 1)[-1].lower() if fname and "." in fname else "", "application/octet-stream")
  123. media_urls.append(dl_code)
  124. media_types.append(mime)
  125. if msg_type == MessageType.TEXT: # image messages, and files with image MIME (a .png attachment) → PHOTO
  126. msg_type = MessageType.PHOTO if (msg_type_str == "image" or mime.startswith("image/")) else MessageType.DOCUMENT
  127. return msg_type, media_urls, media_types
  128. def collect_download_codes(message: Any) -> list[tuple[Any, str]]:
  129. """Return ``(container, key)`` pairs whose download code should be resolved to a URL."""
  130. codes: list[tuple[Any, str]] = []
  131. img_content = getattr(message, "image_content", None)
  132. if img_content and getattr(img_content, "download_code", None):
  133. codes.append((img_content, "download_code"))
  134. rich_text = getattr(message, "rich_text_content", None)
  135. for item in (getattr(rich_text, "rich_text_list", []) or []) if rich_text else []:
  136. if isinstance(item, dict):
  137. codes.extend((item, key) for key in ("downloadCode", "pictureDownloadCode", "download_code") if item.get(key))
  138. if (getattr(message, "message_type", "") or "") in ("file", "image"):
  139. ext_content = _ext_content(message)
  140. if ext_content and ext_content.get("downloadCode"):
  141. codes.append((ext_content, "downloadCode"))
  142. return codes