82

"\n" and "\r\n", tested in text message sent by telegram bot, to create line break. Instead of showing line break, underline _ will appear after using them.

How I could printing line feed in telegram message sent by bot?

CODE

$txt = 'با تشکر از عضویت شما، هر روز حدود ساعت 10 شب یک ویدئوی جالب برای شما ارسال خواهد شد.';
$txt .= " \n ";
$txt .= 'Thanks for joining, Every day at almost 18:30 GMT an intersting video will be sent';

Message Demo enter image description here

Any help will be appreciated.

Charles Okwuagwu
  • 10,538
  • 16
  • 87
  • 157
Hossein Shahsahebi
  • 6,348
  • 5
  • 24
  • 38
  • @Dagon thanks a lot, first one just duplicate the message and print it twice, but second one do the job. Can I ask where is the source of these codes? – Hossein Shahsahebi Aug 09 '15 at 20:56
  • 2
    first google hit for me was: https://www.twilio.com/help/faq/sms/how-do-i-add-a-line-break-in-my-sms-message –  Aug 09 '15 at 20:58
  • a simple `\n` should work, its possible that the bot manager that you are using is doing some kine of clean up before sending the message. I am using nodejs and `\n` works just fine. – Exlord Dec 14 '16 at 11:04

14 Answers14

96

There is a better way! The problem is because of URL encodings... You can use normal PHP text using \n but by passing it to urlencode method, as follows:

$txt = urlencode("here is my text.\n and this is a new line \n another new line");

It works for me!

MahdiY
  • 1,269
  • 21
  • 32
Majid
  • 3,128
  • 1
  • 26
  • 31
  • 1
    Sorry Majid, Now I test another answer and all options failed, So I uncheck his answer and check yours – Hossein Shahsahebi Aug 16 '15 at 18:56
  • 3
    urlencode does not work for me (it encodes everything, including spaces). If you in a habit like me to use single quotes, \n must be in double quotes: $text = ' my text' . "\n"; – Quentin Campbell Dec 05 '18 at 15:40
49

1) If you develop your code in Windows/Linux OS, you can simply use enter in text:

$text = 'test 123
         another text';

Thats all!

2) If your code run on Windows/Linux server, you can use PHP_EOL constant instead of \n:

$text = 'text 123 '.PHP_EOL.'yet another text';

3) And if you search for an OS independent soloution, you can use %0A or chr(10) for this purpose:

$text = 'text 123 '.chr(10).'yet another text';
علیرضا
  • 2,434
  • 1
  • 27
  • 33
  • 2
    @Majid because you are on Mac! in iOS, the `PHP_EOL` and line break, is equal to `%0D` (or `\r`). but in linux and windows, the `PHP_EOL` and line break is equal to `%0A` (or `\n`) AND `%0D%0A` (or `\r\n`). – علیرضا Aug 14 '15 at 03:22
  • @Majid i add third way for you :) – علیرضا Aug 14 '15 at 03:33
  • Thank you for considering the issue. I'm on Mac, but the server that runs the PHP codes is a linux server on the Internet! Anyway thank you for adding the third way :-) – Majid Aug 15 '15 at 02:02
34

For future visitor just I quote @Dagon answer in comments:

Using %0A will make line feed in telegram messages

Hossein Shahsahebi
  • 6,348
  • 5
  • 24
  • 38
  • 2
    This should be the accepted answer. The `urlencode` function is not language agnostic. Your solution works when using the telegram API in any environment or language. – Dawson B Nov 03 '20 at 23:39
11

You can use %0A instead of \n.

Dorozhkin Cyril
  • 111
  • 1
  • 3
6

After reading and trying all of these answers, I just wanted to post my own solution. I have an application in Laravel 5.8 that sends the reservation both by e-mail and a Telegram message.

$telegramMessage = 
        "<strong>Reservation Request</strong>\n".
         '<strong>Name:</strong> ' . $reservation->reserv_name . "\n".
         '<strong>E-mail:</strong> <a href="mailto:' . $reservation->email . '"> ' . $reservation->email . "</a>\n".
         '<strong>Phone:</strong> ' . $reservation->phone . "\n".
         '<strong>Reservation Date/Time:</strong> ' . $reservation->reserv_date_time->format('d-m-Y H:i') . "\n".
         '<strong>Number of people:</strong> ' . $reservation->number_of_people . "\n".
         '<strong>Message:</strong> ' . $reservation->reserv_message . "\n";

Telegram::sendMessage([
            'chat_id' => env('TELEGRAM_CHAT_ID', ''),
            'parse_mode' => 'HTML',
            'text' => $telegramMessage,
        ]);

More or less I have used all the html tags that Telegram API allows. You should pay attention \n must be in double quotes.

Mycodingproject
  • 1,031
  • 1
  • 9
  • 13
4

To avoid coding and encoding issues, I used this simple solution in my code:

  • First, I send my text message in HTML format by setting the parse_mode=HTML argument in the "sendMessage" URL.
  • Then, I insert the following code for each line break:

    <pre>\n</pre>

ie.

... sendMessage?parse_mode=HTML&text="... paragraph1<pre>\n</pre>paragraph2 ..."

Of course the text variable was curl escaped before appended to the URL:

$text = curl_escape($handle, $text);
3

it may be not show result as you wants in Unicode languages like Persian! you can prepare your text and use this:

$txt = implode("\n", explode('\n', $txt));
1

for me this solution works: use double quotation mark

$message='Hi'
$message=$message."\n";
$message=$message.'Guys'
1

All these answers are at the same time "right" and "wrong". In fact in depend a lot of the input you have. For example if you have a text area for input and then send the content to Telegram, if the user write in the text area and press return, the text in Telegram will be

       hello\neverybody 

and not

       hello
       everybody

Performing URL encode will change nothing. After struggling a lot I discover a conflict with the fact sending the text area data from a page to another page escape some data.

The way I solve that is to remplace the escaped \n by a non-escaped one. So:

      $my_msg = str_replace("\\n","\n",$my_msg);

It works on Mac, PC and so on with text from text area.

Peter
  • 1,247
  • 19
  • 33
0

I solved the problem in this way :

$txt = 'aaaaaaaaa\nnew line1 \nnewline2';
$parameters = array('chat_id' => $chatId, "text" => $txt);

$parameters["method"] = "sendMessage";
echo json_encode($parameters);

try here : https://telegram-bot-sdk.readme.io/docs/sendmessage

Pippi
  • 313
  • 1
  • 4
  • 18
0

you should use urlencode to solve this problem:

$text ="sth sth
sth sth";
$text = urlencode($text);
sajad abbasi
  • 1,988
  • 2
  • 22
  • 43
0

try something like this, its work for me, and you need to add parameter parse_mode.

$text = "exampletest \n example"

or someting like this:

$text1 = 'example';
$text2 = 'next';
$data = $text1 . "\n" . $text2;

https://core.telegram.org/bots/api#formatting-options

Syaifudin Zuhri
  • 108
  • 1
  • 2
  • 11
0

ez way

$txt = "Thanks for joining, Every day at </br> almost 18:30 GMT an intersting video will be sent";

or

$txt = "Thanks for joining, Every day \r\n at almost 18:30 GMT an intersting video will be sent";
-1

It it easy just copy break line from anywhare to pass in code. enter image description here

  • Welcome to Stack Overflow! While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Kevin M. Mansour Aug 24 '21 at 00:12