pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
15.21k stars 3.63k forks source link

Bug after read QR closing script and rep #2848

Closed firsttabz closed 6 months ago

firsttabz commented 6 months ago

Is there an existing issue for this?

Describe the bug

I explain them, when I finish reading the QR everything works fine and if I continue running the script without interruptions everything works fine. The problem is when I restart that script and it tries to reactivate the WhatsApp session (the api does it automatically, I have the following error)

Error on WINDOWS 11 C:\Users\altab\pro\node\cbus-whaserver\node_modules\whatsapp-web.js\src\webCache\LocalWebCache.js:34 const version = indexHtml.match(/manifest-([\d\.]+).json/)[1]; ^

TypeError: Cannot read properties of null (reading '1') at LocalWebCache.persist (C:\Users\altab\pro\node\cbus-whaserver\node_modules\whatsapp-web.js\src\webCache\LocalWebCache.js:34:69) at C:\Users\altab\pro\node\cbus-whaserver\node_modules\whatsapp-web.js\src\Client.js:744:36 at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v20.9.0

Error on UBUNTU

(node:3469330) UnhandledPromiseRejectionWarning: TypeError: Cannot read property '1' of null at LocalWebCache.persist (/home/alejandro/cbus-whaserver/node_modules/whatsapp-web.js/src/webCache/LocalWebCache.js:34:69) at /home/alejandro/cbus-whaserver/node_modules/whatsapp-web.js/src/Client.js:744:36 at processTicksAndRejections (internal/process/task_queues.js:95:5) (Use node --trace-warnings ... to show where the warning was created) (node:3469330) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (node:3469330) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Expected behavior

Reopen the whatsapp sessión normaly

Steps to Reproduce the Bug or Issue

  1. Start script
  2. Read QR
  3. Wait 3-5 minutes
  4. Stop script
  5. Start again the script
  6. Wait for the error

Relevant Code

const client = new Client({ authStrategy: new LocalAuth({ clientId: "client-"+process.env.TUNNEL_SUBDOMAIN }) });

client.on('qr', (qr) => { console.log("generando Qr") QRCode.toDataURL(qr, function (err, url) { createFilePNG(url); showQR(); }); //console.log('QR RECEIVED', qr); qrcode.generate(qr, {small: true});

});

client.on('ready', () => { console.log('Client is ready!'); });

client.on('disconnected', () => { console.log('DISCONNECTED'); ACTIVE_SERVICE = false; sendNotification('ERROR', 'Reconecta la sesión de Whatsapp de nuevo.'); });

client.on('change_state', (change_state) => { console.log("change_state", change_state); if(change_state == 'TIMEOUT' /|| change_state == ''/) { //process.exit(0); ACTIVE_SERVICE = false; if(DEVICE_NUMBER == null) { cleanSession(); } } else if(change_state == 'OPENING') { ACTIVE_SERVICE = false; } else if(change_state == 'CONNECTED') { ACTIVE_SERVICE = true; } });

client.on('authenticated', () => { // Save the session object however you prefer. // Convert it to json, save it to a file, store it in a database... console.log("Estamos autenticados"); setTimeout(async () => { DEVICE_NUMBER = client.info.wid.user; console.log((client.info.wid.user)); //console.log(await (client.getFormattedNumber())); sendNotification('SESIÓN ACTIVA', 'La cuenta '+DEVICE_NUMBER+' esta activa.'); client.sendMessage("5219585846497@c.us", 'Sesión activa'); //5219581864341@c.us

    deleteWQR();
    reqMessages();

}, 5000);
ACTIVE_SERVICE = true;

});

client.on('message', message => { console.log("recibimos mensaje...") if(message.body === 'ping!' || message.body === 'Ping!') { client.sendMessage(message.from, 'pong'); console.log(message.from); } else { //client.sendMessage(message.from, 'No puedo responder, soy un bot de notificaciones. Contacta a la empresa.'); } });

client.initialize();

Browser Type

Chromium

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

No, I am not using Multi Device

Environment

WINDOWS node v20.9.0 UBUNTU v14.21.3

"dependencies": { "asciify-image": "^0.1.10", "axios": "^0.27.2", "body-parser": "^1.20.1", "console-title": "^1.1.0", "dotenv": "^16.0.3", "express": "^4.18.2", "express-rate-limit": "^6.6.0", "firebase": "^9.13.0", "firebase-admin": "^10.3.0", "localtunnel": "^2.0.2", "node-notifier": "^10.0.1", "open": "^8.4.0", "pm2": "^5.3.1", "qrcode": "^1.5.1", "qrcode-terminal": "^0.12.0", "whatsapp-web.js": "^1.23.0" },

Additional context

image

firsttabz commented 6 months ago

I saw de problem in the method persis() in node_modules\whatsapp-web.js\src\webCache\LocalWebCache.js

async persist(indexHtml) {
    // extract version from index (e.g. manifest-2.2206.9.json -> 2.2206.9)
    const version =  indexHtml.match(/manifest-([\d\\.]+)\.json/)[1];

    console.log('version', version )

    if(!version) return;

    const filePath = path.join(this.path, `${version}.html`);
    fs.mkdirSync(this.path, { recursive: true });
    fs.writeFileSync(filePath, indexHtml);
}

The problem occurs after read read the QR , close script and re-run the script, in the re-run, the version cannot be retrived.

I hope it helps

pavov commented 6 months ago

Same prolem for me. whatsapp-web.js have been working fine for months. This problem started today.

Help @pedroslopez 🙏

TechnVision commented 6 months ago

I don't know if I should recommend it or not but works for me,

npm i whatsapp-web.js@1.23.1-alpha.5 @pavov @firsttabz

firsttabz commented 6 months ago

@TechnVision, it didn't work for me... the same problem

"dependencies": { "asciify-image": "^0.1.10", "axios": "^0.27.2", "body-parser": "^1.20.1", "console-title": "^1.1.0", "dotenv": "^16.0.3", "express": "^4.18.2", "express-rate-limit": "^6.6.0", "firebase": "^9.13.0", "firebase-admin": "^10.3.0", "localtunnel": "^2.0.2", "node-notifier": "^10.0.1", "open": "^8.4.0", "pm2": "^5.3.1", "qrcode": "^1.5.1", "qrcode-terminal": "^0.12.0", "whatsapp-web.js": "^1.23.1-alpha.5" <--------------- },

TechnVision commented 6 months ago

hance

I don't know if I should recommend it

sorry

cantodelobo commented 6 months ago

Something new? :(

pavov commented 6 months ago

I don't know if I should recommend it or not but works for me,

npm i whatsapp-web.js@1.23.1-alpha.5 @pavov @firsttabz

Did´n work with: whatsapp-web.js@1.23.1-alpha.5

If you erase your .wwebjs_cache folder the problem will ocur when trying initializing the client.

== The problem is in LocalWebCache.js: const version = indexHtml.match(/manifest-([\d\.]+).json/)[1]; "Uncaught TypeError: Cannot read properties of null (reading '1')"

The Index.html from server doesn´t have the manifest. json string anymore like the code above (don´t match the regular expression).

@pedroslopez if you can lead us to any "alternative" or "workaround" it would be very helpful

ramonmariano87 commented 6 months ago

Same problem for me.

This problem started today.

@pedroslopez

alechkos commented 6 months ago

2789