From 02e1c8645b32eb87f6732f8611ebd3be19d231b1 Mon Sep 17 00:00:00 2001 From: Artur Do Lago Date: Sat, 10 Jan 2026 09:36:18 +0100 Subject: [PATCH] fix(gateway): save WhatsApp QR code as PNG file for reliable scanning Terminal QR codes can be distorted by font size/line height. Save QR as PNG file that can be opened with an image viewer for reliable scanning. Co-Authored-By: Claude Opus 4.5 --- packages/agent-core/src/gateway/whatsapp.ts | 32 ++++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/packages/agent-core/src/gateway/whatsapp.ts b/packages/agent-core/src/gateway/whatsapp.ts index c050a892598..81c51bca418 100644 --- a/packages/agent-core/src/gateway/whatsapp.ts +++ b/packages/agent-core/src/gateway/whatsapp.ts @@ -117,11 +117,35 @@ export namespace WhatsAppGateway { }) // QR Code event - user needs to scan - this.client.on("qr", (qr: string) => { + this.client.on("qr", async (qr: string) => { log.info("WhatsApp QR code received - scan with phone") - console.log("\n=== SCAN THIS QR CODE WITH WHATSAPP ===\n") - qrcode.default.generate(qr, { small: true }) - console.log("\n========================================\n") + + // Save QR code as PNG file for proper scanning + const qrImagePath = path.join(sessionDir, "whatsapp-qr.png") + try { + // @ts-ignore - qrcode is an optional dependency + const QRCodeModule = await import("qrcode") + // Handle both ESM default export and CommonJS + const QRCode = QRCodeModule.default || QRCodeModule + await QRCode.toFile(qrImagePath, qr, { + type: "png", + width: 400, + margin: 2, + color: { dark: "#000000", light: "#ffffff" }, + }) + console.log("\n=== SCAN THIS QR CODE WITH WHATSAPP ===") + console.log(`\nQR Code saved to: ${qrImagePath}`) + console.log("\nOpen this file with an image viewer and scan it with WhatsApp.") + console.log("Or run: xdg-open " + qrImagePath) + console.log("\n========================================\n") + } catch (e) { + // Fallback to terminal output + console.error("PNG QR generation failed:", e) + console.log("\n=== SCAN THIS QR CODE WITH WHATSAPP ===\n") + console.log("(Terminal QR may be distorted - try zooming out or use a smaller font)\n") + qrcode.default.generate(qr, { small: true }) + console.log("\n========================================\n") + } }) // Ready event