Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fce6e8d013 | |||
| 57b3706241 |
@@ -201,7 +201,7 @@ public sealed class BotPagesApp
|
|||||||
|
|
||||||
if (page is null)
|
if (page is null)
|
||||||
{
|
{
|
||||||
await ctx.Navigation.GoToHome(ctx, ct);
|
await ctx.Navigation.GoToHomeAsync(ctx, ct);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,47 +22,6 @@ public sealed class PageContext
|
|||||||
/// <summary>Адаптер мессенджера.</summary>
|
/// <summary>Адаптер мессенджера.</summary>
|
||||||
public required IMessengerAdapter Adapter { get; init; }
|
public required IMessengerAdapter Adapter { get; init; }
|
||||||
|
|
||||||
//Storage
|
|
||||||
|
|
||||||
/// <summary>Получить состояние по ключу.</summary>
|
|
||||||
public Task<T?> GetStorageAsync<T>(string key, CancellationToken ct)
|
|
||||||
=> StateStorage.GetAsync<T>(SessionKey, key, ct);
|
|
||||||
|
|
||||||
/// <summary>Сохранить состояние по ключу.</summary>
|
|
||||||
public Task SetStorageAsync<T>(string key, T state, CancellationToken ct)
|
|
||||||
=> StateStorage.SetAsync<T>(SessionKey, key, state, ct);
|
|
||||||
|
|
||||||
/// <summary>Удалить состояние по ключу.</summary>
|
|
||||||
public Task<bool> RemoveStorageAsync(string key, CancellationToken ct)
|
|
||||||
=> StateStorage.RemoveAsync(SessionKey, key, ct);
|
|
||||||
|
|
||||||
/// <summary>Удалить все состояния по ключу.</summary>
|
|
||||||
public Task<bool> ClearStorageAsync(CancellationToken ct)
|
|
||||||
=> StateStorage.ClearAsync(SessionKey, ct);
|
|
||||||
|
|
||||||
//Adapter
|
|
||||||
|
|
||||||
/// <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.Update.Chat.Id, text, format, inline, reply, ct);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Отправить файл.
|
|
||||||
/// </summary>
|
|
||||||
public Task SendFileAsync(FileDescriptor file, string? caption = null, MessageFormat? captionFormat = null, CancellationToken ct = default)
|
|
||||||
=> Adapter.SendFileAsync(this.Update.Chat.Id, file, caption, captionFormat, ct);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Отправить файл.
|
|
||||||
/// </summary>
|
|
||||||
public Task SendFileAsync(FileDescriptor file, string? caption = null, CancellationToken ct = default)
|
|
||||||
=> Adapter.SendFileAsync(this.Update.Chat.Id, file, caption, null, ct);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Получить билдер альбомов.
|
/// Получить билдер альбомов.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
31
BotPages.Core/Context/PageContextAdapterExtensions.cs
Normal file
31
BotPages.Core/Context/PageContextAdapterExtensions.cs
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
using BotPages.Core.Abstractions;
|
||||||
|
using BotPages.Core.Messaging;
|
||||||
|
|
||||||
|
namespace BotPages.Core;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Расширения <see cref="PageContext"/> для работы с <see cref="IMessengerAdapter"/>
|
||||||
|
/// </summary>
|
||||||
|
public static class PageContextAdapterExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Отправить текстовое сообщение.
|
||||||
|
/// </summary>
|
||||||
|
public static Task SendTextAsync(this PageContext ctx, string text, MessageFormat format = MessageFormat.Plain,
|
||||||
|
IEnumerable<IEnumerable<InlineButton>>? inline = null,
|
||||||
|
IEnumerable<IEnumerable<ReplyButton>>? reply = null,
|
||||||
|
CancellationToken ct = default)
|
||||||
|
=> ctx.Adapter.SendTextAsync(ctx.Update.Chat.Id, text, format, inline, reply, ct);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Отправить файл.
|
||||||
|
/// </summary>
|
||||||
|
public static Task SendFileAsync(this PageContext ctx, FileDescriptor file, string? caption = null, MessageFormat? captionFormat = null, CancellationToken ct = default)
|
||||||
|
=> ctx.Adapter.SendFileAsync(ctx.Update.Chat.Id, file, caption, captionFormat, ct);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Отправить файл.
|
||||||
|
/// </summary>
|
||||||
|
public static Task SendFileAsync(this PageContext ctx, FileDescriptor file, string? caption = null, CancellationToken ct = default)
|
||||||
|
=> ctx.Adapter.SendFileAsync(ctx.Update.Chat.Id, file, caption, null, ct);
|
||||||
|
}
|
||||||
38
BotPages.Core/Context/PageContextNavigationExtensions.cs
Normal file
38
BotPages.Core/Context/PageContextNavigationExtensions.cs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
namespace BotPages.Core;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Расширения <see cref="PageContext"/> для работы с <see cref="NavigationService"/>
|
||||||
|
/// </summary>
|
||||||
|
public static class PageContextNavigationExtensions
|
||||||
|
{
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Перейти по маршруту без аргументов.
|
||||||
|
/// </summary>
|
||||||
|
public static Task GoToHomeAsync(this PageContext ctx, CancellationToken ct)
|
||||||
|
=> ctx.Navigation.GoToHomeAsync(ctx, ct);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Перейти по маршруту без аргументов.
|
||||||
|
/// </summary>
|
||||||
|
public static Task GoToAsync(this PageContext ctx, string route, CancellationToken ct)
|
||||||
|
=> ctx.Navigation.GoToAsync(route, ctx, ct);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Перейти по маршруту с аргументами.
|
||||||
|
/// </summary>
|
||||||
|
public static Task GoToAsync<TArgs>(this PageContext ctx, string route, TArgs args, CancellationToken ct)
|
||||||
|
=> ctx.Navigation.GoToAsync<TArgs>(route, args, ctx, ct);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Перейти на страницу без аргументов.
|
||||||
|
/// </summary>
|
||||||
|
public static Task GoToAsync<TPage>(this PageContext ctx, CancellationToken ct) where TPage : Page
|
||||||
|
=> ctx.Navigation.GoToAsync<TPage>(ctx, ct);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Перейти на страницу с аргументами.
|
||||||
|
/// </summary>
|
||||||
|
public static Task GoToAsync<TPage, TArgs>(this PageContext ctx, TArgs args, CancellationToken ct) where TPage : StatefullPage<TArgs>
|
||||||
|
=> ctx.Navigation.GoToAsync<TPage, TArgs>(ctx, args!, ct);
|
||||||
|
}
|
||||||
26
BotPages.Core/Context/PageContextStorageExtensions.cs
Normal file
26
BotPages.Core/Context/PageContextStorageExtensions.cs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
using BotPages.Core.Abstractions;
|
||||||
|
|
||||||
|
namespace BotPages.Core;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Расширения <see cref="PageContext"/> для работы с <see cref="IStateStorage"/>
|
||||||
|
/// </summary>
|
||||||
|
public static class PageContextStorageExtensions
|
||||||
|
{
|
||||||
|
|
||||||
|
/// <summary>Получить состояние по ключу.</summary>
|
||||||
|
public static Task<T?> GetStorageAsync<T>(this PageContext ctx, string key, CancellationToken ct)
|
||||||
|
=> ctx.StateStorage.GetAsync<T>(ctx.SessionKey, key, ct);
|
||||||
|
|
||||||
|
/// <summary>Сохранить состояние по ключу.</summary>
|
||||||
|
public static Task SetStorageAsync<T>(this PageContext ctx, string key, T state, CancellationToken ct)
|
||||||
|
=> ctx.StateStorage.SetAsync<T>(ctx.SessionKey, key, state, ct);
|
||||||
|
|
||||||
|
/// <summary>Удалить состояние по ключу.</summary>
|
||||||
|
public static Task<bool> RemoveStorageAsync(this PageContext ctx, string key, CancellationToken ct)
|
||||||
|
=> ctx.StateStorage.RemoveAsync(ctx.SessionKey, key, ct);
|
||||||
|
|
||||||
|
/// <summary>Удалить все состояния по ключу.</summary>
|
||||||
|
public static Task<bool> ClearStorageAsync(this PageContext ctx, CancellationToken ct)
|
||||||
|
=> ctx.StateStorage.ClearAsync(ctx.SessionKey, ct);
|
||||||
|
}
|
||||||
@@ -17,6 +17,7 @@ public sealed class MessageBuilder
|
|||||||
private string? _progressTitle = null;
|
private string? _progressTitle = null;
|
||||||
private int? _progressPercent = null;
|
private int? _progressPercent = null;
|
||||||
private string? _progressMessageId = null;
|
private string? _progressMessageId = null;
|
||||||
|
private bool _disableReplyKeyboard;
|
||||||
|
|
||||||
/// <summary>Создать билдер сообщений.</summary>
|
/// <summary>Создать билдер сообщений.</summary>
|
||||||
public MessageBuilder(PageContext ctx) => _ctx = ctx;
|
public MessageBuilder(PageContext ctx) => _ctx = ctx;
|
||||||
@@ -64,9 +65,20 @@ public sealed class MessageBuilder
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Отключение Reply клавиатуры.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public MessageBuilder DisableReply()
|
||||||
|
{
|
||||||
|
_disableReplyKeyboard = true;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Добавить reply‑кнопку.</summary>
|
/// <summary>Добавить reply‑кнопку.</summary>
|
||||||
public MessageBuilder Reply(params ReplyButton[] label)
|
public MessageBuilder Reply(params ReplyButton[] label)
|
||||||
{
|
{
|
||||||
|
_disableReplyKeyboard = false;
|
||||||
_reply.Add(label.ToList());
|
_reply.Add(label.ToList());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -74,6 +86,7 @@ public sealed class MessageBuilder
|
|||||||
/// <summary>Добавить строку reply‑кнопок.</summary>
|
/// <summary>Добавить строку reply‑кнопок.</summary>
|
||||||
public MessageBuilder Reply(IEnumerable<ReplyButton> row)
|
public MessageBuilder Reply(IEnumerable<ReplyButton> row)
|
||||||
{
|
{
|
||||||
|
_disableReplyKeyboard = false;
|
||||||
_reply.Add(row.ToList());
|
_reply.Add(row.ToList());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -81,6 +94,7 @@ public sealed class MessageBuilder
|
|||||||
/// <summary>Добавить строку reply‑кнопок.</summary>
|
/// <summary>Добавить строку reply‑кнопок.</summary>
|
||||||
public MessageBuilder Reply(IEnumerable<IEnumerable<ReplyButton>> row)
|
public MessageBuilder Reply(IEnumerable<IEnumerable<ReplyButton>> row)
|
||||||
{
|
{
|
||||||
|
_disableReplyKeyboard = false;
|
||||||
_reply.AddRange(row.Select(t => t.ToList()).ToList());
|
_reply.AddRange(row.Select(t => t.ToList()).ToList());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@@ -113,7 +127,11 @@ public sealed class MessageBuilder
|
|||||||
// Текст
|
// Текст
|
||||||
if (!string.IsNullOrWhiteSpace(_text))
|
if (!string.IsNullOrWhiteSpace(_text))
|
||||||
{
|
{
|
||||||
await _ctx.SendTextAsync(_text, _format, _inline, _reply, ct);
|
List<List<ReplyButton>>? reply = null;
|
||||||
|
if (_disableReplyKeyboard) reply = new();
|
||||||
|
else if (_reply.Any()) reply = _reply;
|
||||||
|
|
||||||
|
await _ctx.SendTextAsync(_text, _format, _inline, reply, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Файлы
|
// Файлы
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public sealed class NavigationService
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Перейти по маршруту без аргументов.
|
/// Перейти по маршруту без аргументов.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Task GoToHome(PageContext ctx, CancellationToken ct)
|
public Task GoToHomeAsync(PageContext ctx, CancellationToken ct)
|
||||||
{
|
{
|
||||||
return NavigateAsync(_defaultPage!, ctx, null, ct);
|
return NavigateAsync(_defaultPage!, ctx, null, ct);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,14 +104,21 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
|||||||
.ToArray()
|
.ToArray()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
else if (reply is not null && reply.Any())
|
else if (reply is not null)
|
||||||
{
|
{
|
||||||
markup = new ReplyKeyboardMarkup(
|
if (reply.Any())
|
||||||
reply.Select(row => row.Select(b => new KeyboardButton(b.Label)).ToArray()).ToArray()
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
ResizeKeyboard = true
|
markup = new ReplyKeyboardMarkup(
|
||||||
};
|
reply.Select(row => row.Select(b => new KeyboardButton(b.Label)).ToArray()).ToArray()
|
||||||
|
)
|
||||||
|
{
|
||||||
|
ResizeKeyboard = true
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
markup = new ReplyKeyboardRemove();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var parseMode = ParseMode.None;
|
var parseMode = ParseMode.None;
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public sealed class SubmitPage : SingletonPage
|
|||||||
}
|
}
|
||||||
while (i < 100);
|
while (i < 100);
|
||||||
|
|
||||||
await ctx.Navigation.GoToHome(ctx, ct);
|
await ctx.Navigation.GoToHomeAsync(ctx, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Task OnLeave(PageContext ctx, CancellationToken ct)
|
public override Task OnLeave(PageContext ctx, CancellationToken ct)
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public sealed class TitlePage : SingletonPage
|
|||||||
{
|
{
|
||||||
if (text == "Меню")
|
if (text == "Меню")
|
||||||
{
|
{
|
||||||
return ctx.Navigation.GoToHome(ctx, ct);
|
return ctx.Navigation.GoToHomeAsync(ctx, ct);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user