From cb26732559a3f1811e71c4d8e556e37b425df8af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jim=20Liu=20=E5=AE=9D=E7=8E=89?= Date: Thu, 14 May 2026 01:45:58 -0500 Subject: [PATCH] refactor(baoyu-post-to-wechat): harden Telegram QR notification - Add 10s timeout to Telegram fetch so unreachable api.telegram.org doesn't block waitForLogin indefinitely. - Move 2s QR-render wait inside sendQrToTelegram, after the env-var early return, so the no-op path doesn't pay the delay. - Use viewport screenshot (captureBeyondViewport: false) as fallback to reduce payload size and keep the QR scannable. --- skills/baoyu-post-to-wechat/scripts/wechat-article.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/skills/baoyu-post-to-wechat/scripts/wechat-article.ts b/skills/baoyu-post-to-wechat/scripts/wechat-article.ts index 5cbd4f7..e0c5198 100644 --- a/skills/baoyu-post-to-wechat/scripts/wechat-article.ts +++ b/skills/baoyu-post-to-wechat/scripts/wechat-article.ts @@ -36,6 +36,9 @@ async function sendQrToTelegram(session: ChromeSession): Promise { const chatId = process.env.TELEGRAM_CHAT_ID; if (!botToken || !chatId) return; + // Wait for QR to render before extracting + await sleep(2000); + try { // Try to extract QR image from DOM first (avoids full-page screenshot noise) const domResult = await session.cdp.send<{ result: { value: string } }>('Runtime.evaluate', { @@ -85,9 +88,9 @@ async function sendQrToTelegram(session: ChromeSession): Promise { }, { sessionId: session.sessionId }); imgBuffer = Buffer.from((inBrowserFetch.result.value as string) ?? '', 'base64'); } else { - // Fallback: full-page screenshot + // Fallback: viewport screenshot (smaller than full-page; QR is usually in viewport) const screenshotResp = await session.cdp.send<{ data: string }>( - 'Page.captureScreenshot', { format: 'png' }, { sessionId: session.sessionId } + 'Page.captureScreenshot', { format: 'png', captureBeyondViewport: false }, { sessionId: session.sessionId } ); imgBuffer = Buffer.from(screenshotResp.data ?? '', 'base64'); } @@ -104,6 +107,7 @@ async function sendQrToTelegram(session: ChromeSession): Promise { method: 'POST', headers: { 'Content-Type': `multipart/form-data; boundary=${boundary}` }, body: Buffer.concat(parts), + signal: AbortSignal.timeout(10_000), }); const tgJson = await tgResp.json() as { ok: boolean; description?: string }; if (tgJson.ok) { @@ -117,8 +121,7 @@ async function sendQrToTelegram(session: ChromeSession): Promise { } async function waitForLogin(session: ChromeSession, timeoutMs = 120_000): Promise { - // Wait for QR to render, then notify via Telegram if configured - await sleep(2000); + // Notify via Telegram if configured (no-op when env vars absent) await sendQrToTelegram(session); const start = Date.now(); while (Date.now() - start < timeoutMs) {