Переработанная версия ядра
All checks were successful
CI / build-test (push) Successful in 42s

This commit is contained in:
2025-12-05 12:57:05 +03:00
parent ee175a35a0
commit d817417a69
81 changed files with 2335 additions and 1453 deletions

View File

@@ -1,18 +1,21 @@
namespace BotPages.Core
{
/// <summary>
/// Описывает чат/конверсацию для универсального контекста.
/// </summary>
public sealed class ChatContext
{
/// <summary>
/// Уникальный идентификатор чата/диалога.
/// </summary>
public long Id { get; init; }
using BotPages.Core.Abstractions;
/// <summary>
/// Человеко-читаемое имя чата (если доступно).
/// </summary>
public string? Title { get; init; }
}
}
namespace BotPages.Core;
/// <summary>
/// Данные чата.
/// </summary>
public sealed class ChatContext
{
/// <summary>Идентификатор чата.</summary>
public required string Id { get; init; }
/// <summary>Название чата (опционально).</summary>
public string? Title { get; init; }
/// <summary>Идентификатор треда (опционально).</summary>
public string? ThreadId { get; init; }
/// <summary>Возможности мессенджера.</summary>
public Capabilities Capabilities { get; init; } = new();
}

View File

@@ -1,13 +0,0 @@
namespace BotPages.Core
{
/// <summary>
/// Универсальный дескриптор файла для операций загрузки/отправки.
/// </summary>
public sealed record FileDescriptor(
string Id,
string Name,
string MimeType,
int Size,
Stream? Content = null
);
}

View File

@@ -0,0 +1,88 @@
using BotPages.Core.Abstractions;
using BotPages.Core.Context;
using BotPages.Core.Messaging;
namespace BotPages.Core;
/// <summary>
/// Контекст страницы, объединяющий пользователя, чат, состояние и адаптер.
/// </summary>
public sealed class PageContext
{
/// <summary>Ключ сессии.</summary>
public required CompositeSessionKey SessionKey { get; init; }
/// <summary>Данные обновления.</summary>
public required UpdateContext Update { get; init; }
/// <summary>Хранилище состояния.</summary>
public required IStateStorage StateStorage { get; init; }
/// <summary>Сервис навигации.</summary>
public required NavigationService Navigation { get; init; }
/// <summary>Адаптер мессенджера.</summary>
public required IMessengerAdapter Adapter { get; init; }
/// <summary>
/// Отправить текстовое сообщение.
/// </summary>
public Task SendTextAsync(string text, MessageFormat format = MessageFormat.Plain,
IEnumerable<IEnumerable<InlineButton>>? inline = null,
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
CancellationToken ct = default)
=> Adapter.SendTextAsync(this, text, format, inline, reply, ct);
/// <summary>
/// Отправить файл.
/// </summary>
public Task SendFileAsync(FileDescriptor file, string? caption = null, CancellationToken ct = default)
=> Adapter.SendFileAsync(this, file, caption, ct);
/// <summary>
/// Получить билдер альбомов.
/// </summary>
public IAlbumBuilder Albums => Adapter.CreateAlbumBuilder(this);
/// <summary>
/// Начать прогресс операции.
/// </summary>
public async Task<string?> StartProgressAsync(string title, CancellationToken ct)
{
var messageId = await Adapter.StartProgressAsync(this, title, ct);
if (messageId != null)
{
_progressMessageId = messageId;
_progressTitle = title;
}
return messageId;
}
/// <summary>
/// Обновить прогресс операции.
/// </summary>
public Task UpdateProgressAsync(int percent, CancellationToken ct)
{
if (_progressMessageId != null)
{
return Adapter.UpdateProgressAsync(this, _progressMessageId, _progressTitle ?? "", percent, ct);
}
else
{
return Task.CompletedTask;
}
}
/// <summary>
/// Обновить прогресс операции.
/// </summary>
public Task UpdateProgressAsync(string messageId, int percent, CancellationToken ct)
{
return Adapter.UpdateProgressAsync(this, messageId, _progressTitle ?? "", percent, ct);
}
private string? _progressMessageId = null;
private string? _progressTitle = null;
}

View File

@@ -1,48 +1,59 @@
namespace BotPages.Core
namespace BotPages.Core.Context;
using BotPages.Core.Abstractions;
/// <summary>
/// Тип входящего обновления.
/// </summary>
[Flags]
public enum UpdateKind
{
/// <summary>Неизвестное сообщение.</summary>
None = 0,
/// <summary>Текстовое сообщение.</summary>
Text = 1 << 0,
/// <summary>Файлы (один или несколько).</summary>
File = 1 << 1,
/// <summary>Нажатие кнопки.</summary>
Button = 1 << 2,
}
/// <summary>
/// Контекст входящего обновления от мессенджера.
/// Содержит нормализованные данные для страниц.
/// </summary>
public sealed class UpdateContext
{
/// <summary>Тип мессенджера.</summary>
public required string MessengerType { get; init; }
/// <summary>
/// Универсальный контекст обновления, независимый от транспорта.
/// Данные пользователя, от которого пришло обновление.
/// </summary>
public sealed class UpdateContext
{
/// <summary>
/// Клиент транспорта для отправки сообщений/файлов.
/// </summary>
public required IChatClient Client { get; init; }
public required UserContext User { get; init; }
/// <summary>
/// Контекст чата.
/// </summary>
public required ChatContext Chat { get; init; }
/// <summary>
/// Данные чата, в котором произошло обновление.
/// </summary>
public required ChatContext Chat { get; init; }
/// <summary>
/// Контекст пользователя.
/// </summary>
public required UserContext User { get; init; }
/// <summary>
/// Тип обновления (текст, файлы, кнопка).
/// </summary>
public required UpdateKind Kind { get; init; }
/// <summary>
/// Текст сообщения или полезная нагрузка колбэка, если доступна.
/// </summary>
public string? Text { get; init; }
/// <summary>
/// Текст сообщения, если Kind = Text.
/// Payload кнопки, если Kind = Button.
/// </summary>
public string? Text { get; init; }
/// <summary>
/// Список полученных файлов (если транспорт поддерживает).
/// </summary>
public IReadOnlyList<FileDescriptor>? IncomingFiles { get; init; }
/// <summary>
/// Сырой объект обновления транспорта (например, Telegram.Update).
/// </summary>
public object? RawUpdate { get; init; }
/// <summary>
/// Сервис навигации страниц.
/// </summary>
public required INavigationService Nav { get; init; }
/// <summary>
/// Хранилище состояния пользователя.
/// </summary>
public required IStateStore State { get; init; }
}
}
/// <summary>
/// Список файлов, если Kind = File.
/// Может содержать один или несколько файлов.
/// </summary>
public List<FileDescriptor> Files { get; init; } = new();
}

View File

@@ -1,18 +1,16 @@
namespace BotPages.Core
{
/// <summary>
/// Описывает пользователя для универсального контекста.
/// </summary>
public sealed class UserContext
{
/// <summary>
/// Уникальный идентификатор пользователя в транспортном слое.
/// </summary>
public long Id { get; init; }
namespace BotPages.Core;
/// <summary>
/// Отображаемое имя пользователя (если доступно).
/// </summary>
public string? DisplayName { get; init; }
}
/// <summary>
/// Данные пользователя.
/// </summary>
public sealed class UserContext
{
/// <summary>Идентификатор пользователя.</summary>
public required string Id { get; init; }
/// <summary>
/// Отображаемое имя пользователя (если доступно).
/// </summary>
public string? DisplayName { get; init; }
}