beecork 1.4.3 → 1.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/channels/whatsapp.js +5 -1
- package/dist/index.js +62 -29
- package/package.json +1 -1
|
@@ -32,12 +32,16 @@ export class WhatsAppChannel {
|
|
|
32
32
|
this.sttProvider = stt;
|
|
33
33
|
this.ttsProvider = tts;
|
|
34
34
|
try {
|
|
35
|
-
const { default: makeWASocket, useMultiFileAuthState, DisconnectReason, downloadMediaMessage } = await import('@whiskeysockets/baileys');
|
|
35
|
+
const { default: makeWASocket, useMultiFileAuthState, DisconnectReason, downloadMediaMessage, fetchLatestBaileysVersion } = await import('@whiskeysockets/baileys');
|
|
36
36
|
const sessionPath = this.ctx.config.whatsapp?.sessionPath ?? `${process.env.HOME}/.beecork/whatsapp-session`;
|
|
37
37
|
fs.mkdirSync(sessionPath, { recursive: true, mode: 0o700 });
|
|
38
38
|
const { state, saveCreds } = await useMultiFileAuthState(sessionPath);
|
|
39
|
+
const { version } = await fetchLatestBaileysVersion().catch(() => ({ version: undefined }));
|
|
40
|
+
const pino = (await import('pino')).default;
|
|
39
41
|
this.sock = makeWASocket({
|
|
40
42
|
auth: state,
|
|
43
|
+
version,
|
|
44
|
+
logger: pino({ level: 'silent' }),
|
|
41
45
|
});
|
|
42
46
|
const sock = this.sock;
|
|
43
47
|
sock.ev.on('creds.update', saveCreds);
|
package/dist/index.js
CHANGED
|
@@ -194,39 +194,72 @@ program
|
|
|
194
194
|
rl.close();
|
|
195
195
|
// Pair immediately — show QR code in this terminal
|
|
196
196
|
try {
|
|
197
|
-
const { default: makeWASocket, useMultiFileAuthState, DisconnectReason } = await import('@whiskeysockets/baileys');
|
|
197
|
+
const { default: makeWASocket, useMultiFileAuthState, DisconnectReason, fetchLatestBaileysVersion } = await import('@whiskeysockets/baileys');
|
|
198
|
+
const pino = (await import('pino')).default;
|
|
199
|
+
const silentLogger = pino({ level: 'silent' });
|
|
198
200
|
fs.mkdirSync(sessionPath, { recursive: true, mode: 0o700 });
|
|
199
|
-
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
201
|
+
let attempts = 0;
|
|
202
|
+
const maxAttempts = 5;
|
|
203
|
+
let paired = false;
|
|
204
|
+
const connect = async () => {
|
|
205
|
+
attempts++;
|
|
206
|
+
const { state, saveCreds } = await useMultiFileAuthState(sessionPath);
|
|
207
|
+
const { version } = await fetchLatestBaileysVersion().catch(() => ({ version: undefined }));
|
|
208
|
+
const sock = makeWASocket({ auth: state, version, logger: silentLogger });
|
|
209
|
+
sock.ev.on('creds.update', saveCreds);
|
|
210
|
+
sock.ev.on('connection.update', async (update) => {
|
|
211
|
+
if (update.qr) {
|
|
212
|
+
try {
|
|
213
|
+
const qrcodeTerminal = await import('qrcode-terminal');
|
|
214
|
+
(qrcodeTerminal.default || qrcodeTerminal).generate(update.qr, { small: true });
|
|
215
|
+
console.log('Scan the QR code above with your phone (WhatsApp → Linked Devices → Link a Device)\n');
|
|
216
|
+
}
|
|
217
|
+
catch {
|
|
218
|
+
console.log('QR data:', update.qr);
|
|
219
|
+
}
|
|
209
220
|
}
|
|
210
|
-
|
|
211
|
-
|
|
221
|
+
if (update.connection === 'open') {
|
|
222
|
+
paired = true;
|
|
223
|
+
console.log('✓ WhatsApp paired successfully!');
|
|
224
|
+
sock.end(undefined);
|
|
225
|
+
// Auto-restart daemon
|
|
226
|
+
const { getDaemonPid } = await import('./cli/helpers.js');
|
|
227
|
+
const pid = getDaemonPid();
|
|
228
|
+
if (pid) {
|
|
229
|
+
console.log(' Restarting daemon with WhatsApp enabled...');
|
|
230
|
+
const { execSync } = await import('node:child_process');
|
|
231
|
+
try {
|
|
232
|
+
execSync('beecork stop', { stdio: 'ignore' });
|
|
233
|
+
execSync('beecork start', { stdio: 'ignore' });
|
|
234
|
+
console.log(' ✓ Daemon restarted.\n');
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
console.log(' Could not restart daemon. Run: beecork stop && beecork start\n');
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
console.log(' Start the daemon: beecork start\n');
|
|
242
|
+
}
|
|
243
|
+
process.exit(0);
|
|
212
244
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
245
|
+
if (update.connection === 'close') {
|
|
246
|
+
if (paired)
|
|
247
|
+
return; // Expected disconnect after pairing
|
|
248
|
+
const reason = update.lastDisconnect?.error?.output?.statusCode;
|
|
249
|
+
if (reason === DisconnectReason.loggedOut) {
|
|
250
|
+
console.log('\n✗ WhatsApp logged out. Please try again.\n');
|
|
251
|
+
process.exit(1);
|
|
252
|
+
}
|
|
253
|
+
if (attempts >= maxAttempts) {
|
|
254
|
+
console.log(`\n✗ Could not connect after ${maxAttempts} attempts. Please try again later.\n`);
|
|
255
|
+
process.exit(1);
|
|
256
|
+
}
|
|
257
|
+
setTimeout(connect, 3000);
|
|
225
258
|
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
console.log('Waiting for
|
|
259
|
+
});
|
|
260
|
+
};
|
|
261
|
+
await connect();
|
|
262
|
+
console.log('Waiting for QR code... (Ctrl+C to cancel)\n');
|
|
230
263
|
}
|
|
231
264
|
catch (err) {
|
|
232
265
|
console.error('Failed to connect to WhatsApp:', err instanceof Error ? err.message : err);
|