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
14.53k stars 3.46k forks source link

Ready event is broken #3181

Open irissonnlima opened 4 days ago

irissonnlima commented 4 days ago

Is there an existing issue for this?

Describe the bug

const { MongoStore } = require("wwebjs-mongo");
const mongoose = require("mongoose");
const { RemoteAuth, Client } = require("whatsapp-web.js");

class WhatsappClient {
  constructor(containerId, userId) {
    this.mongoURI = "mongodb://mongo-uri/";
    this.containerId = containerId;
    this.userId = userId;
    this.client = null;
    this.clientReady = false;
  }

  getContainerId() {
    return this.containerId;
  }

  getUserId() {
    return this.userId;
  }

  setClient(client) {
    if (client) {
      this.client = client;
    } else {
      console.log("setClient: Client inexistente!");
    }
  }

  setReadyClient(state) {
    this.clientReady = state;
  }

  createClientConnection() {
    return new Promise(async (resolve, reject) => {
      const clientId = this.containerId;

      console.log("DADOS OBTIDOS DA CHAMADA API CLIENT:", clientId);

      const mongoURI =
        process.env.MONGO_URI ??
        "mongodb://mongo-uri/";
      await mongoose.connect(mongoURI);
      const store = new MongoStore({ mongoose: mongoose });

      const client = new Client({
        puppeteer: {
          args: ["--no-sandbox"],
        },
        authStrategy: new RemoteAuth({
          clientId: `${clientId}`,
          store: store,
          backupSyncIntervalMs: 300000,
        }),
      });

      client.on("qr", (qr) => {
        console.log("Qrcode gerado!");
        qrcode.generate(qr, { small: true });
      });

      client.on("authenticated", async () => {
        console.log("Authenticated");
      });

      client.once("ready", () => {
        console.log("Client is ready!");
        console.log(client.info);
      });

      client.initialize();
    });
  }
}

module.exports = {
  WhatsappClient,
};

After the authenticated event is successfully triggered, the code encounters an error and does not reach the ready event. The reported error is:

Error: Execution context was destroyed, most likely because of a navigation.
    at rewriteError (/src/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:284:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async ExecutionContext._ExecutionContext_evaluate (/src/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:227:56)
    at async ExecutionContext.evaluate (/src/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:107:16)
    at async Client.inject (/src/node_modules/whatsapp-web.js/src/Client.js:101:13)
    at async /src/node_modules/whatsapp-web.js/src/Client.js:350:13

I'm using the version "whatsapp-web.js": "github:pedroslopez/whatsapp-web.js#webpack-exodus".

When the authenticated event happens on my phone, it appears active, but the ready event never occurs.

Expected behavior

I expected the 'ready' event to be triggered, but this did not happen.

Steps to Reproduce the Bug or Issue

Try to instantiate the class and run createClientConnection

Relevant Code

No response

Browser Type

Chromium

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

No, I am not using Multi Device

Environment

FROM node:18.13.0

WORKDIR /src

# Instale as dependências adicionais necessárias para executar o Chrome no webjs
RUN apt-get update -y \
    && apt-get install -y \
    gconf-service \
    libgbm-dev \
    libasound2 \
    libatk1.0-0 \
    libc6 \
    libcairo2 \
    libcups2 \
    libdbus-1-3 \
    libexpat1 \
    libfontconfig1 \
    libgcc1 \
    libgconf-2-4 \
    libgdk-pixbuf2.0-0 \
    libglib2.0-0 \
    libgtk-3-0 \
    libnspr4 \
    libpango-1.0-0 \
    libpangocairo-1.0-0 \
    libstdc++6 \
    libx11-6 \
    libx11-xcb1 \
    libxcb1 \
    libxcomposite1 \
    libxcursor1 \
    libxdamage1 \
    libxext6 \
    libxfixes3 \
    libxi6 \
    libxrandr2 \
    libxrender1 \
    libxss1 \
    libxtst6 \
    ca-certificates \
    fonts-liberation \
    libappindicator1 \
    libnss3 \
    lsb-release \
    xdg-utils \
    wget

# Copiando arquivos
COPY ./package.json ./package-lock.json* ./

# Instalando dependências
RUN npm init -y && \
    npm install github:pedroslopez/whatsapp-web.js#webpack-exodus qrcode-terminal mongoose wwebjs-mongo mysql2 amqplib

COPY ./src .

EXPOSE 7001

CMD ["node", "app.js"]

Additional context

No response

irissonnlima commented 4 days ago

I tried again and received:

Criando conexão WhatsApp... DADOS OBTIDOS DA CHAMADA API CLIENT: c1c58233-2e04-49b8-805d-8dc02e5b9190 Authenticated Client is ready! ClientInfo { my-client-info } /src/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:284 throw new Error('Execution context was destroyed, most likely because of a navigation.'); ^

Error: Execution context was destroyed, most likely because of a navigation. at rewriteError (/src/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:284:15) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async ExecutionContext._ExecutionContext_evaluate (/src/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:227:56) at async ExecutionContext.evaluate (/src/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:107:16) at async Client.inject (/src/node_modules/whatsapp-web.js/src/Client.js:251:37) at async /src/node_modules/whatsapp-web.js/src/Client.js:350:13

Node.js v18.13.0

ntaraujo commented 4 days ago

I'm having the same issue

PeccinCodes commented 4 days ago

Same error here

victoromarques commented 4 days ago

Same error here to

alexfarfam commented 4 days ago

Same error here to

victoromarques commented 4 days ago

I'm not getting it, I'm trying to use several versions of Remote Path, but without success!

https://github.com/wppconnect-team/wa-version/tree/main/html

I've tried to revert to other versions of the lib, but without success! Has anyone managed to solve it?

JuniorCruzEG commented 3 days ago

Has anyone managed to solve the ready event?

ntaraujo commented 3 days ago

solved with this

https://github.com/pedroslopez/whatsapp-web.js/pull/2816#issuecomment-2204732956

victoromarques commented 3 days ago

resolvido com isso

#2816 (comentário)

this worked for me, thank you very much!

irissonnlima commented 3 days ago

Not working yet

gastonrobledo commented 3 days ago

is not working, it works if I do the authentication intially but if I want to restore a session (remote) ready event is not triggered and 'authentication' event is being triggered twice, I was using github:pedroslopez/whatsapp-web.js#webpack-exodus

irissonnlima commented 2 days ago

To assist the community and library developers, I managed to get it running.

I tried to capture the error but was unsuccessful. I am running in a Kubernetes environment, and when attempting to run the bot with 1vCPU and 2048MB of RAM, the bot encountered the reported error. However, when limiting the resources to 0.5vCPU and 1024MB of RAM, the bot no longer encountered errors during startup and ran smoothly. I was able to replicate the error using LocalAuth instead of RemoteAuth.

Another important point I noticed was that if the process doesn't use cache, the error does not occur. In other words, if I start a new session, the entire process runs naturally, but this is inconvenient as it's susceptible to restarts of my machine.

max-programming commented 2 days ago

For me, whenever I log out from the phone (or remove device - same thing), it fires the ready event for some reason