Whatsapp-web.js: getChats function suddenly stopped working

Created on 22 Dec 2020  路  14Comments  路  Source: pedroslopez/whatsapp-web.js

Bug description

getChats function suddenly stopped working. In a few hours ago it was working. Nothing changed in my envoirment.

Reproduction steps

Steps to reproduce the behavior:

  1. Lib start:
let client = new Client({puppeteer:{ headless: true, 'product': 'chrome', args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage', '--no-first-run', '--no-zygote', '--single-process', '--disable-gpu']}})
  1. Read QRCode:
client.on('qr', (qr) => {
    QRCode.toDataURL(qr, function (err, url) {
        /* SEND BASE64 TO FRONTEND */
    })
})
  1. After ready:
client.on('ready', async () => {
      /* START TO GET CHATS ON NEXT STEP */
  });
  1. Get chats:
client.getChats().then((chats)=>{ /* RETURN DATA TO FRONTEND */ })

...

Expected behavior

Get chats object.

Environment (please complete the following information):

WhatsApp

  • Account type [Standard]
  • Device OS [Android]
  • WhatsApp Web version [2.20.206.24]

Library

  • Browser [Chrome]
  • NodeJS version (12.4.0)
  • npm or yarn version (6.9.0)
  • whatsapp-web.js version: 1.11.1

Additional context

Error on Logentries in Heroku with Puppeteer buildpack:

21 Dec 2020 20:30:08.070340 <190>1 2020-12-21T23:30:07.453981+00:00 app web.1 - - (node:23) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 38)
21 Dec 2020 20:30:08.070188 <190>1 2020-12-21T23:30:07.453774+00:00 app web.1 - - (node:23) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'map' of undefined
    at Client.getChats (/app/node_modules/whatsapp-web.js/src/Client.js:493:22)
    at processTicksAndRejections (internal/process/task_queues.js:89:5)Exception
bug

Most helpful comment

Could you make the following changes in src/util/Injected.js and report your results?

Replace window.WWebJS.getChatModel with:

    window.WWebJS.getChatModel = async chat => {
        let res = chat.serialize();
        res.isGroup = chat.isGroup;
        res.formattedTitle = chat.formattedTitle;
        res.isMuted = chat.mute && chat.mute.isMuted;

        if (chat.groupMetadata) {
            await window.Store.GroupMetadata.update(chat.id._serialized);
            res.groupMetadata = chat.groupMetadata.serialize();
        }

        delete res.msgs;
        delete res.msgUnsyncedButtonReplyMsgs;
        delete res.unsyncedButtonReplies;

        return res;
    };

All 14 comments

Please run this to identify which chat is having the issue:

client.on('ready', () => {
    const chatIds = await client.pupPage.evaluate(async () => {
        const chats = await window.WWebJS.getChats();
        return chats.map(c => c.id._serialized);
    });

    let chatCount = 0;
    for (let chatId of chatIds) {
        try {
            const chat = await client.getChatById(chatId);
            if (!chat) {
                console.log(`Could not serialize chat ${chatId}`);
            } else {
                chatCount++;
            }
        } catch (e) {
            console.log(`Could not serialize chat ${chatId}`, e);
        }
    }
    console.log(`Correctly got ${chatCount} chats`);
});

With your code, Logentries returned:

Correctly got 109 chats
Could not serialize chat [hidden]@c.us TypeError: Cannot read property 'isGroup' of undefined

This is a business contact: 5511999910621

Can you identify any particularities for that chat ID that has a problem with serialization? Are there any special messages in the conversation?

This is a business contact: 5511999910621 (with official whatsapp api enabled to interact)

This is the id? It seems like it has the brazilian aditional 9. Usually whatsapp get ride of it

It's a brazilian number: [email protected]
Error Could not serialize chat TypeError: Cannot read property 'isGroup' of undefined

I tried chatting with the number and everything seems to be okay.

Please run the client in headless: false so you can see the chrome window, open the console and run the following:

await window.WWebJS.getChatModel(Store.Chat.get("[email protected]"));

and post the results.

I believe it worked for you because you didn't interact with the chat. Here for me, the chat featured action buttons (yes or no), waited for a text response at the end of the conversation but I didn't respond. I believe that if you interact with the chat, you can reproduce the error.

Printscreen of chat: https://postimg.cc/HcvmdykD

@frmontini could you please take the steps I outlined in my previous message?
This issue was addressed in a previous release, but there may be other properties that need to be filtered out. The steps I mentioned should help identify what these are.

Please run the client in headless: false so you can see the chrome window, open the console and run the following:

await window.WWebJS.getChatModel(Store.Chat.get("[email protected]"));

and post the results.

The result following your steps:

{"id":"[email protected]","pendingMsgs":false,"lastReceivedKey":{"fromMe":false,"remote":"[email protected]","id":"1606AE6C987B4EDC39","_serialized":"[email protected]_1606AE6C987B4EDC39"},"t":1608518082,"unreadCount":0,"archive":false,"isReadOnly":false,"muteExpiration":0,"notSpam":true,"pin":0,"ephemeralDuration":0,"ephemeralSettingTimestamp":0,"msgUnsyncedButtonReplyMsgs":[{"id":{"fromMe":false,"remote":"[email protected]","id":"77221E977D87F3169C","_serialized":"[email protected]_77221E977D87F3169C"},"unsyncedButtonReplies":[]}],"isGroup":false,"formattedTitle":"Minha Claro","isMuted":false}

Could you make the following changes in src/util/Injected.js and report your results?

Replace window.WWebJS.getChatModel with:

    window.WWebJS.getChatModel = async chat => {
        let res = chat.serialize();
        res.isGroup = chat.isGroup;
        res.formattedTitle = chat.formattedTitle;
        res.isMuted = chat.mute && chat.mute.isMuted;

        if (chat.groupMetadata) {
            await window.Store.GroupMetadata.update(chat.id._serialized);
            res.groupMetadata = chat.groupMetadata.serialize();
        }

        delete res.msgs;
        delete res.msgUnsyncedButtonReplyMsgs;
        delete res.unsyncedButtonReplies;

        return res;
    };

@pedroslopez works fine :D

@frmontini Thanks for reporting! The fix has been released in v1.11.2.

@pedroslopez thank you for the great job :D

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabriel-tandil picture gabriel-tandil  路  5Comments

riagie picture riagie  路  6Comments

ocracy picture ocracy  路  5Comments

iriobf picture iriobf  路  4Comments

dedenhendrap picture dedenhendrap  路  8Comments