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

Link With Phone Number #1921

Closed
emdadgar2 opened this issue Nov 7, 2023 · 11 comments
Closed

Link With Phone Number #1921

emdadgar2 opened this issue Nov 7, 2023 · 11 comments
Labels
enhancement New feature or request needs triage

Comments

@emdadgar2
Copy link

Hi

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

image

@emdadgar2 emdadgar2 added enhancement New feature or request needs triage labels Nov 7, 2023
@icleitoncosta
Copy link
Contributor

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
Copy link

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
Copy link
Author

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
Copy link

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
Copy link
Contributor

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
Copy link
Contributor

Please, test this PR #2097

@aymansyam
Copy link

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
Copy link

awesome, waiting for this in @wppconnect-server

@aymansyam
Copy link

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

@icleitoncosta
Copy link
Contributor

awesome, waiting for this in @wppconnect-server

Next release of @wppconnect-server

@emdadgar2
Copy link
Author

Thanks for your support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs triage
Projects
None yet
Development

No branches or pull requests

5 participants