class ConnectionWhatsApp extends ConnectionWhatsAppInterface {
constructor(clientId) {
super(clientId);
this.connection = null
this.timeout = 2000
this.code = null
this.validConnection = false
}
getConnection() {
if (this.connection) {
return this.connection
} else {
console.log("Não há conexão para obter")
}
}
setConnection(connection) {
if (connection) {
this.connection = connection
} else {
console.log("Erro no set da conexão")
}
}
setValidConnection() {
this.validConnection = true
}
hasValidConnection() {
return this.validConnection
}
isInvalidConnection() {
return !this.validConnection
}
setConnectionCodeStatus(code) {
this.code = code
}
getConnectionCodeStatus() {
if (this.code) {
return this.code
}
}
async createConnection() {
return new Promise(async (resolve, reject) => {
const clientId = this.clientId;
console.log('DADOS OBTIDOS DA CHAMADA API CLIENT:', clientId);
console.log('CRIANDO CLIENTE WEBJS');
if (clientId === undefined) {
console.log("NÃO FOI POSSÍVEL OBTER O CLIENTE ID");
reject("Erro ao obter o cliente ID");
}
const mongoURI = process.env.MONGO_URI ?? 'mongodb://mongo-uri.local/';
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('QR CODE SOLICITADO BOT ID: ', clientId);
reject();
});
client.on('authenticated', (session) => {
console.log('Authenticated');
});
client.on('ready', async () => {
const version = await client.getWWebVersion();
console.log(`WWeb v${version}`);
resolve();
});
client.on('message_create', (message) => {
console.log("Mensagem evento ")
console.log(message.body)
})
await client.initialize();
})}
destroyConnection() {
if (this.connection) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("Encerrando cliente...")
this.connection.destroy()
.then(data => {
console.log("Client destruido com sucesso");
resolve(data);
})
.catch(error => {
console.error("Erro ao destruir cliente:", error);
reject(error);
});
}, this.timeout);
});
}
}
}
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.
After the authenticated event is successfully triggered, the code encounters an error and does not reach the ready event. The reported error is:
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.