mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-10-01 00:32:05 +00:00
fix: send text posts as HTML
This commit is contained in:
@@ -60,6 +60,14 @@ pub trait MediaSender: Send + Sync {
|
|||||||
reply_markup: Option<InlineKeyboardMarkup>,
|
reply_markup: Option<InlineKeyboardMarkup>,
|
||||||
) -> BoxFuture<'_, Result<i64, RequestError>>;
|
) -> 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
|
/// Answers an inline query with `results`, cached by Telegram for
|
||||||
/// `cache_time` seconds. An empty `results` answers *empty*, which is a
|
/// `cache_time` seconds. An empty `results` answers *empty*, which is a
|
||||||
/// real answer: it stops the client spinning and lets Telegram serve 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(
|
fn answer_inline_query(
|
||||||
&self,
|
&self,
|
||||||
id: InlineQueryId,
|
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(
|
fn answer_inline_query(
|
||||||
&self,
|
&self,
|
||||||
_id: InlineQueryId,
|
_id: InlineQueryId,
|
||||||
|
|||||||
@@ -681,12 +681,7 @@ pub(crate) async fn send_text_post(
|
|||||||
) {
|
) {
|
||||||
match ctx
|
match ctx
|
||||||
.sender
|
.sender
|
||||||
.send_message(
|
.send_html_message(ChatId(chat_id), caption, Some(MessageId(reply_to as i32)))
|
||||||
ChatId(chat_id),
|
|
||||||
caption,
|
|
||||||
Some(MessageId(reply_to as i32)),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => log::info!("sent the post's text for chat={chat_id}"),
|
Ok(_) => log::info!("sent the post's text for chat={chat_id}"),
|
||||||
@@ -847,7 +842,7 @@ mod tests {
|
|||||||
)
|
)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
assert_eq!(sender.calls(), vec!["send_message"]);
|
assert_eq!(sender.calls(), vec!["send_html_message"]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
sender.messages(),
|
sender.messages(),
|
||||||
vec!["https://x.com/u/status/1\n<a>u</a>: hello"]
|
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;
|
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"));
|
assert!(sender.messages()[1].contains("Could not send this post's text"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user