fix(telegram): cut the voice transcript echo to 1024 characters

This commit is contained in:
EugeneTes
2026-10-02 09:51:57 +00:00
parent 3a57b81d88
commit 39e7849039
4 changed files with 19 additions and 6 deletions

View File

@@ -20,6 +20,9 @@ public sealed class InboundMedia(TelegramSender sender, TelegramOptions options,
/// <summary>Sent for a voice note or audio file when no speech-to-text service is configured.</summary>
public const string SpeechNotConfiguredNotice = "Voice messages need speech-to-text: set OPENAI_API_KEY (or Telegram:Speech:ApiKey) on the bot host.";
/// <summary>The longest <c>🎤</c> echo, in UTF-16 code units; a long transcript is cut so it doesn't flood the chat.</summary>
public const int MaxEchoLength = 1024;
/// <summary>Sent when the transcript is empty.</summary>
public const string NoWordsNotice = "I couldn't make out any words in that voice message.";
@@ -170,10 +173,20 @@ public sealed class InboundMedia(TelegramSender sender, TelegramOptions options,
await chat.Renderer.SayAsync(NoWordsNotice).ConfigureAwait(false);
return;
}
await chat.Renderer.SayAsync("🎤 " + transcript).ConfigureAwait(false); // plain text, cut to 4096 by the sender
await chat.Renderer.SayAsync(Echo(transcript)).ConfigureAwait(false); // plain text
draft.Body.Add($"[Voice message] {transcript}");
}
/// <summary>The <c>🎤 …</c> echo, cut to <see cref="MaxEchoLength"/> characters with <c>…</c> (the model gets all of it).</summary>
internal static string Echo(string transcript)
{
var echo = "🎤 " + transcript;
if (echo.Length <= MaxEchoLength) return echo;
var cut = MaxEchoLength - 1;
if (char.IsHighSurrogate(echo[cut - 1])) cut--; // don't split an emoji
return echo[..cut] + "…";
}
/// <summary>
/// The name an audio file is sent to speech to text under: OpenAI picks the format from the extension, so it's an
/// ASCII <c>audio.&lt;ext&gt;</c> from the original extension when OpenAI knows it, else from the MIME type.