using BotPages.Core.Abstractions; using BotPages.Core.Context; using BotPages.Core.Messaging; namespace BotPages.Core; /// /// Контекст страницы, объединяющий пользователя, чат, состояние и адаптер. /// public sealed class PageContext { /// Ключ сессии. public required CompositeSessionKey SessionKey { get; init; } /// Данные обновления. public required UpdateContext Update { get; init; } /// Хранилище состояния. public required IStateStorage StateStorage { get; init; } /// Сервис навигации. public required NavigationService Navigation { get; init; } /// Адаптер мессенджера. public required IMessengerAdapter Adapter { get; init; } //Storage /// Получить состояние по ключу. public Task GetStorageAsync(string key, CancellationToken ct) => StateStorage.GetAsync(SessionKey, key, ct); /// Сохранить состояние по ключу. public Task SetStorageAsync(string key, T state, CancellationToken ct) => StateStorage.SetAsync(SessionKey, key, state, ct); /// Удалить состояние по ключу. public Task RemoveStorageAsync(string key, CancellationToken ct) => StateStorage.RemoveAsync(SessionKey, key, ct); /// Удалить все состояния по ключу. public Task ClearStorageAsync(CancellationToken ct) => StateStorage.ClearAsync(SessionKey, ct); //Adapter /// /// Отправить текстовое сообщение. /// public Task SendTextAsync(string text, MessageFormat format = MessageFormat.Plain, IEnumerable>? inline = null, IEnumerable>? reply = null, CancellationToken ct = default) => Adapter.SendTextAsync(this.Update.Chat.Id, text, format, inline, reply, ct); /// /// Отправить файл. /// public Task SendFileAsync(FileDescriptor file, string? caption = null, CancellationToken ct = default) => Adapter.SendFileAsync(this.Update.Chat.Id, file, caption, ct); /// /// Получить билдер альбомов. /// public IAlbumBuilder Albums => Adapter.CreateAlbumBuilder(this); /// /// Начать прогресс операции. /// public async Task StartProgressAsync(string title, CancellationToken ct) { var messageId = await Adapter.StartProgressAsync(this, title, ct); if (messageId != null) { _progressMessageId = messageId; _progressTitle = title; } return messageId; } /// /// Обновить прогресс операции. /// public Task UpdateProgressAsync(int percent, CancellationToken ct) { if (_progressMessageId != null) { return Adapter.UpdateProgressAsync(this, _progressMessageId, _progressTitle ?? "", percent, ct); } else { return Task.CompletedTask; } } /// /// Обновить прогресс операции. /// 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; }