PHP: Telegram Bot: Insert line break to text message

[亡魂溺海] 提交于 2019-11-28 18:33:42
Majid

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!

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

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

Using %0A will make line feed in telegram messages

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

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

you should use urlencode to solve this problem:

$text ="sth sth
sth sth";
$text = urlencode($text);

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

for me this solution works: use double quotation mark

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

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!