Fix number format issues

This commit is contained in:
Daniel Gradman-Svendsen 2025-11-17 09:58:53 +01:00
parent deedec1283
commit 3207c84296
2 changed files with 48 additions and 0 deletions

View File

@ -22,6 +22,11 @@ module.exports = function(RED) {
// Convert to string if number
numb = typeof numb === 'number' ? numb.toString() : numb;
// Check if it's already a formatted ID (contains @)
if (numb.includes('@')) {
return numb;
}
// For group IDs, preserve the hyphen (format: NUMBER-TIMESTAMP@g.us)
// Only strip non-digits and non-hyphens for cleaning
numb = numb.replace(/[^\d-]/g, '');
@ -31,6 +36,13 @@ module.exports = function(RED) {
throw new Error('Invalid number format');
}
// Check if it looks like a group ID (contains hyphen)
if (numb.includes('-')) {
// It's a group ID, format it directly without validation
return `${numb}@g.us`;
}
// It's a regular phone number, validate with getNumberId
try {
var numbID = await node.waClient.getNumberId(numb);
if(numbID) {
@ -57,6 +69,11 @@ module.exports = function(RED) {
// Convert to string if number
numb = typeof numb === 'number' ? numb.toString() : numb;
// Check if it's already a formatted ID (contains @)
if (numb.includes('@')) {
return numb;
}
// For group IDs, preserve the hyphen (format: NUMBER-TIMESTAMP@g.us)
// Only strip non-digits and non-hyphens for cleaning
numb = numb.replace(/[^\d-]/g, '');
@ -66,6 +83,13 @@ module.exports = function(RED) {
throw new Error('Invalid number format');
}
// Check if it looks like a group ID (contains hyphen)
if (numb.includes('-')) {
// It's a group ID, format it directly
return `${numb}@g.us`;
}
// It's a regular phone number, validate with onWhatsApp
try {
const [result] = await (await node.waClient).onWhatsApp(numb)
if (result?.exists){

View File

@ -23,6 +23,11 @@ module.exports = function(RED) {
// Convert to string if number
numb = typeof numb === 'number' ? numb.toString() : numb;
// Check if it's already a formatted ID (contains @)
if (numb.includes('@')) {
return numb;
}
// For group IDs, preserve the hyphen (format: NUMBER-TIMESTAMP@g.us)
// Only strip non-digits and non-hyphens for cleaning
numb = numb.replace(/[^\d-]/g, '');
@ -32,6 +37,13 @@ module.exports = function(RED) {
throw new Error('Invalid number format');
}
// Check if it looks like a group ID (contains hyphen)
if (numb.includes('-')) {
// It's a group ID, format it directly without validation
return `${numb}@g.us`;
}
// It's a regular phone number, validate with getNumberId
try {
var numbID = await node.waClient.getNumberId(numb);
if(numbID) {
@ -58,6 +70,11 @@ module.exports = function(RED) {
// Convert to string if number
numb = typeof numb === 'number' ? numb.toString() : numb;
// Check if it's already a formatted ID (contains @)
if (numb.includes('@')) {
return numb;
}
// For group IDs, preserve the hyphen (format: NUMBER-TIMESTAMP@g.us)
// Only strip non-digits and non-hyphens for cleaning
numb = numb.replace(/[^\d-]/g, '');
@ -67,6 +84,13 @@ module.exports = function(RED) {
throw new Error('Invalid number format');
}
// Check if it looks like a group ID (contains hyphen)
if (numb.includes('-')) {
// It's a group ID, format it directly
return `${numb}@g.us`;
}
// It's a regular phone number, validate with onWhatsApp
try {
const [result] = await (await node.waClient).onWhatsApp(numb)
if (result?.exists){