How to add new lines in sending Whatsapp messages via Twilio Content Template Sending

91 Views Asked by At

In https://www.twilio.com/docs/content/send-templates-created-with-the-content-template-builder explains how to send whatsapp messages.

How do we send messages with newlines in placeholder values?

The current sample is:

        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
        Message message = Message.creator(
                new com.twilio.type.PhoneNumber("whatsapp:+18551234567"),
                new com.twilio.type.PhoneNumber("whatsapp:+15551234567"),
                "HXXXXXXXXX")
            .setContentVariables("{\"1\":\"Name\"}")
            .setMessagingServiceSid("MGXXXXXXXX")
            .create();

What is the proper way to add new lines to Twilio Whatsapp messaging?

Tried .setContentVariables("{\"1\":\"Name\\nSex\"}") and caused an error.

2

There are 2 best solutions below

2
IObert On

I assume you are getting error 63005 which means WhatsApp rejected the message. This could be for a number of reasons, but it's very likely, that it happened because of the line break.

Instead, I would recommend adding the line break in the template and a second content variable, or comma-separate the values.

0
Hugo Antunes On

I think the problem is because you didn't set the contentSid.

Try something like this

Message.creator(
                new com.twilio.type.PhoneNumber("whatsapp:+18551234567"),
                new com.twilio.type.PhoneNumber("whatsapp:+15551234567"),
                "")
            .setContentVariables("{\"1\":\"Name\"}")
            .setContentSid("HXXXXXXXXX")
            .setMessagingServiceSid("MGXXXXXXXX")
            .create();

Please also validate your version. I am currently using version 10.1.0