refactor(telegram): centralize voice decisions

- Share voice compatibility decision logic across send + bot flows
- Keep voice fallback logging consistent
- Simplify voice handling in the audio send path
This commit is contained in:
Jarvis
2026-01-08 14:00:55 +00:00
committed by Peter Steinberger
parent ce786762db
commit 9a7f050568
2 changed files with 23 additions and 11 deletions

View File

@@ -60,7 +60,7 @@ import { resolveTelegramAccount } from "./accounts.js";
import { createTelegramDraftStream } from "./draft-stream.js";
import { resolveTelegramFetch } from "./fetch.js";
import { markdownToTelegramHtml } from "./format.js";
import { isTelegramVoiceCompatible } from "./voice.js";
import { resolveTelegramVoiceDecision } from "./voice.js";
import {
readTelegramAllowFromStore,
upsertTelegramPairingRequest,
@@ -1388,17 +1388,14 @@ async function deliverReplies(params: {
...mediaParams,
});
} else if (kind === "audio") {
const wantsVoice = reply.audioAsVoice === true; // default false (backward compatible)
const canVoice = wantsVoice
? isTelegramVoiceCompatible({
contentType: media.contentType,
fileName,
})
: false;
const useVoice = wantsVoice && canVoice;
if (wantsVoice && !canVoice) {
const { useVoice, reason } = resolveTelegramVoiceDecision({
wantsVoice: reply.audioAsVoice === true, // default false (backward compatible)
contentType: media.contentType,
fileName,
});
if (reason) {
logVerbose(
`Telegram voice requested but media is ${media.contentType ?? "unknown"} (${fileName}); sending as audio file instead.`,
`Telegram voice requested but ${reason}; sending as audio file instead.`,
);
}
if (useVoice) {

View File

@@ -13,3 +13,18 @@ export function isTelegramVoiceCompatible(opts: {
const ext = path.extname(fileName).toLowerCase();
return ext === ".ogg" || ext === ".opus" || ext === ".oga";
}
export function resolveTelegramVoiceDecision(opts: {
wantsVoice: boolean;
contentType?: string | null;
fileName?: string | null;
}): { useVoice: boolean; reason?: string } {
if (!opts.wantsVoice) return { useVoice: false };
if (isTelegramVoiceCompatible(opts)) return { useVoice: true };
const contentType = opts.contentType ?? "unknown";
const fileName = opts.fileName ?? "unknown";
return {
useVoice: false,
reason: `media is ${contentType} (${fileName})`,
};
}