Error sending message with Twilio and WhatsApp using template: "Failed to send freeform message..."

49 Views Asked by At

I'm encountering an error when trying to send a message using Twilio and WhatsApp. I'm following the guidelines for using message templates, but I receive the following error:

Failed to send freeform message because you are outside the allowed window. If you are using WhatsApp, please use a Message Template.

I've verified that I'm using a pre-approved message template, but the issue persists.

Here's the code snippet I'm using with Postman:

curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json" \
--data-urlencode "Body=Here's that picture of an owl you requested." \
--data-urlencode "MediaUrl=https://demo.twilio.com/owl.png" \
--data-urlencode "From=whatsapp:+14155238886" \
--data-urlencode "To=whatsapp:+15017122661" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN

I'm aiming to successfully send the message using Twilio and WhatsApp via the provided template. Any insights or assistance in resolving this error would be greatly appreciated.

1

There are 1 best solutions below

0
IObert On

You are getting this error because your message doesn't exactly match the template string. This could be due to whitespaces or related to the image attachment you want to send.

I'd recommend using the Content Templates as they are easier to use since you just need to refer to the template ID. Also, please note that the WhatsApp templates you are using will be deprecated later this year.

This is how you could use the replacement technology:

CONTENTVARIABLES=$(cat << EOF
{
    "1": "YOUR_VARIABLE1",
    "2": "YOURVARIABLE2"
}
EOF
)

curl -X POST "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages.json" \
--data-urlencode "From=MGXXXXXXXXX" \
--data-urlencode "ContentSid=HX*******" \
--data-urlencode "ContentVariables=$CONTENTVARIABLES" \
--data-urlencode "To=whatsapp:+18005551234" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN