Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Telegram Bot: Insert line break to text message

"\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.

like image 885
Hossein Shahsahebi Avatar asked Aug 09 '15 20:08

Hossein Shahsahebi


People also ask

What is inline button in telegram bot?

Switch to Inline buttons Pressing a switch to inline button prompts the user to select a chat, opens it and inserts the bot's username into the input field. You can also pass a query that will be inserted along with the username – this way your users will immediately get some inline results they can share.

How can I type multiple lines in telegram?

Put “\n” inside your string if you want a new line.


3 Answers

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!

like image 190
Majid Avatar answered Oct 20 '22 01:10

Majid


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

Using %0A will make line feed in telegram messages

like image 28
Hossein Shahsahebi Avatar answered Oct 19 '22 23:10

Hossein Shahsahebi


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';
like image 46
علیرضا Avatar answered Oct 19 '22 23:10

علیرضا