Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • \n --> same error
  • \u000A --> same error
  • %0A --> character displayed as it is. (No new-line)

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.

like image 278
Somanna Avatar asked Oct 30 '25 08:10

Somanna


2 Answers

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.

like image 164
Khalid Yahya Avatar answered Nov 01 '25 12:11

Khalid Yahya


as per the latest update, new lines and line breaks are not supported in the WhatsApp API.

As an alternative:

If you want to pass variables in the next line, you will need to use different variables for each line.

Alternatively, you can add spaces to separate text without using line break parameters

like image 20
Dhruvi Zalavadiya Avatar answered Nov 01 '25 14:11

Dhruvi Zalavadiya