You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
export function normalizeUserIdentifier(input: string): string {
|
|
console.log('Normalizing user identifier:', input);
|
|
|
|
// Handle group JID format (120363418564126395@g.us)
|
|
if (input.endsWith('@g.us')) {
|
|
return input; // Return group JID as-is
|
|
}
|
|
|
|
// Handle user JID format (12345678@s.whatsapp.net)
|
|
// if (input.includes('@s.whatsapp.net')) {
|
|
// const [phone, domain] = input.split('@');
|
|
// const cleanPhone = phone.replace(/\D/g, '');
|
|
// return `${cleanPhone}@${domain}`;
|
|
// }
|
|
if (input.includes('@s.whatsapp.net')) {
|
|
return input.split('@')[0];
|
|
}
|
|
|
|
// Handle raw input (phone number or @mention)
|
|
const cleanInput = input.startsWith('@') ? input.slice(1) : input;
|
|
const phoneNumber = cleanInput.replace(/\D/g, '');
|
|
|
|
// Validate phone number length
|
|
if (phoneNumber.length < 8 || phoneNumber.length > 15) {
|
|
throw new Error('Número de teléfono inválido');
|
|
}
|
|
|
|
const jid = `${phoneNumber}@s.whatsapp.net`;
|
|
console.log('Normalized JID:', jid);
|
|
return jid;
|
|
}
|
|
|
|
export function extractUserFromJid(jid: string): string {
|
|
// Extract phone number from JID (format: 12345678@s.whatsapp.net)
|
|
const phoneNumber = jid.split('@')[0];
|
|
return normalizeUserIdentifier(phoneNumber);
|
|
}
|
|
|
|
export function formatUserMention(phoneNumber?: string): string {
|
|
if (!phoneNumber) {
|
|
return '@unknown';
|
|
}
|
|
|
|
// Handle full JID format
|
|
if (phoneNumber.includes('@s.whatsapp.net')) {
|
|
return `@${phoneNumber.split('@')[0]}`;
|
|
}
|
|
|
|
// Handle already formatted mentions
|
|
if (phoneNumber.startsWith('@')) {
|
|
return phoneNumber;
|
|
}
|
|
|
|
// Handle raw numbers
|
|
return `@${phoneNumber.replace(/\D/g, '')}`;
|
|
}
|