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.96k stars 3.55k forks source link

Incognito usage not possible (presence) #2559

Closed mxhryvo closed 5 months ago

mxhryvo commented 10 months ago

Is there an existing issue for this?

Describe the bug

I am not able use the client in incognito mode, meaning that the client or user stays offline/unavailable and thus not updates the "Last seen online:" timestamp. (I know that baileys client offers a way to do this)

When I run the script below, the user stays offline until the 100% of the loading screen state is reached, and then goes online for less than a second and then offline again (probably because of sending the "unavailable" state).

Is it possible to use the client without appearing online at all (if explicitly wanted). Or is there an earlier state to use client.sendPresenceUnavailable() and thus prevent going online?

When is the client automatically sending an "available" state and can this be disabled potentially?

This would enable use cases where the bot/user should not leave the impression to have been active/available, basically for any READ actions, e.g. if some data (e.g. chats/contacts etc.) should be fetched or media downloaded etc.

Expected behavior

I expect that I am able to not send an "available" state at all and thus not change the account's "last online ..." state (this seems to be possible with Baileys for example), especially If I explicitly use the client.sendPresenceUnavailable() method after client is ready.

Steps to Reproduce the Bug or Issue

(Assuming first time authentication is done)

  1. Use the example code from the docs or something like my code (see below)
  2. Start script
  3. Wait until Loading screen reaches 100%
  4. (Client goes online for a second and then offline, thus changing the "last online timestamp")
  5. Wait until "READY" is logged
  6. Exit script

Relevant Code

const { Client, LocalAuth } = require("whatsapp-web.js");
const qrcode = require("qrcode-terminal");

const client = new Client({
  authStrategy: new LocalAuth(),
  puppeteer: {
    headless: true,
  },
});

// Defining event handlers for WhatsApp client
client.on("loading_screen", (percent, message) => {
  console.log("LOADING SCREEN", percent, message);
});

client.on("qr", (qr) => {
  qrcode.generate(qr, { small: true });
});

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

client.on("auth_failure", (msg) => {
  // Fired if session restore was unsuccessful
  console.error("AUTHENTICATION FAILURE", msg);
});

client.on("ready", () => {
  client.sendPresenceUnavailable();
  console.log("READY");
});

client.on("change_state", (state) => {
  console.log("CHANGE STATE", state);
});

client.on("disconnected", (reason) => {
  console.log("Client was logged out", reason);
});

async function startClient() {
  client.initialize();
}

startClient();

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

OS: MacOS 13.6
Phone OS: iOS
whatsapp-web.js: latest (using version from github)
WhatsApp Web: 2.2341.18
Node.js: v18.13.0

Additional context

No response

PurpShell commented 10 months ago

This could work if we set the puppeteer window to not actively selected / in foreground

L480 commented 10 months ago

This could work if we set the puppeteer window to not actively selected / in foreground

Tested that, but you will be available even when immediately switching to another tab or minimizing the entire browser. Not sure if this is even possible to implement.