Path: redb-route/redb.Route.Telegram/TelegramUpdateMapper.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/TelegramUpdateMapper.cs
----
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using redb.Route.Abstractions;
using TgMessage = Telegram.Bot.Types.Message;
namespace redb.Route.Telegram;
///
/// Maps Telegram / objects to
/// headers using the canonical constants.
///
/// Shared between (long-polling) and
/// (webhook) so both paths
/// produce identical header semantics.
///
///
public static class TelegramUpdateMapper
{
///
/// Populates body and headers from a Telegram message update.
/// Call this when the update carries a payload.
///
/// Telegram message.
/// Update type (e.g. or EditedMessage).
/// Route message to populate.
public static void MapMessage(TgMessage msg, UpdateType updateType, IMessage message)
{
message.Body = msg.Text ?? string.Empty;
message.Headers[TelegramHeaders.UpdateType] = updateType.ToString();
message.Headers[TelegramHeaders.MessageType] = msg.Type.ToString();
message.Headers[TelegramHeaders.MessageId] = msg.MessageId;
AddIfNotNull(message.Headers, TelegramHeaders.Text, msg.Text);
// Mini app payload (WebApp.sendData). Such a message has no Text, so the
// data doubles as the exchange body — matching the "body = what the user sent"
// contract of text messages and callback queries.
if (msg.WebAppData is { } webApp)
{
message.Body = webApp.Data;
message.Headers[TelegramHeaders.WebAppData] = webApp.Data;
AddIfNotNull(message.Headers, TelegramHeaders.WebAppButtonText, webApp.ButtonText);
}
MapAttachment(msg, message);
message.Headers[TelegramHeaders.ChatId] = msg.Chat.Id;
message.Headers[TelegramHeaders.ChatType] = msg.Chat.Type.ToString();
if (msg.From is { } from)
{
message.Headers[TelegramHeaders.UserId] = from.Id;
message.Headers[TelegramHeaders.FirstName] = from.FirstName;
AddIfNotNull(message.Headers, TelegramHeaders.LastName, from.LastName);
AddIfNotNull(message.Headers, TelegramHeaders.Username, from.Username);
AddIfNotNull(message.Headers, TelegramHeaders.LanguageCode, from.LanguageCode);
}
}
///
/// Populates body and headers from a generic .
/// Returns when the update type is handled;
/// for unrecognised / unsupported types (caller should skip the exchange).
///
/// Telegram update.
/// Route message to populate.
/// if the update was mapped; to skip.
public static bool MapUpdate(Update update, IMessage message)
{
message.Headers[TelegramHeaders.UpdateId] = update.Id;
message.Headers[TelegramHeaders.UpdateType] = update.Type.ToString();
switch (update.Type)
{
// ── Message-bearing updates — delegate to MapMessage ──────────────
case UpdateType.Message when update.Message is { } m: MapMessage(m, UpdateType.Message, message); return true;
case UpdateType.EditedMessage when update.EditedMessage is { } m: MapMessage(m, UpdateType.EditedMessage, message); return true;
case UpdateType.ChannelPost when update.ChannelPost is { } m: MapMessage(m, UpdateType.ChannelPost, message); return true;
case UpdateType.EditedChannelPost when update.EditedChannelPost is { } m: MapMessage(m, UpdateType.EditedChannelPost, message); return true;
// ── CallbackQuery ─────────────────────────────────────────────────
case UpdateType.CallbackQuery when update.CallbackQuery is { } cb:
message.Body = cb.Data ?? string.Empty;
message.Headers[TelegramHeaders.CallbackQueryId] = cb.Id;
message.Headers[TelegramHeaders.UserId] = cb.From.Id;
message.Headers[TelegramHeaders.FirstName] = cb.From.FirstName;
AddIfNotNull(message.Headers, TelegramHeaders.CallbackData, cb.Data);
AddIfNotNull(message.Headers, TelegramHeaders.ChatId, cb.Message?.Chat.Id);
AddIfNotNull(message.Headers, TelegramHeaders.Username, cb.From.Username);
return true;
default:
return false;
}
}
// ── Internal helpers ──────────────────────────────────────────────────────
///
/// Exposes the attachment of an incoming message as headers: kind, file_id and
/// whatever metadata Telegram sent with it.
///
/// Without this a file-bearing update is unreachable: the body carries
/// , which a voice note does not have, so a route could see
/// that a recording arrived () but had no
/// way to fetch it. The body is deliberately left alone — a caption stays a header, so a
/// captioned photo is not mistaken for a typed command.
///
///
private static void MapAttachment(TgMessage msg, IMessage message)
{
// Photo is a size ladder rather than a single file; the last entry is the largest.
var photo = msg.Photo is { Length: > 0 } sizes ? sizes[^1] : null;
var (kind, file) = msg switch
{
{ Voice: { } voice } => ("voice", (FileBase)voice),
{ Audio: { } audio } => ("audio", audio),
{ VideoNote: { } note } => ("videoNote", note),
{ Video: { } video } => ("video", video),
{ Animation: { } animation } => ("animation", animation),
{ Document: { } document } => ("document", document),
{ Sticker: { } sticker } => ("sticker", sticker),
_ when photo is not null => ("photo", photo),
_ => (null, null),
};
if (kind is null || file is null)
return;
message.Headers[TelegramHeaders.AttachmentKind] = kind;
message.Headers[TelegramHeaders.AttachmentFileId] = file.FileId;
AddIfNotNull(message.Headers, TelegramHeaders.AttachmentFileSize, file.FileSize);
AddIfNotNull(message.Headers, TelegramHeaders.AttachmentCaption, msg.Caption);
AddIfNotNull(message.Headers, TelegramHeaders.AttachmentMimeType, msg switch
{
{ Voice: { } voice } => voice.MimeType,
{ Audio: { } audio } => audio.MimeType,
{ Video: { } video } => video.MimeType,
{ Animation: { } animation } => animation.MimeType,
{ Document: { } document } => document.MimeType,
_ => null,
});
AddIfNotNull(message.Headers, TelegramHeaders.AttachmentDuration, msg switch
{
{ Voice: { } voice } => voice.Duration,
{ Audio: { } audio } => audio.Duration,
{ Video: { } video } => video.Duration,
{ Animation: { } animation } => animation.Duration,
{ VideoNote: { } note } => note.Duration,
_ => (int?)null,
});
AddIfNotNull(message.Headers, TelegramHeaders.AttachmentFileName, msg switch
{
{ Audio: { } audio } => audio.FileName,
{ Video: { } video } => video.FileName,
{ Document: { } document } => document.FileName,
_ => null,
});
}
private static void AddIfNotNull(IDictionary headers, string key, object? value)
{
if (value is not null) headers[key] = value;
}
}