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 63 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 Issues

closes ##3370 fixes #3371

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,
        },
    }
);
client.on('code', (code) => {
    console.log("Linking code:",code);
});
client.initialize();

The pairing code should be displayed to the terminal every 3 minutes (WhatsApp current default). 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 <[email protected]>
@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
src/Client.js Outdated Show resolved Hide resolved
index.d.ts Outdated Show resolved Hide resolved
@wis-dev
Copy link

wis-dev commented Nov 8, 2024

MobCode100 Could you please update your reposirory with last commit?

👉 https://github.com/MobCode100/whatsapp-web.js

Im using it in my package.json and need the commit 723b211.

@MobCode100
Copy link
Author

MobCode100 Could you please update your reposirory with last commit?

👉 https://github.com/MobCode100/whatsapp-web.js

Im using it in my package.json and need the commit 723b211.

Sorry for late. Done! Thank you very much for your review.

@MobCode100
Copy link
Author

@alechkos I've removed the need for custom interval by using regular expression to read the JavaScript content. Hope this can be merged soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet