Files
BotPages/BotPages.Core/Pipeline/Router.cs

45 lines
1.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace BotPages.Core
{
/// <summary>
/// Простой роутер: команды верхнего уровня и делегирование текущей странице.
/// </summary>
public sealed class Router : IRouter
{
private readonly IPageRegistry _pages;
/// <summary>
/// Создаёт роутер страниц.
/// </summary>
public Router(IPageRegistry pages) => _pages = pages;
/// <summary>
/// Определяет текущую страницу и вызывает её обработчик.
/// </summary>
public async Task RouteAsync(UpdateContext ctx, CancellationToken ct)
{
var text = ctx.Text ?? string.Empty;
if (text.StartsWith("/start", StringComparison.Ordinal))
{
await ctx.Nav.ReplaceAsync(_pages.DefaultPage.Id, null, ctx, ct);
return;
}
var current = (await ctx.Nav.CurrentAsync(ctx, ct))?.PageId;
if (current is not null)
{
var pr = await _pages.Get(current).HandleAsync(ctx, ct);
await ctx.Nav.ApplyResultAsync(ctx, pr, ct);
return;
}
else
{
await ctx.Nav.ReplaceAsync(_pages.DefaultPage.Id, null, ctx, ct);
return;
}
//TODO: Вынести в "дефолтный /start page"
await ctx.Client.SendTextAsync(ctx.Chat.Id, "Не понимаю. Нажмите /start.", null, ct);
}
}
}