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条回答
放我归山
2楼-- · 2019-01-22 02:16

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楼-- · 2019-01-22 02:18

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';
查看更多
对你真心纯属浪费
4楼-- · 2019-01-22 02:19

for me this solution works: use double quotation mark

$message='Hi'
$message=$message."\n";
$message=$message.'Guys'
查看更多
登录 后发表回答