Facebook Messenger API:how to break line in a mess

2019-01-25 04:46发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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



回答4:

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



回答5:

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.



回答6:

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.



回答7:

In Python \\n breaks the line as expected.



回答8:

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


回答9:

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);