using BotPages.Core; using Telegram.Bot; using Telegram.Bot.Types; namespace BotPages.Telegram { /// /// Утилиты для извлечения контекста из Telegram Update. /// public static class TelegramUpdateMapper { /// /// Преобразует Telegram Update в универсальный UpdateContext. /// public static UpdateContext Map(ITelegramBotClient bot, INavigationService nav, IStateStore store, Update update) { var chat = update.Message?.Chat ?? update.CallbackQuery?.Message?.Chat; var user = update.Message?.From ?? update.CallbackQuery?.From; var text = update.Message?.Text ?? update.CallbackQuery?.Data; var files = new List(); if (update.Message?.Document is { } doc) { files.Add(new FileDescriptor(doc.FileId, doc.FileName ?? "file", doc.MimeType ?? "application/octet-stream")); } if (update.Message?.Photo is { } photos && photos.Count() > 0) { var largest = photos.OrderBy(p => p.FileSize).Last(); files.Add(new FileDescriptor(largest.FileId, "photo.jpg", "image/jpeg")); } return new UpdateContext { Client = new TelegramClientAdapter(bot), Chat = new ChatContext { Id = chat!.Id, Title = chat.Title }, User = new UserContext { Id = user!.Id, DisplayName = $"{user.FirstName} {user.LastName}" }, Text = text, IncomingFiles = files, RawUpdate = update, Nav = nav, State = store }; } } }