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.09k stars 3.6k forks source link

Rules to verify number in Brazil #596

Closed wagnerfillio closed 3 years ago

wagnerfillio commented 3 years ago

I would like to ask a question about phone numbers in Brazil. We have in Brazil the country code 55, plus the DDD (state code) and the phone number with 8 digits. Some time ago a new digit (9) was added to the numbers. But doing an analysis, when searching for a contact on WhatsApp, there are cases that whatsapp does not recognize if I enter the ninth digit and in others that it recognizes. What is the rule for that?

Example: 5531996883193, replace 55319 with 5531 is successful.

Example: 552164180266, replace 5521 with 55219 is successful.

haap500 commented 3 years ago

The rule is as follows: remove the ninth digit for DDD's over 29. For DDD's below 30, add 9 to the phone number if there is no ninth digit.

wagnerfillio commented 3 years ago

I did it this way and it worked .. until I found a case with DDD21 that was not found with 9, but was found without the 9

guicuton commented 3 years ago

You need to have on your mind one key point:

WhatsApp "doesn't care" about number/roaming/portabilty/rules about how the phone numbers are handled on each country.

What do i want mean with this is that the WhatsApp uses our phone numbers as "login" just like Facebook uses our email. So, do not waste your time trying to handle of which state has 9th or not because this will not work and will consume your time.

Example:

Example 2:

So how to handle this on easy way? Getting the serialized id with client.getNumberId() With this function you'll get the registered id.

On my project i use:

var _phoneId = await client.getNumberId("5511933333333")
var _isValid = await client.isRegisteredUser(_phoneId._serialized)
if(_isValid) {
    //here use _phoneId._serialized with valid whatsapp_id 
} else {
    //Handle invalid number
}
haap500 commented 3 years ago

Dear Guicuton: We have known this BUT your code is good when you don't know if your customer's cell phone is registered or not on Whatsapp. Presumably, a company in Brazil has already informed in its files whether its customer's cell phone is registered or not on Whatsapp. So, in that case, why take two steps or procedures when you can apply the rule directly?

guicuton commented 3 years ago

🤔 if we're using this lib to send messages through WhatsApp, why don't check if destination is registered or not on it? Besides, the example that i've show up handle the number serialization about the 9th digit.

Presumably, a company in Brazil has already informed in its files whether its customer's cell phone is registered or not on Whatsapp. So, in that case, why take two steps or procedures when you can apply the rule directly?

For two things:

Note that getNumberId() have a distinct use of isRegisteredUser() The first handle the 9th digit and return the valid id, the second one doesn't.

What i've posted is a simple example to check if number is registered or not on WhatsApp.

Based on my example the if block could be only with the _phoneId since the getNumberId() return null if given id isn't registered

pedroslopez commented 3 years ago

@guicuton You shouldn't need to use isRegisteredUser if you're already using getNumberId. Internally they use the exact same function from WhatsApp so getNumberId covers both cases.

@wagnerfillio From what I've discussed with Brazilian folks, getNumberId should be enough to correctly get the WhatsApp ID for a number, regardless of extra 9s.

wagnerfillio commented 3 years ago

@pedroslopez Before, I used only isRegisteredUser and, although whatsapp returned true, it was actually a false positive. So, with comments on this topic and other research, I came up with getNumberId.

I was wondering how can I get the id using getNumberId.

As in the example below, I return contactId and it works, but if I try to returncontactId._serielized I get a type error.

export interface ContactId {
  server: string;
  user: string;
  _serialized: string;
}

const checkIsValidConnection = async (
  number: string
): Promise<ContactId | null> => {
  if (!number.endsWith("@c.us")) {
    number += "@c.us";
  }

  const defaultConnection = await GetDefaultConnection();
  const wbot = getWbot(defaultConnection.id);

  const contactId = await wbot.getNumberId(`${number}`);

  if (!contactId) {
    throw new AppError("No number found with this ID.");
  }

  return contactId;
};

export default checkIsValidConnection;

Error if return contactId._serialized image

That would be enough for me!

pedroslopez commented 3 years ago

@wagnerfillio I believe your issue when returning contactId._serialized is that you've specified that the function should return Promise<ContactId | null>. You would need to change this to return Promise<string>

wagnerfillio commented 3 years ago

@pedroslopez Promise <string> could not be returned, because contactId is an object.

So I used it like this example here https://github.com/pedroslopez/whatsapp-web.js/blob/master/index.d.ts

Where do you have this code:

export interface ContactId {
        server: string,
        user: string,
        _serialized: string,
    }

/** Get the registered WhatsApp ID for a number. Returns null if the number is not registered on WhatsApp. */
getNumberId(number: string): Promise<ContactId | null>
pedroslopez commented 3 years ago

@pedroslopez Promise <string> could not be returned, because contactId is an object.

So I used it like this example here https://github.com/pedroslopez/whatsapp-web.js/blob/master/index.d.ts

Where do you have this code:


export interface ContactId {

        server: string,

        user: string,

        _serialized: string,

    }

/** Get the registered WhatsApp ID for a number. Returns null if the number is not registered on WhatsApp. */

getNumberId(number: string): Promise<ContactId | null>

Yes contactId is an object, but you're trying to return contactId._serialized, which is a string.

wagnerfillio commented 3 years ago

You're right, thanks for the time..

pedropuppim commented 2 years ago

getNumberId() saved my life! Thank's