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.58k forks source link

GroupNotification.getChat() does not work in the client.on("group_join") Event #1329

Closed LighteningCode closed 2 years ago

LighteningCode commented 2 years ago

Is there an existing issue for this?

Describe the bug

Seems like the GroupNofication.getChat() function does not work

client.on("group_join", async (notification) => {
  console.log("SOMEONEs JOINED", notification);
  try {
    const chat = await notification.getChat()

    const welcome = "Welcome to the group"

    await chat.sendMessage(welcome)
  } catch (error) {
    console.log("Something went wrong", error);
  }
});

error that is displayed

Error: Evaluation failed: TypeError: Cannot read properties of undefined (reading 'match')
    at new c (https://web.whatsapp.com/bootstrap_qr.79aba964bea38dbcc501.js:65:24872)
    at Object.s [as createWid] (https://web.whatsapp.com/bootstrap_qr.79aba964bea38dbcc501.js:65:29471)
    at **Object.window.WWebJS.getChat** (__puppeteer_evaluation_script__:342:49)
    at __puppeteer_evaluation_script__:2:40
    at ExecutionContext._evaluateInternal (C:\Users\elvis\Desktop\elvis\testing-whatsapp-bot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:221:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async ExecutionContext.evaluate (C:\Users\elvis\Desktop\elvis\testing-whatsapp-bot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:110:16)
    at async Client.getChatById (C:\Users\elvis\Desktop\elvis\testing-whatsapp-bot\node_modules\whatsapp-web.js\src\Client.js:646:20)
    at async Client.<anonymous> (C:\Users\elvis\Desktop\elvis\testing-whatsapp-bot\index.js:386:18)

Expected behavior

I expect the to be able to get the chat of the Notification and then send a welcome message to all the new members

Steps to Reproduce the Bug or Issue

  1. Add a new user to a group

Relevant Code

client.on("group_join", async (notification) => {
  console.log("SOMEONEs JOINED", notification);
  try {
    const chat = await notification.getChat()

    const welcome = "Welcome to the group"

    await chat.sendMessage(welcome)
  } catch (error) {
    console.log("Something went wrong", error);
  }
});

Browser Type

Chromium

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

Yes, I am using Multi Device

Environment

Additional context

No response

DALIHILLARY commented 2 years ago

In the notification, the chatId is "undefined" GroupNotification { id: { fromMe: false, remote: '120783039379794960@g.us', id: '3EB010BEAECF2569DF64', participant: 'xxxxxxxxxxxxx@c.us', _serialized: 'false_120783039379794960@g.us_3EB010BEAECF2569DF64_xxxxxxxxxx@c.us' }, body: '', type: 'remove', timestamp: 1647416043, chatId: undefined, author: 'xxxxxxxxxxx@c.us', recipientIds: [ 'xxxxxxxxxx@c.us' ] } When can get this from notification.id.remote And then send messages const chatId = notification.id.remote; await client.sendMessage(chatId,"Hello Welcome");

LighteningCode commented 2 years ago

Anyone 👀 @pedroslopez ....any information?

DALIHILLARY commented 2 years ago

Anyone eyes @pedroslopez ....any information?

const chatId = notification.id.remote
const groupChat = await client.getChatById(chatId)

This will get you the chat object so you can work with it. This is what am using

LighteningCode commented 2 years ago

Alright thank you

Qwabena-Proxy commented 2 years ago

In the notification, the chatId is not defined GroupNotification { id: { fromMe: false, remote: '120783039379794960@g.us', id: '3EB010BEAECF2569DF64', participant: 'xxxxxxxxxxxxx@c.us', _serialized: 'false_120783039379794960@g.us_3EB010BEAECF2569DF64_xxxxxxxxxx@c.us' }, body: '', type: 'remove', timestamp: 1647416043, chatId: undefined, author: 'xxxxxxxxxxx@c.us', recipientIds: [ 'xxxxxxxxxx@c.us' ] } When can get this from notification.id.remote And then send messages const chatId = notification.id.remote; await client.sendMessage(chatId,"Hello Welcome");

@LighteningCode Is right

LighteningCode commented 2 years ago

That should work, if doesn't, use the one above to send the message.

@Qwabena-Proxy