voxter
voxter

Reputation: 925

Facebook Messenger API:how to break line in a message

In Facebook Messenger chat,we can break a line by press "SHIFT+ENTER".
So how to break line by Facebook Graph API(Messenger API).
I've seen in a few answers that the Graph API accepts <center></center> instead of <br> and some other parts of their API seem to accept \r\n.

Is there currently any way of sending a line break and if there is where it it documented?

Upvotes: 22

Views: 24526

Answers (9)

Patrick Sierak
Patrick Sierak

Reputation: 189

I had to use \n\n for the line break to work.

e.g.

"Sorry, We don't have any information ragarding this.\n\nSay 'Hi' to startover"

shows following in facebook messenger

Sorry, We don't have any information ragarding this.
Say 'Hi' to startover

Upvotes: 6

Marcin Rapacz
Marcin Rapacz

Reputation: 646

In Python \\n breaks the line as expected.

Upvotes: 1

vinay mavi
vinay mavi

Reputation: 652

Use language specific line separators.

Java System.lineseprator 

php PHP_EOL 

Python os.linesep 

Nodejs os.EOL 

When we use special character in string then JSON conversion understand it as part of string.

Upvotes: 2

CodeZi.pro
CodeZi.pro

Reputation: 236

Convert "\n" in your text to "\n" => it working... With Php this is my code: (tested)

$_text = str_replace(array("\r\n", "\r", "\n"), "\\n", $_text); 

Upvotes: 0

ScottWasserman
ScottWasserman

Reputation: 1715

I was trying to get a line break in the welcome text that shows up before users touch Get Started in my Messenger bot. I found that "\n" worked but ONLY in the mobile version of Messenger. It doesn't work on the web at the moment. Assuming that will get fixed at some point because Facebook shows line breaks in their blog post this week (9/12/2016) https://messengerblog.com/bots/messenger-platform-1-2-link-ads-to-messenger-enhanced-mobile-websites-payments-and-more

Upvotes: 3

TommyBs
TommyBs

Reputation: 9646

I'm not 100% sure what language you're using to build your bot, but if you're using PHP then \n needs to be wrapped in double quote strings e.g

  $message = "Message \n with a line break";

using single quotes (') won't work.

Though a better solution if using PHP would be to use the PHP_EOL constant

Whatever language you're using to build your bot may have similar quirks

Upvotes: 2

Kumar Rakesh
Kumar Rakesh

Reputation: 2708

If you are using php, You should be use chr(10) . Its working as like '\n' or '<br>'. Also you can use <center></center> . Its working for me.

Upvotes: 12

DhruvPathak
DhruvPathak

Reputation: 43245

Though it is not documented, but I suppose "\r\n" would work. Graph api returns the json response as "\r\n" for messages or posts having a line break.

Upvotes: 2

tyrex
tyrex

Reputation: 8869

Turns out Line break in Facebook status update via Graph API might give you what you are looking for:

Use \u000A

For me it solved my similar issue I had with Facebook SendApi for a Facebook Messenger Bot.

Upvotes: 16

Related Questions