forked from admin/VolcanicAgenticCore
Content step 3 (InboundMedia.AddSpeechAsync): voice and audio go through ISpeechToText, the transcript is echoed as '🎤 …' and prompted as '[Voice message] …'. Spec §4 edited: audio is transcribed from the file step 2 saved (one download); an oversize or undownloadable voice note sends its notice and isn't prompted.
179 lines
8.5 KiB
C#
179 lines
8.5 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Telegram.Bot;
|
|
using Telegram.Bot.Exceptions;
|
|
using Telegram.Bot.Types;
|
|
using Volcanic.AgenticCore.Telegram.Chats;
|
|
using Volcanic.AgenticCore.Telegram.Rendering;
|
|
using Volcanic.AgenticCore.Telegram.Speech;
|
|
using ImageContent = Volcanic.AgenticCore.Abstractions.Messages.ImageContent;
|
|
|
|
namespace Volcanic.AgenticCore.Telegram.Inbound;
|
|
|
|
/// <summary>
|
|
/// Content steps for attachments (spec: telegram-attachments §4): step 2 saves documents, videos and audio to the
|
|
/// workspace inbox, step 3 transcribes voice notes and audio through <see cref="ISpeechToText"/> (null: speech is off).
|
|
/// Notices and the echo go through the chat renderer (<see cref="ChatRenderer.SayAsync"/>) so they stay in order.
|
|
/// </summary>
|
|
public sealed class InboundMedia(TelegramSender sender, TelegramOptions options, TimeProvider time, ILogger<InboundMedia> logger,
|
|
ISpeechToText? speech = null)
|
|
{
|
|
/// <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>Sent when the transcript is empty.</summary>
|
|
public const string NoWordsNotice = "I couldn't make out any words in that voice message.";
|
|
|
|
/// <summary>Sent when a file's download fails (the partial file is deleted and the message isn't prompted).</summary>
|
|
public const string DownloadFailedNotice = "I could not download that file.";
|
|
|
|
/// <summary>Suffix of a <c>[File received: …]</c> line for an image too large to attach inline.</summary>
|
|
public const string TooLargeInlineNote = " (too large to show inline; downscale it with bash if you need to see it)";
|
|
|
|
private static readonly HashSet<string> InlineImageTypes = new(StringComparer.OrdinalIgnoreCase) { "image/jpeg", "image/png", "image/webp", "image/gif" };
|
|
|
|
/// <summary>The notice for a file over the 20 MB download limit; <c>over 20 MB</c> when the size is unknown.</summary>
|
|
public static string TooBigNotice(long? bytes) =>
|
|
$"That file is {(bytes is { } b ? TelegramLimits.Megabytes(b) + " MB" : "over 20 MB")}; Telegram lets bots download at most 20 MB. Share a link or a smaller file.";
|
|
|
|
/// <summary>Download a file by id into <paramref name="destination"/> (shared with step 1's photos).</summary>
|
|
internal Task DownloadAsync(string fileId, Stream destination, CancellationToken cancellationToken) =>
|
|
sender.Bot.GetInfoAndDownloadFile(fileId, destination, cancellationToken);
|
|
|
|
/// <summary>Telegram's <c>getFile</c> refusal for a file over the download limit.</summary>
|
|
internal static bool IsTooBig(ApiRequestException ex) => ex.Message.Contains("file is too big", StringComparison.OrdinalIgnoreCase);
|
|
|
|
/// <summary>Step 2: a document, video or audio file goes to the inbox and gets a <c>[File received: …]</c> line.</summary>
|
|
internal async Task AddFilesAsync(ChatContext chat, Message message, InboundDraft draft, CancellationToken cancellationToken)
|
|
{
|
|
var (fileId, size, name, mime) = message switch
|
|
{
|
|
{ Document: { } d } => (d.FileId, d.FileSize, d.FileName ?? "file", d.MimeType ?? "application/octet-stream"),
|
|
{ Video: { } v } => (v.FileId, v.FileSize, v.FileName ?? "video.mp4", v.MimeType ?? "video/mp4"),
|
|
{ Audio: { } a } => (a.FileId, a.FileSize, a.FileName ?? "audio", a.MimeType ?? "audio/mpeg"),
|
|
_ => ((string?)null, (long?)null, "", ""),
|
|
};
|
|
if (fileId is null) return;
|
|
if (size > TelegramLimits.MaxDownloadBytes)
|
|
{
|
|
await chat.Renderer.SayAsync(TooBigNotice(size)).ConfigureAwait(false);
|
|
draft.Refused = true;
|
|
return;
|
|
}
|
|
|
|
var workspace = chat.Session.Cwd;
|
|
var (path, stream) = InboxFiles.Create(workspace, time.GetLocalNow(), name);
|
|
try
|
|
{
|
|
await using (stream.ConfigureAwait(false))
|
|
await DownloadAsync(fileId, stream, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
TryDelete(path);
|
|
throw;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
TryDelete(path);
|
|
logger.LogWarning(ex, "Downloading {File} in chat {ChatId} failed", name, chat.ChatId);
|
|
await chat.Renderer.SayAsync(ex is ApiRequestException api && IsTooBig(api) ? TooBigNotice(size) : DownloadFailedNotice).ConfigureAwait(false);
|
|
draft.Refused = true;
|
|
return;
|
|
}
|
|
|
|
var bytes = new FileInfo(path).Length;
|
|
draft.SavedPath = path;
|
|
var note = "";
|
|
if (InlineImageTypes.Contains(mime))
|
|
{
|
|
if (bytes <= options.InlineImageMaxBytes)
|
|
{
|
|
var data = await File.ReadAllBytesAsync(path, cancellationToken).ConfigureAwait(false);
|
|
draft.Images.Add(new ImageContent(Convert.ToBase64String(data), mime.ToLowerInvariant()));
|
|
}
|
|
else
|
|
{
|
|
note = TooLargeInlineNote;
|
|
}
|
|
}
|
|
draft.Body.Add($"[File received: {InboxFiles.Relative(workspace, path)} ({mime}, {TelegramLimits.FormatSize(bytes)}){note}]");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Step 3: a voice note or audio file is transcribed, echoed as <c>🎤 …</c> and added as <c>[Voice message] …</c>.
|
|
/// Audio is read back from the file step 2 saved (one download). Without a transcript nothing is added (the caption,
|
|
/// if any, is still prompted); an oversize or undownloadable voice note refuses the message.
|
|
/// </summary>
|
|
internal async Task AddSpeechAsync(ChatContext chat, Message message, InboundDraft draft, CancellationToken cancellationToken)
|
|
{
|
|
var (fileId, size, name, mime, isVoice) = message switch
|
|
{
|
|
{ Voice: { } v } => (v.FileId, v.FileSize, "voice.ogg", "audio/ogg", true), // Telegram's .oga is rejected by OpenAI
|
|
{ Audio: { } a } => (a.FileId, a.FileSize, a.FileName ?? "audio", a.MimeType ?? "audio/mpeg", false),
|
|
_ => ((string?)null, (long?)null, "", "", false),
|
|
};
|
|
if (fileId is null) return;
|
|
if (speech is null)
|
|
{
|
|
await chat.Renderer.SayAsync(SpeechNotConfiguredNotice).ConfigureAwait(false);
|
|
return;
|
|
}
|
|
if (isVoice && size > TelegramLimits.MaxDownloadBytes)
|
|
{
|
|
await chat.Renderer.SayAsync(TooBigNotice(size)).ConfigureAwait(false);
|
|
draft.Refused = true;
|
|
return;
|
|
}
|
|
|
|
byte[] audio;
|
|
if (draft.SavedPath is { } saved) // audio: step 2 saved it
|
|
{
|
|
audio = await File.ReadAllBytesAsync(saved, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
else
|
|
{
|
|
using var buffer = new MemoryStream();
|
|
try
|
|
{
|
|
await DownloadAsync(fileId, buffer, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex) when (ex is not OperationCanceledException)
|
|
{
|
|
logger.LogWarning(ex, "Downloading a voice message in chat {ChatId} failed", chat.ChatId);
|
|
await chat.Renderer.SayAsync(ex is ApiRequestException api && IsTooBig(api) ? TooBigNotice(size) : DownloadFailedNotice).ConfigureAwait(false);
|
|
draft.Refused = true;
|
|
return;
|
|
}
|
|
audio = buffer.ToArray();
|
|
}
|
|
|
|
string transcript;
|
|
try
|
|
{
|
|
transcript = (await speech.TranscribeAsync(audio, name, mime, cancellationToken).ConfigureAwait(false)).Trim();
|
|
}
|
|
catch (Exception ex) when (ex is not OperationCanceledException)
|
|
{
|
|
logger.LogWarning(ex, "Transcribing a voice message in chat {ChatId} failed", chat.ChatId);
|
|
await chat.Renderer.SayAsync($"Could not transcribe the voice message: {ex.Message}").ConfigureAwait(false);
|
|
return;
|
|
}
|
|
if (transcript.Length == 0)
|
|
{
|
|
await chat.Renderer.SayAsync(NoWordsNotice).ConfigureAwait(false);
|
|
return;
|
|
}
|
|
await chat.Renderer.SayAsync("🎤 " + transcript).ConfigureAwait(false); // plain text, cut to 4096 by the sender
|
|
draft.Body.Add($"[Voice message] {transcript}");
|
|
}
|
|
|
|
private void TryDelete(string path)
|
|
{
|
|
try { File.Delete(path); }
|
|
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
|
{
|
|
logger.LogDebug(ex, "Could not delete the partial file {Path}", path);
|
|
}
|
|
}
|
|
}
|