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:
committed by
Peter Steinberger
parent
ce786762db
commit
9a7f050568
@@ -60,7 +60,7 @@ import { resolveTelegramAccount } from "./accounts.js";
|
|||||||
import { createTelegramDraftStream } from "./draft-stream.js";
|
import { createTelegramDraftStream } from "./draft-stream.js";
|
||||||
import { resolveTelegramFetch } from "./fetch.js";
|
import { resolveTelegramFetch } from "./fetch.js";
|
||||||
import { markdownToTelegramHtml } from "./format.js";
|
import { markdownToTelegramHtml } from "./format.js";
|
||||||
import { isTelegramVoiceCompatible } from "./voice.js";
|
import { resolveTelegramVoiceDecision } from "./voice.js";
|
||||||
import {
|
import {
|
||||||
readTelegramAllowFromStore,
|
readTelegramAllowFromStore,
|
||||||
upsertTelegramPairingRequest,
|
upsertTelegramPairingRequest,
|
||||||
@@ -1388,17 +1388,14 @@ async function deliverReplies(params: {
|
|||||||
...mediaParams,
|
...mediaParams,
|
||||||
});
|
});
|
||||||
} else if (kind === "audio") {
|
} else if (kind === "audio") {
|
||||||
const wantsVoice = reply.audioAsVoice === true; // default false (backward compatible)
|
const { useVoice, reason } = resolveTelegramVoiceDecision({
|
||||||
const canVoice = wantsVoice
|
wantsVoice: reply.audioAsVoice === true, // default false (backward compatible)
|
||||||
? isTelegramVoiceCompatible({
|
contentType: media.contentType,
|
||||||
contentType: media.contentType,
|
fileName,
|
||||||
fileName,
|
});
|
||||||
})
|
if (reason) {
|
||||||
: false;
|
|
||||||
const useVoice = wantsVoice && canVoice;
|
|
||||||
if (wantsVoice && !canVoice) {
|
|
||||||
logVerbose(
|
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) {
|
if (useVoice) {
|
||||||
|
|||||||
@@ -13,3 +13,18 @@ export function isTelegramVoiceCompatible(opts: {
|
|||||||
const ext = path.extname(fileName).toLowerCase();
|
const ext = path.extname(fileName).toLowerCase();
|
||||||
return ext === ".ogg" || ext === ".opus" || ext === ".oga";
|
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})`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user