Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetchMessages({limit:Infinity}) does not return all messages #1325

Open
1 task done
aidulcandra opened this issue Mar 11, 2022 · 10 comments
Open
1 task done

fetchMessages({limit:Infinity}) does not return all messages #1325

aidulcandra opened this issue Mar 11, 2022 · 10 comments
Labels
bug Something isn't working

Comments

@aidulcandra
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When I try to fetch all messages and passing the limit of Infinity, it does not return all the messages. In my case I have 59 messages, but it only returns 21. But then when I passed a large number like 100 it would return all the 59 messages.

Expected behavior

Expected that the fetchMessages function return all the messages to the earliest

Steps to Reproduce the Bug or Issue

Use fetchMessages and pass limit:Infinity, try it with chats with large number of messages.

Relevant Code

No response

Browser Type

Chromium

WhatsApp Account Type

Standard

Does your WhatsApp account have multidevice enabled?

No, I am not using Multi Device

Environment

OS: Windows
Phone OS: Android
whatsapp-web.js version 1.16.4
WhatApp Web version 2.2208.7
Node.js version 16.13.1

Additional context

No response

@aidulcandra aidulcandra added the bug Something isn't working label Mar 11, 2022
@allfii
Copy link

allfii commented Mar 16, 2022

I also experienced the same issue

@dsandrade
Copy link

I had the same prooblem

@sergiooak
Copy link

let allMessages = await chat.fetchMessages({
      limit: 99,
    });

Until last version of WhatsApp, if you fetch more messages than the chat have it just works fine, but since it updated (to me, it was yesterday) it enters an infinity loop, I guess both problem is related

@PurpShell
Copy link
Sponsor Collaborator

guessing this is a rate limit thing, take the messages in incremental bites (like get 50 and delay, and then repeat)

@allfii
Copy link

allfii commented Apr 22, 2022

@PurpShell could you show me how to do incremental bites? I only see "limit" option and there is no "offset" option in the fetchMessages function

@PurpShell PurpShell reopened this Apr 22, 2022
@PurpShell
Copy link
Sponsor Collaborator

Seems like we should work on pagination or a delay functionality

@satonio
Copy link

satonio commented Jun 21, 2022

Same problem. With limit 50 it hangs most of times.

@deepto98
Copy link

For me var messages = await chat.fetchMessages({ limit: Number.MAX_VALUE });, loads around 5000 messages in a group chat, but not all. Using Infinity didn't work at all for me.

@jasonnathan
Copy link

jasonnathan commented Jan 23, 2024

The workaround will be to manually load up the messages using the browser. Here's what I did:

  1. Using the example shell.js in the repository, I created a repl instance on my mac
  2. This loads up chrome via puppeteer
  3. Painfully scrolled up... and scrolled up... and scrolled up...
  4. Back in the repl:
wwebjs> let chats = await client.getChats();
undefined
wwebjs> let chat = chats.find(c => // logic to retrieve the conversation you want)
undefined
wwebjs> let messages = await chat.fetchMessages({limit: 1e4}) // 10000 messages
undefined
  1. Now you'd have all the messages you loaded via the browser. Painful but... if you're lazy like I am, create this functionality beforehand and rerun it:
const rerun = (client) => {
  let myChat = null
  let messages = []
  const getMyChat = async (findFn) => {
    const chats = await client.getChats()
    myChat = chats.find(findFn)
  }
  const fetchMessages = async (limit = Infinity) => {
    if(myChat)  {
      messages = await myChat.fetchMessages({limit})
    }
    return Promise.resolve(messages) // in case we didn't get into the if block
  }
  return async () => {
    await getMyChat(chat => chat.name === "DoNotMessageManually")
    await fetchMessages()
    console.log(`Retrieved ${messages.length} messages`)
    return messages
  }
}
  1. Then bind it to repl's context:
client.on('ready', () => {
  const shell = repl.start('wwebjs> ');
  shell.context.client = client;
  shell.context.runner = rerun(client)
  shell.on('exit', async () => {
      await client.destroy();
  });
});
  1. Now after the manual labour of scrolling endlessly in the browser...
wwebjs> await runner()
Retrieved 3123 messages

@icheered
Copy link

@jasonnathan Did you scroll up manually or is it scriptable?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants