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

feat: Get the registered chat id in whatsapp #421

Closed
wants to merge 3 commits into from
Closed

feat: Get the registered chat id in whatsapp #421

wants to merge 3 commits into from

Conversation

lucasrosa90
Copy link
Contributor

@lucasrosa90 lucasrosa90 commented Nov 7, 2020

Get the registered chat id in whatsapp.

Reason?

  • In Brazil, after change the rule for cell numbers, WhatsApp didn't change they database for the already registered numbers, just to the new ones, so here we have to get what's the chatId registered in WhatsApp before send the messages.

I'll show it in codes:

Using my phone number, the actual client.isRegisteredUser() return true for the valid and the invalid chatId, you guys can check there:

> npm run shell
wwebjs> client.isRegisteredUser("5548991581407@c.us").then(result => { console.log(`isRegistered: ${result}`); });
Promise { <pending> }
wwebjs> isRegistered: true
wwebjs> client.isRegisteredUser("554891581407@c.us").then(result => { console.log(`isRegistered: ${result}`); });
Promise { <pending> }
wwebjs> isRegistered: true

The numbers are different, the 5548991581407 is the new rule, but in WhatsApp it doesn't exists, so I have to send the message to the old one, without this additional 9.

So, to keep the compability and resolve this problem, with some help from @guicuton I created this function at client.js:

> npm run shell
wwebjs> client.getRegisteredChatId("5548991581407@c.us").then(result => { console.log(`Registered chatId: ${result}`); });
Promise { <pending> }
wwebjs> Registered chatId: 554891581407@c.us

I dont know if other countries has this problem, but in Brazil we have, and it prevent from future problems to another countries too.

@victormga
Copy link
Contributor

lucas, do you have any additional info about if this call causes ban?
in a older version of my code, i had an check like this when importing contacts from a csv file, and i've had a banned wa account.
only happened once, and it was a new whatsapp account, but that made me worried.

anyway, i think this is duplicate because you can call: await window.WWebJS.getNumberId(id); and it returns the real wa id, even here in brazil where the phone numbers had changed.

@lucasrosa90
Copy link
Contributor Author

lucasrosa90 commented Nov 8, 2020

@victormga how can i get window.WWebJS from "new Client()"? And I dont think this can causes ban, bcoz you have to check what's the real number before start a conversation

@lucasrosa90
Copy link
Contributor Author

I did some changes to use that function, but I didnt find another way to export that to use in my client. If there's another way to use that, tell me.

@victormga
Copy link
Contributor

victormga commented Nov 9, 2020

lucas, you can evaluate on client.pupPage.

try {
  const whatsapp_id = await client.pupPage.evaluate((number) => {
     return await window.WWebJS.getNumberId(number);
  }, number);
  console.log(whatsapp_id);
} catch(e) {
  console.error(e);
}

@lucasrosa90
Copy link
Contributor Author

Thanks @victormga , I didnt know that I can do that.

@lucasrosa90 lucasrosa90 closed this Nov 9, 2020
@lucasrosa90 lucasrosa90 deleted the feat/getRegisteredChatId branch November 9, 2020 18:23
@lucasrosa90 lucasrosa90 restored the feat/getRegisteredChatId branch December 18, 2020 01:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants