Skip to content

Commit 94db204

Browse files
authored
Merge pull request #170 from Marenz/feat/telegram-send-audio
feat(telegram): use send_audio for audio MIME types
2 parents b42810b + 23e1668 commit 94db204

File tree

1 file changed

+65
-27
lines changed

1 file changed

+65
-27
lines changed

src/messaging/telegram.rs

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -312,42 +312,80 @@ impl Messaging for TelegramAdapter {
312312
OutboundResponse::File {
313313
filename,
314314
data,
315-
mime_type: _,
315+
mime_type,
316316
caption,
317317
} => {
318318
self.stop_typing(&message.conversation_id).await;
319319

320-
let input_file = InputFile::memory(data.clone()).file_name(filename.clone());
321-
let sent = if let Some(ref caption_text) = caption {
322-
let html_caption = markdown_to_telegram_html(caption_text);
323-
self.bot
324-
.send_document(chat_id, input_file)
325-
.caption(&html_caption)
326-
.parse_mode(ParseMode::Html)
327-
.send()
328-
.await
329-
} else {
330-
self.bot.send_document(chat_id, input_file).send().await
331-
};
320+
// Use send_audio for audio files so Telegram renders an inline player.
321+
// Fall back to send_document for everything else.
322+
if mime_type.starts_with("audio/") {
323+
let input_file = InputFile::memory(data.clone()).file_name(filename.clone());
324+
let sent = if let Some(ref caption_text) = caption {
325+
let html_caption = markdown_to_telegram_html(caption_text);
326+
self.bot
327+
.send_audio(chat_id, input_file)
328+
.caption(&html_caption)
329+
.parse_mode(ParseMode::Html)
330+
.send()
331+
.await
332+
} else {
333+
self.bot.send_audio(chat_id, input_file).send().await
334+
};
332335

333-
if let Err(error) = sent {
334-
if should_retry_plain_caption(&error) {
335-
tracing::debug!(
336-
%error,
337-
"HTML caption parse failed, retrying telegram file with plain caption"
338-
);
339-
let fallback_file = InputFile::memory(data).file_name(filename);
340-
let mut request = self.bot.send_document(chat_id, fallback_file);
341-
if let Some(caption_text) = caption {
342-
request = request.caption(caption_text);
336+
if let Err(error) = sent {
337+
if should_retry_plain_caption(&error) {
338+
tracing::debug!(
339+
%error,
340+
"HTML caption parse failed, retrying telegram audio with plain caption"
341+
);
342+
let fallback_file = InputFile::memory(data).file_name(filename);
343+
let mut request = self.bot.send_audio(chat_id, fallback_file);
344+
if let Some(caption_text) = caption {
345+
request = request.caption(caption_text);
346+
}
347+
request
348+
.send()
349+
.await
350+
.context("failed to send telegram audio")?;
351+
} else {
352+
return Err(error)
353+
.context("failed to send telegram audio with HTML caption")?;
343354
}
344-
request
355+
}
356+
} else {
357+
let input_file = InputFile::memory(data.clone()).file_name(filename.clone());
358+
let sent = if let Some(ref caption_text) = caption {
359+
let html_caption = markdown_to_telegram_html(caption_text);
360+
self.bot
361+
.send_document(chat_id, input_file)
362+
.caption(&html_caption)
363+
.parse_mode(ParseMode::Html)
345364
.send()
346365
.await
347-
.context("failed to send telegram file")?;
348366
} else {
349-
return Err(error)
350-
.context("failed to send telegram file with HTML caption")?;
367+
self.bot.send_document(chat_id, input_file).send().await
368+
};
369+
370+
if let Err(error) = sent {
371+
if should_retry_plain_caption(&error) {
372+
tracing::debug!(
373+
%error,
374+
"HTML caption parse failed, retrying telegram file with plain caption"
375+
);
376+
let fallback_file = InputFile::memory(data).file_name(filename);
377+
let mut request = self.bot.send_document(chat_id, fallback_file);
378+
if let Some(caption_text) = caption {
379+
request = request.caption(caption_text);
380+
}
381+
request
382+
.send()
383+
.await
384+
.context("failed to send telegram file")?;
385+
} else {
386+
return Err(error)
387+
.context("failed to send telegram file with HTML caption")?;
388+
}
351389
}
352390
}
353391
}

0 commit comments

Comments
 (0)