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.05k stars 3.59k forks source link

Losing my mind! Error getting group chat: Evaluation failed: Error: wid error: invalid wid #2725

Closed ceekfadeblack closed 8 months ago

ceekfadeblack commented 8 months ago

Is there an existing issue for this?

Describe the bug

const qrcode = require('qrcode-terminal'); const { Client } = require('whatsapp-web.js');

const client = new Client({ puppeteer: { args: ['--no-sandbox'] }, });

// Step 1: Display QR code for client authentication client.on('qr', qr => { qrcode.generate(qr, { small: true }); });

// Step 2: When client is ready client.on('ready', () => { console.log('Step 2: Client is ready!'); });

// Step 3: Listen for incoming messages client.on('message', async message => { if (message.body === '!addMember') { try { const groupId = 'DoJ7CWhXvXtAF6YcEacxxx'; // New group ID const phoneNumber = 'XXXXXXXXXX'; // Phone number to add (without '+' at the beginning)

        // Step 4: Validate the phone number
        const isValidNumber = await client.isRegisteredUser(phoneNumber);
        if (isValidNumber) {
            console.log('Step 4: The provided phone number is valid and registered on WhatsApp.');
        } else {
            console.log('Step 4: The provided phone number is not valid or not registered on WhatsApp.');
            return; // If the number is invalid, exit the process
        }

        // Step 5: Get the group chat by its ID
        let groupChat;
        try {
            groupChat = await client.getChatById(groupId);
            console.log('Step 5: Group Chat:', groupChat);
        } catch (error) {
            console.error('Step 5: Error getting group chat:', error.message || error);
            return; // If unable to get group chat, exit the process
        }

        // Step 6: Send a test message to the group
        if (groupChat.isGroup) {
            const testMessage = await groupChat.sendMessage('This is a test message.');
            console.log('Step 6: Test message sent:', testMessage);
        } else {
            console.log('Step 6: The specified ID does not belong to a group chat.');
            return; // If the ID is not a group chat, exit the process
        }

        // Step 7: Add the phone number to the group
        const numberId = await client.getNumberId(phoneNumber);
        if (numberId) {
            const participantId = numberId._serialized;
            await groupChat.addParticipants([participantId]);
            console.log(Step 7: Phone number ${participantId} has been successfully added to the group.);
        } else {
            console.log('Step 7: The provided phone number is not valid or not on WhatsApp.');
        }
    } catch (error) {
        console.error('Error adding member to group:', error.message || error);
    }
}

});

// Step 8: Initialize the client client.initialize();

I've been struggling for at least 10 hours trying to add a single phone number to a group, and I just can't seem to do it! I'm losing my mind. Please help me solve this problem.

Step 2: Error getting group chat: Evaluation failed: Error: wid error: invalid wid at e (https://web.whatsapp.com/:2:4911) at new f (https://web.whatsapp.com/app.d61b42d11fedf617d4e8.js:311:221995) at Object.d [as createWid] (https://web.whatsapp.com/app.d61b42d11fedf617d4e8.js:311:228400) at Object.window.WWebJS.getChat (_puppeteer_evaluationscript:439:49) at _puppeteer_evaluationscript:2:40

Expected behavior

Evaluation failed: Error: wid error: invalid wid at e (https://web.whatsapp.com/:2:4911) at new f (https://web.whatsapp.com/app.d61b42d11fedf617d4e8.js:311:221995) at Object.d [as createWid] (https://web.whatsapp.com/app.d61b42d11fedf617d4e8.js:311:228400) at Object.window.WWebJS.getChat (puppeteer_evaluation_script:439:49) at puppeteer_evaluation_script:2:40

Steps to Reproduce the Bug or Issue

I did everything tried everything whatever you dream. but still same.

Relevant Code

Evaluation failed: Error: wid error: invalid wid at e (https://web.whatsapp.com/:2:4911) at new f (https://web.whatsapp.com/app.d61b42d11fedf617d4e8.js:311:221995) at Object.d [as createWid] (https://web.whatsapp.com/app.d61b42d11fedf617d4e8.js:311:228400) at Object.window.WWebJS.getChat (puppeteer_evaluation_script:439:49) at puppeteer_evaluation_script:2:40

Browser Type

Chromium

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

Evaluation failed: Error: wid error: invalid wid at e (https://web.whatsapp.com/:2:4911) at new f (https://web.whatsapp.com/app.d61b42d11fedf617d4e8.js:311:221995) at Object.d [as createWid] (https://web.whatsapp.com/app.d61b42d11fedf617d4e8.js:311:228400) at Object.window.WWebJS.getChat (puppeteer_evaluation_script:439:49) at puppeteer_evaluation_script:2:40

Additional context

really needed help

alechkos commented 8 months ago

@ceekfadeblack The group ID have to be in a format XXXXXXXXXX@g.us, i.e.: 123456789@g.us

alechkos commented 8 months ago

const groupId = 'DoJ7CWhXvXtAF6YcEacxxx'; // New group ID

This in invalid group ID

ceekfadeblack commented 8 months ago

I have tried bro still same you can try if you want my mode.

alechkos commented 8 months ago

@ceekfadeblack To find your group ID use this:


// ...

const groupId = (await client.getChats()).filter((x) => x.isGroup && x.name === 'YOUR GROUP NAME')[0].id._serialized;

// ...
alechkos commented 8 months ago

@ceekfadeblack

const numberId = await client.getNumberId(phoneNumber);

if (numberId) {
    await groupChat.addParticipants([numberId._serialized]);
    console.log(`Step 7: Phone number ${phoneNumber} has been successfully added to the group.`);
}
ceekfadeblack commented 8 months ago

MATE! YOU SAVE MY HOURS. I LOVE YOU. THANK YOU!

ceekfadeblack commented 8 months ago

I wanna do something for you. Please tell me brother.. thanks alot!

alechkos commented 8 months ago

@ceekfadeblack

MATE! YOU SAVE MY HOURS. I LOVE YOU. THANK YOU!

You are welcome :)