Files
BotPages/Demo/Program.cs
FrigaT 4fa140d91f
Some checks failed
CI / build-test (push) Successful in 29s
Release / pack-and-publish (release) Failing after 1m58s
Добавлена поддержка цветов кнопок телеграмма
2026-02-11 00:40:31 +03:00

67 lines
2.4 KiB
C#
Raw Permalink 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.
using BotPages.Core;
using BotPages.Core.Logging;
using BotPages.Core.Middleware;
using BotPages.Core.Routing;
using BotPages.Core.Storage;
using BotPages.Telegram;
using Demo.Pages;
namespace Demo
{
internal class Program
{
public static async Task Main(string[] args)
{
var token = Environment.GetEnvironmentVariable("TELEGRAM_TOKEN")
?? throw new InvalidOperationException("TELEGRAM_TOKEN not set");
var logger = new ConsoleLogger();
var state = new InMemoryStateStorage();
// Можно использовать команды для открытия страниц с роутингом
// /open Welcome
// /open FileSend
CommandHandler openHandler = async (ctx, args, ct) =>
{
if (args is null || !args.TryGetValue("page", out var pageName))
{
var req = new BotPages.Core.Abstractions.SendRequest
{
ChatId = ctx.Update.Chat.Id,
Text = "Не указана страница для открытия."
};
await ctx.SendAsync(req, ct: ct);
return;
}
// Навигация на страницу по имени
await ctx.Navigation.GoToAsync(pageName, ctx, ct);
};
var app = new BotPagesApp(state, logger)
.AddDefaultPage<WelcomePage>()
.MapCommand<WelcomePage>("/start", true, "Главная")
.MapCommand("/open {page}", openHandler, true, "открыть страницу /open {page}")
.MapCommand(DetailsPage.Command, DetailsPage.CommandHandler, true, DetailsPage.CommandDescription)
.AutoMapRoute()
.AddTelegramAdapter(token, "Telegram")
.AddMiddleware(new ErrorHandlingMiddleware(logger))
.AddMiddleware(new LoggingMiddleware(logger));
await app.RunAsync();
logger.Log(LogLevel.Info, "Bot is running. Press Ctrl+C to stop.");
Console.CancelKeyPress += (sender, e) =>
{
Console.WriteLine("Cancel key pressed");
app.Stop();
e.Cancel = true;
};
await app.WaitAsync();
}
}
}