fix: send text posts as HTML

This commit is contained in:
2026-09-24 20:29:04 +08:00
parent 6a2ba6a50a
commit 5e7a87c0dd
3 changed files with 45 additions and 8 deletions
+26
View File
@@ -60,6 +60,14 @@ pub trait MediaSender: Send + Sync {
reply_markup: Option<InlineKeyboardMarkup>,
) -> BoxFuture<'_, Result<i64, RequestError>>;
/// Sends an HTML-formatted plain message.
fn send_html_message(
&self,
chat_id: ChatId,
text: String,
reply_to: Option<MessageId>,
) -> BoxFuture<'_, Result<i64, RequestError>>;
/// Answers an inline query with `results`, cached by Telegram for
/// `cache_time` seconds. An empty `results` answers *empty*, which is a
/// real answer: it stops the client spinning and lets Telegram serve a
@@ -201,6 +209,24 @@ impl MediaSender for Bot {
})
}
fn send_html_message(
&self,
chat_id: ChatId,
text: String,
reply_to: Option<MessageId>,
) -> BoxFuture<'_, Result<i64, RequestError>> {
Box::pin(async move {
crate::rate_limit::acquire_global(1.0).await;
let mut request =
<Bot as Requester>::send_message(self, chat_id, text).parse_mode(ParseMode::Html);
if let Some(reply_to) = reply_to {
request = request
.reply_parameters(ReplyParameters::new(reply_to).allow_sending_without_reply());
}
request.await.map(|message| message.id.0 as i64)
})
}
fn answer_inline_query(
&self,
id: InlineQueryId,
@@ -428,6 +428,22 @@ impl MediaSender for MockSender {
})
}
fn send_html_message(
&self,
_chat_id: ChatId,
text: String,
_reply_to: Option<MessageId>,
) -> BoxFuture<'_, Result<i64, RequestError>> {
Box::pin(async move {
self.messages.lock().push(text);
match self.next("send_html_message") {
Outcome::MessageOk => Ok(MockSender::SENT_ID),
Outcome::MessageErr => Err(self.error()),
other => panic!("unexpected outcome {other:?} for send_html_message"),
}
})
}
fn answer_inline_query(
&self,
_id: InlineQueryId,
+3 -8
View File
@@ -681,12 +681,7 @@ pub(crate) async fn send_text_post(
) {
match ctx
.sender
.send_message(
ChatId(chat_id),
caption,
Some(MessageId(reply_to as i32)),
None,
)
.send_html_message(ChatId(chat_id), caption, Some(MessageId(reply_to as i32)))
.await
{
Ok(_) => log::info!("sent the post's text for chat={chat_id}"),
@@ -847,7 +842,7 @@ mod tests {
)
.await;
assert_eq!(sender.calls(), vec!["send_message"]);
assert_eq!(sender.calls(), vec!["send_html_message"]);
assert_eq!(
sender.messages(),
vec!["https://x.com/u/status/1\n<a>u</a>: hello"]
@@ -861,7 +856,7 @@ mod tests {
send_text_post(&ctx, 1, 2, "text".into()).await;
assert_eq!(sender.calls(), vec!["send_message", "send_message"]);
assert_eq!(sender.calls(), vec!["send_html_message", "send_message"]);
assert!(sender.messages()[1].contains("Could not send this post's text"));
}