返回 JoyAI-Echo
index.ts
1 #!/usr/bin/env node
2 /**
3 * nanobot WhatsApp Bridge
4 *
5 * This bridge connects WhatsApp Web to nanobot's Python backend
6 * via WebSocket. It handles authentication, message forwarding,
7 * and reconnection logic.
8 *
9 * Usage:
10 * npm run build && npm start
11 *
12 * Or with custom settings:
13 * BRIDGE_PORT=3001 AUTH_DIR=~/.nanobot/whatsapp npm start
14 */
15
16 // Polyfill crypto for Baileys in ESM
17 import { webcrypto } from 'crypto';
18 if (!globalThis.crypto) {
19 (globalThis as any).crypto = webcrypto;
20 }
21
22 import { BridgeServer } from './server.js';
23 import { homedir } from 'os';
24 import { join } from 'path';
25
26 const PORT = parseInt(process.env.BRIDGE_PORT || '3001', 10);
27 const AUTH_DIR = process.env.AUTH_DIR || join(homedir(), '.nanobot', 'whatsapp-auth');
28 const TOKEN = process.env.BRIDGE_TOKEN?.trim();
29
30 if (!TOKEN) {
31 console.error('BRIDGE_TOKEN is required. Start the bridge via nanobot so it can provision a local secret automatically.');
32 process.exit(1);
33 }
34
35 console.log('🐈 nanobot WhatsApp Bridge');
36 console.log('========================\n');
37
38 const server = new BridgeServer(PORT, AUTH_DIR, TOKEN);
39
40 // Handle graceful shutdown
41 process.on('SIGINT', async () => {
42 console.log('\n\nShutting down...');
43 await server.stop();
44 process.exit(0);
45 });
46
47 process.on('SIGTERM', async () => {
48 await server.stop();
49 process.exit(0);
50 });
51
52 // Start the server
53 server.start().catch((error) => {
54 console.error('Failed to start bridge:', error);
55 process.exit(1);
56 });
57
57 lines TYPESCRIPT