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.46k stars 3.68k forks source link

QR code genration taking much time and other event listener not working #2586

Closed getsettalk closed 11 months ago

getsettalk commented 1 year ago

Is there an existing issue for this?

Describe the bug

I'm genrating request to make qr code to scann it , but its taking much longer time

here is my full code please check once :

const express = require('express');
const { Client } = require('whatsapp-web.js');
const qrcode = require('qrcode');
const fs = require('fs');

const app = express();
const port = 3000;
const SESSION_FILE_PATH = 'YourSessionName.json';

let client;

// Initialize WhatsApp client
if (fs.existsSync(SESSION_FILE_PATH)) {
  // If a session file exists, load the session
  const sessionData = require(SESSION_FILE_PATH);
  client = new Client({ session: sessionData });
} else {
  client = new Client();
}

// QR Code generation and display
app.get('/', (req, res) => {
  if (!client.session) {
    client.on('qr', (qrCode) => {
      qrcode.toFileStream(res, qrCode);
    });
    client.initialize();
  } else {
    res.send('WhatsApp Web is already connected.');
  }
});

// Event listener for incoming messages
client.on('message', (message) => {
  // Handle incoming message
  console.log(`Received message from ${message.from}: ${message.body}`);

  // Send a reply
  client.sendMessage(message.from, 'Your reply goes here');
});

// Event listener for authentication
client.on('authenticated', (session) => {
  console.log('Authenticated!');
  fs.writeFileSync(SESSION_FILE_PATH, JSON.stringify(session));
});

// Start the Express server
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

// Start the WhatsApp client
client.initialize();

When we scanned than this code not showing console:

client.on('authenticated', (session) => {
  console.log('Authenticated!');
  fs.writeFileSync(SESSION_FILE_PATH, JSON.stringify(session));
});

Expected behavior

Qr code should be genrated fast

Steps to Reproduce the Bug or Issue

i have provide above full code , use that and run here is my package.json

{
  "name": "whatsappweb",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^1.5.1",
    "ejs": "^3.1.9",
    "express": "^4.18.2",
    "qrcode": "^1.5.3",
    "qrcode-terminal": "^0.12.0",
    "whatsapp-web.js": "^1.22.1"
  }
}

Relevant Code

No response

Browser Type

Google Chrome

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

No, I am not using Multi Device

Environment

OS: windows Phone OS: Android whatsapp-web.js version : ^1.22.1 Nodejs: ^18

Additional context

in my laptop have 4gb RAM and 256 GB SSD (Compac book)

getsettalk commented 1 year ago

i forget to tell you that other event lister not working when qr code scanned but ready or authenticated event not triggered,

  client.on('authenticated', (session) => {
        // Save the session data to a file for future use
        fs.writeFileSync(SESSION_FILE_PATH, JSON.stringify(session));
        console.log("authenticated")
        // Respond to the client
        res.send('Authentication successful! You can now close this page.');
    });

    client.on('ready', () => {
        console.log('Client is ready!');
    });
Anshuhk commented 1 year ago

Same Issue I am facing. How much time it will take to fix it

themazim commented 1 year ago

Have you tried using the last main branch version which is currenltly in alpha instead?

There a re quite a lot of fixes implemented and it works on my end.

npm i whatsapp-web.js#main

Anshuhk commented 1 year ago

Not working showing error "Invalid tag name "whatsapp-web.js#main" of package "whatsapp-web.js#main": Tags may not have any characters that encodeURIComponent encodes."

themazim commented 1 year ago

try this instead :

npm i https://github.com/pedroslopez/whatsapp-web.js#main

Anshuhk commented 1 year ago

same error

themazim commented 1 year ago

Please check your npm version and update. Should work fine:

> npm i https://github.com/pedroslopez/whatsapp-web.js#main

up to date, audited 486 packages in 3s

29 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
Anshuhk commented 1 year ago

my npm version is 9.5.1

themazim commented 1 year ago

well, last resort would be to add --force but I would strongly suggest you check what's up with your npm installation.

Anshuhk commented 1 year ago

can u guide me throw remotly?

Anshuhk commented 1 year ago

// Event listener for authentication client.on('authenticated', (session) => { console.log('Authenticated!'); fs.writeFileSync(SESSION_FILE_PATH, JSON.stringify(session)); });

this is not working showing undefined

getsettalk commented 1 year ago

// Event listener for authentication client.on('authenticated', (session) => { console.log('Authenticated!'); fs.writeFileSync(SESSION_FILE_PATH, JSON.stringify(session)); });

this is not working showing undefined

Yeah its not working .... always come undefined under session variable

getsettalk commented 1 year ago

try this instead :

npm i https://github.com/pedroslopez/whatsapp-web.js#main

i have tryed many ways but alwaus come undefined

Anshuhk commented 1 year ago

Any Solution for this

megaatacadao commented 1 year ago

I have the same issue,

jgHax commented 1 year ago

Same issue here :(

getsettalk commented 1 year ago

I think this repo is deprecated

On Mon, 16 Oct 2023, 6:10 pm jgHax, @.***> wrote:

Same issue here :(

— Reply to this email directly, view it on GitHub https://github.com/pedroslopez/whatsapp-web.js/issues/2586#issuecomment-1764387515, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALY3KNABKR44JCAZZ27CY33X7UTKVAVCNFSM6AAAAAA576UCFWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRUGM4DONJRGU . You are receiving this because you authored the thread.Message ID: @.***>

manuel-marg commented 1 year ago

To fix the issue, follow the steps below. First, run the following command in your terminal:

npm uninstall whatsapp-web.js

Then, in the package.json file, in the dependencies, write the following:

"whatsapp-web.js": "https://github.com/pedroslopez/whatsapp-web.js/tarball/main"

Finally, run the following command in the terminal:

npm install

I hope these suggestions are useful to you. In this way I managed to solve this problem.

N0N4M3BNS commented 1 year ago

To fix the issue, follow the steps below. First, run the following command in your terminal:

npm uninstall whatsapp-web.js

Then, in the package.json file, in the dependencies, write the following:

"whatsapp-web.js": "https://github.com/pedroslopez/whatsapp-web.js/tarball/main"

Finally, run the following command in the terminal:

npm install

I hope these suggestions are useful to you. In this way I managed to solve this problem.

I have problem to receive the message, I try you suggestion but still not work.

getsettalk commented 1 year ago

This method is not working.

On Mon, 16 Oct 2023, 9:27 pm Manuel, @.***> wrote:

To fix the issue, follow the steps below. First, run the following command in your terminal:

npm uninstall whatsapp-web.js

Then, in the package.json file, in the dependencies, write the following:

"whatsapp-web.js": " https://github.com/pedroslopez/whatsapp-web.js/tarball/main"

Finally, run the following command in the terminal:

npm install

I hope these suggestions are useful to you. In this way I managed to solve this problem.

— Reply to this email directly, view it on GitHub https://github.com/pedroslopez/whatsapp-web.js/issues/2586#issuecomment-1764796117, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALY3KNFWCAZHB5ARR7XPH5DX7VKPNAVCNFSM6AAAAAA576UCFWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRUG44TMMJRG4 . You are receiving this because you authored the thread.Message ID: @.***>

fcostaprojects commented 1 year ago

same issue, not ready

mrsawy commented 1 year ago

SAMEEEEEEE

Diegocndd commented 1 year ago

same error, but themazim solution worked here

EinGeist commented 1 year ago

had the same issue followed @themazim solution and it worked I first uninstalled whatsapp-web: npm uninstall whatsapp-web.js then reinstall it: npm i https://github.com/pedroslopez/whatsapp-web.js#main

getsettalk commented 1 year ago

Ok, I'll check it out.

On Sat, 21 Oct 2023, 2:09 am EinGeist, @.***> wrote:

had the same issue followed @themazim https://github.com/themazim solution and it worked I first uninstalled whatsapp-web: npm uninstall whatsapp-web.js then reinstall it: npm i https://github.com/pedroslopez/whatsapp-web.js#main

— Reply to this email directly, view it on GitHub https://github.com/pedroslopez/whatsapp-web.js/issues/2586#issuecomment-1773365926, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALY3KNARL4R6WD4U7YOL6HTYALOQZAVCNFSM6AAAAAA576UCFWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZTGM3DKOJSGY . You are receiving this because you authored the thread.Message ID: @.***>

getsettalk commented 1 year ago

had the same issue followed @themazim solution and it worked I first uninstalled whatsapp-web: npm uninstall whatsapp-web.js then reinstall it: npm i https://github.com/pedroslopez/whatsapp-web.js#main

Still not getting session , see here code and screenshot: index.js

   client.on('authenticated', (session) => {
        if (session) {
            // Save the session data to a file for future use
            console.log('Authentication successful. Session data saved.');
        } else {
            console.error('Authentication event triggered, but session data is missing or invalid.');
        }
    });

package.json:

  "dependencies": {
    "axios": "^1.5.1",
    "dotenv": "^16.3.1",
    "ejs": "^3.1.9",
    "express": "^4.18.2",
    "morgan": "^1.10.0",
    "qr-image": "^3.2.0",
    "qrcode": "^1.5.3",
    "qrcode-terminal": "^0.12.0",
    "whatsapp-web.js": "github:pedroslopez/whatsapp-web.js#main"
  }

here in screenshot :

image

duduneto commented 1 year ago

Same problem here. I've tried install the lib github:pedroslopez/whatsapp-web.js#main but haven't worked

andresmv94 commented 1 year ago

the "whatsapp-web.js": "^1.22.2-alpha.3", version is working correctly for me.

getsettalk commented 1 year ago

I will check it later , but this package is very slow and so now I'm using bailey for WhatsApp

On Tue, 24 Oct 2023, 1:55 am andresmv94, @.***> wrote:

the "whatsapp-web.js": "^1.22.2-alpha.3", version is working correctly for me.

— Reply to this email directly, view it on GitHub https://github.com/pedroslopez/whatsapp-web.js/issues/2586#issuecomment-1775964976, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALY3KNHLFZPEL2Y7OOUW3DTYA3HFJAVCNFSM6AAAAAA576UCFWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZVHE3DIOJXGY . You are receiving this because you authored the thread.Message ID: @.***>

duduneto commented 1 year ago

I'm still getting this the session data as undefined image

image

I've tested the version ^1.22.2-alpha.3 mencioned above and 1.23.1-alpha.0

alechkos commented 11 months ago

Saving sessions within authenticated event worked with LegacySessionAuth strategy before the WA implemented the multidevice-enabled accounts