Path: redb-route/redb.Route.Telegram/TelegramProducer.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Telegram/TelegramProducer.cs ---- using System.Diagnostics; using System.Globalization; using Microsoft.Extensions.Logging; using Telegram.Bot; using Telegram.Bot.Exceptions; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; using Telegram.Bot.Types.ReplyMarkups; using redb.Route.Abstractions; using redb.Route.Core; using redb.Route.Telemetry; using TgMessage = Telegram.Bot.Types.Message; namespace redb.Route.Telegram; /// /// Telegram Bot producer. /// Supported modes: send, document, photo, answer, edit, delete, download. /// /// ChatId is resolved (in priority order): /// 1. Header telegram.chatId on the exchange /// 2. Options.ChatId (from URI or DSL) /// /// The answer and download modes need no chatId: the first is addressed by /// callback query id, the second by file id. /// /// On successful send/document/photo/edit the resulting Message.MessageId /// is written back to the exchange as header ; /// download writes the bytes to Out.Body. /// public sealed class TelegramProducer : ConnectableProducer { private readonly TelegramEndpoint _endpoint; private readonly TelegramEndpointOptions _options; private ITelegramBotClient? _bot; private ITelegramBotClient? _testClient; /// protected override IEndpoint ProducerEndpoint => _endpoint; /// protected override string ProducerName => $"telegram:{_endpoint.Mode}"; /// Creates a Telegram producer. public TelegramProducer(TelegramEndpoint endpoint, TelegramEndpointOptions options) { _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint)); _options = options ?? throw new ArgumentNullException(nameof(options)); } /// protected override Task ConnectAsync(CancellationToken ct) { // Test seam: a mock ITelegramBotClient injected via UseTestClient bypasses the component. if (_testClient is not null) { _bot = _testClient; return Task.CompletedTask; } // Client is owned and pooled by TelegramComponent (shared per resolved token). // Producer/consumer for the same token get the same instance. if (_endpoint.Component is not TelegramComponent component) throw new InvalidOperationException( "Telegram producer requires TelegramComponent as its owning component."); _bot = component.GetOrCreateClient(_options.Token); Logger?.LogInformation("Telegram producer connected (mode={Mode})", _endpoint.Mode); return Task.CompletedTask; } /// Test seam: inject a mock used instead of the component client. internal void UseTestClient(ITelegramBotClient bot) => _testClient = bot; /// protected override Task DisconnectAsync(CancellationToken ct) { // Ownership of the client (and its HttpClient) stays with TelegramComponent. // Releasing our reference is enough here. _bot = null; return Task.CompletedTask; } /// public override async Task Process(IExchange exchange, CancellationToken ct = default) { EnsureStarted(); // Capture the client locally: DisconnectAsync nulls _bot on Stop(), and a concurrent Process // must not dereference a null field (shutdown race). var bot = _bot ?? throw new InvalidOperationException( $"{ProducerName}: producer client is unavailable (stopped concurrently?)."); using var activity = RouteTelemetryExtensions.StartTransportSpan( (_endpoint.Component as ComponentBase)?.Context, $"telegram.{_endpoint.Mode} publish", ActivityKind.Producer, "messaging.system", "telegram", _endpoint.Uri.NormalizedKey, operation: "publish"); if (activity is { IsAllDataRequested: true }) activity.SetTag("messaging.telegram.mode", _endpoint.Mode); // Per-request timeout linked to the caller ct. Uses the dedicated producer send timeout // (SendTimeoutSeconds) — independent from the long-poll HTTP ceiling on the shared client, so a // large upload over a slow link is not capped at the ~50s long-poll window. using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct); cts.CancelAfter(TimeSpan.FromSeconds(_options.SendTimeoutSeconds)); var reqCt = cts.Token; try { // answer is addressed by callbackQueryId, download by fileId — neither needs a chat. if (_endpoint.Mode == "answer") { await AnswerCallbackQueryAsync(bot, exchange, activity, reqCt).ConfigureAwait(false); } else if (_endpoint.Mode == "download") { await DownloadFileAsync(bot, exchange, activity, reqCt).ConfigureAwait(false); } else { var chatId = ResolveChatId(exchange); switch (_endpoint.Mode) { case "document": await SendDocumentAsync(bot, exchange, chatId, reqCt).ConfigureAwait(false); break; case "photo": await SendPhotoAsync(bot, exchange, chatId, reqCt).ConfigureAwait(false); break; case "edit": await EditMessageAsync(bot, exchange, chatId, reqCt).ConfigureAwait(false); break; case "delete": await DeleteMessageAsync(bot, exchange, chatId, reqCt).ConfigureAwait(false); break; default: // "send" await SendMessageAsync(bot, exchange, chatId, reqCt).ConfigureAwait(false); break; } } // No RecordMessageOut: a routed .To() is counted by ToProcessor and a template send // by the ProducerTemplate - self-recording here doubled both (ownership audit). } catch (ApiRequestException ex) { activity.RecordFailure(ex); LogPublishDiagnostics(ex, exchange); throw; } catch (Exception ex) when (ex is not OperationCanceledException || !ct.IsCancellationRequested) { // Only our own token cancelling the send is a stop; the per-request timeout or any other failure marks the span. activity.RecordFailure(ex); throw; } } // ── Rate-limit (429) retry ──────────────────────────────────────────────── /// /// Telegram enforces per-chat/global flood limits and answers with HTTP 429 plus a /// retry_after hint. Bot API contract: wait that long and retry. Failing to wait (and /// letting the route retry immediately) escalates to a temporary token ban, so this is the one /// obligation a Telegram producer must implement. Retries are bounded and honour the request /// cancellation token; retry_after is clamped to [1, 60]s to avoid an unbounded stall. /// private async Task WithRateLimitRetryAsync(Func> action, CancellationToken ct) { const int maxRetries = 3; for (var attempt = 0; ; attempt++) { try { return await action(ct).ConfigureAwait(false); } catch (ApiRequestException ex) when (ex.ErrorCode == 429 && attempt < maxRetries) { var retryAfter = Math.Clamp(ex.Parameters?.RetryAfter ?? 1, 1, 60); Logger?.LogWarning( "Telegram: rate-limited (429) on mode={Mode}; waiting {RetryAfter}s before retry {Attempt}/{Max}.", _endpoint.Mode, retryAfter, attempt + 1, maxRetries); await Task.Delay(TimeSpan.FromSeconds(retryAfter), ct).ConfigureAwait(false); } } } private Task WithRateLimitRetryAsync(Func action, CancellationToken ct) => WithRateLimitRetryAsync(async c => { await action(c).ConfigureAwait(false); return null; }, ct); // ── Send modes ──────────────────────────────────────────────────────────── private async Task SendMessageAsync( ITelegramBotClient bot, IExchange exchange, ChatId chatId, CancellationToken ct) { var text = ResolveText(exchange); var parseMode = ResolveParseMode(exchange); var markup = ResolveReplyMarkup(exchange); var replyToId = ResolveReplyToMessageId(exchange); var disableNotification = ResolveDisableNotification(exchange); var replyParams = replyToId.HasValue ? new ReplyParameters { MessageId = replyToId.Value } : null; var result = await WithRateLimitRetryAsync(c => bot.SendMessage( chatId, text, parseMode: parseMode, replyMarkup: markup, replyParameters: replyParams, disableNotification: disableNotification, cancellationToken: c ), ct).ConfigureAwait(false); SetSentMessageId(exchange, result); Logger?.LogDebug("Telegram: sent message {MessageId}", result.MessageId); } private async Task SendDocumentAsync( ITelegramBotClient bot, IExchange exchange, ChatId chatId, CancellationToken ct) { var inputFile = ResolveInputFile(exchange); var caption = ResolveCaption(exchange); var parseMode = ResolveParseMode(exchange); var disableNotification = ResolveDisableNotification(exchange); var replyToId = ResolveReplyToMessageId(exchange); var replyParams = replyToId.HasValue ? new ReplyParameters { MessageId = replyToId.Value } : null; var result = await WithRateLimitRetryAsync(c => bot.SendDocument( chatId, inputFile, caption: caption, parseMode: parseMode, replyParameters: replyParams, replyMarkup: ResolveReplyMarkup(exchange), disableNotification: disableNotification, cancellationToken: c ), ct).ConfigureAwait(false); SetSentMessageId(exchange, result); Logger?.LogDebug("Telegram: sent document {MessageId}", result.MessageId); } private async Task SendPhotoAsync( ITelegramBotClient bot, IExchange exchange, ChatId chatId, CancellationToken ct) { var inputFile = ResolveInputFile(exchange); var caption = ResolveCaption(exchange); var parseMode = ResolveParseMode(exchange); var disableNotification = ResolveDisableNotification(exchange); var replyToId = ResolveReplyToMessageId(exchange); var replyParams = replyToId.HasValue ? new ReplyParameters { MessageId = replyToId.Value } : null; var result = await WithRateLimitRetryAsync(c => bot.SendPhoto( chatId, inputFile, caption: caption, parseMode: parseMode, replyParameters: replyParams, replyMarkup: ResolveReplyMarkup(exchange), disableNotification: disableNotification, cancellationToken: c ), ct).ConfigureAwait(false); SetSentMessageId(exchange, result); Logger?.LogDebug("Telegram: sent photo {MessageId}", result.MessageId); } private async Task AnswerCallbackQueryAsync( ITelegramBotClient bot, IExchange exchange, Activity? activity, CancellationToken ct) { if (!exchange.In.Headers.TryGetValue(TelegramHeaders.CallbackQueryId, out var v) || v is null) throw new InvalidOperationException( "Telegram answer: header 'telegram.callbackQueryId' is required. " + "Use Tg.Answer(token) after receiving a CallbackQuery update."); var callbackQueryId = v.ToString()!; // Optional notification text. Coerce non-string bodies rather than silently dropping them. var text = exchange.In.Body switch { null => null, string s => s, var other => BodyText.Of(other) }; activity?.SetTag("messaging.destination.name", callbackQueryId); activity?.SetTag("messaging.telegram.callback_query.id", callbackQueryId); await WithRateLimitRetryAsync( c => bot.AnswerCallbackQuery(callbackQueryId, text, showAlert: ResolveShowAlert(exchange), cancellationToken: c), ct) .ConfigureAwait(false); Logger?.LogDebug("Telegram: answered callback query {CallbackQueryId}", callbackQueryId); } /// /// The Telegram Bot API method editMessageText does not accept a /// disable_notification parameter — editing an existing message never /// triggers a notification. The DisableNotification option and the /// telegram.disableNotification header are intentionally ignored here. /// private async Task EditMessageAsync( ITelegramBotClient bot, IExchange exchange, ChatId chatId, CancellationToken ct) { var messageId = ResolveMessageId(exchange); var text = ResolveText(exchange); var parseMode = ResolveParseMode(exchange); // EditMessageText only accepts InlineKeyboardMarkup, not ReplyKeyboard. InlineKeyboardMarkup? markup = null; if (exchange.In.Headers.TryGetValue(TelegramHeaders.ReplyMarkup, out var v) && v is not null) { markup = v as InlineKeyboardMarkup ?? throw new InvalidOperationException( $"Telegram edit: header '{TelegramHeaders.ReplyMarkup}' must be an " + $"InlineKeyboardMarkup, but was '{v.GetType().Name}'."); } var result = await WithRateLimitRetryAsync(c => bot.EditMessageText( chatId, messageId, text, parseMode: parseMode, replyMarkup: markup, cancellationToken: c), ct) .ConfigureAwait(false); SetSentMessageId(exchange, result); Logger?.LogDebug("Telegram: edited message {MessageId}", messageId); } private async Task DeleteMessageAsync( ITelegramBotClient bot, IExchange exchange, ChatId chatId, CancellationToken ct) { var messageId = ResolveMessageId(exchange); await WithRateLimitRetryAsync( c => bot.DeleteMessage(chatId, messageId, cancellationToken: c), ct) .ConfigureAwait(false); Logger?.LogDebug("Telegram: deleted message {MessageId}", messageId); } // ── Download ────────────────────────────────────────────────────────────── /// /// Fetches a file the user sent and writes the bytes to Out.Body. /// /// Two calls, because the Bot API is two calls: getFile resolves a /// file_id to a server-side path valid for at least an hour, and the transfer /// itself is an ordinary download from that path. Neither is something a route should /// have to spell out — the consumer already reports telegram.attachment.fileId, /// and everything between that header and the bytes is transport. /// /// The download URL embeds the bot token, which is why it is built inside the /// client and never surfaces on the exchange: /// carries the path alone. /// private async Task DownloadFileAsync( ITelegramBotClient bot, IExchange exchange, Activity? activity, CancellationToken ct) { var fileId = ResolveDownloadFileId(exchange); var file = await WithRateLimitRetryAsync(c => bot.GetFile(fileId, c), ct).ConfigureAwait(false); if (string.IsNullOrEmpty(file.FilePath)) throw new InvalidOperationException( "Telegram download: getFile returned no file_path. The file is either expired or " + "too large for a bot to download (Bot API caps bot downloads at 20 MB)."); activity?.SetTag("messaging.destination.name", file.FilePath); activity?.SetTag("messaging.telegram.file.unique_id", file.FileUniqueId); // Cheap refusal first: when Telegram states the size up front there is no reason to // start a transfer that is going to be rejected at the end of it. var ceiling = _options.MaxDownloadBytes; if (ceiling > 0 && file.FileSize is { } declared && declared > ceiling) throw new InvalidOperationException( $"Telegram download: file is {declared} bytes, over the {ceiling}-byte ceiling " + $"(maxDownloadBytes). Reject oversized attachments before the download, or raise the option."); using var buffer = new CappedMemoryStream(ceiling); await WithRateLimitRetryAsync(async c => { // A 429 retry re-downloads from the start, so the buffer has to start from the // start too — otherwise the second attempt appends to the first one's bytes and // the route gets a file that is longer than any file Telegram holds. buffer.SetLength(0); await bot.DownloadFile(file.FilePath, buffer, c).ConfigureAwait(false); }, ct).ConfigureAwait(false); var bytes = buffer.ToArray(); exchange.Out ??= exchange.In.Clone(); exchange.Out.Body = bytes; exchange.Out.Headers[TelegramHeaders.FilePath] = file.FilePath; exchange.Out.Headers[TelegramHeaders.FileSize] = (long)bytes.Length; exchange.Out.Headers[TelegramHeaders.FileUniqueId] = file.FileUniqueId; activity?.SetTag("messaging.telegram.file.size", bytes.Length); Logger?.LogDebug( "Telegram: downloaded {Bytes} bytes from {FilePath}", bytes.Length, file.FilePath); } /// /// Which file download fetches, in priority order: the explicit producer /// instruction, then the option expression, then the attachment the consumer reported, /// then a bare string body. /// /// The attachment header is third and not first on purpose — it is the default, not the /// override, and a route that says which file it wants must be obeyed. It is present at /// all so that the common case ("download what the user just sent") needs no configuration. /// /// private string ResolveDownloadFileId(IExchange exchange) { if (exchange.In.Headers.TryGetValue(TelegramHeaders.FileId, out var explicitId) && explicitId is string id && !string.IsNullOrWhiteSpace(id)) return id; if (!string.IsNullOrWhiteSpace(_options.FileId)) { // An empty resolution means the referenced header is absent on this update — // fall through to the attachment rather than fail the exchange, the same way // ReplyToMessageId treats an unresolved expression. var resolved = _options.ResolveOption(_options.FileId, exchange); if (!string.IsNullOrWhiteSpace(resolved)) return resolved; } if (exchange.In.Headers.TryGetValue(TelegramHeaders.AttachmentFileId, out var attachment) && attachment is string attachmentId && !string.IsNullOrWhiteSpace(attachmentId)) return attachmentId; if (exchange.In.Body is string body && !string.IsNullOrWhiteSpace(body)) return body; throw new InvalidOperationException( $"Telegram download: no file id. Set header '{TelegramHeaders.FileId}', use " + $".FileId(...) in the fluent builder, or route an update that carries " + $"'{TelegramHeaders.AttachmentFileId}'."); } /// /// Buffer that refuses to grow past a ceiling. /// /// A downloaded file becomes an array in this process's memory, and file_size is /// optional in the Bot API — so the pre-flight check above can be skipped by an update /// that simply omits it. Trusting it alone would leave "how much memory does one message /// cost" to the sender. /// /// private sealed class CappedMemoryStream(long limit) : MemoryStream { /// public override void Write(byte[] buffer, int offset, int count) { Guard(count); base.Write(buffer, offset, count); } /// public override void Write(ReadOnlySpan buffer) { Guard(buffer.Length); base.Write(buffer); } /// public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) { Guard(count); return base.WriteAsync(buffer, offset, count, cancellationToken); } /// public override ValueTask WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default) { Guard(buffer.Length); return base.WriteAsync(buffer, cancellationToken); } /// public override void WriteByte(byte value) { Guard(1); base.WriteByte(value); } private void Guard(int incoming) { if (limit > 0 && Length + incoming > limit) throw new InvalidOperationException( $"Telegram download: the transfer passed the {limit}-byte ceiling (maxDownloadBytes) " + "and was stopped. Telegram did not declare the size up front."); } } // ── Resolution helpers ──────────────────────────────────────────────────── private ChatId ResolveChatId(IExchange exchange) { // 1. Header set by consumer or upstream processor if (exchange.In.Headers.TryGetValue(TelegramHeaders.ChatId, out var headerValue) && headerValue is not null) { switch (headerValue) { case long l: return new ChatId(l); case int i: return new ChatId(i); case string s when long.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsed): return new ChatId(parsed); case string s: return new ChatId(s); // @channelusername } try { return new ChatId(Convert.ToInt64(headerValue, CultureInfo.InvariantCulture)); } catch (Exception ex) when (ex is FormatException or InvalidCastException or OverflowException) { throw new InvalidOperationException( $"Telegram producer: header '{TelegramHeaders.ChatId}' value '{headerValue}' " + "is not a valid chat id (expected long or '@channelusername').", ex); } } // 2. Options (from URI or DSL builder) if (!string.IsNullOrWhiteSpace(_options.ChatId)) { var resolved = _options.ResolveOption(_options.ChatId, exchange); if (string.IsNullOrWhiteSpace(resolved)) throw new InvalidOperationException( "Telegram producer: chatId expression resolved to an empty value. " + "Check that the header or environment variable referenced in ChatId is set."); if (long.TryParse(resolved, NumberStyles.Integer, CultureInfo.InvariantCulture, out var id)) return new ChatId(id); return new ChatId(resolved); // @channelusername } throw new InvalidOperationException( "Telegram producer: no chatId. Set header 'telegram.chatId' or use " + ".ChatId(...) in the fluent builder."); } private string ResolveText(IExchange exchange) { var body = exchange.In.Body; var text = body switch { string s => s, byte[] b => System.Text.Encoding.UTF8.GetString(b), null => null, _ => BodyText.Of(body) }; if (string.IsNullOrEmpty(text)) throw new InvalidOperationException( $"Telegram {_endpoint.Mode}: exchange.In.Body must contain non-empty text " + "(string or UTF-8 byte[]). Telegram rejects empty message text with HTTP 400."); return text; } private ParseMode ResolveParseMode(IExchange exchange) { // Header takes precedence over options for per-message overrides. string? raw = null; if (exchange.In.Headers.TryGetValue(TelegramHeaders.ParseMode, out var h) && h is not null) raw = h.ToString(); if (string.IsNullOrWhiteSpace(raw)) raw = _options.ParseMode; // Fail loud on a typo (e.g. "Markdwn") instead of silently downgrading to plain text and // shipping raw *bold* / markup to the user. if (!TelegramEndpointOptions.TryParseParseMode(raw, out var mode)) throw new InvalidOperationException( $"Telegram {_endpoint.Mode}: invalid parseMode '{raw}'. Expected HTML, Markdown, MarkdownV2 or empty."); return mode; } private bool ResolveDisableNotification(IExchange exchange) { if (exchange.In.Headers.TryGetValue(TelegramHeaders.DisableNotification, out var v) && v is not null) { return v switch { bool b => b, string s when bool.TryParse(s, out var parsed) => parsed, _ => _options.DisableNotification }; } return _options.DisableNotification; } private string? ResolveCaption(IExchange exchange) { // Header takes precedence over options for per-message overrides. if (exchange.In.Headers.TryGetValue(TelegramHeaders.Caption, out var h) && h is string s) return s; if (!string.IsNullOrWhiteSpace(_options.Caption)) return _options.ResolveOption(_options.Caption, exchange); return null; } private bool ResolveShowAlert(IExchange exchange) { if (exchange.In.Headers.TryGetValue(TelegramHeaders.ShowAlert, out var v) && v is not null) { return v switch { bool b => b, string s when bool.TryParse(s, out var parsed) => parsed, _ => _options.ShowAlert }; } return _options.ShowAlert; } private InputFile ResolveInputFile(IExchange exchange) { // 1. Explicit file_id header — reuse a file already hosted on Telegram. if (exchange.In.Headers.TryGetValue(TelegramHeaders.FileId, out var fileIdHeader) && fileIdHeader is string fileId && !string.IsNullOrWhiteSpace(fileId)) { return InputFile.FromFileId(fileId); } var body = exchange.In.Body; var fileName = ResolveFileName(exchange); switch (body) { case Stream s: return InputFile.FromStream(s, fileName ?? "file"); case byte[] bytes: return InputFile.FromStream(new MemoryStream(bytes), fileName ?? "file"); // URL string is checked before the file_id opt-in because URLs are always // preferred when they parse as absolute http(s). case string s when Uri.TryCreate(s, UriKind.Absolute, out var uri) && (uri.Scheme == "http" || uri.Scheme == "https"): return InputFile.FromUri(uri); // Explicit URI opt-in: bodyIsFileId=true treats the string body as a file_id. case string s when _options.BodyIsFileId: return InputFile.FromFileId(s); case string: throw new InvalidOperationException( $"Telegram {_endpoint.Mode}: string body is not a valid input. " + "Provide a Stream, byte[], or HTTP(S) URL. " + $"To reuse an existing Telegram file, set header '{TelegramHeaders.FileId}' " + "or add 'bodyIsFileId=true' to the URI."); default: throw new InvalidOperationException( $"Telegram {_endpoint.Mode}: unsupported body type '{body?.GetType().Name ?? "null"}'. " + "Expected Stream, byte[], HTTP(S) URL string, or 'telegram.fileId' header."); } } private string? ResolveFileName(IExchange exchange) { if (!string.IsNullOrWhiteSpace(_options.FileName)) return _options.ResolveOption(_options.FileName, exchange); if (exchange.In.Headers.TryGetValue(TelegramHeaders.FileName, out var h) && h is string s) return s; return null; } private static ReplyMarkup? ResolveReplyMarkup(IExchange exchange) { if (!exchange.In.Headers.TryGetValue(TelegramHeaders.ReplyMarkup, out var v) || v is null) return null; if (v is ReplyMarkup markup) return markup; throw new InvalidOperationException( $"Telegram: header '{TelegramHeaders.ReplyMarkup}' must be a Telegram.Bot ReplyMarkup " + $"(InlineKeyboardMarkup / ReplyKeyboardMarkup / ...), but was '{v.GetType().Name}'."); } private int? ResolveReplyToMessageId(IExchange exchange) { // Explicit header always wins (per-message override, same as chatId). if (exchange.In.Headers.TryGetValue(TelegramHeaders.ReplyToMessageId, out var v) && v is not null) return ToMessageId(v); if (!string.IsNullOrWhiteSpace(_options.ReplyToMessageId)) { var resolved = _options.ResolveOption(_options.ReplyToMessageId, exchange); // Empty resolution: the referenced header is absent on this update type // (e.g. .ReplyToIncoming() on an update without telegram.messageId) — // send unthreaded rather than fail the exchange. if (string.IsNullOrWhiteSpace(resolved)) return null; if (int.TryParse(resolved, NumberStyles.Integer, CultureInfo.InvariantCulture, out var id)) return id; throw new InvalidOperationException( $"Telegram {_endpoint.Mode}: replyToMessageId resolved to '{resolved}', " + "which is not a valid message id."); } return null; } private static int? ToMessageId(object v) => v switch { int i => i, long l => (int)l, string s when int.TryParse(s, NumberStyles.Integer, CultureInfo.InvariantCulture, out var id) => id, _ => null }; private int ResolveMessageId(IExchange exchange) { // Explicit header always wins (per-message override, same as chatId). if (exchange.In.Headers.TryGetValue(TelegramHeaders.MessageId, out var v) && v is not null) return ToMessageId(v) ?? throw new InvalidOperationException( $"Telegram producer: cannot convert 'telegram.messageId' value '{v}' to int."); if (!string.IsNullOrWhiteSpace(_options.MessageId)) { var resolved = _options.ResolveOption(_options.MessageId, exchange); if (int.TryParse(resolved, NumberStyles.Integer, CultureInfo.InvariantCulture, out var id)) return id; throw new InvalidOperationException( $"Telegram {_endpoint.Mode}: messageId resolved to '{resolved}', " + "which is not a valid message id."); } throw new InvalidOperationException( "Telegram producer: no message id for edit/delete. Set header 'telegram.messageId' " + "or use .MessageId(...) in the fluent builder."); } private static void SetSentMessageId(IExchange exchange, TgMessage? result) { if (result is null) return; exchange.In.Headers[TelegramHeaders.SentMessageId] = result.MessageId; } private void LogPublishDiagnostics(ApiRequestException ex, IExchange exchange) { var body = exchange.In.Body; var bodySize = body switch { string s => s.Length, byte[] b => b.Length, Stream s when s.CanSeek => (int)Math.Min(int.MaxValue, s.Length), _ => -1 }; Logger?.LogError(ex, "Telegram publish failed: mode={Mode} parseMode={ParseMode} " + "bodySize={BodySize} errorCode={ErrorCode} retryAfter={RetryAfter}", _endpoint.Mode, _options.ParseMode ?? "None", bodySize, ex.ErrorCode, ex.Parameters?.RetryAfter); } }