mirror of
https://github.com/qwibitai/nanoclaw.git
synced 2026-06-04 10:14:47 +08:00
fix(whatsapp): upgrade Baileys 6.7→6.17, fix proto import and 515 restart
Baileys 6.7.21 silently failed the pairing handshake. Upgrade to 6.17.16 which fixes this. Three related issues: 1. proto is no longer a named ESM export in 6.17.x — use createRequire to import via CJS (matching the proven v1 pattern). 2. Setup auth script didn't handle the 515 stream restart that WhatsApp sends after successful pairing. Refactored to reconnect (matching v1's connectSocket(isReconnect) pattern) instead of hanging until timeout. 3. Added succeeded guard and process.exit(0) to prevent timeout race after successful auth. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -46,74 +46,112 @@ npm run build
|
||||
|
||||
WhatsApp uses linked-device authentication — no API key, just a one-time pairing from your phone.
|
||||
|
||||
### Check current state
|
||||
|
||||
Check if WhatsApp is already authenticated. If `store/auth/creds.json` exists, skip to "Shared vs dedicated number".
|
||||
|
||||
```bash
|
||||
test -f store/auth/creds.json && echo "WhatsApp auth exists" || echo "No WhatsApp auth"
|
||||
```
|
||||
|
||||
### Detect environment
|
||||
|
||||
Check whether the environment is headless (no display server):
|
||||
|
||||
```bash
|
||||
[[ -z "$DISPLAY" && -z "$WAYLAND_DISPLAY" && "$OSTYPE" != darwin* ]] && echo "IS_HEADLESS=true" || echo "IS_HEADLESS=false"
|
||||
```
|
||||
|
||||
### Ask the user
|
||||
|
||||
AskUserQuestion: How do you want to authenticate WhatsApp?
|
||||
- **Pairing code** (Recommended for headless/VM) — enter a numeric code on your phone, requires phone number
|
||||
- **QR code in terminal** — displays QR code in the terminal
|
||||
Use `AskUserQuestion` to collect configuration. **Adapt auth options based on environment:**
|
||||
|
||||
If pairing code:
|
||||
If IS_HEADLESS=true AND not WSL → AskUserQuestion: How do you want to authenticate WhatsApp?
|
||||
- **Pairing code** (Recommended) - Enter a numeric code on your phone (no camera needed, requires phone number)
|
||||
- **QR code in terminal** - Displays QR code in the terminal (can be too small on some displays)
|
||||
|
||||
AskUserQuestion: What is your phone number? (Digits only — country code + number, no + prefix, spaces, or dashes. Example: 14155551234 where 1 is the US country code and 4155551234 is the phone number.)
|
||||
Otherwise (macOS, desktop Linux, or WSL) → AskUserQuestion: How do you want to authenticate WhatsApp?
|
||||
- **QR code in browser** (Recommended) - Opens a browser window with a large, scannable QR code
|
||||
- **Pairing code** - Enter a numeric code on your phone (no camera needed, requires phone number)
|
||||
- **QR code in terminal** - Displays QR code in the terminal (can be too small on some displays)
|
||||
|
||||
### Configure auth method
|
||||
If they chose pairing code:
|
||||
|
||||
For **pairing code**, set the phone number in `.env`:
|
||||
AskUserQuestion: What is your phone number? (Digits only — country code followed by your 10-digit number, no + prefix, spaces, or dashes. Example: 14155551234 where 1 is the US country code and 4155551234 is the phone number.)
|
||||
|
||||
### Clean previous auth state (if re-authenticating)
|
||||
|
||||
```bash
|
||||
grep -q WHATSAPP_PHONE_NUMBER .env 2>/dev/null || echo "WHATSAPP_PHONE_NUMBER=<their-number>" >> .env
|
||||
rm -rf store/auth/
|
||||
```
|
||||
|
||||
For **QR code**, ensure WHATSAPP_PHONE_NUMBER is NOT set (comment it out if present).
|
||||
### Run WhatsApp authentication
|
||||
|
||||
### Authenticate
|
||||
|
||||
The adapter authenticates on first startup. Restart the service:
|
||||
For QR code in browser (recommended):
|
||||
|
||||
```bash
|
||||
# Linux
|
||||
systemctl --user restart nanoclaw
|
||||
|
||||
# macOS
|
||||
launchctl kickstart -k gui/$(id -u)/com.nanoclaw
|
||||
npx tsx setup/index.ts --step whatsapp-auth -- --method qr-browser
|
||||
```
|
||||
|
||||
**Pairing code flow** — poll for the code:
|
||||
|
||||
```bash
|
||||
for i in $(seq 1 30); do [ -f data/whatsapp-pairing-code.txt ] && cat data/whatsapp-pairing-code.txt && break; sleep 1; done
|
||||
```
|
||||
(Bash timeout: 150000ms)
|
||||
|
||||
Tell the user:
|
||||
|
||||
> A browser window will open with a QR code.
|
||||
>
|
||||
> 1. Open WhatsApp > **Settings** > **Linked Devices** > **Link a Device**
|
||||
> 2. Scan the QR code in the browser
|
||||
> 3. The page will show "Authenticated!" when done
|
||||
|
||||
For QR code in terminal:
|
||||
|
||||
```bash
|
||||
npx tsx setup/index.ts --step whatsapp-auth -- --method qr-terminal
|
||||
```
|
||||
|
||||
(Bash timeout: 150000ms)
|
||||
|
||||
Tell the user:
|
||||
|
||||
> 1. Open WhatsApp > **Settings** > **Linked Devices** > **Link a Device**
|
||||
> 2. Scan the QR code displayed in the terminal
|
||||
|
||||
For pairing code:
|
||||
|
||||
Tell the user to have WhatsApp open on **Settings > Linked Devices > Link a Device**, ready to tap **"Link with phone number instead"** — the code expires in ~60 seconds and must be entered immediately.
|
||||
|
||||
Run the auth process in the background and poll `store/pairing-code.txt` for the code:
|
||||
|
||||
```bash
|
||||
rm -f store/pairing-code.txt && npx tsx setup/index.ts --step whatsapp-auth -- --method pairing-code --phone <their-phone-number> > /tmp/wa-auth.log 2>&1 &
|
||||
```
|
||||
|
||||
Then immediately poll for the code (do NOT wait for the background command to finish):
|
||||
|
||||
```bash
|
||||
for i in $(seq 1 20); do [ -f store/pairing-code.txt ] && cat store/pairing-code.txt && break; sleep 1; done
|
||||
```
|
||||
|
||||
Display the code to the user the moment it appears. Tell them:
|
||||
|
||||
> **Enter this code now** — it expires in ~60 seconds.
|
||||
>
|
||||
> 1. Open WhatsApp > **Settings** > **Linked Devices** > **Link a Device**
|
||||
> 2. Tap **Link with phone number instead**
|
||||
> 3. Enter the code immediately
|
||||
|
||||
**QR code flow** — watch logs:
|
||||
After the user enters the code, poll for authentication to complete:
|
||||
|
||||
```bash
|
||||
tail -f logs/nanoclaw.log | grep -A 30 "WhatsApp QR code"
|
||||
for i in $(seq 1 60); do grep -q 'STATUS: authenticated' /tmp/wa-auth.log 2>/dev/null && echo "authenticated" && break; grep -q 'STATUS: failed' /tmp/wa-auth.log 2>/dev/null && echo "failed" && break; sleep 2; done
|
||||
```
|
||||
|
||||
Tell the user:
|
||||
**If failed:** logged_out → delete `store/auth/` and re-run. timeout → ask user, offer retry.
|
||||
|
||||
> 1. Open WhatsApp > **Settings** > **Linked Devices** > **Link a Device**
|
||||
> 2. Scan the QR code displayed in the logs
|
||||
|
||||
### Verify authentication
|
||||
### Verify authentication succeeded
|
||||
|
||||
```bash
|
||||
test -f data/whatsapp-auth/creds.json && echo "Authentication successful" || echo "Authentication failed"
|
||||
grep "Connected to WhatsApp" logs/nanoclaw.log | tail -1
|
||||
test -f store/auth/creds.json && echo "Authentication successful" || echo "Authentication failed"
|
||||
```
|
||||
|
||||
### Shared vs dedicated number
|
||||
@@ -138,7 +176,7 @@ Otherwise, run `/manage-channels` to wire this channel to an agent group.
|
||||
|
||||
- **type**: `whatsapp`
|
||||
- **terminology**: WhatsApp calls them "groups" and "chats." A "chat" is a 1:1 DM; a "group" has multiple members.
|
||||
- **how-to-find-id**: DMs use `<phone>@s.whatsapp.net` (e.g. `14155551234@s.whatsapp.net`). Groups use `<id>@g.us`. To find your number: `node -e "const c=JSON.parse(require('fs').readFileSync('data/whatsapp-auth/creds.json','utf-8'));console.log(c.me?.id?.split(':')[0]+'@s.whatsapp.net')"`. Groups are auto-discovered — check `sqlite3 data/v2.db "SELECT platform_id, name FROM messaging_groups WHERE channel_type='whatsapp' AND is_group=1"`.
|
||||
- **how-to-find-id**: DMs use `<phone>@s.whatsapp.net` (e.g. `14155551234@s.whatsapp.net`). Groups use `<id>@g.us`. To find your number: `node -e "const c=JSON.parse(require('fs').readFileSync('store/auth/creds.json','utf-8'));console.log(c.me?.id?.split(':')[0]+'@s.whatsapp.net')"`. Groups are auto-discovered — check `sqlite3 data/v2.db "SELECT platform_id, name FROM messaging_groups WHERE channel_type='whatsapp' AND is_group=1"`.
|
||||
- **supports-threads**: no
|
||||
- **typical-use**: Interactive chat — direct messages or small groups
|
||||
- **default-isolation**: Same agent group if you're the only participant across multiple chats. Separate agent group if different people are in different groups.
|
||||
@@ -156,29 +194,43 @@ Not supported (WhatsApp linked device limitation): edit messages, delete message
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### QR code expired
|
||||
|
||||
QR codes expire after ~60 seconds. Re-run the auth command:
|
||||
|
||||
```bash
|
||||
rm -rf store/auth/ && npx tsx setup/index.ts --step whatsapp-auth -- --method qr-browser
|
||||
```
|
||||
|
||||
### Pairing code not working
|
||||
|
||||
Codes expire in ~60 seconds. Delete auth and retry:
|
||||
|
||||
```bash
|
||||
rm -rf data/whatsapp-auth/ && systemctl --user restart nanoclaw
|
||||
rm -rf store/auth/ && npx tsx setup/index.ts --step whatsapp-auth -- --method pairing-code --phone <phone>
|
||||
```
|
||||
|
||||
Ensure: digits only (no `+`), phone has internet, WhatsApp is updated.
|
||||
|
||||
If pairing code keeps failing, switch to QR-browser auth instead:
|
||||
|
||||
```bash
|
||||
rm -rf store/auth/ && npx tsx setup/index.ts --step whatsapp-auth -- --method qr-browser
|
||||
```
|
||||
|
||||
### "waiting for this message" on reactions
|
||||
|
||||
Signal sessions corrupted from rapid restarts. Clear sessions:
|
||||
|
||||
```bash
|
||||
systemctl --user stop nanoclaw
|
||||
rm data/whatsapp-auth/session-*.json
|
||||
rm store/auth/session-*.json
|
||||
systemctl --user start nanoclaw
|
||||
```
|
||||
|
||||
### Bot not responding
|
||||
|
||||
1. Auth exists: `test -f data/whatsapp-auth/creds.json`
|
||||
1. Auth exists: `test -f store/auth/creds.json`
|
||||
2. Connected: `grep "Connected to WhatsApp" logs/nanoclaw.log | tail -1`
|
||||
3. Channel wired: `sqlite3 data/v2.db "SELECT mg.platform_id, mg.name FROM messaging_groups mg JOIN messaging_group_agents mga ON mg.id=mga.messaging_group_id WHERE mg.channel_type='whatsapp'"`
|
||||
4. Service running: `systemctl --user status nanoclaw`
|
||||
|
||||
@@ -15,7 +15,7 @@ Use `AskUserQuestion` with these options:
|
||||
2. **No thanks** — description: "I'll approve each command individually as it comes up."
|
||||
3. **Show me the list first** — description: "Show me exactly which commands will be pre-approved before I decide."
|
||||
|
||||
If they pick option 1: read `.claude/skills/setup/setup-permissions.json` and use the Skill tool to invoke `update-config` with: "Add these permissions to the project settings allow list: <paste the JSON array>".
|
||||
If they pick option 1: read `.claude/skills/setup/setup-permissions.json`, then read the project settings file at `.claude/settings.json` (create it if it doesn't exist with `{}`), and directly edit it to add/merge the permissions into the `permissions.allow` array. Do NOT use the `update-config` skill.
|
||||
|
||||
If they pick option 3: read and display `.claude/skills/setup/setup-permissions.json`, then re-ask with just options 1 and 2.
|
||||
|
||||
|
||||
Generated
+773
-250
File diff suppressed because it is too large
Load Diff
+1
-1
@@ -35,7 +35,7 @@
|
||||
"@onecli-sh/sdk": "^0.3.1",
|
||||
"@resend/chat-sdk-adapter": "^0.1.1",
|
||||
"@types/qrcode": "^1.5.6",
|
||||
"@whiskeysockets/baileys": "^6.7.21",
|
||||
"@whiskeysockets/baileys": "^6.17.16",
|
||||
"better-sqlite3": "11.10.0",
|
||||
"chat": "^4.24.0",
|
||||
"chat-adapter-imessage": "^0.1.1",
|
||||
|
||||
@@ -15,6 +15,7 @@ const STEPS: Record<
|
||||
groups: () => import('./groups.js'),
|
||||
register: () => import('./register.js'),
|
||||
'pair-telegram': () => import('./pair-telegram.js'),
|
||||
'whatsapp-auth': () => import('./whatsapp-auth.js'),
|
||||
mounts: () => import('./mounts.js'),
|
||||
service: () => import('./service.js'),
|
||||
verify: () => import('./verify.js'),
|
||||
|
||||
@@ -0,0 +1,299 @@
|
||||
/**
|
||||
* Step: whatsapp-auth — standalone WhatsApp authentication.
|
||||
*
|
||||
* Supports three methods:
|
||||
* --method qr-browser Opens a local HTTP server with a large scannable QR code
|
||||
* --method qr-terminal Prints QR code in the terminal
|
||||
* --method pairing-code Requests a pairing code (requires --phone <number>)
|
||||
*
|
||||
* On success, credentials are saved to store/auth/ and the process exits.
|
||||
*/
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import http from 'http';
|
||||
import { createRequire } from 'module';
|
||||
import pino from 'pino';
|
||||
|
||||
import {
|
||||
makeWASocket,
|
||||
Browsers,
|
||||
DisconnectReason,
|
||||
fetchLatestWaWebVersion,
|
||||
makeCacheableSignalKeyStore,
|
||||
useMultiFileAuthState,
|
||||
} from '@whiskeysockets/baileys';
|
||||
import { emitStatus } from './status.js';
|
||||
|
||||
const AUTH_DIR = path.join(process.cwd(), 'store', 'auth');
|
||||
const PAIRING_CODE_FILE = path.join(process.cwd(), 'store', 'pairing-code.txt');
|
||||
const baileysLogger = pino({ level: 'silent' });
|
||||
|
||||
// proto is not available as a named ESM export — use createRequire (same as v1)
|
||||
const _require = createRequire(import.meta.url);
|
||||
const { proto } = _require('@whiskeysockets/baileys') as { proto: any };
|
||||
try {
|
||||
const _generics = _require('@whiskeysockets/baileys/lib/Utils/generics') as Record<string, unknown>;
|
||||
_generics.getPlatformId = (browser: string): string => {
|
||||
const platformType =
|
||||
proto.DeviceProps.PlatformType[browser.toUpperCase() as keyof typeof proto.DeviceProps.PlatformType];
|
||||
return platformType ? platformType.toString() : '1';
|
||||
};
|
||||
} catch {
|
||||
// QR auth still works without this patch
|
||||
}
|
||||
|
||||
type AuthMethod = 'qr-browser' | 'qr-terminal' | 'pairing-code';
|
||||
|
||||
function parseArgs(args: string[]): { method: AuthMethod; phone?: string } {
|
||||
let method: AuthMethod = 'qr-terminal';
|
||||
let phone: string | undefined;
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
switch (args[i]) {
|
||||
case '--method':
|
||||
method = args[++i] as AuthMethod;
|
||||
break;
|
||||
case '--phone':
|
||||
phone = args[++i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (method === 'pairing-code' && !phone) {
|
||||
console.error('--phone is required for pairing-code method');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
return { method, phone };
|
||||
}
|
||||
|
||||
/** Serve a web page with a large QR code. Returns cleanup function. */
|
||||
function startQrServer(port: number): {
|
||||
updateQr: (qr: string) => void;
|
||||
close: () => void;
|
||||
url: string;
|
||||
} {
|
||||
let currentQr = '';
|
||||
let waitingClients: Array<http.ServerResponse> = [];
|
||||
|
||||
const server = http.createServer((_req, res) => {
|
||||
if (_req.url === '/poll') {
|
||||
// Long-poll endpoint for QR updates
|
||||
if (currentQr) {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end(currentQr);
|
||||
} else {
|
||||
waitingClients.push(res);
|
||||
// Timeout after 30s
|
||||
setTimeout(() => {
|
||||
const idx = waitingClients.indexOf(res);
|
||||
if (idx !== -1) {
|
||||
waitingClients.splice(idx, 1);
|
||||
res.writeHead(204);
|
||||
res.end();
|
||||
}
|
||||
}, 30000);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (_req.url === '/authenticated') {
|
||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||
res.end(`<!DOCTYPE html><html><body style="display:flex;justify-content:center;align-items:center;height:100vh;margin:0;font-family:system-ui;font-size:2em;color:#22c55e">Authenticated!</body></html>`);
|
||||
return;
|
||||
}
|
||||
|
||||
res.writeHead(200, { 'Content-Type': 'text/html' });
|
||||
res.end(`<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>WhatsApp Auth</title>
|
||||
<script src="https://cdn.jsdelivr.net/npm/qrcode@1/build/qrcode.min.js"></script>
|
||||
<style>
|
||||
body { display:flex; flex-direction:column; justify-content:center; align-items:center; height:100vh; margin:0; font-family:system-ui; background:#111; color:#fff; }
|
||||
#qr { margin:2em 0; }
|
||||
canvas { border-radius: 12px; }
|
||||
.status { font-size:1.2em; opacity:0.7; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h2>Scan with WhatsApp</h2>
|
||||
<p class="status">Settings → Linked Devices → Link a Device</p>
|
||||
<div id="qr"></div>
|
||||
<p class="status" id="timer">Waiting for QR code...</p>
|
||||
<script>
|
||||
let lastQr = '';
|
||||
async function poll() {
|
||||
try {
|
||||
const res = await fetch('/poll');
|
||||
if (res.status === 200) {
|
||||
const qr = await res.text();
|
||||
if (qr && qr !== lastQr) {
|
||||
lastQr = qr;
|
||||
document.getElementById('qr').innerHTML = '';
|
||||
QRCode.toCanvas(qr, { width: 400, margin: 2 }, (err, canvas) => {
|
||||
if (!err) document.getElementById('qr').appendChild(canvas);
|
||||
});
|
||||
document.getElementById('timer').textContent = 'QR code ready — scan now';
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
setTimeout(poll, 1000);
|
||||
}
|
||||
poll();
|
||||
</script>
|
||||
</body>
|
||||
</html>`);
|
||||
});
|
||||
|
||||
server.listen(port, '127.0.0.1');
|
||||
|
||||
return {
|
||||
updateQr(qr: string) {
|
||||
currentQr = qr;
|
||||
for (const res of waitingClients) {
|
||||
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
||||
res.end(qr);
|
||||
}
|
||||
waitingClients = [];
|
||||
},
|
||||
close() {
|
||||
server.close();
|
||||
},
|
||||
url: `http://127.0.0.1:${port}`,
|
||||
};
|
||||
}
|
||||
|
||||
export async function run(args: string[]): Promise<void> {
|
||||
const { method, phone } = parseArgs(args);
|
||||
|
||||
// Clean previous auth if present
|
||||
if (fs.existsSync(path.join(AUTH_DIR, 'creds.json'))) {
|
||||
emitStatus('WHATSAPP_AUTH', {
|
||||
STATUS: 'already-authenticated',
|
||||
AUTH_DIR,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
fs.mkdirSync(AUTH_DIR, { recursive: true });
|
||||
|
||||
let qrServer: ReturnType<typeof startQrServer> | undefined;
|
||||
if (method === 'qr-browser') {
|
||||
qrServer = startQrServer(9437);
|
||||
|
||||
emitStatus('WHATSAPP_AUTH', {
|
||||
STATUS: 'qr-browser-started',
|
||||
URL: qrServer.url,
|
||||
});
|
||||
// Try to open browser
|
||||
const { exec } = await import('child_process');
|
||||
const openCmd = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'start' : 'xdg-open';
|
||||
exec(`${openCmd} ${qrServer.url}`);
|
||||
}
|
||||
|
||||
return new Promise<void>((resolve) => {
|
||||
const timeout = setTimeout(() => {
|
||||
emitStatus('WHATSAPP_AUTH', { STATUS: 'failed', ERROR: 'timeout' });
|
||||
qrServer?.close();
|
||||
process.exit(1);
|
||||
}, 120_000);
|
||||
|
||||
let succeeded = false;
|
||||
function succeed(): void {
|
||||
if (succeeded) return;
|
||||
succeeded = true;
|
||||
clearTimeout(timeout);
|
||||
try { if (fs.existsSync(PAIRING_CODE_FILE)) fs.unlinkSync(PAIRING_CODE_FILE); } catch {}
|
||||
emitStatus('WHATSAPP_AUTH', { STATUS: 'authenticated' });
|
||||
qrServer?.close();
|
||||
resolve();
|
||||
// Give a moment for creds to flush, then exit
|
||||
setTimeout(() => process.exit(0), 1000);
|
||||
}
|
||||
|
||||
async function connectSocket(isReconnect = false): Promise<void> {
|
||||
const { state, saveCreds } = await useMultiFileAuthState(AUTH_DIR);
|
||||
const { version } = await fetchLatestWaWebVersion({}).catch(() => ({ version: undefined }));
|
||||
|
||||
const sock = makeWASocket({
|
||||
version,
|
||||
auth: {
|
||||
creds: state.creds,
|
||||
keys: makeCacheableSignalKeyStore(state.keys, baileysLogger),
|
||||
},
|
||||
printQRInTerminal: false,
|
||||
logger: baileysLogger,
|
||||
browser: Browsers.macOS('Chrome'),
|
||||
});
|
||||
|
||||
// Request pairing code only on first connect (not reconnect after 515)
|
||||
if (!isReconnect && method === 'pairing-code' && phone && !state.creds.registered) {
|
||||
setTimeout(async () => {
|
||||
try {
|
||||
const code = await sock.requestPairingCode(phone);
|
||||
fs.writeFileSync(PAIRING_CODE_FILE, code, 'utf-8');
|
||||
emitStatus('WHATSAPP_AUTH', {
|
||||
STATUS: 'pairing-code-ready',
|
||||
CODE: code,
|
||||
REMINDER_TO_ASSISTANT: 'Your next user-visible message MUST include this CODE in plain text.',
|
||||
});
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
emitStatus('WHATSAPP_AUTH', { STATUS: 'failed', ERROR: message });
|
||||
process.exit(1);
|
||||
}
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
sock.ev.on('connection.update', (update) => {
|
||||
const { connection, lastDisconnect, qr } = update;
|
||||
|
||||
if (qr) {
|
||||
if (method === 'qr-browser' && qrServer) {
|
||||
qrServer.updateQr(qr);
|
||||
} else if (method === 'qr-terminal') {
|
||||
(async () => {
|
||||
try {
|
||||
const QRCode = await import('qrcode');
|
||||
const qrText = await QRCode.toString(qr, { type: 'terminal' });
|
||||
console.log('\nWhatsApp QR code — scan with WhatsApp > Linked Devices:\n');
|
||||
console.log(qrText);
|
||||
} catch {
|
||||
console.log('QR code (raw):', qr);
|
||||
}
|
||||
})();
|
||||
}
|
||||
}
|
||||
|
||||
if (connection === 'open') {
|
||||
succeed();
|
||||
sock.end(undefined);
|
||||
}
|
||||
|
||||
if (connection === 'close') {
|
||||
const reason = (lastDisconnect?.error as { output?: { statusCode?: number } })?.output?.statusCode;
|
||||
if (reason === DisconnectReason.loggedOut) {
|
||||
clearTimeout(timeout);
|
||||
emitStatus('WHATSAPP_AUTH', { STATUS: 'failed', ERROR: 'logged_out' });
|
||||
qrServer?.close();
|
||||
process.exit(1);
|
||||
} else if (reason === DisconnectReason.timedOut) {
|
||||
clearTimeout(timeout);
|
||||
emitStatus('WHATSAPP_AUTH', { STATUS: 'failed', ERROR: 'qr_timeout' });
|
||||
qrServer?.close();
|
||||
process.exit(1);
|
||||
} else if (reason === 515) {
|
||||
// 515 = stream error, happens after pairing succeeds but before
|
||||
// registration completes. Reconnect to finish the handshake.
|
||||
connectSocket(true);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
sock.ev.on('creds.update', saveCreds);
|
||||
}
|
||||
|
||||
connectSocket();
|
||||
});
|
||||
}
|
||||
@@ -20,11 +20,7 @@ export interface ChannelSetup {
|
||||
conversations: ConversationConfig[];
|
||||
|
||||
/** Called when an inbound message arrives from the platform. */
|
||||
onInbound(
|
||||
platformId: string,
|
||||
threadId: string | null,
|
||||
message: InboundMessage,
|
||||
): void | Promise<void>;
|
||||
onInbound(platformId: string, threadId: string | null, message: InboundMessage): void | Promise<void>;
|
||||
|
||||
/** Called when the adapter discovers metadata about a conversation. */
|
||||
onMetadata(platformId: string, name?: string, isGroup?: boolean): void;
|
||||
|
||||
@@ -181,7 +181,12 @@ export function createChatSdkBridge(config: ChatSdkBridgeConfig): ChannelAdapter
|
||||
// (is_group=0 short-circuits the per-thread escalation).
|
||||
chat.onDirectMessage(async (thread, message) => {
|
||||
const channelId = adapter.channelIdFromThreadId(thread.id);
|
||||
log.info('Inbound DM received', { adapter: adapter.name, channelId, sender: (message.author as any)?.fullName ?? (message.author as any)?.userId ?? 'unknown', threadId: thread.id });
|
||||
log.info('Inbound DM received', {
|
||||
adapter: adapter.name,
|
||||
channelId,
|
||||
sender: (message.author as any)?.fullName ?? (message.author as any)?.userId ?? 'unknown',
|
||||
threadId: thread.id,
|
||||
});
|
||||
await setupConfig.onInbound(channelId, thread.id, await messageToInbound(message));
|
||||
await thread.subscribe();
|
||||
});
|
||||
|
||||
@@ -310,9 +310,7 @@ export async function waitForPairing(code: string, opts: WaitForPairingOptions =
|
||||
?.slice()
|
||||
.reverse()
|
||||
.find((a) => !a.matched);
|
||||
reject(new Error(
|
||||
`Pairing ${code} invalidated by wrong code${lastMiss ? ` (${lastMiss.candidate})` : ''}`
|
||||
));
|
||||
reject(new Error(`Pairing ${code} invalidated by wrong code${lastMiss ? ` (${lastMiss.candidate})` : ''}`));
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
* getMessage fallback, outgoing queue, group metadata cache, LID mapping,
|
||||
* reconnection with backoff.
|
||||
*
|
||||
* Auth credentials persist in data/whatsapp-auth/. On first run:
|
||||
* Auth credentials persist in store/auth/. On first run:
|
||||
* - If WHATSAPP_PHONE_NUMBER is set → pairing code (printed to log)
|
||||
* - Otherwise → QR code (printed to log)
|
||||
* Subsequent restarts reuse the saved session automatically.
|
||||
@@ -24,7 +24,6 @@ import {
|
||||
makeCacheableSignalKeyStore,
|
||||
normalizeMessageContent,
|
||||
useMultiFileAuthState,
|
||||
proto,
|
||||
} from '@whiskeysockets/baileys';
|
||||
import type { GroupMetadata, WAMessageKey, WAMessage, WASocket } from '@whiskeysockets/baileys';
|
||||
|
||||
@@ -46,8 +45,10 @@ import type {
|
||||
// Fixed in Baileys 7.x but not backported. Without this, pairing codes fail with
|
||||
// "couldn't link device" because WhatsApp receives an invalid platform ID.
|
||||
// Must use createRequire — ESM `import *` creates a read-only namespace.
|
||||
// proto is not available as a named ESM export — use createRequire (same as v1)
|
||||
import { createRequire } from 'module';
|
||||
const _require = createRequire(import.meta.url);
|
||||
const { proto } = _require('@whiskeysockets/baileys') as { proto: any };
|
||||
try {
|
||||
const _generics = _require('@whiskeysockets/baileys/lib/Utils/generics') as Record<string, unknown>;
|
||||
_generics.getPlatformId = (browser: string): string => {
|
||||
@@ -63,7 +64,7 @@ try {
|
||||
|
||||
const baileysLogger = pino({ level: 'silent' });
|
||||
|
||||
const AUTH_DIR_NAME = 'whatsapp-auth';
|
||||
const AUTH_DIR = path.join(process.cwd(), 'store', 'auth');
|
||||
const GROUP_SYNC_INTERVAL_MS = 24 * 60 * 60 * 1000; // 24h
|
||||
const GROUP_METADATA_CACHE_TTL_MS = 60_000; // 1 min for outbound sends
|
||||
const SENT_MESSAGE_CACHE_MAX = 256;
|
||||
@@ -148,13 +149,13 @@ function buildMediaMessage(data: Buffer, filename: string, ext: string, caption?
|
||||
|
||||
registerChannelAdapter('whatsapp', {
|
||||
factory: () => {
|
||||
const env = readEnvFile(['WHATSAPP_PHONE_NUMBER']);
|
||||
const env = readEnvFile(['WHATSAPP_PHONE_NUMBER', 'WHATSAPP_ENABLED']);
|
||||
const phoneNumber = env.WHATSAPP_PHONE_NUMBER;
|
||||
const authDir = path.join(DATA_DIR, AUTH_DIR_NAME);
|
||||
const authDir = AUTH_DIR;
|
||||
|
||||
// Skip if no existing auth and no phone number for pairing
|
||||
// Skip if no existing auth, no phone number for pairing, and not explicitly enabled (QR mode)
|
||||
const hasAuth = fs.existsSync(path.join(authDir, 'creds.json'));
|
||||
if (!hasAuth && !phoneNumber) return null;
|
||||
if (!hasAuth && !phoneNumber && !env.WHATSAPP_ENABLED) return null;
|
||||
|
||||
fs.mkdirSync(authDir, { recursive: true });
|
||||
|
||||
@@ -173,7 +174,7 @@ registerChannelAdapter('whatsapp', {
|
||||
let flushing = false;
|
||||
|
||||
// Sent message cache for retry/re-encrypt requests
|
||||
const sentMessageCache = new Map<string, proto.IMessage>();
|
||||
const sentMessageCache = new Map<string, any>();
|
||||
|
||||
// Group metadata cache with TTL
|
||||
const groupMetadataCache = new Map<string, { metadata: GroupMetadata; expiresAt: number }>();
|
||||
@@ -197,7 +198,7 @@ registerChannelAdapter('whatsapp', {
|
||||
let rejectFirstOpen: ((err: Error) => void) | undefined;
|
||||
|
||||
// Pairing code file for the setup skill to poll
|
||||
const pairingCodeFile = path.join(DATA_DIR, 'whatsapp-pairing-code.txt');
|
||||
const pairingCodeFile = path.join(process.cwd(), 'store', 'pairing-code.txt');
|
||||
|
||||
// --- Helpers ---
|
||||
|
||||
|
||||
@@ -15,7 +15,15 @@ export interface Migration {
|
||||
up: (db: Database.Database) => void;
|
||||
}
|
||||
|
||||
const migrations: Migration[] = [migration001, migration002, migration003, migration004, migration005, migration007, migration008];
|
||||
const migrations: Migration[] = [
|
||||
migration001,
|
||||
migration002,
|
||||
migration003,
|
||||
migration004,
|
||||
migration005,
|
||||
migration007,
|
||||
migration008,
|
||||
];
|
||||
|
||||
export function runMigrations(db: Database.Database): void {
|
||||
db.exec(`
|
||||
|
||||
@@ -314,4 +314,3 @@ function safeParseContent(raw: string): { text?: string; sender?: string; sender
|
||||
return { text: raw };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user