facebook-graph-apinewlinewhatsappwhatsapp-cloud-api

Cannot send Whatsapp messages with new-lines via Facebook Graph API


I want to print dynamic multiline text in whatsapp messages. The messages are sent via Facebook Graph API. I have used the \\n character to insert a new line. See request below.

Request:

POST https://graph.facebook.com/v20.0/XXXXXXXXXXXXXXXX/messages

Content Type: application/json; charset=UTF-8

Body:
{"messaging_product":"whatsapp","to":"XXXXXXXXXXX","type":"template","template":{"language":{"code":"en"},"name":"order_updated_message","components":[{"type":"body","parameters":[{"type":"text","text":"Jon Doe"},{"type":"text","text":2},{"type":"text","text":" *1. Wrapped Plate, 8 in | 1 Pc*\\nRate: 3.000\\nQty: 8000\\nAmount: 24000.00\\n\\n *2. Bottle Brush | 1 Pc*\\nRate: 18.644\\nQty: 11\\nAmount: 242.00"},{"type":"text","text":24242.00},{"type":"text","text":"John Lee"},{"type":"text","text":"+91XXXXXXXXX"}]}]}}

The request succeeded till a few days ago, but then kept failing. See the failure response below.

Response:

HTTP Status: 400 Bad Request

{
    "error": {
        "message": "(#100) Invalid parameter",
        "type": "OAuthException",
        "code": 100,
        "error_data": {
            "messaging_product": "whatsapp",
            "details": "Param text cannot have new-line/tab characters or more than 4 consecutive spaces"
        },
        "fbtrace_id": "XXXXXXXXXXXXXXXXXXXX"
    }
}

If I remove the \\n character, the request succeeds. So Im assuming the failure is caused by \\n itself.

I also tried replacing \\n with the following:

Can you tell me what has gone wrong?

Note: The message body contains our customer's ordered items. The number of items can vary by order and hence the number of lines can vary in each message. Thus, I cannot add new lines in the message template.


Solution

  • I've run into this issue too. It looks like WhatsApp has changed the rules. They used to allow \\n in the parameter data, but now it seems that's no longer allowed.

    I did find a workaround. You can use \r instead. It's a carriage return, which moves the cursor back to the start of the line without adding a new line.