[英]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 中创建动态键盘电报机器人,它看起来像这样:
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;
}
GetInlineKeyboard
to InlineKeyboardMarkup
instead of InlineKeyboardButton[][]
GetInlineKeyboard
到InlineKeyboardMarkup
而不是InlineKeyboardButton[][]
var
)var
)List<InlineKeyboardButton>
instead of Array ( InlineKeyboardButton[]
)List<InlineKeyboardButton>
而不是 Array ( InlineKeyboardButton[]
)IDictionary<string, string>
to get text and callback dataIDictionary<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:下面有两种方式:
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;
}
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.