Добавьте файлы проекта.
This commit is contained in:
14
Demo/Demo.csproj
Normal file
14
Demo/Demo.csproj
Normal file
@@ -0,0 +1,14 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\BotPages\BotPages.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
39
Demo/Pages/FilesPage.cs
Normal file
39
Demo/Pages/FilesPage.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using BotPages.Core;
|
||||
|
||||
namespace Demo.Pages
|
||||
{
|
||||
public sealed class FilesPage : Page
|
||||
{
|
||||
public static string Id => nameof(FilesPage);
|
||||
|
||||
public override Task<PageResult> EnterAsync(UpdateContext ctx, CancellationToken ct)
|
||||
{
|
||||
var actions = new[]
|
||||
{
|
||||
new PageAction { Label = "⬅️ Назад", Value = "back", Placement = ActionPlacement.Reply, Row = 0 }
|
||||
};
|
||||
|
||||
return Task.FromResult(
|
||||
PageResultBuilder.Empty()
|
||||
.WithText("📂 Здесь можно загрузить или отправить файл.")
|
||||
.WithKeyboard(actions)
|
||||
.Build()
|
||||
);
|
||||
}
|
||||
|
||||
public override async Task<PageResult> HandleAsync(UpdateContext ctx, CancellationToken ct)
|
||||
{
|
||||
if (ctx.Text == "⬅️ Назад")
|
||||
return PageResultBuilder.Empty().WithNavigate(nameof(MainPage)).Build();
|
||||
|
||||
if (ctx.IncomingFiles?.Count > 0)
|
||||
{
|
||||
await ctx.Client.SendFilesAsync(ctx.Chat.Id, ctx.IncomingFiles, ct);
|
||||
return PageResultBuilder.Empty().WithText("Файл получен и отправлен обратно.").Build();
|
||||
}
|
||||
|
||||
return PageResultBuilder.Empty().WithText("Пришлите файл или нажмите 'Назад'.").Build();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
35
Demo/Pages/InlinePage.cs
Normal file
35
Demo/Pages/InlinePage.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using BotPages.Core;
|
||||
|
||||
namespace Demo.Pages
|
||||
{
|
||||
public sealed class InlinePage : Page
|
||||
{
|
||||
public override string Id => nameof(InlinePage);
|
||||
|
||||
public override Task<PageResult> EnterAsync(UpdateContext ctx, CancellationToken ct)
|
||||
{
|
||||
var actions = new[]
|
||||
{
|
||||
new PageAction { Label = "⬅️ Назад", Value = "back", Placement = ActionPlacement.Inline, Row = 0 }
|
||||
};
|
||||
|
||||
return Task.FromResult(
|
||||
PageResultBuilder.Empty()
|
||||
.WithText("Это страница с Inline‑кнопками.")
|
||||
.WithKeyboard(actions)
|
||||
.Build()
|
||||
);
|
||||
}
|
||||
|
||||
public override Task<PageResult> HandleAsync(UpdateContext ctx, CancellationToken ct)
|
||||
{
|
||||
if (ctx.Text == "back")
|
||||
return Task.FromResult(PageResultBuilder.Empty().WithNavigate(nameof(MainPage)).Build());
|
||||
|
||||
return Task.FromResult(PageResultBuilder.Empty().WithText("Нажмите кнопку 'Назад'.").Build());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
38
Demo/Pages/MainPage.cs
Normal file
38
Demo/Pages/MainPage.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using BotPages.Core;
|
||||
|
||||
namespace Demo.Pages
|
||||
{
|
||||
public sealed class MainPage : Page
|
||||
{
|
||||
public override string Id => nameof(MainPage);
|
||||
|
||||
public override Task<PageResult> EnterAsync(UpdateContext ctx, CancellationToken ct)
|
||||
{
|
||||
var actions = new[]
|
||||
{
|
||||
new PageAction { Label = "📌 Inline", Value = "inline", Placement = ActionPlacement.Reply, Row = 0 },
|
||||
new PageAction { Label = "⌨️ Reply", Value = "reply", Placement = ActionPlacement.Reply, Row = 1 },
|
||||
new PageAction { Label = "📂 Файлы", Value = "files", Placement = ActionPlacement.Reply, Row = 2 }
|
||||
};
|
||||
|
||||
return Task.FromResult(
|
||||
PageResultBuilder.Empty()
|
||||
.WithText("🏠 Главная страница.\nВыберите куда перейти:")
|
||||
.WithKeyboard(actions)
|
||||
.Build()
|
||||
);
|
||||
}
|
||||
|
||||
public override Task<PageResult> HandleAsync(UpdateContext ctx, CancellationToken ct)
|
||||
{
|
||||
return ctx.Text switch
|
||||
{
|
||||
"📌 Inline" => Task.FromResult(PageResultBuilder.Empty().WithNavigate(nameof(InlinePage)).Build()),
|
||||
"⌨️ Reply" => Task.FromResult(PageResultBuilder.Empty().WithNavigate(nameof(ReplyPage)).Build()),
|
||||
"📂 Файлы" => Task.FromResult(PageResultBuilder.Empty().WithNavigate(nameof(FilesPage)).Build()),
|
||||
_ => Task.FromResult(PageResultBuilder.Empty().WithText("Выберите действие с кнопок.").Build())
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
33
Demo/Pages/ReplyPage.cs
Normal file
33
Demo/Pages/ReplyPage.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using BotPages.Core;
|
||||
|
||||
namespace Demo.Pages
|
||||
{
|
||||
public sealed class ReplyPage : Page
|
||||
{
|
||||
public override string Id => nameof(ReplyPage);
|
||||
|
||||
public override Task<PageResult> EnterAsync(UpdateContext ctx, CancellationToken ct)
|
||||
{
|
||||
var actions = new[]
|
||||
{
|
||||
new PageAction { Label = "⬅️ Назад", Value = "back", Placement = ActionPlacement.Reply, Row = 0 }
|
||||
};
|
||||
|
||||
return Task.FromResult(
|
||||
PageResultBuilder.Empty()
|
||||
.WithText("Это страница с Reply‑клавиатурой.")
|
||||
.WithKeyboard(actions)
|
||||
.Build()
|
||||
);
|
||||
}
|
||||
|
||||
public override Task<PageResult> HandleAsync(UpdateContext ctx, CancellationToken ct)
|
||||
{
|
||||
if (ctx.Text == "⬅️ Назад")
|
||||
return Task.FromResult(PageResultBuilder.Empty().WithNavigate(nameof(MainPage)).Build());
|
||||
|
||||
return Task.FromResult(PageResultBuilder.Empty().WithText("Нажмите кнопку 'Назад'.").Build());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
65
Demo/Program.cs
Normal file
65
Demo/Program.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using BotPages.Core;
|
||||
using BotPages.Telegram;
|
||||
using Demo.Pages;
|
||||
using Telegram.Bot;
|
||||
|
||||
namespace Demo
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
public static async Task Main(string[] args)
|
||||
{
|
||||
// Токен Telegram бота
|
||||
var token = Environment.GetEnvironmentVariable("TELEGRAM_TOKEN") ?? throw new InvalidOperationException("TELEGRAM_TOKEN not set");
|
||||
|
||||
// Инициализация Telegram клиента
|
||||
var botClient = new TelegramBotClient(token);
|
||||
var chatClient = new TelegramClientAdapter(botClient);
|
||||
|
||||
|
||||
// Регистрируем страницы
|
||||
var pages = new IPage[]
|
||||
{
|
||||
new MainPage(),
|
||||
new InlinePage(),
|
||||
new ReplyPage(),
|
||||
new FilesPage()
|
||||
};
|
||||
var registry = new PageRegistry(pages, pages[0]);
|
||||
|
||||
// Навигация и состояние
|
||||
IStateStore store = new InMemoryStateStore();
|
||||
INavigationService nav = new NavigationService(registry, store);
|
||||
var router = new Router(registry);
|
||||
|
||||
var middleware = new IUpdateMiddleware[]
|
||||
{
|
||||
new LoggingMiddleware(), //логирование вызова в консоль
|
||||
new ErrorHandlingMiddleware(), //обработчик ошибок
|
||||
//new ThrottleMiddleware(TimeSpan.FromMilliseconds(150)), //задержка в 150мс перед ответом
|
||||
};
|
||||
|
||||
var pipeline = new Pipeline(middleware, router);
|
||||
|
||||
botClient.StartReceiving(
|
||||
async (bot, update, ct) =>
|
||||
{
|
||||
var ctx = TelegramUpdateMapper.Map(bot, nav, store, update);
|
||||
|
||||
await pipeline.ExecuteAsync(ctx, ct);
|
||||
},
|
||||
(bot, error, ct) =>
|
||||
{
|
||||
Console.WriteLine($"⚠️ Ошибка Telegram: {error}");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
);
|
||||
|
||||
var me = await botClient.GetMe();
|
||||
|
||||
Console.WriteLine($"BotPages Demo (@{me.Username}) запущен. Нажмите Enter для выхода.");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
10
Demo/Properties/launchSettings.json
Normal file
10
Demo/Properties/launchSettings.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Demo": {
|
||||
"commandName": "Project",
|
||||
"environmentVariables": {
|
||||
"TELEGRAM_TOKEN": "7992309062:AAHkb4wnFi8w7H4V0zvUW_LJg55jtzuhahU"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user