This commit is contained in:
98
BotPages.Core/Messaging/ButtonAttribute.cs
Normal file
98
BotPages.Core/Messaging/ButtonAttribute.cs
Normal file
@@ -0,0 +1,98 @@
|
||||
using System.Reflection;
|
||||
|
||||
namespace BotPages.Core.Messaging;
|
||||
|
||||
/// <summary>
|
||||
/// Описание для кнопки.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Field)]
|
||||
public class ButtonAttribute : Attribute
|
||||
{
|
||||
///<inheritdoc/>
|
||||
public ButtonAttribute(string label)
|
||||
{
|
||||
Label = label;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Описание кнопки.
|
||||
/// </summary>
|
||||
public string Label { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Значение кнопки. Используется в InlineButton.
|
||||
/// </summary>
|
||||
public string? Value { get; }
|
||||
}
|
||||
|
||||
public static class ButtonExtensions
|
||||
{
|
||||
private static readonly Dictionary<Type, Dictionary<string, object>> _cacheName = new();
|
||||
|
||||
/// <summary>
|
||||
/// Получить подпись кнопки.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Enum тип.</typeparam>
|
||||
/// <param name="value">Значение enum.</param>
|
||||
/// <returns></returns>
|
||||
public static string GetButtonLabel<T>(this T value)
|
||||
where T : Enum
|
||||
{
|
||||
var fieldName = value.ToString();
|
||||
var type = value.GetType();
|
||||
var field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.Static);
|
||||
return field?.GetCustomAttribute<ButtonAttribute>()?.Label ?? fieldName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получить значение кнопки.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Enum тип.</typeparam>
|
||||
/// <param name="value">Значение enum.</param>
|
||||
/// <returns></returns>
|
||||
public static string GetButtonValue<T>(this T value)
|
||||
where T : Enum
|
||||
{
|
||||
var fieldName = value.ToString();
|
||||
var type = value.GetType();
|
||||
var field = type.GetField(fieldName, BindingFlags.Public | BindingFlags.Static);
|
||||
return field?.GetCustomAttribute<ButtonAttribute>()?.Value ?? fieldName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получить значение enum из подписи кнопки.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static T? FromButtonLabel<T>(string? value) where T : struct, Enum
|
||||
{
|
||||
if (value == null) return null;
|
||||
|
||||
var type = typeof(T);
|
||||
if (!_cacheName.TryGetValue(type, out var map))
|
||||
{
|
||||
map = new Dictionary<string, object>();
|
||||
|
||||
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static);
|
||||
|
||||
foreach (var field in fields)
|
||||
{
|
||||
var fieldValue = field.GetValue(null)!;
|
||||
var fieldName = field.Name;
|
||||
|
||||
var attr = field.GetCustomAttribute<ButtonAttribute>();
|
||||
|
||||
if (attr != null)
|
||||
{
|
||||
fieldName = attr.Label;
|
||||
}
|
||||
|
||||
map[fieldName] = fieldValue;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return map.TryGetValue(value, out var result) ? (T)result : null;
|
||||
}
|
||||
}
|
||||
37
BotPages.Core/Messaging/InlineButton.cs
Normal file
37
BotPages.Core/Messaging/InlineButton.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
namespace BotPages.Core.Messaging;
|
||||
|
||||
/// <summary>
|
||||
/// Кнопки под сообщением
|
||||
/// </summary>
|
||||
public class InlineButton
|
||||
{
|
||||
/// <summary>
|
||||
/// Подпись на кнопке
|
||||
/// </summary>
|
||||
public string Label { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Значение кнопки
|
||||
/// </summary>
|
||||
public string Value { get; private set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public InlineButton(string label, string value)
|
||||
{
|
||||
this.Label = label;
|
||||
this.Value = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public InlineButton(Enum value)
|
||||
{
|
||||
this.Label = value.GetButtonLabel();
|
||||
this.Value = value.GetButtonValue();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Преобразование enum к кнопке.
|
||||
/// </summary>
|
||||
/// <param name="en"></param>
|
||||
public static implicit operator InlineButton(Enum en) => new InlineButton(en);
|
||||
}
|
||||
153
BotPages.Core/Messaging/MessageBuilder.cs
Normal file
153
BotPages.Core/Messaging/MessageBuilder.cs
Normal file
@@ -0,0 +1,153 @@
|
||||
using BotPages.Core.Abstractions;
|
||||
|
||||
namespace BotPages.Core.Messaging;
|
||||
|
||||
/// <summary>
|
||||
/// Fluent‑билдер для отправки сообщений (текст, кнопки, файлы, альбомы, прогресс).
|
||||
/// </summary>
|
||||
public sealed class MessageBuilder
|
||||
{
|
||||
private readonly PageContext _ctx;
|
||||
private string? _text = null;
|
||||
private MessageFormat _format = MessageFormat.Plain;
|
||||
private readonly List<List<InlineButton>> _inline = new();
|
||||
private readonly List<List<ReplyButton>> _reply = new();
|
||||
private readonly List<(FileDescriptor file, string? caption)> _files = new();
|
||||
private readonly List<(FileDescriptor file, string? caption)> _album = new();
|
||||
private string? _progressTitle = null;
|
||||
private int? _progressPercent = null;
|
||||
private string? _progressMessageId = null;
|
||||
|
||||
/// <summary>Создать билдер сообщений.</summary>
|
||||
public MessageBuilder(PageContext ctx) => _ctx = ctx;
|
||||
|
||||
/// <summary>Текст сообщения.</summary>
|
||||
public MessageBuilder Text(string text, MessageFormat format = MessageFormat.Plain)
|
||||
{
|
||||
_text = text;
|
||||
_format = format;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Добавить inline‑кнопку.</summary>
|
||||
public MessageBuilder Inline(string label, string value)
|
||||
{
|
||||
_inline.Add(new() { new(label, value) });
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Добавить inline‑кнопку.</summary>
|
||||
public MessageBuilder Inline(InlineButton button)
|
||||
{
|
||||
_inline.Add(new() { button });
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Добавить inline‑кнопку.</summary>
|
||||
public MessageBuilder Inline(params InlineButton[] buttons)
|
||||
{
|
||||
_inline.Add(buttons.ToList());
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Добавить строку inline‑кнопок.</summary>
|
||||
public MessageBuilder Inline(IEnumerable<InlineButton> row)
|
||||
{
|
||||
_inline.Add(row.ToList());
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Добавить строку inline‑кнопок.</summary>
|
||||
public MessageBuilder Inline(IEnumerable<IEnumerable<InlineButton>> row)
|
||||
{
|
||||
_inline.AddRange(row.Select(t => t.ToList()).ToList());
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Добавить reply‑кнопку.</summary>
|
||||
public MessageBuilder Reply(params ReplyButton[] label)
|
||||
{
|
||||
_reply.Add(label.ToList());
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Добавить строку reply‑кнопок.</summary>
|
||||
public MessageBuilder Reply(IEnumerable<ReplyButton> row)
|
||||
{
|
||||
_reply.Add(row.ToList());
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Добавить строку reply‑кнопок.</summary>
|
||||
public MessageBuilder Reply(IEnumerable<IEnumerable<ReplyButton>> row)
|
||||
{
|
||||
_reply.AddRange(row.Select(t => t.ToList()).ToList());
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Добавить файл для отправки.</summary>
|
||||
public MessageBuilder File(FileDescriptor file, string? caption = null)
|
||||
{
|
||||
_files.Add((file, caption));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Добавить файл в альбом.</summary>
|
||||
public MessageBuilder Album(FileDescriptor file, string? caption = null)
|
||||
{
|
||||
_album.Add((file, caption));
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Установить прогресс операции.</summary>
|
||||
public MessageBuilder Progress(string title, int percent = 0)
|
||||
{
|
||||
_progressTitle = title;
|
||||
_progressPercent = percent;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// <summary>Отправить собранное сообщение.</summary>
|
||||
public async Task SendAsync(CancellationToken ct = default)
|
||||
{
|
||||
// Текст
|
||||
if (!string.IsNullOrWhiteSpace(_text))
|
||||
{
|
||||
await _ctx.SendTextAsync(_text, _format, _inline, _reply, ct);
|
||||
}
|
||||
|
||||
// Файлы
|
||||
foreach (var (file, caption) in _files)
|
||||
await _ctx.SendFileAsync(file, caption, ct);
|
||||
|
||||
// Альбом
|
||||
if (_album.Count > 0)
|
||||
{
|
||||
var builder = _ctx.Albums;
|
||||
foreach (var (file, caption) in _album)
|
||||
builder.Add(file, caption);
|
||||
await builder.SendAsync(ct);
|
||||
}
|
||||
|
||||
// Прогресс
|
||||
if (_progressTitle is not null)
|
||||
{
|
||||
if (_progressMessageId is null)
|
||||
_progressMessageId = await _ctx.StartProgressAsync(_progressTitle, ct);
|
||||
|
||||
if (_progressPercent > 0 && !string.IsNullOrEmpty(_progressMessageId))
|
||||
await _ctx.UpdateProgressAsync(_progressMessageId, _progressPercent.Value, ct);
|
||||
}
|
||||
|
||||
_text = null;
|
||||
_files.Clear();
|
||||
_album.Clear();
|
||||
|
||||
if (_progressPercent >= 100)
|
||||
{
|
||||
_progressTitle = null;
|
||||
_progressMessageId = null;
|
||||
_progressPercent = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
36
BotPages.Core/Messaging/ReplyButton.cs
Normal file
36
BotPages.Core/Messaging/ReplyButton.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
namespace BotPages.Core.Messaging;
|
||||
|
||||
/// <summary>
|
||||
/// Кнопки снизу чата
|
||||
/// </summary>
|
||||
public class ReplyButton
|
||||
{
|
||||
/// <summary>
|
||||
/// Подпись на кнопке
|
||||
/// </summary>
|
||||
public string Label { get; private set; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ReplyButton(string label)
|
||||
{
|
||||
this.Label = label;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ReplyButton(Enum value)
|
||||
{
|
||||
this.Label = value.GetButtonLabel();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Преобразование строки к кнопке.
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
public static implicit operator ReplyButton(string str) => new ReplyButton(str);
|
||||
|
||||
/// <summary>
|
||||
/// Преобразование enum к кнопке.
|
||||
/// </summary>
|
||||
/// <param name="en"></param>
|
||||
public static implicit operator ReplyButton(Enum en) => new ReplyButton(en);
|
||||
}
|
||||
Reference in New Issue
Block a user