Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(dashboard): read a CSV column as one recipient, not the whole row
Yudhi Armyndharis9dd37a828aff
diff --git a/dashboard/src/utils/bulkRecipients.test.ts b/dashboard/src/utils/bulkRecipients.test.tsindex 224d0cec3..aa82be0b7 100644--- a/dashboard/src/utils/bulkRecipients.test.ts+++ b/dashboard/src/utils/bulkRecipients.test.ts@@ -34,3 +34,33 @@ test('returns an empty list for empty input', () => {assert.deepEqual(parseBulkRecipients(''), []);assert.deepEqual(parseBulkRecipients(' \n \n'), []);});++// A real spreadsheet export. The digit strip used to run over the WHOLE line, so the index column+// and the number were concatenated into a different number that still looked plausible.+test('a multi-column CSV row yields the phone column, not the columns concatenated', () => {+ const csv = 'id,phone\n1,628123456789\n2,628987654321';+ assert.deepEqual(parseBulkRecipients(csv), ['[email protected]', '[email protected]']);+});++test('a comma-separated list on one line is several recipients', () => {+ assert.deepEqual(parseBulkRecipients('628111222333, 628222333444'), ['[email protected]', '[email protected]']);+});++test('semicolon and tab separate entries too', () => {+ assert.deepEqual(parseBulkRecipients('628111222333;628222333444'), ['[email protected]', '[email protected]']);+ assert.deepEqual(parseBulkRecipients('628111222333\t628222333444'), ['[email protected]', '[email protected]']);+});++// The formatting characters must NOT split: this is one number, and the existing test above pins it.+test('phone formatting is not a field separator', () => {+ assert.deepEqual(parseBulkRecipients('+62 (812) 345-6789'), ['[email protected]']);+});++test('a chat ID sitting in a CSV column still passes through', () => {+ assert.deepEqual(parseBulkRecipients('alice,[email protected]'), ['[email protected]']);+});++test('a number too short to be a phone is dropped rather than sent', () => {+ assert.deepEqual(parseBulkRecipients('12345'), []);+ assert.deepEqual(parseBulkRecipients('123456'), ['[email protected]']);+});diff --git a/dashboard/src/utils/bulkRecipients.ts b/dashboard/src/utils/bulkRecipients.tsindex 9d44fd211..f8763a04e 100644--- a/dashboard/src/utils/bulkRecipients.ts+++ b/dashboard/src/utils/bulkRecipients.ts@@ -2,26 +2,52 @@export const BULK_MAX_RECIPIENTS = 100;/**- * Parse the bulk-recipients textarea (one entry per line) into chat IDs: trims whitespace,- * drops blank lines, de-dupes, and normalizes bare phone numbers to `<digits>@c.us`. Lines- * containing '@' are treated as full chat IDs and pass through untouched; lines with no '@'- * and no digits at all are dropped rather than sent as the meaningless '@c.us'.+ * Parse the bulk-recipients textarea into chat IDs: trims whitespace, drops blanks, de-dupes, and+ * normalizes bare phone numbers to `<digits>@c.us`. An entry containing '@' is treated as a full+ * chat ID and passes through untouched; anything else must carry at least MIN_PHONE_DIGITS digits+ * or it is dropped rather than sent as a meaningless or truncated id.+ *+ * Entries are separated by line endings AND by the comma/semicolon/tab a spreadsheet export writes,+ * because the file picker offers `.csv`. Phone-number formatting (spaces, parentheses, hyphens,+ * a leading plus) is not a separator and is stripped within one entry.** All three line endings are accepted. A textarea normalizes CR and CRLF to LF in its own value, but* an uploaded file does not go through one: FileReader hands the bytes over as written, so a file* saved with classic Mac endings arrives as a single bare-CR line.*/+/**+ * Field separators a spreadsheet export uses. Deliberately NOT space, parentheses or hyphen: those+ * are phone-number formatting (`+1 (555) 010-2233` is one entry, not five), and no WhatsApp chat id+ * shape contains a comma, a semicolon or a tab.+ */+const FIELD_SEPARATORS = /[,;\t]/;++/**+ * Fewest digits a bare number may carry to be treated as a phone number, matching the bound the+ * gateway and the session form already enforce (`/^[0-9]{6,15}$/`).+ *+ * It exists because of the separator split above. A spreadsheet row like `1,628123456789` yields an+ * index column alongside the number, and without a floor that `1` would ship as `[email protected]`.+ */+const MIN_PHONE_DIGITS = 6;+export function parseBulkRecipients(text: string): string[] {const seen = new Set<string>();for (const rawLine of text.split(/\r\n?|\n/)) {- const line = rawLine.trim();- if (!line) continue;- if (line.includes('@')) {- seen.add(line);- continue;+ // Split into fields before anything else. The button offers `.csv`, and a row with more than one+ // column used to have its columns' digits CONCATENATED by the strip below: `1,628123456789`+ // became `[email protected]`, a different number that looks entirely plausible in the+ // recipients box. Splitting first keeps each column a candidate on its own.+ for (const rawField of rawLine.split(FIELD_SEPARATORS)) {+ const field = rawField.trim();+ if (!field) continue;+ if (field.includes('@')) {+ seen.add(field);+ continue;+ }+ const digits = field.replace(/[^0-9]/g, '');+ if (digits.length >= MIN_PHONE_DIGITS) seen.add(`${digits}@c.us`);}- const digits = line.replace(/[^0-9]/g, '');- if (digits) seen.add(`${digits}@c.us`);}return [...seen];}