fix(media): recognize MP3 and M4A as voice-compatible audio (#15438)

* fix(media): recognize MP3 and M4A as voice-compatible audio

Telegram sendVoice supports OGG/Opus, MP3, and M4A, but
isVoiceCompatibleAudio only recognized OGG/Opus formats.

- Add MP3 and M4A extensions and MIME types
- Use explicit MIME set instead of substring matching
- Handle MIME parameters (e.g. 'audio/ogg; codecs=opus')
- Add test coverage for all supported and unsupported formats

* fix: narrow MIME allowlist per review feedback

Remove audio/mp4 and audio/aac from voice MIME types — too broad.
Keep only M4A-specific types (audio/x-m4a, audio/m4a).
Add audio/mp4 and audio/aac as negative test cases.

* fix: align voice compatibility and channel coverage (#15438) (thanks @azade-c)

---------

Co-authored-by: Peter Steinberger <steipete@gmail.com>
This commit is contained in:
Azade 🐐
2026-02-14 02:03:02 +00:00
committed by GitHub
parent 0b8227fa92
commit 1b95220a99
6 changed files with 187 additions and 11 deletions

View File

@@ -436,6 +436,41 @@ describe("sendMessageTelegram", () => {
sendVoice: typeof sendVoice;
};
loadWebMedia.mockResolvedValueOnce({
buffer: Buffer.from("audio"),
contentType: "audio/wav",
fileName: "clip.wav",
});
await sendMessageTelegram(chatId, "caption", {
token: "tok",
api,
mediaUrl: "https://example.com/clip.wav",
asVoice: true,
});
expect(sendAudio).toHaveBeenCalledWith(chatId, expect.anything(), {
caption: "caption",
parse_mode: "HTML",
});
expect(sendVoice).not.toHaveBeenCalled();
});
it("sends MP3 as voice when asVoice is true", async () => {
const chatId = "123";
const sendAudio = vi.fn().mockResolvedValue({
message_id: 16,
chat: { id: chatId },
});
const sendVoice = vi.fn().mockResolvedValue({
message_id: 17,
chat: { id: chatId },
});
const api = { sendAudio, sendVoice } as unknown as {
sendAudio: typeof sendAudio;
sendVoice: typeof sendVoice;
};
loadWebMedia.mockResolvedValueOnce({
buffer: Buffer.from("audio"),
contentType: "audio/mpeg",
@@ -449,11 +484,11 @@ describe("sendMessageTelegram", () => {
asVoice: true,
});
expect(sendAudio).toHaveBeenCalledWith(chatId, expect.anything(), {
expect(sendVoice).toHaveBeenCalledWith(chatId, expect.anything(), {
caption: "caption",
parse_mode: "HTML",
});
expect(sendVoice).not.toHaveBeenCalled();
expect(sendAudio).not.toHaveBeenCalled();
});
it("includes message_thread_id for forum topic messages", async () => {

View File

@@ -18,13 +18,13 @@ describe("resolveTelegramVoiceSend", () => {
const logFallback = vi.fn();
const result = resolveTelegramVoiceSend({
wantsVoice: true,
contentType: "audio/mpeg",
fileName: "track.mp3",
contentType: "audio/wav",
fileName: "track.wav",
logFallback,
});
expect(result.useVoice).toBe(false);
expect(logFallback).toHaveBeenCalledWith(
"Telegram voice requested but media is audio/mpeg (track.mp3); sending as audio file instead.",
"Telegram voice requested but media is audio/wav (track.wav); sending as audio file instead.",
);
});
@@ -39,4 +39,19 @@ describe("resolveTelegramVoiceSend", () => {
expect(result.useVoice).toBe(true);
expect(logFallback).not.toHaveBeenCalled();
});
it.each([
{ contentType: "audio/mpeg", fileName: "track.mp3" },
{ contentType: "audio/mp4", fileName: "track.m4a" },
])("keeps voice for compatible MIME $contentType", ({ contentType, fileName }) => {
const logFallback = vi.fn();
const result = resolveTelegramVoiceSend({
wantsVoice: true,
contentType,
fileName,
logFallback,
});
expect(result.useVoice).toBe(true);
expect(logFallback).not.toHaveBeenCalled();
});
});