| | | 1 | | using TeleFlow.Telegram.Internal; |
| | | 2 | | using TeleFlow.Telegram.Schema.Types; |
| | | 3 | | |
| | | 4 | | namespace TeleFlow.Telegram; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Builds Telegram inline keyboard markup for common button scenarios while keeping native |
| | | 8 | | /// <see cref="InlineKeyboardMarkup"/> available for full Bot API control. |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class InlineKeyboardBuilder |
| | | 11 | | { |
| | 14 | 12 | | private readonly List<List<InlineKeyboardButtonIntent>> _rows = [[]]; |
| | | 13 | | |
| | | 14 | | private InlineKeyboardBuilder() |
| | | 15 | | { |
| | 14 | 16 | | } |
| | | 17 | | |
| | | 18 | | public static InlineKeyboardBuilder Create() |
| | | 19 | | { |
| | 14 | 20 | | return new InlineKeyboardBuilder(); |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | public InlineKeyboardBuilder Button<TPayload>( |
| | | 24 | | string text, |
| | | 25 | | TPayload payload, |
| | | 26 | | InlineKeyboardButtonOptions? options = null) |
| | | 27 | | { |
| | 10 | 28 | | ArgumentException.ThrowIfNullOrWhiteSpace(text); |
| | 10 | 29 | | ArgumentNullException.ThrowIfNull(payload); |
| | | 30 | | |
| | 10 | 31 | | ValidateOptions(options); |
| | 10 | 32 | | CurrentRow.Add(InlineKeyboardButtonIntent.TypedPayload(text, payload, options)); |
| | 10 | 33 | | return this; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | public InlineKeyboardBuilder Button( |
| | | 37 | | string text, |
| | | 38 | | string callbackData, |
| | | 39 | | InlineKeyboardButtonOptions? options = null) |
| | | 40 | | { |
| | 4 | 41 | | ArgumentException.ThrowIfNullOrWhiteSpace(text); |
| | 4 | 42 | | ArgumentException.ThrowIfNullOrWhiteSpace(callbackData); |
| | 4 | 43 | | ValidateCallbackData(callbackData); |
| | | 44 | | |
| | 3 | 45 | | ValidateOptions(options); |
| | 3 | 46 | | CurrentRow.Add(InlineKeyboardButtonIntent.RawCallbackData(text, callbackData, options)); |
| | 3 | 47 | | return this; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public InlineKeyboardBuilder Url( |
| | | 51 | | string text, |
| | | 52 | | string url, |
| | | 53 | | InlineKeyboardButtonOptions? options = null) |
| | | 54 | | { |
| | 2 | 55 | | ArgumentException.ThrowIfNullOrWhiteSpace(text); |
| | 2 | 56 | | ArgumentException.ThrowIfNullOrWhiteSpace(url); |
| | | 57 | | |
| | 2 | 58 | | if (!Uri.TryCreate(url, UriKind.Absolute, out _)) |
| | | 59 | | { |
| | 0 | 60 | | throw new ArgumentException("Inline keyboard URL button must use an absolute URL.", nameof(url)); |
| | | 61 | | } |
| | | 62 | | |
| | 2 | 63 | | ValidateOptions(options); |
| | 2 | 64 | | return UrlCore(text, url, options); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | public InlineKeyboardBuilder Url( |
| | | 68 | | string text, |
| | | 69 | | Uri url, |
| | | 70 | | InlineKeyboardButtonOptions? options = null) |
| | | 71 | | { |
| | 0 | 72 | | ArgumentException.ThrowIfNullOrWhiteSpace(text); |
| | 0 | 73 | | ArgumentNullException.ThrowIfNull(url); |
| | | 74 | | |
| | 0 | 75 | | if (!url.IsAbsoluteUri) |
| | | 76 | | { |
| | 0 | 77 | | throw new ArgumentException("Inline keyboard URL button must use an absolute URL.", nameof(url)); |
| | | 78 | | } |
| | | 79 | | |
| | 0 | 80 | | ValidateOptions(options); |
| | 0 | 81 | | return UrlCore(text, url.OriginalString, options); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public InlineKeyboardBuilder Row() |
| | | 85 | | { |
| | 1 | 86 | | if (CurrentRow.Count > 0) |
| | | 87 | | { |
| | 1 | 88 | | _rows.Add([]); |
| | | 89 | | } |
| | | 90 | | |
| | 1 | 91 | | return this; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | public InlineKeyboardMarkup Build() |
| | | 95 | | { |
| | 13 | 96 | | return BuildCore(); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private InlineKeyboardMarkup BuildCore() |
| | | 100 | | { |
| | 13 | 101 | | var rows = _rows |
| | 14 | 102 | | .Where(static row => row.Count > 0) |
| | 13 | 103 | | .Select(row => row |
| | 15 | 104 | | .Select(static intent => intent.ToButton()) |
| | 13 | 105 | | .ToArray()) |
| | 13 | 106 | | .ToArray(); |
| | | 107 | | |
| | 11 | 108 | | if (rows.Length == 0) |
| | | 109 | | { |
| | 1 | 110 | | throw new InvalidOperationException("Inline keyboard must contain at least one button."); |
| | | 111 | | } |
| | | 112 | | |
| | 10 | 113 | | return new InlineKeyboardMarkup |
| | 10 | 114 | | { |
| | 10 | 115 | | InlineKeyboard = rows |
| | 10 | 116 | | }; |
| | | 117 | | } |
| | | 118 | | |
| | 16 | 119 | | private List<InlineKeyboardButtonIntent> CurrentRow => _rows[^1]; |
| | | 120 | | |
| | | 121 | | private InlineKeyboardBuilder UrlCore( |
| | | 122 | | string text, |
| | | 123 | | string url, |
| | | 124 | | InlineKeyboardButtonOptions? options) |
| | | 125 | | { |
| | 2 | 126 | | CurrentRow.Add(InlineKeyboardButtonIntent.Link(text, url, options)); |
| | 2 | 127 | | return this; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | private static void ValidateCallbackData(string callbackData) |
| | | 131 | | { |
| | 4 | 132 | | CallbackDataCodec.ValidateCallbackData(callbackData, "Telegram callback data"); |
| | 3 | 133 | | } |
| | | 134 | | |
| | | 135 | | private static void ValidateOptions(InlineKeyboardButtonOptions? options) |
| | | 136 | | { |
| | 15 | 137 | | if (options is null) |
| | | 138 | | { |
| | 12 | 139 | | return; |
| | | 140 | | } |
| | | 141 | | |
| | 3 | 142 | | if (options.Style is { Length: 0 }) |
| | | 143 | | { |
| | 0 | 144 | | throw new ArgumentException("Inline keyboard button style cannot be empty.", nameof(options)); |
| | | 145 | | } |
| | | 146 | | |
| | 3 | 147 | | if (options.IconCustomEmojiId is { Length: 0 }) |
| | | 148 | | { |
| | 0 | 149 | | throw new ArgumentException("Inline keyboard button custom emoji icon id cannot be empty.", nameof(options)) |
| | | 150 | | } |
| | 3 | 151 | | } |
| | | 152 | | |
| | | 153 | | private sealed record InlineKeyboardButtonIntent( |
| | | 154 | | string Text, |
| | | 155 | | object? Payload, |
| | | 156 | | Type? PayloadType, |
| | | 157 | | string? CallbackData, |
| | | 158 | | string? Url, |
| | | 159 | | InlineKeyboardButtonOptions? Options) |
| | | 160 | | { |
| | | 161 | | public static InlineKeyboardButtonIntent TypedPayload<TPayload>( |
| | | 162 | | string text, |
| | | 163 | | TPayload payload, |
| | | 164 | | InlineKeyboardButtonOptions? options) |
| | | 165 | | { |
| | 10 | 166 | | ArgumentNullException.ThrowIfNull(payload); |
| | | 167 | | |
| | 10 | 168 | | return new InlineKeyboardButtonIntent( |
| | 10 | 169 | | text, |
| | 10 | 170 | | payload, |
| | 10 | 171 | | payload.GetType(), |
| | 10 | 172 | | CallbackData: null, |
| | 10 | 173 | | Url: null, |
| | 10 | 174 | | options); |
| | | 175 | | } |
| | | 176 | | |
| | | 177 | | public static InlineKeyboardButtonIntent RawCallbackData( |
| | | 178 | | string text, |
| | | 179 | | string callbackData, |
| | | 180 | | InlineKeyboardButtonOptions? options) |
| | | 181 | | { |
| | 3 | 182 | | return new InlineKeyboardButtonIntent( |
| | 3 | 183 | | text, |
| | 3 | 184 | | Payload: null, |
| | 3 | 185 | | PayloadType: null, |
| | 3 | 186 | | callbackData, |
| | 3 | 187 | | Url: null, |
| | 3 | 188 | | options); |
| | | 189 | | } |
| | | 190 | | |
| | | 191 | | public static InlineKeyboardButtonIntent Link( |
| | | 192 | | string text, |
| | | 193 | | string url, |
| | | 194 | | InlineKeyboardButtonOptions? options) |
| | | 195 | | { |
| | 2 | 196 | | return new InlineKeyboardButtonIntent( |
| | 2 | 197 | | text, |
| | 2 | 198 | | Payload: null, |
| | 2 | 199 | | PayloadType: null, |
| | 2 | 200 | | CallbackData: null, |
| | 2 | 201 | | url, |
| | 2 | 202 | | options); |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | public InlineKeyboardButton ToButton() |
| | | 206 | | { |
| | 15 | 207 | | if (Payload is not null) |
| | | 208 | | { |
| | 10 | 209 | | if (PayloadType is null) |
| | | 210 | | { |
| | 0 | 211 | | throw new InvalidOperationException("Inline keyboard typed callback payload type is missing."); |
| | | 212 | | } |
| | | 213 | | |
| | 10 | 214 | | var serializedPayload = CallbackDataCodec.PackRequired(Payload, PayloadType); |
| | | 215 | | |
| | 8 | 216 | | return ApplyOptions(new InlineKeyboardButton |
| | 8 | 217 | | { |
| | 8 | 218 | | Text = Text, |
| | 8 | 219 | | CallbackData = serializedPayload |
| | 8 | 220 | | }); |
| | | 221 | | } |
| | | 222 | | |
| | 5 | 223 | | if (CallbackData is not null) |
| | | 224 | | { |
| | 3 | 225 | | return ApplyOptions(new InlineKeyboardButton |
| | 3 | 226 | | { |
| | 3 | 227 | | Text = Text, |
| | 3 | 228 | | CallbackData = CallbackData |
| | 3 | 229 | | }); |
| | | 230 | | } |
| | | 231 | | |
| | 2 | 232 | | if (Url is not null) |
| | | 233 | | { |
| | 2 | 234 | | return ApplyOptions(new InlineKeyboardButton |
| | 2 | 235 | | { |
| | 2 | 236 | | Text = Text, |
| | 2 | 237 | | Url = Url |
| | 2 | 238 | | }); |
| | | 239 | | } |
| | | 240 | | |
| | 0 | 241 | | throw new InvalidOperationException("Inline keyboard button intent does not contain an action."); |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | private InlineKeyboardButton ApplyOptions(InlineKeyboardButton button) |
| | | 245 | | { |
| | 13 | 246 | | return button with |
| | 13 | 247 | | { |
| | 13 | 248 | | IconCustomEmojiId = Options?.IconCustomEmojiId, |
| | 13 | 249 | | Style = Options?.Style |
| | 13 | 250 | | }; |
| | | 251 | | } |
| | | 252 | | } |
| | | 253 | | } |