简体   繁体   English

如何在 telegram-api 的列中放置一个按钮(内联键盘)?

[英]How to place a button(inline keyboard) in a column on the telegram-api?

I try to create custom keyboard for telegram bot.我尝试为电报机器人创建自定义键盘。 Use the solution by AmatuerDev : create dynamic Keyboard telegram bot in c#, MrRoundRobin API and it looks like this:使用AmatuerDev的解决方案: 在 c#、MrRoundRobin API 中创建动态键盘电报机器人,它看起来像这样:

button in row行中的按钮

How to place a button in a column?如何在列中放置按钮?

Thanks You!感谢您!

PS Source code:聚苯乙烯源代码:

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;
}

Notes:笔记:

  1. Change return type for GetInlineKeyboard to InlineKeyboardMarkup instead of InlineKeyboardButton[][]更改返回类型GetInlineKeyboardInlineKeyboardMarkup而不是InlineKeyboardButton[][]
  2. Use explicit types instead of implicit ( var )使用显式类型而不是隐式 ( var )
  3. Use List<InlineKeyboardButton> instead of Array ( InlineKeyboardButton[] )使用List<InlineKeyboardButton>而不是 Array ( InlineKeyboardButton[] )
  4. Don't create buttons with same callback data不要创建具有相同回调数据的按钮
  5. Replace method parameters to take IDictionary<string, string> to get text and callback data替换方法参数以取IDictionary<string, string>获取文本和回调数据

Every IEnumerable<InlineKeyboardButton> Is single row, This means you should pass list of rows.每个IEnumerable<InlineKeyboardButton>都是单行,这意味着您应该传递行列表。

You shouldn't use var keyboardInline = new InlineKeyboardButton[1][];你不应该使用var keyboardInline = new InlineKeyboardButton[1][]; because this means only one row with buttons in this row!因为这意味着该行中只有一行带有按钮!

There are two ways below:下面有两种方式:


Code you wants:你想要的代码:

I don't suggest this code我不建议使用此代码

private static InlineKeyboardButton[][] GetInlineKeyboard(string[] stringArray)
{
    var keyboardInline = new InlineKeyboardButton[stringArray.Length][];

    for (var i = 0; i < stringArray.Length; i++)
    {
        keyboardInline[i] = new InlineKeyboardButton[]
        {
            new InlineKeyboardButton
            {
                Text = stringArray[i],
                CallbackData = "Some Callback Data",
            };
        }
    }

    return keyboardInline;
}

Best way:最好的办法:

I suggest this code:我建议使用以下代码:

private static InlineKeyboardMarkup GetInlineKeyboard(IDictionary<string, string> buttonsData)
{
    // Rows Count
    int count = buttonsData.Length;

    // List of rows 
    // Every 'List<InlineKeyboardButton>' is row
    List<List<InlineKeyboardButton>> buttons = new List<List<InlineKeyboardButton>>(count);    

    for (int i = 0; i < count; i++)
    {
        // Add new row with one button capacity
        buttons.Add(new List<InlineKeyboardButton>(1)
        {
            // Add button to this row
            new InlineKeyboardButton
            {
                Text = buttonsData.Values.ElementAt(i),
                CallbackData = buttonsData.Keys.ElementAt(i),
            }
        });
    }
    
    return new InlineKeyboardMarkup(buttons);
}

I'm not sure if the code has massively changed as I haven't tried the above method(answered by Muaath), but this was the fix for me, inspiration from Muaath answer above:我不确定代码是否发生了巨大变化,因为我没有尝试过上述方法(由 Muaath 回答),但这对我来说是解决方法,灵感来自上面的 Muaath 回答:

public async Task<InlineKeyboardMarkup> GetInlineKeyboardMarkup(IReadOnlyList<SomeClassObject> someClassObject)
        {

            int count = someClassObject.Count;
            List<List<InlineKeyboardButton>> buttons = new List<List<InlineKeyboardButton>>(someClassObject.Count);

            for (int i = 0; i < count; i++)
            {
                // Add new row with one button capacity
                buttons.Add(new List<InlineKeyboardButton>(1)
                {
                      InlineKeyboardButton.WithCallbackData(text: $"{someClassObject[i].Title}", callbackData: $"{someClassObject[i].Id}"),
                });
            }

            return new InlineKeyboardMarkup(buttons);
        }

Note the difference is there is now a new method: InlineKeyboardButton.WithCallbackData()请注意不同之处在于现在有一个新方法: InlineKeyboardButton.WithCallbackData()

As per documentation: https://telegrambots.github.io/book/2/reply-markup.html根据文档: https://telegrambots.github.io/book/2/reply-markup.html

You need to put each button in array.您需要将每个按钮放在数组中。 Reply markup: keyboard & inline keyboard 回复标记:键盘和内联键盘

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM