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: Switch between QR or phone number pairing method #3180

Open
wants to merge 54 commits into
base: main
Choose a base branch
from

Conversation

MobCode100
Copy link

@MobCode100 MobCode100 commented Jul 3, 2024

PR Details

The ability to change the preferred login/authentication method either use QR code or pair with a phone number (Linked devices -> Link a device -> Link with phone number instead).

Description

  1. Added new option pairWithPhoneNumber which is an object containing the parameters for requestPairingCode function in Client.js. The attributes of this object are:
    • phoneNumber: Phone number in international, symbol-free format <COUNTRY_CODE><PHONE_NUMBER>
    • showNotification: Show notification to pair for the specified phone number
    • intervalMs: The interval in milliseconds on how frequent to generate pairing code (WhatsApp default to 3 minutes)
  2. On code and window.onCodeReceivedEvent event will run a callback once a code has been generated.

The inject function in Client.js will check if the phoneNumber option is set, and will switch to phone number pairing mode without running any QR related functions. intervalMs is the time to wait for the next generation of code to mitigate the problem of expired code.

Related Issue

closes #1787 resolves #3316

Motivation and Context

  1. Make it easier to user the requestPairingCode without having to manually utilize the function
  2. The code feels cleaner this way, normalizing the authentication method of QR and phone number
  3. Automatically generate new pairing code on/before expired.

How Has This Been Tested

To run or test this feature, run node app.js where app.js contains the following code:

const { Client, LocalAuth } = require('whatsapp-web.js');
const client = new Client(
    {
        puppeteer: {
            args: ['--no-sandbox'],
            headless: false
        },
        authStrategy: new LocalAuth({ dataPath: "." }),
        pairWithPhoneNumber: {
            phoneNumber: "12324354646",
            showNotification: true,
            intervalMs: 180000
        },
    }
);
client.on('code', (code) => {
    console.log("Linking code:",code);
});
client.initialize();

The pairing code should be displayed to the terminal every 3 minutes. Developer tools are used to inspect some variables while running in non-headless mode. So far, no issues have been encountered during testing on QR code functions or any other existing features.

Types of changes

  • Dependency change
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist

  • My code follows the code style of this project.
  • I have updated the documentation accordingly (index.d.ts).

PurpShell and others added 30 commits March 14, 2024 11:15
…edroslopez#3001)

* Change window.Store.ProfilePic.profilePicFind to window.Store.ProfilePic.requestProfilePicFromServer

* Prevent breaking v2.2x

* refactor: make it more readable

---------

Co-authored-by: alechkos <93551621+alechkos@users.noreply.github.com>
@MobCode100

This comment was marked as off-topic.

@ojauumdev

This comment was marked as outdated.

@MobCode100

This comment was marked as off-topic.

@AKwoKWH
Copy link

AKwoKWH commented Jul 14, 2024

This is great! thanks

@alechkos alechkos changed the base branch from webpack-exodus to main July 24, 2024 01:23
@M-Sakr
Copy link

M-Sakr commented Aug 17, 2024

When this pull request will be merged?
It is important for us to use it.

@MobCode100
Copy link
Author

While waiting for merge, anyone who wants to use this feature can do so by installing this version:
npm install https://github.com/MobCode100/whatsapp-web.js.git

I'll do my best to keep it updated with the main branch of this project.

@GuilhermeRX
Copy link

This merge is really very useful and would help a lot!!
Congratulations on the work @MobCode100

@pedroslopez

@MobCode100 MobCode100 closed this Oct 1, 2024
@MobCode100 MobCode100 deleted the webpack-exodus branch October 1, 2024 10:31
@MobCode100 MobCode100 restored the webpack-exodus branch October 1, 2024 10:32
@MobCode100 MobCode100 reopened this Oct 1, 2024
@alechkos alechkos changed the title (webpack-exodus) Switch between QR or phone number pairing method feat: Switch between QR or phone number pairing method Oct 4, 2024
Comment on lines +371 to +380
if (window.codeInterval) {
clearInterval(window.codeInterval); // remove existing interval
}
window.codeInterval = setInterval(async () => {
if (window.AuthStore.AppState.state != 'UNPAIRED' && window.AuthStore.AppState.state != 'UNPAIRED_IDLE') {
clearInterval(window.codeInterval);
return;
}
window.onCodeReceivedEvent(await getCode());
}, intervalMs);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MobCode100 Is there a way we could get rid of this custom interval and use the WhatsApp one? Maybe there is an event that triggered once the code is expired and we can subscribe to this event?

Copy link
Author

@MobCode100 MobCode100 Oct 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anyway to find out? Using inspect element or some tool? I will do my own research on this later.

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.

TypeError: client.requestPairingCode is not a function Use phone number instead of scan qr code