PHP:Telegram Bot:在短信中插入换行符
PHP: Telegram Bot: Insert line break to text message
"\n"
和 "\r\n"
,在电报机器人发送的文本消息中测试,以创建换行符。使用后会出现下划线 _
而不是换行符。
如何在机器人发送的电报消息中打印换行符?
代码
$txt = 'با تشکر از عضویت شما، هر روز حدود ساعت 10 شب یک ویدئوی جالب برای شما ارسال خواهد شد.';
$txt .= " \n ";
$txt .= 'Thanks for joining, Every day at almost 18:30 GMT an intersting video will be sent';
消息演示
我们将不胜感激。
对于未来的访问者,我在评论中引用@Dagon 的回答:
使用%0A
将在电报消息中换行
1) 如果您在 Windows/Linux OS 中开发代码,您可以简单地使用 enter in text:
$text = 'test 123
another text';
就这些!
2) 如果您的代码 运行 在 Windows/Linux 服务器上,您可以使用 PHP_EOL
常量代替 \n
:
$text = 'text 123 '.PHP_EOL.'yet another text';
3) 如果您搜索 OS 独立解决方案,您可以使用 %0A
或 chr(10)
来达到此目的:
$text = 'text 123 '.chr(10).'yet another text';
有更好的方法!问题是因为 URL 编码...
您可以通过 \n
使用普通 PHP 文本,但将其传递给 urlencode
方法,如下所示:
$txt = urlencode("here is my text.\n and this is a new line \n another new line");
对我有用!
我是这样解决问题的:
$txt = 'aaaaaaaaa\nnew line1 \nnewline2';
$parameters = array('chat_id' => $chatId, "text" => $txt);
$parameters["method"] = "sendMessage";
echo json_encode($parameters);
你应该使用 urlencode 来解决这个问题:
$text ="sth sth
sth sth";
$text = urlencode($text);
在波斯语等 Unicode 语言中可能无法显示您想要的结果!
你可以准备你的文字并使用这个:
$txt = implode("\n", explode('\n', $txt));
为了避免编码和编码问题,我在我的代码中使用了这个简单的解决方案:
- 首先,我通过设置parse_mode=HTML[=以HTML格式发送短信"sendMessage" URL.
中的 31=] 参数
然后,我在每个换行符处插入如下代码:
<pre>\n</pre>
即
... sendMessage?parse_mode=HTML&text="... paragraph1<pre>\n</pre>paragraph2 ..."
当然 text 变量在附加到 URL:
之前进行了 curl 转义
$text = curl_escape($handle, $text);
对我来说,这个解决方案有效:
使用双引号
$message='Hi'
$message=$message."\n";
$message=$message.'Guys'
所有这些答案同时 "right" 和 "wrong"。事实上,很大程度上取决于您的输入。例如,如果您有一个用于输入的文本区域,然后将内容发送到 Telegram,如果用户在文本区域中书写并按 return,Telegram 中的文本将是
hello\neverybody
而不是
hello
everybody
执行URL编码不会有任何改变。经过一番努力后,我发现与将文本区域数据从一个页面发送到另一个页面转义一些数据的事实存在冲突。
我解决的方法是用非转义的替换转义的\n。
所以:
$my_msg = str_replace("\n","\n",$my_msg);
它可以在 Mac、PC 等设备上使用文本区域中的文本。
在阅读并尝试了所有这些答案之后,我只想 post 我自己的解决方案。我在 Laravel 5.8 中有一个应用程序可以通过 e-mail 和 Telegram 消息发送预订。
$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,
]);
我或多或少地使用了 Telegram API 允许的所有 html 标签。你要注意\n必须是双引号。
您可以使用 %0A 代替 \n。
尝试这样的事情,它对我有用,你需要添加参数 parse_mode。
$text = "exampletest \n example"
或者像这样的东西:
$text1 = 'example';
$text2 = 'next';
$data = $text1 . "\n" . $text2;
只需从任何地方复制断行即可轻松传入代码。
enter image description here
方式
$txt = "Thanks for joining, Every day at </br> almost 18:30 GMT an intersting video will be sent";
或
$txt = "Thanks for joining, Every day \r\n at almost 18:30 GMT an intersting video will be sent";
"\n"
和 "\r\n"
,在电报机器人发送的文本消息中测试,以创建换行符。使用后会出现下划线 _
而不是换行符。
如何在机器人发送的电报消息中打印换行符?
代码
$txt = 'با تشکر از عضویت شما، هر روز حدود ساعت 10 شب یک ویدئوی جالب برای شما ارسال خواهد شد.';
$txt .= " \n ";
$txt .= 'Thanks for joining, Every day at almost 18:30 GMT an intersting video will be sent';
消息演示
我们将不胜感激。
对于未来的访问者,我在评论中引用@Dagon 的回答:
使用%0A
将在电报消息中换行
1) 如果您在 Windows/Linux OS 中开发代码,您可以简单地使用 enter in text:
$text = 'test 123
another text';
就这些!
2) 如果您的代码 运行 在 Windows/Linux 服务器上,您可以使用 PHP_EOL
常量代替 \n
:
$text = 'text 123 '.PHP_EOL.'yet another text';
3) 如果您搜索 OS 独立解决方案,您可以使用 %0A
或 chr(10)
来达到此目的:
$text = 'text 123 '.chr(10).'yet another text';
有更好的方法!问题是因为 URL 编码...
您可以通过 \n
使用普通 PHP 文本,但将其传递给 urlencode
方法,如下所示:
$txt = urlencode("here is my text.\n and this is a new line \n another new line");
对我有用!
我是这样解决问题的:
$txt = 'aaaaaaaaa\nnew line1 \nnewline2';
$parameters = array('chat_id' => $chatId, "text" => $txt);
$parameters["method"] = "sendMessage";
echo json_encode($parameters);
你应该使用 urlencode 来解决这个问题:
$text ="sth sth
sth sth";
$text = urlencode($text);
在波斯语等 Unicode 语言中可能无法显示您想要的结果! 你可以准备你的文字并使用这个:
$txt = implode("\n", explode('\n', $txt));
为了避免编码和编码问题,我在我的代码中使用了这个简单的解决方案:
- 首先,我通过设置parse_mode=HTML[=以HTML格式发送短信"sendMessage" URL. 中的 31=] 参数
然后,我在每个换行符处插入如下代码:
<pre>\n</pre>
即
... sendMessage?parse_mode=HTML&text="... paragraph1<pre>\n</pre>paragraph2 ..."
当然 text 变量在附加到 URL:
之前进行了 curl 转义$text = curl_escape($handle, $text);
对我来说,这个解决方案有效: 使用双引号
$message='Hi'
$message=$message."\n";
$message=$message.'Guys'
所有这些答案同时 "right" 和 "wrong"。事实上,很大程度上取决于您的输入。例如,如果您有一个用于输入的文本区域,然后将内容发送到 Telegram,如果用户在文本区域中书写并按 return,Telegram 中的文本将是
hello\neverybody
而不是
hello
everybody
执行URL编码不会有任何改变。经过一番努力后,我发现与将文本区域数据从一个页面发送到另一个页面转义一些数据的事实存在冲突。
我解决的方法是用非转义的替换转义的\n。 所以:
$my_msg = str_replace("\n","\n",$my_msg);
它可以在 Mac、PC 等设备上使用文本区域中的文本。
在阅读并尝试了所有这些答案之后,我只想 post 我自己的解决方案。我在 Laravel 5.8 中有一个应用程序可以通过 e-mail 和 Telegram 消息发送预订。
$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,
]);
我或多或少地使用了 Telegram API 允许的所有 html 标签。你要注意\n必须是双引号。
您可以使用 %0A 代替 \n。
尝试这样的事情,它对我有用,你需要添加参数 parse_mode。
$text = "exampletest \n example"
或者像这样的东西:
$text1 = 'example';
$text2 = 'next';
$data = $text1 . "\n" . $text2;
只需从任何地方复制断行即可轻松传入代码。 enter image description here
方式
$txt = "Thanks for joining, Every day at </br> almost 18:30 GMT an intersting video will be sent";
或
$txt = "Thanks for joining, Every day \r\n at almost 18:30 GMT an intersting video will be sent";