open-wa / wa-automate-nodejs

πŸ’¬ πŸ€– The most reliable tool for chatbots with advanced features. Be sure to 🌟 this repository for updates!
https://docs.openwa.dev/
Other
3.15k stars 600 forks source link

client.sendSeen() doesn't always work #2418

Closed bauti-defi closed 2 years ago

bauti-defi commented 2 years ago

Are you using the latest version of the library?

What type of session are you experiencing this issue on?

Legacy (Normal)

What type of host account are you experiencing this issue on?

Business account

Mode

My own code

Current Behavior

client.sendSeen(chatId) is very inconsistent.

As far as I can tell, It only works as expected for messages received after the client starts up and is idle (waiting for inbound messages).

I have noticed that it doesn't work as expected when trying to handle messages that came in during client downtime.

Example:

At start up I use client.getUnreadMessages() to pull all the messages that were received and not answered during client downtime. Calling client.sendSeen() on the chat of the respective messages returns true but nothing actually happens. The messages continue to be "unread" on the device.

Expected Behavior

client.sendSeen(chatId) should set all messages inside a chat to read if the return is true.

Steps To Reproduce

const handleUnreadMessages = async (bot_id: string, client: Client) => {
  const unReadChats = await client.getUnreadMessages(false, false, true);

  for (var chat of unReadChats) {
    //@ts-ignore
    const markAsSeen  = await client.sendSeen(chat.id);
    console.log("Seen: ", markAsSeen)

    if (!chat["isGroup"]) {
      //@ts-ignore
      await client.sendText(chat.id, "Hola, volvi! πŸ€–");

      for (var message of chat["messages"]) {
        await MessageRelayer(bot_id, client)(message);
      }
    }
  }
};

const start = async (client: Client) => {
const phoneNumber: string = await client.getHostNumber();

await handleUnreadMessages(phoneNumber, client);
}

wa.create({
  sessionId: "beto-wapp-server-v4",
  disableSpins: true,
  headless: true,
  useStealth: true,
  sessionDataPath: "./data/tokens",
  authTimeout: 0,
  multiDevice: false, // process.env.DEV_MODE ? false : true
  licenseKey: process.env.DEV_MODE ? undefined : process.env.OPEN_WA_KEY,
  cachedPatch: true,
  restartOnCrash: start,
})
  .then(start)
  .catch((error) => {
    if (!error.silent) {
      console.log("Error:", error.message);
    }
  });

create() code

wa.create({
  sessionId: "beto-wapp-server-v4",
  disableSpins: true,
  headless: true,
  useStealth: true,
  sessionDataPath: "./data/tokens",
  authTimeout: 0,
  multiDevice: false, // process.env.DEV_MODE ? false : true
  licenseKey: process.env.DEV_MODE ? undefined : process.env.OPEN_WA_KEY,
  cachedPatch: true,
  restartOnCrash: start,
})
  .then(start)
  .catch((error) => {
    if (!error.silent) {
      console.log("Error:", error.message);
    }
  });

DEBUG INFO

- Starting
- Version: 4.28.13
- Initializing WA
- Launching Browser
Subscribed to ALL channels
- Setting Up Page
- Loading session data
- Found session data file: /home/bautista/Desktop/Beto/Beto-WAPP/data/tokens/beto-wapp-server-v4.data.json
- Existing session data detected. Injecting...
- Existing session data injected
- Navigating to WA
- Page loaded in 737ms: 200
- Browser Launched
- Debug info: {
  "WA_VERSION": "2.2149.4",
  "PAGE_UA": "WhatsApp/2.2147.16 Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36",
  "WA_AUTOMATE_VERSION": "4.28.13",
  "BROWSER_VERSION": "HeadlessChrome/93.0.4577.0",
  "OS": "Linux 5.11",
  "START_TS": 1642087626979
}
- Use this easy pre-filled link to report an issue: https://github.com/open-wa/wa-automate-nodejs/issues/new?template=bug_report.yaml&d_info=%7B%0A%20%20%22WA_VERSION%22:%20%222.2149.4%22,%0A%20%20%22WA_AUTOMATE_VERSION%22:%20%224.28.13%22,%0A%20%20%22BROWSER_VERSION%22:%20%22HeadlessChrome/93.0.4577.0%22,%0A%20%20%22START_TS%22:%201642087626979,%0A%20%20%22LATEST_VERSION%22:%20true,%0A%20%20%22CLI%22:%20false%0A%7D&enviro=-%20OS:%20Linux%205.11%0A-%20Node:%2017.3.1%0A-%20npm:%208.3.0&labels=Legacy
- Injecting api
- WAPI injected
- Authenticating
- Authenticated
- Reinjecting api
- WAPI Reinjected
- Checking if session is valid
- Searching for cached patch
- Found cached patch
- Cached patch loaded
- Downloading cached patches from https://cdn.openwa.dev/patches.json
- Client is ready
- Installing patches
- Patches Installed: CACHED-46c7b
- Client loaded for normal account with 944 contacts, 845 chats & 878 messages in 5.628s
- Finalizing web session...
- Finalizing client...
- Downloaded patches in 0.457s
- Saving patches to current working directory
- Saved patches to current working directory
- πŸš€ @OPEN-WA ready for account: 3298

Environment

- OS: ubuntu 20.04
- Node: 8.3.0
- npm: 17.3.1

Screenshots/Logs

No response

Anything else?

No response

bauti-defi commented 2 years ago

Any news on this?

mrizkypk commented 2 years ago

This happened to me too. Even worse. I use the regular WhatsApp application and the sendSend() doesn't work at all.

smashah commented 2 years ago

@Bautista-Baiocchi-lora @mrizkypk

I've issued a patch that double checks the unread count and attempts alternative methods to clear the unseen messages from the chat.

Please restart and try agian.

thanks

mrizkypk commented 2 years ago

I have restarted the process but sendUnsend() is still not working. This just happened after WhatsApp forced MD for my account (no BETA label inside WhatsApp app). WhatsApp Image 2022-04-25 at 11 56 57 AM

Because of that I have to use MD otherwise logout occurs after few minutes after login.

mrizkypk commented 2 years ago

After couple of restarts, sendUnsend() is working right now. Thanks...

EDIT

But somehow it's not always works.

smashah commented 2 years ago

@mrizkypk I've tested this on MD.

Can u explain in what situation exactly it does not work?

And by "it does not work" do you mean it doesn't clear unread messages indicator on the phone or the session? Please be as clear as possible.

smashah commented 2 years ago

Should be working fine now

alanknm01 commented 2 years ago

The problem still exists. SendSeen() does not work, so when i catch the unread messages they are not marked as seen by the method SendSeen() and always when you restart the session those message appear again.

Steps To Reproduce:


const handleUnreadMessages = async (client: Client) => {
  const unReadChats = await client.getUnreadMessages(false, false, true);

  for (var chat in unReadChats) { 
    const markAsSeen  = await client.sendSeen(unReadChats[chat].id);
    console.log("Seen: ", markAsSeen)

    if (!unReadChats[chat].isGroup) {
      //@ts-ignore
      await client.sendButtons(unReadChats[chat].id,"Hola, volvi! πŸ€–",[{id:"go-back-to-menu", text:"Hola Beto"}], "", "")
    }
  }
};```
smashah commented 2 years ago

@alanknm01 try again making sure the host account device has the correct seen state.

smashah commented 2 years ago

should be improved with latest patches. please comment if still experiencing issues