From 9a7f05056838da8ee9f4560898e1f9333761ac57 Mon Sep 17 00:00:00 2001 From: Jarvis Date: Thu, 8 Jan 2026 14:00:55 +0000 Subject: [PATCH] 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 --- src/telegram/bot.ts | 19 ++++++++----------- src/telegram/voice.ts | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/telegram/bot.ts b/src/telegram/bot.ts index 4ece77cc8..75d339c41 100644 --- a/src/telegram/bot.ts +++ b/src/telegram/bot.ts @@ -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) { diff --git a/src/telegram/voice.ts b/src/telegram/voice.ts index 623bc58ef..d1527a4ce 100644 --- a/src/telegram/voice.ts +++ b/src/telegram/voice.ts @@ -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})`, + }; +}