"""End-to-end voice test: TTS -> wav -> WeChat voice bubble. Usage: python voice_test.py ["文本"] ["联系人"] """ from __future__ import annotations import asyncio import json import sys import time from pathlib import Path import requests BRIDGE = Path(r"C:\Users\GuoAn\.dsh\wechat-bridge") sys.path.insert(0, str(BRIDGE)) import wechat_uia as w # noqa: E402 TTS = "http://192.168.1.17:8080/api/tts" def synthesize(text: str) -> Path: resp = requests.post(TTS, json={"text": text}, timeout=120, headers={"Content-Type": "application/json"}) resp.raise_for_status() out = BRIDGE / "tmp" / f"tts_{int(time.time())}.wav" out.parent.mkdir(parents=True, exist_ok=True) out.write_bytes(resp.content) print("tts bytes:", len(resp.content), "->", out) return out async def main() -> int: text = sys.argv[1] if len(sys.argv) > 1 else "你好呀,这是一条测试语音。" contact = sys.argv[2] if len(sys.argv) > 2 else "Always" wav = synthesize(text) from winsdk.windows.globalization import Language from winsdk.windows.media.ocr import OcrEngine events = [] async def emit(ev): events.append(ev) ch = w.WeChatChannel(emit, 8_000_000_000_000, 8_100_000_000_000) ch._ocr = OcrEngine.try_create_from_language(Language("zh-Hans-CN")) ch._ensure_window() if ch.hwnd is None: print("no main window") return 2 print("hwnd", ch.hwnd) result = await ch.send_voice(contact, str(wav)) print(json.dumps(result, ensure_ascii=False)) return 0 if result.get("status") == "ok" else 1 if __name__ == "__main__": raise SystemExit(asyncio.run(main()))