Telegram REST API,在消息文本中发送换行符?

Telegram REST API, send newline in message text?

我想通过 Telegram API 在 <pre> 块或 ``` 中发送消息(HTML 或 markdown 解析模式,我没有偏好)。

文本是一个带有一些换行符的长字符串。为了便于阅读,我想将其作为代码发送。新行采用 \n 格式,因此 Telegram API 可以处理。

但是在代码块中我看不到换行符。我使用过其他可以在代码块中向我发送一些行的机器人,所以我非常确定这是可能的。

有人可以帮我解决这个问题吗?

这是我目前使用的代码:

$url = "https://api.telegram.org/$telegram_apikey/sendMessage?chat_id=$telegram_chatid&parse_mode=Markdown&text=```" . $message ."```";
        $telegramResult = file_get_contents($url
);

消息是这样的:

-------------------------------------------- \n
------------ IMPORT RESULTS ---------------- \n
-------------------------------------------- \n
Product count: 12345 \n
Created: 1234 \n
Total time:  200 \n
-------------------------------------------- \n

我成功了。

您需要发送 %0A,而不是发送 \n 到 Telegram。

我看到您找到了解决方案,但您最好使用 urlencode 来编码您的 $message

这应该将您的换行符转换为 %0A 以及转换任何其他非法或潜在危险的字符,例如 &#?,如果它们出现在您的留言。

@Azquilt 完全正确,必须使用urlencode

$telegram_apikey = 'API_KEY';
$telegram_chatid = 777;

$product_count = 12345;
$created = 1234;
$total_time = 200;

$message = <<<TEXT
--------------------------------------------
------------ IMPORT RESULTS ----------------
--------------------------------------------
Product count: $product_count
Created: $created
Total time:  $total_time
--------------------------------------------
TEXT;

$message = urlencode("```$message```");

$url = "https://api.telegram.org/$telegram_apikey/sendMessage?chat_id=$telegram_chatid&parse_mode=Markdown&text=$message";
$telegramResult = file_get_contents($url);