| | | 1 | | using TeleFlow.Telegram.Internal; |
| | | 2 | | using TeleFlow.Telegram.Schema.Types; |
| | | 3 | | |
| | | 4 | | namespace TeleFlow.Telegram; |
| | | 5 | | |
| | | 6 | | public sealed class ReplyKeyboard |
| | | 7 | | { |
| | 5 | 8 | | private readonly List<List<KeyboardButton>> _rows = [[]]; |
| | | 9 | | private bool? _isPersistent; |
| | | 10 | | private bool? _resizeKeyboard; |
| | | 11 | | private bool? _oneTimeKeyboard; |
| | | 12 | | private string? _inputFieldPlaceholder; |
| | | 13 | | private bool? _selective; |
| | | 14 | | |
| | | 15 | | private ReplyKeyboard() |
| | | 16 | | { |
| | 5 | 17 | | } |
| | | 18 | | |
| | | 19 | | public static ReplyKeyboard Create() |
| | | 20 | | { |
| | 5 | 21 | | return new ReplyKeyboard(); |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | public ReplyKeyboard Button(string text) |
| | | 25 | | { |
| | 6 | 26 | | ArgumentException.ThrowIfNullOrWhiteSpace(text); |
| | | 27 | | |
| | 6 | 28 | | CurrentRow.Add(new KeyboardButton { Text = text }); |
| | 6 | 29 | | return this; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | public ReplyKeyboard Button(KeyboardButton button) |
| | | 33 | | { |
| | 1 | 34 | | ArgumentNullException.ThrowIfNull(button); |
| | | 35 | | |
| | 1 | 36 | | if (string.IsNullOrWhiteSpace(button.Text)) |
| | | 37 | | { |
| | 0 | 38 | | throw new ArgumentException("Reply keyboard button text must not be empty.", nameof(button)); |
| | | 39 | | } |
| | | 40 | | |
| | 1 | 41 | | CurrentRow.Add(button); |
| | 1 | 42 | | return this; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | public ReplyKeyboard Row() |
| | | 46 | | { |
| | 3 | 47 | | if (CurrentRow.Count > 0) |
| | | 48 | | { |
| | 2 | 49 | | _rows.Add([]); |
| | | 50 | | } |
| | | 51 | | |
| | 3 | 52 | | return this; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public ReplyKeyboard Persistent(bool value = true) |
| | | 56 | | { |
| | 1 | 57 | | _isPersistent = value; |
| | 1 | 58 | | return this; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | public ReplyKeyboard Resize(bool value = true) |
| | | 62 | | { |
| | 2 | 63 | | _resizeKeyboard = value; |
| | 2 | 64 | | return this; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | public ReplyKeyboard OneTime(bool value = true) |
| | | 68 | | { |
| | 1 | 69 | | _oneTimeKeyboard = value; |
| | 1 | 70 | | return this; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | public ReplyKeyboard Placeholder(string placeholder) |
| | | 74 | | { |
| | 2 | 75 | | TelegramKeyboardPlaceholderValidator.ValidateReplyKeyboardInputFieldPlaceholder(placeholder); |
| | | 76 | | |
| | 1 | 77 | | _inputFieldPlaceholder = placeholder; |
| | 1 | 78 | | return this; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | public ReplyKeyboard Selective(bool value = true) |
| | | 82 | | { |
| | 1 | 83 | | _selective = value; |
| | 1 | 84 | | return this; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | public ReplyKeyboardMarkup ToMarkup() |
| | | 88 | | { |
| | 4 | 89 | | var rows = _rows |
| | 6 | 90 | | .Where(static row => row.Count > 0) |
| | 4 | 91 | | .Select(static row => row.ToArray()) |
| | 4 | 92 | | .ToArray(); |
| | | 93 | | |
| | 4 | 94 | | if (rows.Length == 0) |
| | | 95 | | { |
| | 1 | 96 | | throw new InvalidOperationException("Reply keyboard must contain at least one button."); |
| | | 97 | | } |
| | | 98 | | |
| | 3 | 99 | | return new ReplyKeyboardMarkup |
| | 3 | 100 | | { |
| | 3 | 101 | | Keyboard = rows, |
| | 3 | 102 | | IsPersistent = _isPersistent, |
| | 3 | 103 | | ResizeKeyboard = _resizeKeyboard, |
| | 3 | 104 | | OneTimeKeyboard = _oneTimeKeyboard, |
| | 3 | 105 | | InputFieldPlaceholder = _inputFieldPlaceholder, |
| | 3 | 106 | | Selective = _selective |
| | 3 | 107 | | }; |
| | | 108 | | } |
| | | 109 | | |
| | 10 | 110 | | private List<KeyboardButton> CurrentRow => _rows[^1]; |
| | | 111 | | } |