c# - 在 C# 中创建动态键盘 Telegram Bot ,MrRoundRobin API

标签 c# keyboard telegram telegram-bot

我想在 telegram.bot 中创建自定义键盘

例如:
我们有一个从数据库或其他递归中获取的字符串数组 我们如何将数组中的数据推送到 for 循环或函数中的 InlineKeyboardMarkup

//array  of Button
string[] ButtonItem= new string[] { "one", "two", "three", "Four" };

//function or solution to create keyboard like this 
var keyboard = new InlineKeyboardMarkup(new[]
    {
        new[] 
        {
            new InlineKeyboardButton("one"),
            new InlineKeyboardButton("two"),
        },
        new[] 
        {
            new InlineKeyboardButton("three"),
            new InlineKeyboardButton("Four"),
        }
    });

最佳答案

您可以使用单独的函数来获取 InlineKeyboardButton 数组

private static InlineKeyboardButton[][] GetInlineKeyboard(string [] stringArray)
{
    var keyboardInline = new InlineKeyboardButton[1][];
    var keyboardButtons = new InlineKeyboardButton[stringArray.Length];
    for (var i = 0; i < stringArray.Length; i++)
    {
        keyboardButtons[i] = new InlineKeyboardButton
        {
            Text = stringArray[i],
            CallbackData = "Some Callback Data",
        };
    }
    keyboardInline[0] = keyboardButtons;
    return keyboardInline;
}

然后调用函数:

var buttonItem = new[] { "one", "two", "three", "Four" };
var keyboardMarkup = new InlineKeyboardMarkup(GetInlineKeyboard(buttonItem));

关于c# - 在 C# 中创建动态键盘 Telegram Bot ,MrRoundRobin API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39884961/

相关文章:

android - 状态栏敏感信息,如 Telegram X

c# - 如何在运行时暂时禁用捆绑和缩小?

macos - 在哪里可以找到 Mac 虚拟按键代码列表?

c# - 将 SelectedIndexChanged 添加到生成的复选框列表

ios - 正确检测键盘的出现和消失

ios - Swift:如何更改键盘背景和字母的颜色?

c# - 如何在 C# 中处理 Telegram bot 上的多个用户?

bots - 我的 Telegram Bot 不断地发送消息

c# - WPF : How to not block the GUI thread in HwndHost. BuildWindowCore 中的死锁?

c# - 遍历时计算嵌套列表对象的数量