Skip to content

Commit a06c955

Browse files
committed
fix: Update WhatsApp OTP template to single variable format
WhatsApp authentication templates now only accept a single variable (the OTP code). This change removes the organization name from the OTP template configuration to comply with Twilio/Meta's updated authentication template requirements. Fixes Twilio error 21656: "The ContentVariables you are using are not valid"
1 parent 007dac4 commit a06c955

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

internal/message/twilio_whatsapp_client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ var messageTemplateConfig = map[MessageType]templateMapping{
8383
MessageTypeReceiverOTP: {
8484
requiredVars: map[TemplateVariable]string{
8585
TemplateVarReceiverOTP: "1",
86-
TemplateVarOrgName: "2",
8786
},
8887
},
8988
}
@@ -95,10 +94,11 @@ func formatContentVariables(messageType MessageType, vars map[TemplateVariable]s
9594
return "", fmt.Errorf("unsupported message type %s for WhatsApp template variables", messageType)
9695
}
9796

98-
// Validate all required variables are present
99-
if len(vars) != len(config.requiredVars) {
100-
return "", fmt.Errorf("expected %d template variables for message type %s, got %d",
101-
len(config.requiredVars), messageType, len(vars))
97+
// Validate all required variables are present (extra variables are allowed and ignored)
98+
for templateVar := range config.requiredVars {
99+
if _, ok := vars[templateVar]; !ok {
100+
return "", fmt.Errorf("missing required template variable %s for message type %s", templateVar, messageType)
101+
}
102102
}
103103

104104
// Build content variables map with position mapping

internal/message/twilio_whatsapp_client_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,9 @@ func Test_formatContentVariables(t *testing.T) {
344344
messageType: MessageTypeReceiverOTP,
345345
vars: map[TemplateVariable]string{
346346
TemplateVarReceiverOTP: "123456",
347-
TemplateVarOrgName: "MyOrg",
347+
TemplateVarOrgName: "MyOrg", // extra variable is allowed but ignored
348348
},
349-
wantResult: `{"1":"123456","2":"MyOrg"}`,
349+
wantResult: `{"1":"123456"}`,
350350
},
351351
{
352352
name: "unsupported message type",
@@ -362,17 +362,17 @@ func Test_formatContentVariables(t *testing.T) {
362362
vars: map[TemplateVariable]string{
363363
TemplateVarOrgName: "Test Organization",
364364
},
365-
wantErr: "expected 2 template variables for message type receiver_invitation, got 1",
365+
wantErr: "missing required template variable registration_link for message type receiver_invitation",
366366
},
367367
{
368-
name: "too many variables for receiver OTP",
368+
name: "extra variables are allowed for receiver OTP",
369369
messageType: MessageTypeReceiverOTP,
370370
vars: map[TemplateVariable]string{
371371
TemplateVarReceiverOTP: "123456",
372372
TemplateVarOrgName: "MyOrg",
373373
TemplateVarFirstName: "Extra",
374374
},
375-
wantErr: "expected 2 template variables for message type receiver_otp, got 3",
375+
wantResult: `{"1":"123456"}`, // extra variables are ignored
376376
},
377377
{
378378
name: "incorrect variable for receiver invitation",
@@ -381,7 +381,7 @@ func Test_formatContentVariables(t *testing.T) {
381381
TemplateVarOrgName: "Test Organization",
382382
TemplateVarFirstName: "Wrong Variable",
383383
},
384-
wantErr: "missing template variable registration_link for message type receiver_invitation",
384+
wantErr: "missing required template variable registration_link for message type receiver_invitation",
385385
},
386386
{
387387
name: "incorrect variable for receiver OTP",
@@ -390,13 +390,13 @@ func Test_formatContentVariables(t *testing.T) {
390390
TemplateVarFirstName: "Wrong Variable",
391391
TemplateVarOrgName: "MyOrg",
392392
},
393-
wantErr: "missing template variable receiver_otp for message type receiver_otp",
393+
wantErr: "missing required template variable receiver_otp for message type receiver_otp",
394394
},
395395
{
396396
name: "empty variables map for receiver invitation",
397397
messageType: MessageTypeReceiverInvitation,
398398
vars: map[TemplateVariable]string{},
399-
wantErr: "expected 2 template variables for message type receiver_invitation, got 0",
399+
wantErr: "missing required template variable",
400400
},
401401
}
402402

0 commit comments

Comments
 (0)