Weixin adapter Markdown formatting and message bubble handling
Cluster 7468 · gateway · Feature. Data as of
1 open item. Fix merged in #11571 on 17 Apr 2026. Check it covers your case.
The short answer
- Known?
- Yes. 7 related items grouped here, 2 issues already closed.
- Fix event
- A fix was merged to main in PR #11571. Check it covers your case. No release data is tracked. Fix link checked: 17 Apr 2026. This confirmation is old and may be out of date (a revert or reopen would not have invalidated it here); re-verify.
- Tracker
- The maintainers' tracker marks this
partially resolved, deciding PR #11571. That is separate from the fix event.
Most members concern how the Weixin adapter handles Markdown: preserving native rendering of tables, headers, and links, a config option for rewriting, and keeping multi-line messages in one bubble instead of fragments. One member (a batch salvage) also mentions voice fallback and MEDIA extraction, which are outside that theme.
Open issues0
Open PRs1
- PR#18388fix(weixin): add rewrite_markdown config option for text selection
Fixes on record
-
#7903 fix(weixin): keep multi-line messages in single bubble by default
files:
gateway/config.py,gateway/platforms/weixin.py,tests/gateway/test_weixin.py,website/docs/user-guide/messaging/weixin.mdShow the diff
-
gateway/config.py@@ -1017,6 +1017,9 @@ def _apply_env_overrides(config: GatewayConfig) -> None: weixin_group_allowed_users = os.getenv("WEIXIN_GROUP_ALLOWED_USERS", "").strip() if weixin_group_allowed_users: extra["group_allow_from"] = weixin_group_allowed_users + weixin_split_multiline = os.getenv("WEIXIN_SPLIT_MULTILINE_MESSAGES", "").strip() + if weixin_split_multiline: + extra["split_multiline_messages"] = weixin_split_multiline weixin_home = os.getenv("WEIXIN_HOME_CHANNEL", "").strip() if weixin_home: config.platforms[Platform.WEIXIN].home_channel = HomeChannel( -
gateway/platforms/weixin.py@@ -755,23 +755,58 @@ def _pack_markdown_blocks_for_weixin(content: str, max_length: int) -> List[str] return packed -def _split_text_for_weixin_delivery(content: str, max_length: int) -> List[str]: +def _split_text_for_weixin_delivery( + content: str, max_length: int, split_per_line: bool = False, +) -> List[str]: """Split content into sequential Weixin messages. - Prefer one message per top-level line/markdown unit when the author used - explicit line breaks. Oversized units fall back to block-aware packing so - long code fences still split safely. + *compact* (default): Keep everything in a single message whenever it fits + within the platform limit, even when the author used explicit line breaks. + Only fall back to block-aware packing when the payload exceeds + ``max_length``. + + *per_line* (``split_per_line=True``): Legacy behavior — top-level line + breaks become separate chat messages; oversized units still use + block-aware packing. + + The active mode is controlled via ``config.yaml`` -> + ``platforms.weixin.extra.split_multiline_messages`` (``true`` / ``false``) + or the env var ``WEIXIN_SPLIT_MULTILINE_MESSAGES``. """ - if len(content) <= max_length and "\n" not in content: - return [content] + if split_per_line: + # Legacy: one message per top-level delivery unit. + if len(content) <= max_length and "\n" not in content: + return [content] + chunks: List[str] = [] + for unit in _split_delivery_units_for_weixin(content): + if len(unit) <= max_length: + chunks.append(unit) + continue + chunks.extend(_pack_markdown_blocks_for_weixin(unit, max_length)) + return chunks or [content] - chunks: List[str] = [] - for unit in _split_delivery_units_for_weixin(content): - if len(unit) <= max_length: - chunks.append(unit) - continue - chunks.extend(_pack_markdown_blocks_for_weixin(unit, max_length)) - return chunks or [content] + # Compact (default): single message when under the limit. + if len(content) <= max_length: + return [content] + return _pack_markdown_blocks_for_weixin(content, max_length) or [content] + + +def _coerce_bool(value: Any, default: bool = True) -> bool: + """Coerce a config value to bool, tolerating strings like ``"true"``.""" + if value is None: + return default + if isinstance(value, bool): + return value + if isinstance(value, (int, float)): + return bool(value) + text = str(value).strip().lower() + if not text: + return default + if text in {"1", "true", "yes", "on"}: + return True + if text in {"0", "false", "no", "off"}: + return False + return default def _extract_text(item_list: List[Dict[str, Any]]) -> str: @@ -991,6 +1026,11 @@ def __init__(self, config: PlatformConfig): group_allow_from = os.getenv("WEIXIN_GROUP_ALLOWED_USERS", "") self._allow_from = self._coerce_list(allow_from) self._group_allow_from = self._coerce_list(group_allow_from) + self._split_multiline_messages = _coerce_bool( + extra.get("split_multiline_messages") + or os.getenv("WEIXIN_SPLIT_MULTILINE_MESSAGES"), + default=False, + ) if self._account_id and not self._token: persisted = load_weixin_account(hermes_home, self._account_id) @@ -1330,7 +1370,9 @@ async def _maybe_fetch_typing_ticket(self, user_id: str, context_token: Optional logger.debug("[%s] getConfig failed for %s: %s", self.name, _safe_id(user_id), exc) def _split_text(self, content: str) -> List[str]: - return _split_text_for_weixin_delivery(content, self.MAX_MESSAGE_LENGTH) + return _split_text_for_weixin_delivery( + content, self.MAX_MESSAGE_LENGTH, self._split_multiline_messages, + ) async def send( self, @@ -1344,7 +1386,10 @@ async def send( context_token = [redacted:secret-assignment] chat_id) last_message_id: Optional[str] = None try: - for chunk in self._split_text(self.format_message(content)): + chunks = self._split_text(self.format_message(content)) + for idx, chunk in enumerate(chunks): + if idx > 0: + await asyncio.sleep(0.3) client_id = f"hermes-weixin-{uuid.uuid4().hex}" await _send_message( self._session, -
tests/gateway/test_weixin.py@@ -62,15 +62,15 @@ def test_format_message_returns_empty_string_for_none(self): class TestWeixinChunking: - def test_split_text_sends_top_level_newlines_as_separate_messages(self): + def test_split_text_keeps_short_multiline_message_in_single_chunk(self): adapter = _make_adapter() content = adapter.format_message("第一行\n第二行\n第三行") chunks = adapter._split_text(content) - assert chunks == ["第一行", "第二行", "第三行"] + assert chunks == ["第一行\n第二行\n第三行"] - def test_split_text_keeps_indented_followup_with_previous_line(self): + def test_split_text_keeps_short_reformatted_table_in_single_chunk(self): adapter = _make_adapter() content = adapter.format_message( @@ -81,10 +81,7 @@ def test_split_text_keeps_indented_followup_with_previous_line(self): ) chunks = adapter._split_text(content) - assert chunks == [ - "- Setting: Timeout\n Value: 30s", - "- Setting: Retries\n Value: 3", - ] + assert chunks == [content] def test_split_text_keeps_complete_code_block_together_when_possible(self): adapter = _make_adapter() @@ -114,6 +111,23 @@ def test_split_text_safely_splits_long_code_blocks(self): assert all(len(chunk) <= adapter.MAX_MESSAGE_LENGTH for chunk in chunks) assert all(chunk.count("```") >= 2 for chunk in chunks) + def test_split_text_can_restore_legacy_multiline_splitting_via_config(self): + adapter = WeixinAdapter( + PlatformConfig( + enabled=True, + extra={ + "account_id": "acct", + "token": "***", + "split_multiline_messages": True, + }, + ) + ) + + content = adapter.format_message("第一行\n第二行\n第三行") + chunks = adapter._split_text(content) + + assert chunks == ["第一行", "第二行", "第三行"] + class TestWeixinConfig: def test_apply_env_overrides_configures_weixin(self): @@ -127,6 +141,7 @@ def test_apply_env_overrides_configures_weixin(self): "WEIXIN_BASE_URL": "https://ilink.example.com/", "WEIXIN_CDN_BASE_URL": "https://cdn.example.com/c2c/", "WEIXIN_DM_POLICY": "allowlist", + "WEIXIN_SPLIT_MULTILINE_MESSAGES": "true", "WEIXIN_ALLOWED_USERS": "wxid_1,wxid_2", "WEIXIN_HOME_CHANNEL": "wxid_1", "WEIXIN_HOME_CHANNEL_NAME": "Primary DM", @@ -142,6 +157,7 @@ def test_apply_env_overrides_configures_weixin(self): assert platform_config.extra["base_url"] == "https://ilink.example.com" assert platform_config.extra["cdn_base_url"] == "https://cdn.example.com/c2c" assert platform_config.extra["dm_policy"] == "allowlist" + assert platform_config.extra["split_multiline_messages"] == "true" assert platform_config.extra["allow_from"] == "wxid_1,wxid_2" assert platform_config.home_channel == HomeChannel(Platform.WEIXIN, "wxid_1", "Primary DM") -
website/docs/user-guide/messaging/weixin.md@@ -66,6 +66,9 @@ WEIXIN_ACCOUNT_ID=your-account-id WEIXIN_DM_POLICY=open WEIXIN_ALLOWED_USERS=user_id_1,user_id_2 +# Optional: restore legacy multiline splitting behavior +# WEIXIN_SPLIT_MULTILINE_MESSAGES=true + # Optional: home channel for cron/notifications WEIXIN_HOME_CHANNEL=chat_id WEIXIN_HOME_CHANNEL_NAME=Home @@ -88,7 +91,7 @@ The adapter will restore saved credentials, connect to the iLink API, and begin - **AES-128-ECB encrypted CDN** — automatic encryption/decryption for all media transfers - **Context token persistence** — disk-backed reply continuity across restarts - **Markdown formatting** — headers, tables, and code blocks are reformatted for WeChat readability -- **Smart message chunking** — long messages are split at logical boundaries (paragraphs, code fences) +- **Smart message chunking** — messages stay as a single bubble when under the limit; only oversized payloads split at logical boundaries - **Typing indicators** — shows "typing…" status in the WeChat client while the agent processes - **SSRF protection** — outbound media URLs are validated before download - **Message deduplication** — 5-minute sliding window prevents double-processing @@ -108,6 +111,7 @@ Set these in `config.yaml` under `platforms.weixin.extra`: | `group_policy` | `disabled` | Group access: `open`, `allowlist`, `disabled` | | `allow_from` | `[]` | User IDs allowed for DMs (when dm_policy=allowlist) | | `group_allow_from` | `[]` | Group IDs allowed (when group_policy=allowlist) | +| `split_multiline_messages` | `false` | When `true`, split multi-line replies into multiple chat messages (legacy behavior). When `false`, keep multi-line replies as one message unless they exceed the length limit. | ## Access Policies @@ -211,13 +215,14 @@ WeChat's personal chat does not natively render full Markdown. The adapter refor ## Message Chunking -Long messages are split intelligently for chat delivery: +Messages are delivered as a single chat message whenever they fit within the platform limit. Only oversized payloads are split for delivery: - Maximum message length: **4000 characters** -- Split points prefer paragraph boundaries and blank lines -- Code fences are kept intact (never split mid-block) -- Indented continuation lines (sub-items in reformatted tables/lists) stay with their parent +- Messages under the limit stay intact even when they contain multiple paragraphs or line breaks +- Oversized messages split at logical boundaries (paragraphs, blank lines, code fences) +- Code fences are kept intact whenever possible (never split mid-block unless the fence itself exceeds the limit) - Oversized individual blocks fall back to the base adapter's truncation logic +- A 0.3 s inter-chunk delay prevents WeChat rate-limit drops when multiple chunks are sent ## Typing Indicators
-
-
#11571 fix(weixin): Batch-3 salvage — voice fallback, MEDIA extraction, native markdown rendering
files:
gateway/platforms/weixin.py,gateway/run.py,scripts/release.py,tests/gateway/test_weixin.py,website/docs/user-guide/messaging/weixin.mdShow the diff
-
gateway/platforms/weixin.py@@ -623,42 +623,31 @@ def _rewrite_table_block_for_weixin(lines: List[str]) -> str: def _normalize_markdown_blocks(content: str) -> str: lines = content.splitlines() result: List[str] = [] - i = 0 in_code_block = False + blank_run = 0 - while i < len(lines): - line = lines[i].rstrip() - fence_match = _FENCE_RE.match(line.strip()) - if fence_match: + for raw_line in lines: + line = raw_line.rstrip() + if _FENCE_RE.match(line.strip()): in_code_block = not in_code_block result.append(line) - i += 1 + blank_run = 0 continue if in_code_block: result.append(line) - i += 1 continue - if ( - i + 1 < len(lines) - and "|" in lines[i] - and _TABLE_RULE_RE.match(lines[i + 1].rstrip()) - ): - table_lines = [lines[i].rstrip(), lines[i + 1].rstrip()] - i += 2 - while i < len(lines) and "|" in lines[i]: - table_lines.append(lines[i].rstrip()) - i += 1 - result.append(_rewrite_table_block_for_weixin(table_lines)) + if not line.strip(): + blank_run += 1 + if blank_run <= 1: + result.append("") continue - result.append(_MARKDOWN_LINK_RE.sub(r"\1 (\2)", _rewrite_headers_for_weixin(line))) - i += 1 + blank_run = 0 + result.append(line) - normalized = "\n".join(item.rstrip() for item in result) - normalized = re.sub(r"\n{3,}", "\n\n", normalized) - return normalized.strip() + return "\n".join(result).strip() def _split_markdown_blocks(content: str) -> List[str]: @@ -704,8 +693,8 @@ def _split_delivery_units_for_weixin(content: str) -> List[str]: Weixin can render Markdown, but chat readability is better when top-level line breaks become separate messages. Keep fenced code blocks intact and - attach indented continuation lines to the previous top-level line so - transformed tables/lists do not get torn apart. + attach indented continuation lines to the previous top-level line so nested + list items do not get torn apart. """ units: List[str] = [] @@ -747,7 +736,9 @@ def _looks_like_chatty_line_for_weixin(line: str) -> bool: return False if line.startswith((" ", "\t")): return False - if stripped.startswith((">", "-", "*", "【")): + if stripped.startswith((">", "-", "*", "【", "#", "|")): + return False + if _TABLE_RULE_RE.match(stripped): return False if re.match(r"^\*\*[^*]+\*\*$", stripped): return False @@ -757,10 +748,12 @@ def _looks_like_chatty_line_for_weixin(line: str) -> bool: def _looks_like_heading_line_for_weixin(line: str) -> bool: - """Return True when a short line behaves like a plain-text heading.""" + """Return True when a short line behaves like a heading.""" stripped = line.strip() if not stripped: return False + if _HEADER_RE.match(stripped): + return True return len(stripped) <= 24 and stripped.endswith((":", ":")) @@ -1460,8 +1453,44 @@ async def send( return SendResult(success=False, error="Not connected") context_token = [redacted:secret-assignment] chat_id) last_message_id: Optional[str] = None + + # Extract MEDIA: tags and bare local file paths before text delivery. + media_files, cleaned_content = self.extract_media(content) + _, image_cleaned = self.extract_images(cleaned_content) + local_files, final_content = self.extract_local_files(image_cleaned) + + _AUDIO_EXTS = {".ogg", ".opus", ".mp3", ".wav", ".m4a"} + _VIDEO_EXTS = {".mp4", ".mov", ".avi", ".mkv", ".webm", ".3gp"} + _IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".webp", ".gif"} + + async def _deliver_media(path: str, is_voice: bool = False) -> None: + ext = Path(path).suffix.lower() + if is_voice or ext in _AUDIO_EXTS: + await self.send_voice(chat_id=chat_id, audio_path=path, metadata=metadata) + elif ext in _VIDEO_EXTS: + await self.send_video(chat_id=chat_id, video_path=path, metadata=metadata) + elif ext in _IMAGE_EXTS: + await self.send_image_file(chat_id=chat_id, image_path=path, metadata=metadata) + else: + await self.send_document(chat_id=chat_id, file_path=path, metadata=metadata) + try: - chunks = [c for c in self._split_text(self.format_message(content)) if c and c.strip()] + # Deliver extracted MEDIA: attachments first. + for media_path, is_voice in media_files: + try: + await _deliver_media(media_path, is_voice) + except Exception as exc: + logger.warning("[%s] media delivery failed for %s: %s", self.name, media_path, exc) + + # Deliver bare local file paths. + for file_path in local_files: + try: + await _deliver_media(file_path, is_voice=False) + except Exception as exc: + logger.warning("[%s] local file delivery failed for %s: %s", self.name, file_path, exc) + + # Deliver text content. + chunks = [c for c in self._split_text(self.format_message(final_content)) if c and c.strip()] for idx, chunk in enumerate(chunks): client_id = f"hermes-weixin-{uuid.uuid4().hex}" await self._send_text_chunk( @@ -1590,7 +1619,24 @@ async def send_voice( reply_to: Optional[str] = None, metadata: Optional[Dict[str, Any]] = None, ) -> SendResult: - return await self.send_document(chat_id, audio_path, caption=caption or "", metadata=metadata) + if not self._session or not self._token: + return SendResult(success=False, error="Not connected") + + # Native outbound Weixin voice bubbles are not proven-working in the + # upstream reference implementation. Prefer a reliable file attachment + # fallback so users at least receive playable audio, even for .silk. + fallback_caption = caption or "[voice message as attachment]" + try: + message_id = await self._send_file( + chat_id, + audio_path, + fallback_caption, + force_file_attachment=True, + ) + return SendResult(success=True, message_id=message_id) + except Exception as exc: + logger.error("[%s] send_voice failed to=%s: %s", self.name, _safe_id(chat_id), exc) + return SendResult(success=False, error=str(exc)) async def _download_remote_media(self, url: str) -> str: from tools.url_safety import is_safe_url @@ -1607,10 +1653,16 @@ async def _download_remote_media(self, url: str) -> str: handle.write(data) return handle.name - async def _send_file(self, chat_id: str, path: str, caption: str) -> str: + async def _send_file( + self, + chat_id: str, + path: str, + caption: str, + force_file_attachment: bool = False, + ) -> str: assert self._session is not None and self._token is not None plaintext = Path(path).read_bytes() - media_type, item_builder = self._outbound_media_builder(path) + media_type, item_builder = self._outbound_media_builder(path, force_file_attachment=force_file_attachment) filekey = secrets.token_hex(16) aes_key = secrets.token_bytes(16) rawsize = len(plaintext) @@ -1652,14 +1704,19 @@ async def _send_file(self, chat_id: str, path: str, caption: str) -> str: # Sending base64(raw_bytes) causes images to show as grey boxes on the # receiver side because the decryption key doesn't match. aes_key_for_api = base64.b64encode(aes_key.hex().encode("ascii")).decode("ascii") - media_item = item_builder( - encrypt_query_param=encrypted_query_param, - aes_key_for_api=aes_key_for_api, - ciphertext_size=len(ciphertext), - plaintext_size=rawsize, - filename=Path(path).name, - rawfilemd5=rawfilemd5, - ) + item_kwargs = { + "encrypt_query_param": encrypted_query_param, + "aes_key_for_api": aes_key_for_api, + "ciphertext_size": len(ciphertext), + "plaintext_size": rawsize, + "filename": Path(path).name, + "rawfilemd5": rawfilemd5, + } + if media_type == MEDIA_VOICE and path.endswith(".silk"): + item_kwargs["encode_type"] = 6 + item_kwargs["sample_rate"] = 24000 + item_kwargs["bits_per_sample"] = 16 + media_item = item_builder(**item_kwargs) last_message_id = None if caption: @@ -1695,7 +1752,7 @@ async def _send_file(self, chat_id: str, path: str, caption: str) -> str: ) return last_message_id - def _outbound_media_builder(self, path: str): + def _outbound_media_builder(self, path: str, force_file_attachment: bool = False): mime = mimetypes.guess_type(path)[0] or "application/octet-stream" if mime.startswith("image/"): return MEDIA_IMAGE, lambda **kw: { @@ -1723,7 +1780,7 @@ def _outbound_media_builder(self, path: str): "video_md5": kw.get("rawfilemd5", ""), }, } - if mime.startswith("audio/") or path.endswith(".silk"): + if path.endswith(".silk") and not force_file_attachment: return MEDIA_VOICE, lambda **kw: { "type": ITEM_VOICE, "voice_item": { @@ -1732,9 +1789,25 @@ def _outbound_media_builder(self, path: str): "aes_key": kw["aes_key_for_api"], "encrypt_type": 1, }, + "encode_type": kw.get("encode_type"), + "bits_per_sample": kw.get("bits_per_sample"), + "sample_rate": kw.get("sample_rate"), "playtime": kw.get("playtime", 0), }, } + if mime.startswith("audio/"): + return MEDIA_FILE, lambda **kw: { + "type": ITEM_FILE, + "file_item": { + "media": { + "encrypt_query_param": kw["encrypt_query_param"], + "aes_key": kw["aes_key_for_api"], + "encrypt_type": 1, + }, + "file_name": kw["filename"], + "len": str(kw["plaintext_size"]), + }, + } return MEDIA_FILE, lambda **kw: { "type": ITEM_FILE, "file_item": { -
gateway/run.py@@ -5811,7 +5811,7 @@ def run_sync(): pass # Send media files - for media_path in (media_files or []): + for media_path, _is_voice in (media_files or []): try: await adapter.send_document( chat_id=source.chat_id, @@ -5989,7 +5989,7 @@ def run_sync(): except Exception: pass - for media_path in (media_files or []): + for media_path, _is_voice in (media_files or []): try: await adapter.send_file(chat_id=source.chat_id, file_path=media_path) except Exception: -
scripts/release.py@@ -238,6 +238,9 @@ "[redacted:email]": "v1k22", "[redacted:email]": "RucchiZ", "[redacted:email]": "LehaoLin", + "[redacted:email]": "imink", + "[redacted:email]": "hedgeho9X", + "[redacted:email]": "bernylinville", } -
tests/gateway/test_weixin.py@@ -3,6 +3,7 @@ import asyncio import json import os +from pathlib import Path from unittest.mock import AsyncMock, patch from gateway.config import PlatformConfig @@ -23,17 +24,14 @@ def _make_adapter() -> WeixinAdapter: class TestWeixinFormatting: - def test_format_message_preserves_markdown_and_rewrites_headers(self): + def test_format_message_preserves_markdown(self): adapter = _make_adapter() content = "# Title\n\n## Plan\n\nUse **bold** and [docs](https://example.com)." - assert ( - adapter.format_message(content) - == "【Title】\n\n**Plan**\n\nUse **bold** and docs (https://example.com)." - ) + assert adapter.format_message(content) == content - def test_format_message_rewrites_markdown_tables(self): + def test_format_message_preserves_markdown_tables(self): adapter = _make_adapter() content = ( @@ -43,19 +41,14 @@ def test_format_message_rewrites_markdown_tables(self): "| Retries | 3 |\n" ) - assert adapter.format_message(content) == ( - "- Setting: Timeout\n" - " Value: 30s\n" - "- Setting: Retries\n" - " Value: 3" - ) + assert adapter.format_message(content) == content.strip() def test_format_message_preserves_fenced_code_blocks(self): adapter = _make_adapter() content = "## Snippet\n\n```python\nprint('hi')\n```" - assert adapter.format_message(content) == "**Snippet**\n\n```python\nprint('hi')\n```" + assert adapter.format_message(content) == content def test_format_message_returns_empty_string_for_none(self): adapter = _make_adapter() @@ -101,7 +94,7 @@ def test_split_text_keeps_heading_with_body_together(self): content = adapter.format_message("## 结论\n这是正文") chunks = adapter._split_text(content) - assert chunks == ["**结论**\n这是正文"] + assert chunks == ["## 结论\n这是正文"] def test_split_text_keeps_short_reformatted_table_in_single_chunk(self): adapter = _make_adapter() @@ -377,16 +370,13 @@ def test_download_remote_media_blocks_unsafe_urls(self): class TestWeixinMarkdownLinks: - """Markdown links should be converted to plaintext since WeChat can't render them.""" + """Markdown links should be preserved so WeChat can render them natively.""" - def test_format_message_converts_markdown_links_to_plain_text(self): + def test_format_message_preserves_markdown_links(self): adapter = _make_adapter() content = "Check [the docs](https://example.com) and [GitHub](https://github.com) for details" - assert ( - adapter.format_message(content) - == "Check the docs (https://example.com) and GitHub (https://github.com) for details" - ) + assert adapter.format_message(content) == content def test_format_message_preserves_links_inside_code_blocks(self): adapter = _make_adapter() @@ -500,10 +490,10 @@ def test_video_builder_includes_md5(self): ) assert item["video_item"]["video_md5"] == "deadbeef" - def test_voice_builder_for_audio_files(self): + def test_voice_builder_for_audio_files_uses_file_attachment_type(self): adapter = _make_adapter() media_type, builder = adapter._outbound_media_builder("note.mp3") - assert media_type == weixin.MEDIA_VOICE + assert media_type == weixin.MEDIA_FILE item = builder( encrypt_query_param="eq", @@ -513,8 +503,8 @@ def test_voice_builder_for_audio_files(self): filename="note.mp3", rawfilemd5="abc", ) - assert item["type"] == weixin.ITEM_VOICE - assert "voice_item" in item + assert item["type"] == weixin.ITEM_FILE + assert item["file_item"]["file_name"] == "note.mp3" def test_voice_builder_for_silk_files(self): adapter = _make_adapter() @@ -580,3 +570,75 @@ def test_send_image_file_works_without_optional_params(self, send_document_mock) caption="", metadata=None, ) + + +class TestWeixinVoiceSending: + def _connected_adapter(self) -> WeixinAdapter: + adapter = _make_adapter() + adapter._session = object() + adapter._token = "[redacted:secret-assignment]" + adapter._base_url = "https://weixin.example.com" + adapter._token_store.get = lambda account_id, chat_id: "ctx-token" + return adapter + + @patch.object(WeixinAdapter, "_send_file", new_callable=AsyncMock) + def test_send_voice_downgrades_to_document_attachment(self, send_file_mock, tmp_path): + adapter = self._connected_adapter() + source = tmp_path / "voice.ogg" + source.write_bytes(b"ogg") + send_file_mock.return_value = "msg-1" + + result = asyncio.run(adapter.send_voice("wxid_test123", str(source))) + + assert result.success is True + send_file_mock.assert_awaited_once_with( + "wxid_test123", + str(source), + "[voice message as attachment]", + force_file_attachment=True, + ) + + def test_voice_builder_for_silk_files_can_be_forced_to_file_attachment(self): + adapter = _make_adapter() + media_type, builder = adapter._outbound_media_builder( + "recording.silk", + force_file_attachment=True, + ) + assert media_type == weixin.MEDIA_FILE + + item = builder( + encrypt_query_param="eq", + aes_key_for_api="fakekey", + ciphertext_size=512, + plaintext_size=500, + filename="recording.silk", + rawfilemd5="abc", + ) + assert item["type"] == weixin.ITEM_FILE + assert item["file_item"]["file_name"] == "recording.silk" + + @patch.object(weixin, "_api_post", new_callable=AsyncMock) + @patch.object(weixin, "_upload_ciphertext", new_callable=AsyncMock) + @patch.object(weixin, "_get_upload_url", new_callable=AsyncMock) + def test_send_file_sets_voice_metadata_for_silk_payload( + self, + get_upload_url_mock, + upload_ciphertext_mock, + api_post_mock, + tmp_path, + ): + adapter = self._connected_adapter() + silk = tmp_path / "voice.silk" + silk.write_bytes(b"\x02#!SILK_V3\x01\x00") + get_upload_url_mock.return_value = {"upload_full_url": "https://cdn.example.com/upload"} + upload_ciphertext_mock.return_value = "enc-q" + api_post_mock.return_value = {"success": True} + + asyncio.run(adapter._send_file("wxid_test123", str(silk), "")) + + payload = api_post_mock.await_args.kwargs["payload"] + voice_item = payload["msg"]["item_list"][0]["voice_item"] + assert voice_item.get("playtime", 0) == 0 + assert voice_item["encode_type"] == 6 + assert voice_item["sample_rate"] == 24000 + assert voice_item["bits_per_sample"] == 16 -
website/docs/user-guide/messaging/weixin.md@@ -90,7 +90,7 @@ The adapter will restore saved credentials, connect to the iLink API, and begin - **Media support** — images, video, files, and voice messages - **AES-128-ECB encrypted CDN** — automatic encryption/decryption for all media transfers - **Context token persistence** — disk-backed reply continuity across restarts -- **Markdown formatting** — headers, tables, and code blocks are reformatted for WeChat readability +- **Markdown formatting** — preserves Markdown, including headers, tables, and code blocks, so WeChat clients that support Markdown can render it natively - **Smart message chunking** — messages stay as a single bubble when under the limit; only oversized payloads split at logical boundaries - **Typing indicators** — shows "typing…" status in the WeChat client while the agent processes - **SSRF protection** — outbound media URLs are validated before download @@ -206,12 +206,12 @@ This ensures reply continuity even after gateway restarts. ## Markdown Formatting -WeChat's personal chat does not natively render full Markdown. The adapter reformats content for better readability: +WeChat clients connected through the iLink Bot API can render Markdown directly, so the adapter preserves Markdown instead of rewriting it: -- **Headers** (`# Title`) → converted to `【Title】` (level 1) or `**Title**` (level 2+) -- **Tables** → reformatted as labeled key-value lists (e.g., `- Column: Value`) -- **Code fences** → preserved as-is (WeChat renders these adequately) -- **Excessive blank lines** → collapsed to double newlines +- **Headers** stay as Markdown headings (`#`, `##`, ...) +- **Tables** stay as Markdown tables +- **Code fences** stay as fenced code blocks +- **Excessive blank lines** are collapsed to double newlines outside fenced code blocks ## Message Chunking
-
2 PRs closed without merging
From the close comments: merged via another change 2
Closed issues · 2
How this cluster was grouped
-
Link evidence (
partly structural links): some members are connected by structural evidence (shared changed files, or a closing/superseded reference); the rest are joined by an automated relevance judgement over their text. - Members outside that structural core may belong to a different underlying problem.
-
Pairwise link density
0.26: the share of item pairs that carry a link, counting a structural link in full and a relevance-only link at 0.36.1.00means every pair is linked.