PHP:Telegram Bot:在文本消息中插入换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31908527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Telegram Bot: Insert line break to text message
提问by Hossein Shahsahebi
"\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.
"\n"
和"\r\n"
,在电报机器人发送的文本消息中进行测试,以创建换行符。_
使用它们后,将出现下划线,而不是显示换行符。
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';
Any help will be appreciated.
任何帮助将不胜感激。
回答by 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:
有一个更好的方法!问题是因为 URL 编码......您可以使用普通的 PHP 文本 using\n
但通过将其传递给urlencode
方法,如下所示:
$txt = urlencode("here is my text.\n and this is a new line \n another new line");
It works for me!
这个对我有用!
回答by ??????
1) If you develop your code in Windows/Linux OS, you can simply use enter in text:
1) 如果你在 Windows/Linux 操作系统中开发你的代码,你可以简单地使用输入文本:
$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
:
2) 如果您的代码在 Windows/Linux 服务器上运行,您可以使用PHP_EOL
常量代替\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:
3)如果您搜索独立于操作系统的解决方案,您可以使用%0A
或chr(10)
用于此目的:
$text = 'text 123 '.chr(10).'yet another text';
回答by Hossein Shahsahebi
For future visitor just I quote @Dagon answer in comments:
对于未来的访客,我在评论中引用@Dagon 的回答:
Using %0A
will make line feed in telegram messages
使用%0A
将在电报消息中换行
回答by Payam Aryanfard
it may be not show result as you wants in Unicode languages like Persian! you can prepare your text and use this:
在像波斯语这样的 Unicode 语言中,它可能不会像您想要的那样显示结果!您可以准备文本并使用它:
$txt = implode("\n", explode('\n', $txt));
回答by Pippi
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
回答by sajad abbasi
you should use urlencode to solve this problem:
你应该使用 urlencode 来解决这个问题:
$text ="sth sth
sth sth";
$text = urlencode($text);
回答by Hassan AFIF
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=HTMLargument in the "sendMessage" URL.
Then, I insert the following code for each line break:
<pre>\n</pre>
- 首先,我通过在“sendMessage”URL 中设置parse_mode=HTML参数以HTML 格式发送我的文本消息。
然后,我为每个换行符插入以下代码:
<pre>\n</pre>
ie.
IE。
... sendMessage?parse_mode=HTML&text="... paragraph1<pre>\n</pre>paragraph2 ..."
Of course the textvariable was curl escaped before appended to the URL:
当然,文本变量在附加到 URL 之前被 curl 转义:
$text = curl_escape($handle, $text);
回答by Moein Karimi
for me this solution works: use double quotation mark
对我来说,这个解决方案有效:使用双引号
$message='Hi'
$message=$message."\n";
$message=$message.'Guys'
回答by Peter
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
所有这些答案同时是“对”和“错”。事实上,在很大程度上取决于您的输入。例如,如果您有一个文本区域用于输入,然后将内容发送到 Telegram,如果用户在文本区域中写入并按回车,则 Telegram 中的文本将是
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.
执行 URL 编码不会改变任何东西。在挣扎了很多之后,我发现将文本区域数据从一个页面发送到另一个页面这一事实存在冲突,这会转义一些数据。
The way I solve that is to remplace the escaped \n by a non-escaped one. So:
我解决这个问题的方法是用非转义的 \n 替换转义的 \n 。所以:
$my_msg = str_replace("\n","\n",$my_msg);
It works on Mac, PC and so on with text from text area.
它适用于 Mac、PC 等,可处理文本区域中的文本。
回答by Mycodingproject
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.
在阅读并尝试了所有这些答案后,我只想发布我自己的解决方案。我在 Laravel 5.8 中有一个应用程序,它通过电子邮件和电报消息发送预订。
$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.
我或多或少使用了 Telegram API 允许的所有 html 标签。你应该注意 \n 必须是双引号。