PHP: Telegram Bot: Insert line break to text messa

2019-01-22 01:36发布

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

9条回答
你好瞎i
2楼-- · 2019-01-22 02:05

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.

查看更多
迷人小祖宗
3楼-- · 2019-01-22 02:06

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

Using %0A will make line feed in telegram messages

查看更多
成全新的幸福
4楼-- · 2019-01-22 02:06

you should use urlencode to solve this problem:

$text ="sth sth
sth sth";
$text = urlencode($text);
查看更多
We Are One
5楼-- · 2019-01-22 02:14

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));
查看更多
做自己的国王
6楼-- · 2019-01-22 02:15

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!

查看更多
祖国的老花朵
7楼-- · 2019-01-22 02:16

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

查看更多
登录 后发表回答