Facebook Messenger API:how to break line in a message
Asked Answered
L

9

21

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?

Lifeguard answered 29/6, 2016 at 13:6 Comment(1)
Since the text property has a 320 character limit anyway, I would not be surprised if they did not enable posting text with line breaks. Maybe rather split the text at line breaks on your end, and then send them as individual messages?Allegiance
S
12

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.

Spheno answered 12/8, 2016 at 5:51 Comment(2)
@FMan .. k dear.. But its working fine for me.. If your answer working good for you.. then enjoy the code.. if You want any more help from me.. please provide me your code .Spheno
Ideally he should use PHP_EOL for thisPinckney
L
16

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.

Lananna answered 5/8, 2016 at 9:0 Comment(0)
S
12

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.

Spheno answered 12/8, 2016 at 5:51 Comment(2)
@FMan .. k dear.. But its working fine for me.. If your answer working good for you.. then enjoy the code.. if You want any more help from me.. please provide me your code .Spheno
Ideally he should use PHP_EOL for thisPinckney
F
6

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
Foah answered 22/7, 2018 at 18:26 Comment(2)
Hi there. Can you please add some example for more clear answer. tnxGusgusba
I used "\n\n" as a new line break inside my string which I sent to the chatbot. It worked for PHP. Example "This is some text I am sending to my chatbot. \n\n This is a new line".Foah
P
3

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

Pyramid answered 16/9, 2016 at 20:11 Comment(0)
M
2

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.

Marci answered 11/8, 2016 at 9:41 Comment(0)
P
2

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

Pinckney answered 11/9, 2016 at 18:5 Comment(0)
L
2

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.

Longheaded answered 19/2, 2017 at 16:36 Comment(0)
G
1

In Python \\n breaks the line as expected.

Gpo answered 2/7, 2018 at 11:46 Comment(0)
R
0

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); 
Resolution answered 17/4, 2017 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.