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
15.21k stars 3.63k forks source link

Chat.fetchMessages with limit: Infinity return only one message #2297

Closed TheMacros closed 10 months ago

TheMacros commented 1 year ago

Is there an existing issue for this?

Describe the bug

I'm trying to fetch all messages from all chats. As described in the documentation, I need to pass the Infinity to the limit property of the searchOptions https://docs.wwebjs.dev/Chat.html#fetchMessages. But I received only the latest message from the chat instead of all messages.

Expected behavior

As described in the documentation the method should return all messages from the chat.

Steps to Reproduce the Bug or Issue

  1. use fetchMessages() over a chat.
  2. See the response, you'll see only the latest message in the chat is returned

Relevant Code

const chats = await this.client.getChats();
chats.forEach((chat) => {
    // Also, the chat.fetchMessages without searchOptions returns the same
    chat.fetchMessages({
        fromMe: undefined,
        limit: Infinity
    })
        .then((messages) => {
            console.log('Chat -----', chat.name, chat.id._serialized);
            messages.forEach((message) => {
                console.log("\t Message ----", message.from, message.to, message.body, message.id.id);
            });
        });

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

Additional context

No response

krumpking commented 1 year ago

You have to wait a bit while it fetches messages before calling this method, so after client connects, set a TimeOut or something to delay a bit while all messages are loading, then try get the messages, I hope this helps : )

TheMacros commented 1 year ago

You have to wait a bit while it fetches messages before calling this method, so after client connects, set a TimeOut or something to delay a bit while all messages are loading, then try get the messages, I hope this helps : )

Thanks, but if I run this

chat.fetchMessages({
        fromMe: undefined,
        limit: 1024
});

I get all messages (or 1024 max).

krumpking commented 1 year ago

interesting, I am yet to experience that, I believe someone with more experience in that

HardRockPorn commented 1 year ago

"Infinity" should be a string. Works fine.

const messages = await chat.fetchMessages({limit: "Infinity"});

ivanzigoni commented 1 year ago

i've stumbled upon this error too

does anyone know if there's a pr for it already? i volunteer to fix this.

@HardRockPorn

this is supposed to be a number (or Infinity) image

ivanseidel commented 1 year ago

I just spent the last 4 hours debugging this behavior.

What I can resume from my findings:

There are 3 Layers of data:

Looking into what actually happens is as follows:

The thing is, this method is not whell documented, but I have figured out how to call it and force it to load more messages, and even, be able to change "how many messages" to fetch from device at once.

The steps are (assuming moduleRaid exists):

Step 0: (optional) set how many messages to force a history sync

mR.findModule('bulkCreateOrReplaceABPropConfigs')[0].bulkCreateOrReplaceABPropConfigs([{
    "configCode": 3811,
    "configValue": 1000, // Default value is 50
    "configExpoKey": null,
    "hasAccessed": false
}])

Step 1: Trigger a History sync on demand

const HISTORY_SYNC_ON_DEMAND = 3 // Operation code declared a few times in source code
await mR.findModule('sendPeerDataOperationRequest')[0].sendPeerDataOperationRequest(HISTORY_SYNC_ON_DEMAND, {
    "chatId": mR.findModule('createWid')[0].createWid('5511999999999@c.us') // The WID to Sync on demand goes here
})

Step 2: Use loadEarlierMsgs after finished (notice that this function is async, but doesn't wait until completion to return, need to investigate further)

Step 3: Read from getModelsArray as usual

This should be enough to force a on demand sync of a specific chat.

hdem607 commented 1 year ago

@ivanseidel, I didn't get your suggestion. Kind of hard to understand what to do to solve it, despite it is easy to understand the layers of data and how WA works behind the scenes. Why don't you PR with the suggestion? Your idea of forcing fetch, as user do (clicking on the button), is awesome.