Repository navigation
feat(whatsapp-bridge): add phone-number pairing code support #17907
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
Open
keiravoss94
wants to merge
2
commits into
NousResearch:main
Choose a base branch
from
pebble-tech:upstream-pr/whatsapp-pair-with-number
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /** E.164-like phone number for pairing: country code plus 10β15 digits total. */ | ||
| export const E164_PAIRING_REGEX = /^\+?[1-9]\d{9,14}$/; | ||
|
|
||
| /** | ||
| * @param {string} raw - CLI value passed to `--pair-with-number` | ||
| * @returns {{ ok: true, digits: string } | { ok: false, error: string }} | ||
| */ | ||
| export function validateE164ForPairing(raw) { | ||
| const s = String(raw ?? '').trim(); | ||
| if (!E164_PAIRING_REGEX.test(s)) { | ||
| return { | ||
| ok: false, | ||
| error: 'Invalid phone number. Use E.164 (e.g. +15551234567): country code, digits only besides an optional leading +.', | ||
| }; | ||
|
Comment on lines
+1
to
+15
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated the validation error to state the 10β15 digit requirement explicitly instead of the generic "Use E.164" wording, matching |
||
| } | ||
| const digits = s.startsWith('+') ? s.slice(1) : s; | ||
| return { ok: true, digits }; | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} code - Raw pairing code from Baileys (typically 8 Crockford chars) | ||
| */ | ||
| export function formatPairingCodeForDisplay(code) { | ||
| const c = String(code || '').replace(/\s/g, '').toUpperCase(); | ||
| if (c.length === 8) return `${c.slice(0, 4)}-${c.slice(4)}`; | ||
| return c; | ||
| } | ||
|
|
||
| /** | ||
| * @param {string[]} argv - e.g. process.argv | ||
| */ | ||
| export function parsePairWithNumberArg(argv) { | ||
| const args = argv.slice(2); | ||
| const idx = args.indexOf('--pair-with-number'); | ||
| if (idx === -1) return { found: false, value: null }; | ||
| const v = args[idx + 1]; | ||
| if (!v || v.startsWith('--')) { | ||
| return { | ||
| found: true, | ||
| value: null, | ||
| error: | ||
| '--pair-with-number requires a phone number argument (E.164, e.g. +15551234567)', | ||
| }; | ||
| } | ||
| return { found: true, value: v }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import test from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
| import { | ||
| formatPairingCodeForDisplay, | ||
| parsePairWithNumberArg, | ||
| validateE164ForPairing, | ||
| } from './pairing-args.js'; | ||
|
|
||
| test('validateE164ForPairing accepts E.164 with +', () => { | ||
| assert.deepEqual(validateE164ForPairing('+15551234567'), { ok: true, digits: '15551234567' }); | ||
| }); | ||
|
|
||
| test('validateE164ForPairing accepts E.164 without +', () => { | ||
| assert.deepEqual(validateE164ForPairing('15551234567'), { ok: true, digits: '15551234567' }); | ||
| }); | ||
|
|
||
| test('validateE164ForPairing accepts UK-format E.164 with +', () => { | ||
| assert.deepEqual(validateE164ForPairing('+447911123456'), { ok: true, digits: '447911123456' }); | ||
| }); | ||
|
|
||
| test('validateE164ForPairing rejects leading zero', () => { | ||
| assert.equal(validateE164ForPairing('+015551234567').ok, false); | ||
| }); | ||
|
|
||
| test('validateE164ForPairing rejects spaces', () => { | ||
| assert.equal(validateE164ForPairing('+1 555 123 4567').ok, false); | ||
| }); | ||
|
|
||
| test('validateE164ForPairing rejects hyphenated numbers', () => { | ||
| assert.equal(validateE164ForPairing('+1-555-1234567').ok, false); | ||
| }); | ||
|
|
||
| test('validateE164ForPairing rejects too-short numbers', () => { | ||
| assert.equal(validateE164ForPairing('+155512345').ok, false); | ||
| }); | ||
|
|
||
| test('formatPairingCodeForDisplay hyphenates length-8 codes', () => { | ||
| assert.equal(formatPairingCodeForDisplay('abcdefgh'), 'ABCD-EFGH'); | ||
| }); | ||
|
|
||
| test('formatPairingCodeForDisplay leaves non-8 length unchanged', () => { | ||
| assert.equal(formatPairingCodeForDisplay('abc'), 'ABC'); | ||
| }); | ||
|
|
||
| test('parsePairWithNumberArg returns found false when absent', () => { | ||
| assert.deepEqual(parsePairWithNumberArg(['node', 'bridge.js']), { found: false, value: null }); | ||
| }); | ||
|
|
||
| test('parsePairWithNumberArg parses value', () => { | ||
| assert.deepEqual( | ||
| parsePairWithNumberArg(['node', 'bridge.js', '--pair-with-number', '+15551234567']), | ||
| { | ||
| found: true, | ||
| value: '+15551234567', | ||
| }, | ||
| ); | ||
| }); | ||
|
|
||
| test('parsePairWithNumberArg errors when value missing', () => { | ||
| const r = parsePairWithNumberArg(['node', 'bridge.js', '--pair-with-number']); | ||
| assert.equal(r.found, true); | ||
| assert.ok(r.error); | ||
| }); | ||
|
|
||
| test('parsePairWithNumberArg errors when value is empty', () => { | ||
| const r = parsePairWithNumberArg(['node', 'bridge.js', '--pair-with-number', '']); | ||
| assert.equal(r.found, true); | ||
| assert.ok(r.error); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed
npm testto pass explicit test file paths (pairing-args.test.mjs allowlist.test.mjs) so Node runs them without shell glob expansion.