Добавьте файлы проекта.
This commit is contained in:
46
BotPages.Telegram/TelegramUpdateMapper.cs
Normal file
46
BotPages.Telegram/TelegramUpdateMapper.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using BotPages.Core;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Types;
|
||||
|
||||
namespace BotPages.Telegram
|
||||
{
|
||||
/// <summary>
|
||||
/// Утилиты для извлечения контекста из Telegram Update.
|
||||
/// </summary>
|
||||
public static class TelegramUpdateMapper
|
||||
{
|
||||
/// <summary>
|
||||
/// Преобразует Telegram Update в универсальный UpdateContext.
|
||||
/// </summary>
|
||||
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<FileDescriptor>();
|
||||
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
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user