|
| 1 | +/** |
| 2 | + * VCF (vCard) Parser for WhatsApp contact cards |
| 3 | + * |
| 4 | + * Extracts contact information from .vcf files included in WhatsApp exports |
| 5 | + * These files contain name and phone number for contacts shared in chat |
| 6 | + */ |
| 7 | + |
| 8 | +export interface ContactInfo { |
| 9 | + name: string; |
| 10 | + phoneNumber: string | null; |
| 11 | + whatsappId: string | null; // The waid from WhatsApp |
| 12 | +} |
| 13 | + |
| 14 | +/** |
| 15 | + * Parse a single vCard file content |
| 16 | + * |
| 17 | + * Example VCF content: |
| 18 | + * BEGIN:VCARD |
| 19 | + * VERSION:3.0 |
| 20 | + * N:Fontoura;Claudio;;; |
| 21 | + * FN:Claudio Fontoura |
| 22 | + * item1.TEL;waid=555191786084:+55 51 9178-6084 |
| 23 | + * item1.X-ABLabel:Celular |
| 24 | + * END:VCARD |
| 25 | + */ |
| 26 | +export function parseVcf(content: string): ContactInfo | null { |
| 27 | + const lines = content.split(/\r?\n/); |
| 28 | + |
| 29 | + let name: string | null = null; |
| 30 | + let phoneNumber: string | null = null; |
| 31 | + let whatsappId: string | null = null; |
| 32 | + |
| 33 | + for (const line of lines) { |
| 34 | + // Extract formatted name (FN) |
| 35 | + if (line.startsWith('FN:')) { |
| 36 | + name = line.substring(3).trim(); |
| 37 | + } |
| 38 | + |
| 39 | + // Extract phone number and WhatsApp ID |
| 40 | + // Format: item1.TEL;waid=555192049865:+55 51 9204-9865 |
| 41 | + // Or: TEL;TYPE=CELL:+55 51 9204-9865 |
| 42 | + if (line.includes('TEL')) { |
| 43 | + // Try to extract waid (WhatsApp ID) |
| 44 | + const waidMatch = line.match(/waid=(\d+)/); |
| 45 | + if (waidMatch) { |
| 46 | + whatsappId = waidMatch[1]; |
| 47 | + } |
| 48 | + |
| 49 | + // Extract phone number (after the last colon) |
| 50 | + const colonIndex = line.lastIndexOf(':'); |
| 51 | + if (colonIndex !== -1) { |
| 52 | + phoneNumber = line.substring(colonIndex + 1).trim(); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + if (!name) { |
| 58 | + return null; |
| 59 | + } |
| 60 | + |
| 61 | + return { |
| 62 | + name, |
| 63 | + phoneNumber, |
| 64 | + whatsappId, |
| 65 | + }; |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * Format a phone number for display |
| 70 | + * Keeps the original formatting if it looks good, otherwise tries to clean it up |
| 71 | + */ |
| 72 | +export function formatPhoneNumber(phone: string): string { |
| 73 | + // Already well-formatted (has spaces or dashes) |
| 74 | + if (phone.includes(' ') || phone.includes('-')) { |
| 75 | + return phone; |
| 76 | + } |
| 77 | + |
| 78 | + // Try to format Brazilian numbers (+55...) |
| 79 | + if (phone.startsWith('+55') && phone.length >= 13) { |
| 80 | + const digits = phone.replace(/\D/g, ''); |
| 81 | + // Format: +55 XX XXXXX-XXXX |
| 82 | + if (digits.length === 13) { |
| 83 | + return `+${digits.slice(0, 2)} ${digits.slice(2, 4)} ${digits.slice(4, 9)}-${digits.slice(9)}`; |
| 84 | + } |
| 85 | + // Format: +55 XX XXXX-XXXX (older format) |
| 86 | + if (digits.length === 12) { |
| 87 | + return `+${digits.slice(0, 2)} ${digits.slice(2, 4)} ${digits.slice(4, 8)}-${digits.slice(8)}`; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + // Return as-is if we can't format it |
| 92 | + return phone; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Check if a string looks like a phone number |
| 97 | + */ |
| 98 | +export function isPhoneNumber(str: string): boolean { |
| 99 | + // Remove common separators and check if mostly digits |
| 100 | + const cleaned = str.replace(/[\s\-\+\(\)]/g, ''); |
| 101 | + // Should be at least 8 digits and mostly numeric |
| 102 | + return cleaned.length >= 8 && /^\d+$/.test(cleaned); |
| 103 | +} |
| 104 | + |
| 105 | +/** |
| 106 | + * Extract phone number from WhatsApp ID format |
| 107 | + * WhatsApp IDs are usually just the phone number without formatting |
| 108 | + */ |
| 109 | +export function phoneFromWhatsAppId(waid: string): string { |
| 110 | + // Add + prefix if it's a raw number |
| 111 | + if (/^\d+$/.test(waid)) { |
| 112 | + return `+${waid}`; |
| 113 | + } |
| 114 | + return waid; |
| 115 | +} |
0 commit comments