92 lines
2.8 KiB
Python
92 lines
2.8 KiB
Python
"""One-shot end-to-end WeChat voice send (open chat -> talk -> play -> release)."""
|
|
import asyncio
|
|
import ctypes
|
|
import glob
|
|
import os
|
|
import sys
|
|
import time
|
|
from ctypes import wintypes
|
|
|
|
sys.path.insert(0, r"C:\Users\GuoAn\.dsh\wechat-bridge")
|
|
import wechat_uia as w # noqa: E402
|
|
import wx_audio # noqa: E402
|
|
from winsdk.windows.globalization import Language # noqa: E402
|
|
from winsdk.windows.media.ocr import OcrEngine # noqa: E402
|
|
|
|
TMP = r"C:\Users\GuoAn\AppData\Local\Temp"
|
|
user32 = ctypes.WinDLL("user32", use_last_error=True)
|
|
|
|
|
|
class MOUSEINPUT(ctypes.Structure):
|
|
_fields_ = [("dx", wintypes.LONG), ("dy", wintypes.LONG), ("mouseData", wintypes.DWORD),
|
|
("dwFlags", wintypes.DWORD), ("time", wintypes.DWORD),
|
|
("dwExtraInfo", ctypes.POINTER(ctypes.c_ulong))]
|
|
|
|
|
|
class INPUT(ctypes.Structure):
|
|
class _I(ctypes.Union):
|
|
_fields_ = [("mi", MOUSEINPUT)]
|
|
|
|
_anonymous_ = ("i",)
|
|
_fields_ = [("type", wintypes.DWORD), ("i", _I)]
|
|
|
|
|
|
def mouse(flag):
|
|
inp = INPUT()
|
|
inp.type = 0
|
|
inp.mi = MOUSEINPUT(0, 0, 0, flag, 0, None)
|
|
return user32.SendInput(1, ctypes.byref(inp), ctypes.sizeof(INPUT))
|
|
|
|
|
|
async def main():
|
|
wavs = sorted(glob.glob(r"C:\Users\GuoAn\.dsh\wechat-bridge\tmp\tts_*.wav"), key=os.path.getmtime)
|
|
wav = wavs[-1]
|
|
ch = w.WeChatChannel(lambda e: None, 8_000_000_000_000, 8_100_000_000_000)
|
|
ch._ocr = OcrEngine.try_create_from_language(Language("zh-Hans-CN"))
|
|
ch._ensure_window()
|
|
hwnd = ch.hwnd
|
|
print("hwnd", hwnd)
|
|
|
|
img = await ch.snapshot()
|
|
rows = await ch.chat_rows(img)
|
|
target = next((r for r in rows if r["name"] == "文件传输助手"), None)
|
|
if not target:
|
|
print("row missing", [r["name"] for r in rows])
|
|
return
|
|
ch._click(hwnd, min(ch._geo(img)["list_w"] - 20, target["x"] + 10), target["y"])
|
|
await asyncio.sleep(1.4)
|
|
img = await ch.snapshot()
|
|
img.save(rf"{TMP}\e2e1.png")
|
|
print("pane lines:", len(await ch.read_pane(img)))
|
|
|
|
for attempt in range(3):
|
|
mx, my = ch._mic_point(img)
|
|
ch._click(hwnd, mx, my)
|
|
await asyncio.sleep(1.0)
|
|
img = await ch.snapshot()
|
|
txt = await ch.input_text(img)
|
|
print("attempt", attempt, "input:", repr(txt))
|
|
if "长按" in txt or "鼠标" in txt:
|
|
break
|
|
|
|
g = ch._geo(img)
|
|
bx = (g["pane_x0"] + g["pane_right"]) // 2
|
|
by = int(g["h"] * 0.86)
|
|
print("press at", bx, by)
|
|
w.real_down(hwnd, bx, by)
|
|
await asyncio.sleep(1.0)
|
|
mid = await ch.snapshot()
|
|
mid.save(rf"{TMP}\e2e2.png")
|
|
print("mid input:", repr(await ch.input_text(mid)))
|
|
dur = wx_audio.play_wav(wav, None, True)
|
|
print("played", dur)
|
|
await asyncio.sleep(1.8)
|
|
w.real_up()
|
|
await asyncio.sleep(2.0)
|
|
img = await ch.snapshot()
|
|
img.save(rf"{TMP}\e2e3.png")
|
|
print("done")
|
|
|
|
|
|
asyncio.run(main())
|