wppconnect-team / wppconnect

WPPConnect is an open source project developed by the JavaScript community with the aim of exporting functions from WhatsApp Web to the node, which can be used to support the creation of any interaction, such as customer service, media sending, intelligence recognition based on phrases artificial and many other things, use your imagination
https://wppconnect.io
Other
1.89k stars 332 forks source link

Link With Phone Number #1921

Closed emdadgar2 closed 7 months ago

emdadgar2 commented 11 months ago

Hi

Is there any plan to use "Link with Phone Number" instead of QRCode?

image

icleitoncosta commented 11 months ago

Hi @edgardmessias , I took a look at how I could implement this function in the library, but I encountered a problem.

Since the client has not been started yet, it is not possible to call its functions to generate the login number. The only alternative I thought of would be to pass it as a parameter in the session create, but this would require the user to enter the phone number in the create as well. Do you have any other alternative implementation suggestions?

fereinaux commented 10 months ago

that would be great to create the session with an optional number parameter, to cases where its needed to start a session from the mobile phone where whatsapp is installed

emdadgar2 commented 9 months ago

Hi @edgardmessias , I took a look at how I could implement this function in the library, but I encountered a problem.

Since the client has not been started yet, it is not possible to call its functions to generate the login number. The only alternative I thought of would be to pass it as a parameter in the session create, but this would require the user to enter the phone number in the create as well. Do you have any other alternative implementation suggestions?

Phone number can be set in "wppconnect.create" call as a parameter

aymansyam commented 7 months ago

I've tested locally using wa-js, and I'm pretty sure it's doable. It even worked once while debugging, although I cannot replicate it for some reason. It goes something like this:

if (this.options.phoneNumber !== null) {
  this.logger.info(
    'Logging in with Phone Number. Please Wait..................'
  );
  const code = await this.page.evaluate(() => {
    return WPP.conn.genLinkDeviceCodeForPhoneNumber('<phone number>');
  });
  this.logger.info(`Code generated: ${code}`);
}

I'm guessing it should be somewhere inside initializer.ts or host.layer.ts (I tried both), but I cannot get it to work. This feature would be awesome to have !

icleitoncosta commented 7 months ago

I've tested locally using wa-js, and I'm pretty sure it's doable. It even worked once while debugging, although I cannot replicate it for some reason. It goes something like this:

if (this.options.phoneNumber !== null) {
  this.logger.info(
    'Logging in with Phone Number. Please Wait..................'
  );
  const code = await this.page.evaluate(() => {
    return WPP.conn.genLinkDeviceCodeForPhoneNumber('<phone number>');
  });
  this.logger.info(`Code generated: ${code}`);
}

I'm guessing it should be somewhere inside initializer.ts or host.layer.ts (I tried both), but I cannot get it to work. This feature would be awesome to have !

Hello, I have already created the code, however, it is being tested locally. I will implement it soon, the code is very close to what you did, however, I have to make adjustments to prevent injection errors.

icleitoncosta commented 7 months ago

Please, test this PR https://github.com/wppconnect-team/wppconnect/pull/2097

aymansyam commented 7 months ago

Will do, also it so happens that I worked on it as well, and here is what I ended up with

under host.layer.ts in HostLayer I declared a couple of variables:

  protected phoneLinkCode = null;
  protected didGetCode = false;
  protected sendPhoneLinkInterval?: NodeJS.Timer = null;

Inside the start() function:


  public async start() {
    if (this.isStarted) {
      return;
    }

    this.isStarted = true;

    await initWhatsapp(
      this.page,
      null,
      false,
      this.options.whatsappVersion,
      this.log.bind(this)
    );

    await this.page.exposeFunction('checkQrCode', () => this.checkQrCode());
    await this.page.exposeFunction('checkInChat', () => this.checkInChat());

    this.checkStartInterval = setInterval(() => this.checkStart(), 5000);
    // phoneNumber is defined, generate the code for login
    if (this.options.phoneNumber !== null) {
      this.sendPhoneLinkInterval = setInterval(async () => {
        await this.sendPhoneLink();

        // clear the interval
        // No need to start a new interval process, phoneLinkCode should be valid for 5 minutes
        if (this.phoneLinkCode !== null) {
          clearInterval(this.sendPhoneLinkInterval as NodeJS.Timeout);
        }
      }, 5000);
    }

    this.page.on('close', () => {
      clearInterval(this.checkStartInterval as NodeJS.Timeout);
    });
  }

defined a sendPhoneLink function:

  protected async sendPhoneLink() {
    try {
      const connected = await isAuthenticated(this.page);
      if (connected) {
        this.phoneLinkCode = null;
        return;
      }
      // Only use genLinkDeviceCodeForPhoneNumber once, otherwise it spams the user
      if (this.phoneLinkCode === null) {
        try {
          this.phoneLinkCode = await this.page.evaluate((phoneNumber) => {
            return WPP.conn.genLinkDeviceCodeForPhoneNumber(phoneNumber);
          }, this.options.phoneNumber);

          if (this.phoneLinkCode) {
            this.logger.info(`Code is: ${this.phoneLinkCode}`);
          }
        } catch (error) {
          this.logger.info('Generating Code...');
        }
      }
    } catch (error) {
      this.logger.info('Generating Code...');
    }
  }

Now we just need to add the config in create-config.ts


  /**
   * Login using phone number
   * @default null
   */
  phoneNumber?: string | null;

I hope this helps, I've tested this and it seems to work quite fine...

Saifallak commented 7 months ago

awesome, waiting for this in @wppconnect-server

aymansyam commented 7 months ago

Same, this would be a great addition in @wppconnect-server

icleitoncosta commented 7 months ago

awesome, waiting for this in @wppconnect-server

Next release of @wppconnect-server

emdadgar2 commented 7 months ago

Thanks for your support