using BotPages.Core; using BotPages.Core.Abstractions; using BotPages.Core.Context; using System; using System.Collections.Generic; using System.IO; using System.Threading; using System.Threading.Tasks; using Telegram.Bot; using Telegram.Bot.Types; namespace BotPages.Telegram; /// /// Утилита для маппинга Telegram Update → UpdateContext. /// public static class TelegramUpdateMapper { /// /// Маппинг Telegram Update в UpdateContext BotPages. /// public static UpdateContext Map(string MessengerType, Update update, TelegramBotClient client) { var chat = update.Message?.Chat ?? update.CallbackQuery?.Message?.Chat; var user = update.Message?.From ?? update.CallbackQuery?.From; var userContext = new UserContext { Id = user?.Id.ToString() ?? "unknown", DisplayName = user?.Username, }; var chatContext = new ChatContext { Id = chat?.Id.ToString() ?? "unknown", Title = chat?.Title, }; string? text = null; UpdateKind kind = UpdateKind.None; var files = new List(); if (update.Message is { } msg) { if (msg.Text is not null) { text = msg.Text; kind |= UpdateKind.Text; } if (msg.Photo is { Length: > 0 }) { foreach (var p in msg.Photo) { files.Add(new FileDescriptor { Id = p.FileId, Name = "photo", Extension = "jpg", Size = p.FileSize ?? 0, Kind = FileKind.Photo, GetStreamAsync = GetStreamAsync(client, p.FileId), }); } } if (msg.Document is not null) { files.Add(new FileDescriptor { Id = msg.Document.FileId, Name = msg.Document.FileName ?? "document", Extension = System.IO.Path.GetExtension(msg.Document.FileName) ?? "bin", Size = msg.Document.FileSize ?? 0, Kind = FileKind.Document, Mime = msg.Document.MimeType, GetStreamAsync = GetStreamAsync(client, msg.Document.FileId), }); } if (msg.Audio is not null) { files.Add(new FileDescriptor { Id = msg.Audio.FileId, Name = msg.Audio.FileName ?? "audio", Extension = System.IO.Path.GetExtension(msg.Audio.FileName) ?? "mp3", Size = msg.Audio.FileSize ?? 0, Kind = FileKind.Audio, Mime = msg.Audio.MimeType, GetStreamAsync = GetStreamAsync(client, msg.Audio.FileId), }); } if (msg.Video is not null) { files.Add(new FileDescriptor { Id = msg.Video.FileId, Name = "video", Extension = "mp4", Size = msg.Video.FileSize ?? 0, Kind = FileKind.Video, Mime = msg.Video.MimeType, GetStreamAsync = GetStreamAsync(client, msg.Video.FileId), }); } if (files.Count > 0) { kind |= UpdateKind.File; } } if (update.CallbackQuery is { } cb) { kind |= UpdateKind.Button; text = cb.Data; } return new UpdateContext { MessengerType = MessengerType, User = userContext, Chat = chatContext, Text = text, Kind = kind, Files = files }; } private static Func> GetStreamAsync(TelegramBotClient client, string fileId) { Func> getStreamAsync = async _ => { var file = await client.GetFile(fileId); var stream = new MemoryStream(); await client.DownloadFile(file, stream); stream.Position = 0; return stream; }; return getStreamAsync; } }