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.12k stars 590 forks source link

client.sendSeen sometimes doesn't work #2205

Closed bauti-defi closed 2 years ago

bauti-defi commented 2 years ago

Are you using the latest version of the library?

Current Behavior

Calling client.sendSeen(chatId) sometimes works and sometimes doesn't. There is no error thrown or stacktrace to follow.

Expected Behavior

Calling client.sendSeen(chatId) should mark a chat as read. If there were any unread messages, those should marked as read. Calling the method on a chat with 5 unread messages should set it to 0 unread messages (make the badge disappear)

Steps To Reproduce

client.sendSeen() has no affect in the following code snippet. This is executed in start function passed to create.then(start)

 // Respond to any messages we may have missed
  const unreadMessages = await client.getUnreadMessages(false, false, true);

  unreadMessages.forEach(async (unreadChat) => {
    if (!unreadChat["isGroup"]) {
      // @ts-ignore
      await client.sendText(unreadChat["id"], "Hola, volvi! 🤖");

      unreadChat["messages"].forEach(async (message: Message) => {
        const context: InteractionContext = {
          client: new WAPPClient(client, message),
          nativeMessage: message,
        };

       //client.sendSeen IS THE FIRST THING CALLED INSIDE bot.process
        await bot.process(context).catch((error) => {
          if (!error.silent) {
            console.log(error);
          }
        });
      });
    }
  });

client.sendSeen() works as expected in the following code snippet. This is executed in start function passed to create.then(start)

 await client.onMessage(async (message: Message) => {
    const context: InteractionContext = {
      client: new WAPPClient(client, message),
      nativeMessage: message,
    };

    await bot.process(context).catch((error) => {
      if (!error.silent) {
        console.log(error);
      }
    });
  });

Here is the bot.process function.

public async process(context: InteractionContext) {
    const { nativeMessage, client } = context;
    const { type, selectedButtonId } = nativeMessage;

    // Mark all messages as seen
    await client.sendSeen();
    ......
    ..
    ......
}

And this is the client.sendSeen() function inside my WhatsApp client wrapper class I use.

sendSeen(): Promise<boolean> {
    return this.client.sendSeen(this.previousMessage.from);
  }

Mode

My own code

create() code

wa.create({
  sessionId: "beto-wapp-server-v3",
  disableSpins: true,
  headless: true,
  useStealth: true,
  sessionDataPath: "./data/tokens",
  authTimeout: 0,
  cachedPatch: true,
  restartOnCrash: start,
})
  .then(start)
  .catch((error) => {
    if (!error.silent) {
      console.log(error);
    }
  });

DEBUG INFO

- Starting
- Version: 4.22.5
- Initializing WA
- Launching Browser
- Setting Up Browser
- Loading session data
- Found session data file: /Users/bautista/Desktop/Beto-Bot/Beto-WAPP/data/tokens/beto-wapp-server-v3.data.json
- Existing session data detected. Injecting...
- Existing session data injected
- Navigating to WA
- Page loaded in 1047ms: 200
- Browser Launched
- Debug info: {
  "WA_VERSION": "2.2138.13",
  "PAGE_UA": "WhatsApp/2.2108.8 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.22.5",
  "BROWSER_VERSION": "HeadlessChrome/93.0.4577.0",
  "OS": "macOS Big Sur",
  "START_TS": 1633451357482
}
- Use this easy pre-filled link to report an issue: https://github.com/open-wa/wa-automate-nodejs/issues/new?template=bug_report.yaml&debug_info=%7B%0A%20%20%22WA_VERSION%22:%20%222.2138.13%22,%0A%20%20%22WA_AUTOMATE_VERSION%22:%20%224.22.5%22,%0A%20%20%22BROWSER_VERSION%22:%20%22HeadlessChrome/93.0.4577.0%22,%0A%20%20%22START_TS%22:%201633451357482%0A%7D&environment=-%20OS:%20macOS%20Big%20Sur%0A-%20Node:%2014.15.5%0A-%20npm:%20%0A
- Injecting api
- WAPI injected
- Authenticating
- Authenticated
- Waiting for ripe session...
- Reinjecting api
- WAPI Reinjected
- Checking if session is valid
- Downloading cached patches from https://raw.githubusercontent.com/open-wa/wa-automate-nodejs/master/patches.json
- Client is ready
- Downloaded patches in 0.36s
- Installing patches
- Patches Installed: 76539
- Client loaded for business account with 940 contacts, 842 chats & 911 messages in 6.306s
- 🚀 @OPEN-WA ready for account: 3298

Environment

- OS: macOS Big Sur
- Node: 14.15.5
- npm: 6.14.11

Screenshots

No response

Anything else?

No response

smashah commented 2 years ago

Thanks for the comprehensive breakdown.

One last thing I would need from you is to log the id being sent to sendSeen and the value of the command itself

So, in your wrapper class change it to this:

async sendSeen(): Promise<boolean> {
    const res = await this.client.sendSeen(this.previousMessage.from);
    console.log("SendSeen result: ", res, " ID: ", this.previousMessage.from);
    return res
  }

sendSeen will return false if either the ID is missing [if (!id) return false;] or the chat related to the ID is undefined

smashah commented 2 years ago

@Bautista-Baiocchi-lora please provide the logs with the updates i suggested

bauti-defi commented 2 years ago

Thanks for the comprehensive breakdown.

One last thing I would need from you is to log the id being sent to sendSeen and the value of the command itself

So, in your wrapper class change it to this:

async sendSeen(): Promise<boolean> {
    const res = await this.client.sendSeen(this.previousMessage.from);
    console.log("SendSeen result: ", res, " ID: ", this.previousMessage.from);
  return res
  }

sendSeen will return false if either the ID is missing [if (!id) return false;] or the chat related to the ID is undefined

Hello, sorry for not getting back to you sooner. I no longer have the code to reproduce this issue, its been heavily refactored. But I do recall doing exactly what you suggested before creating this formal bug report. The print out was correct. That is to say, it printed out the expected this.previousMessage.from. That is when I concluded that It must be a library bug.

smashah commented 2 years ago

Ok if it's not reproducible I will close this for now then.

Thanks