beecork 1.4.3 → 1.4.4
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 +3 -1
- package/dist/index.js +38 -27
- package/package.json +1 -1
|
@@ -32,12 +32,14 @@ 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 }));
|
|
39
40
|
this.sock = makeWASocket({
|
|
40
41
|
auth: state,
|
|
42
|
+
version,
|
|
41
43
|
});
|
|
42
44
|
const sock = this.sock;
|
|
43
45
|
sock.ev.on('creds.update', saveCreds);
|
package/dist/index.js
CHANGED
|
@@ -194,37 +194,48 @@ 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
198
|
fs.mkdirSync(sessionPath, { recursive: true, mode: 0o700 });
|
|
199
|
-
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
199
|
+
let attempts = 0;
|
|
200
|
+
const maxAttempts = 5;
|
|
201
|
+
const connect = async () => {
|
|
202
|
+
attempts++;
|
|
203
|
+
const { state, saveCreds } = await useMultiFileAuthState(sessionPath);
|
|
204
|
+
const { version } = await fetchLatestBaileysVersion().catch(() => ({ version: undefined }));
|
|
205
|
+
const sock = makeWASocket({ auth: state, version });
|
|
206
|
+
sock.ev.on('creds.update', saveCreds);
|
|
207
|
+
sock.ev.on('connection.update', async (update) => {
|
|
208
|
+
if (update.qr) {
|
|
209
|
+
try {
|
|
210
|
+
const qrcodeTerminal = await import('qrcode-terminal');
|
|
211
|
+
(qrcodeTerminal.default || qrcodeTerminal).generate(update.qr, { small: true });
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
console.log('QR data:', update.qr);
|
|
215
|
+
}
|
|
209
216
|
}
|
|
210
|
-
|
|
211
|
-
console.log('
|
|
217
|
+
if (update.connection === 'open') {
|
|
218
|
+
console.log('\n✓ WhatsApp paired successfully!');
|
|
219
|
+
console.log(' You can now start the daemon: beecork start\n');
|
|
220
|
+
sock.end(undefined);
|
|
221
|
+
process.exit(0);
|
|
212
222
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
console.log(
|
|
224
|
-
|
|
223
|
+
if (update.connection === 'close') {
|
|
224
|
+
const reason = update.lastDisconnect?.error?.output?.statusCode;
|
|
225
|
+
if (reason === DisconnectReason.loggedOut) {
|
|
226
|
+
console.log('\n✗ WhatsApp logged out. Please try again.\n');
|
|
227
|
+
process.exit(1);
|
|
228
|
+
}
|
|
229
|
+
if (attempts >= maxAttempts) {
|
|
230
|
+
console.log(`\n✗ Could not connect after ${maxAttempts} attempts. Please try again later.\n`);
|
|
231
|
+
process.exit(1);
|
|
232
|
+
}
|
|
233
|
+
console.log(`Connection dropped, retrying (${attempts}/${maxAttempts})...`);
|
|
234
|
+
setTimeout(connect, 3000);
|
|
225
235
|
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
236
|
+
});
|
|
237
|
+
};
|
|
238
|
+
await connect();
|
|
228
239
|
console.log('Scan the QR code above with your phone (WhatsApp → Linked Devices → Link a Device)');
|
|
229
240
|
console.log('Waiting for pairing... (Ctrl+C to cancel)\n');
|
|
230
241
|
}
|