Skip to content

Commit 5c0c89c

Browse files
committed
fix(whatsapp): support text header parameters
1 parent 9c7a125 commit 5c0c89c

2 files changed

Lines changed: 35 additions & 16 deletions

File tree

src/template.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,13 @@ const bodyHasVariables = (template: WhatsAppMessageTemplate): boolean => {
123123
return body ? body.text.match(VARIABLE_REGEX) !== null : false;
124124
};
125125

126+
const textHeaderVariables = (template: WhatsAppMessageTemplate): string[] => {
127+
const header = findComponentByType(template, 'HEADER');
128+
if (header?.format !== 'TEXT' || !header.text) return [];
129+
130+
return extractVariables(header.text);
131+
};
132+
126133
// Collects the URL/COPY_CODE buttons that require a user-supplied parameter,
127134
// preserving their positional index (sparse array).
128135
const buildButtonParams = (
@@ -158,22 +165,20 @@ const buildButtonParams = (
158165
};
159166

160167
/**
161-
* Builds the empty WhatsApp processed_params scaffold for a template: body keys,
162-
* a media header block, and the button parameters that need filling.
168+
* Builds the empty WhatsApp processed_params scaffold for a template: text or
169+
* media header values, body keys, and button parameters that need filling.
163170
*/
164171
export const buildWhatsAppProcessedParams = (
165172
template: WhatsAppMessageTemplate
166173
): WhatsAppProcessedParams => {
167174
const params: WhatsAppProcessedParams = {};
168175

169176
const body = findComponentByType(template, 'BODY');
170-
if (!body) return params;
171-
172-
const matchedVariables = body.text.match(VARIABLE_REGEX);
173-
if (matchedVariables) {
177+
const bodyVariables = body ? extractVariables(body.text) : [];
178+
if (bodyVariables.length > 0) {
174179
const bodyParams: Record<string, string> = {};
175-
matchedVariables.forEach(variable => {
176-
bodyParams[processVariable(variable)] = '';
180+
bodyVariables.forEach(variable => {
181+
bodyParams[variable] = '';
177182
});
178183
params.body = bodyParams;
179184
}
@@ -182,6 +187,14 @@ export const buildWhatsAppProcessedParams = (
182187
const format = getMediaType(template);
183188
params.header = { media_url: '', media_type: format };
184189
if (format === 'document') params.header.media_name = '';
190+
} else {
191+
const headerVariables = textHeaderVariables(template);
192+
if (headerVariables.length > 0) {
193+
params.header = {};
194+
headerVariables.forEach(variable => {
195+
params.header![variable] = '';
196+
});
197+
}
185198
}
186199

187200
const buttons = buildButtonParams(template);
@@ -191,22 +204,27 @@ export const buildWhatsAppProcessedParams = (
191204
};
192205

193206
/**
194-
* Whether every required WhatsApp input has been filled. A template with no body
195-
* variables and no media header is considered complete even if it has buttons
196-
* (mirrors the composer's validation).
207+
* Whether every required WhatsApp input has been filled. A template with no
208+
* text variables and no media header is considered complete even if it has
209+
* buttons (mirrors the composer's validation).
197210
*/
198211
export const isWhatsAppComplete = (
199212
template: WhatsAppMessageTemplate,
200213
processedParams: WhatsAppProcessedParams
201214
): boolean => {
202-
const hasVariables = bodyHasVariables(template);
215+
const hasBodyVariables = bodyHasVariables(template);
216+
const headerVariables = textHeaderVariables(template);
203217
const media = hasMediaHeader(template);
204218

205-
if (!hasVariables && !media) return true;
219+
if (!hasBodyVariables && headerVariables.length === 0 && !media) return true;
206220

207221
if (media && !processedParams.header?.media_url) return false;
208222

209-
if (hasVariables && processedParams.body) {
223+
if (headerVariables.some(variable => !processedParams.header?.[variable])) {
224+
return false;
225+
}
226+
227+
if (hasBodyVariables && processedParams.body) {
210228
const hasEmptyBodyVariable = Object.keys(processedParams.body).some(
211229
key => !processedParams.body?.[key]
212230
);

src/types/template.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,9 @@ export interface TemplateButtonParam {
6767
export interface WhatsAppProcessedParams {
6868
body?: Record<string, string>;
6969
header?: {
70-
media_url: string;
71-
media_type: string;
70+
[key: string]: string | undefined;
71+
media_url?: string;
72+
media_type?: string;
7273
media_name?: string;
7374
};
7475
buttons?: TemplateButtonParam[];

0 commit comments

Comments
 (0)