Path: redb-route/redb.Route.Telegram/Fluent/TelegramKeyboard.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Telegram/Fluent/TelegramKeyboard.cs
----
using Telegram.Bot.Types;
using Telegram.Bot.Types.ReplyMarkups;
namespace redb.Route.Telegram.Fluent;
///
/// Typed builders for Telegram reply markup (keyboards).
///
/// The result is a Telegram.Bot markup object that is placed on the exchange
/// under — the same header the producer reads.
/// Use these factories to construct keyboards without referencing
/// Telegram.Bot.Types.ReplyMarkups directly in route code.
///
///
/// // Static keyboard via the route-extension sugar:
/// From(Tg.Receive(token))
/// .SetBody("Choose:")
/// .WithInlineKeyboard(k => k
/// .Row(r => r.Callback("✅ Yes", "act:yes").Callback("❌ No", "act:no")))
/// .To(Tg.Send(token).ChatId(Header(TelegramHeaders.ChatId)));
///
/// // Dynamic keyboard inside a processor:
/// exchange.In.Headers[TelegramHeaders.ReplyMarkup] = TgKeyboard.Inline(k =>
/// {
/// foreach (var o in orders) k.Row(r => r.Callback(o.Title, $"order:{o.Id}"));
/// });
///
///
public static class TgKeyboard
{
/// Builds an inline keyboard (callback / URL / web-app buttons attached to the message).
/// Callback that adds rows of buttons.
public static InlineKeyboardMarkup Inline(Action build)
{
ArgumentNullException.ThrowIfNull(build);
var builder = new InlineKeyboardBuilder();
build(builder);
return builder.Build();
}
/// Builds a reply keyboard (buttons shown below the text input).
/// Callback that adds rows of buttons and configures options.
public static ReplyKeyboardMarkup Reply(Action build)
{
ArgumentNullException.ThrowIfNull(build);
var builder = new ReplyKeyboardBuilder();
build(builder);
return builder.Build();
}
/// Builds a markup that removes the current custom reply keyboard.
/// Remove the keyboard only for specific users (see Bot API docs).
public static ReplyKeyboardRemove Remove(bool selective = false)
{
var markup = new ReplyKeyboardRemove();
if (selective) markup.Selective = true;
return markup;
}
}
/// Fluent builder for an .
public sealed class InlineKeyboardBuilder
{
private readonly List> _rows = new();
/// Adds a row of buttons. Empty rows are ignored.
/// Callback that adds buttons to the row.
public InlineKeyboardBuilder Row(Action build)
{
ArgumentNullException.ThrowIfNull(build);
var row = new InlineRowBuilder();
build(row);
if (row.Buttons.Count > 0)
_rows.Add(row.Buttons);
return this;
}
internal InlineKeyboardMarkup Build() => new(_rows);
}
/// Fluent builder for a single row of inline keyboard buttons.
public sealed class InlineRowBuilder
{
private readonly List _buttons = new();
internal List Buttons => _buttons;
/// Adds a callback button. The tap arrives as a CallbackQuery update carrying .
public InlineRowBuilder Callback(string text, string data)
{
_buttons.Add(InlineKeyboardButton.WithCallbackData(text, data));
return this;
}
/// Adds a button that opens a URL.
public InlineRowBuilder Url(string text, string url)
{
_buttons.Add(InlineKeyboardButton.WithUrl(text, url));
return this;
}
/// Adds a button that launches a Web App at .
public InlineRowBuilder WebApp(string text, string url)
{
_buttons.Add(InlineKeyboardButton.WithWebApp(text, new WebAppInfo { Url = url }));
return this;
}
/// Adds a button that prompts the user to pick a chat and switches to inline mode with .
public InlineRowBuilder SwitchInlineQuery(string text, string query = "")
{
_buttons.Add(InlineKeyboardButton.WithSwitchInlineQuery(text, query));
return this;
}
/// Adds a button that switches to inline mode in the current chat with .
public InlineRowBuilder SwitchInlineQueryCurrentChat(string text, string query = "")
{
_buttons.Add(InlineKeyboardButton.WithSwitchInlineQueryCurrentChat(text, query));
return this;
}
}
/// Fluent builder for a .
public sealed class ReplyKeyboardBuilder
{
private readonly List> _rows = new();
private bool _resize;
private bool _oneTime;
private bool _persistent;
private bool _selective;
private string? _placeholder;
/// Adds a row of buttons. Empty rows are ignored.
/// Callback that adds buttons to the row.
public ReplyKeyboardBuilder Row(Action build)
{
ArgumentNullException.ThrowIfNull(build);
var row = new ReplyRowBuilder();
build(row);
if (row.Buttons.Count > 0)
_rows.Add(row.Buttons);
return this;
}
/// Resize the keyboard vertically to fit its buttons.
public ReplyKeyboardBuilder Resize(bool value = true) { _resize = value; return this; }
/// Hide the keyboard after the user taps a button.
public ReplyKeyboardBuilder OneTime(bool value = true) { _oneTime = value; return this; }
/// Keep the keyboard open until explicitly removed.
public ReplyKeyboardBuilder Persistent(bool value = true) { _persistent = value; return this; }
/// Show the keyboard only to specific users (see Bot API docs).
public ReplyKeyboardBuilder Selective(bool value = true) { _selective = value; return this; }
/// Placeholder text shown in the input field while the keyboard is active.
public ReplyKeyboardBuilder Placeholder(string text) { _placeholder = text; return this; }
internal ReplyKeyboardMarkup Build()
{
var markup = new ReplyKeyboardMarkup(_rows);
if (_resize) markup.ResizeKeyboard = true;
if (_oneTime) markup.OneTimeKeyboard = true;
if (_persistent) markup.IsPersistent = true;
if (_selective) markup.Selective = true;
if (_placeholder is not null) markup.InputFieldPlaceholder = _placeholder;
return markup;
}
}
/// Fluent builder for a single row of reply keyboard buttons.
public sealed class ReplyRowBuilder
{
private readonly List _buttons = new();
internal List Buttons => _buttons;
/// Adds a plain text button; tapping it sends the text as a message.
public ReplyRowBuilder Button(string text)
{
_buttons.Add(new KeyboardButton(text));
return this;
}
/// Adds a button that requests the user's phone number.
public ReplyRowBuilder ContactButton(string text)
{
_buttons.Add(KeyboardButton.WithRequestContact(text));
return this;
}
/// Adds a button that requests the user's location.
public ReplyRowBuilder LocationButton(string text)
{
_buttons.Add(KeyboardButton.WithRequestLocation(text));
return this;
}
/// Adds a button that launches a Web App at .
public ReplyRowBuilder WebAppButton(string text, string url)
{
_buttons.Add(KeyboardButton.WithWebApp(text, new WebAppInfo { Url = url }));
return this;
}
}