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:
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[][]
var
)List<InlineKeyboardButton>
instead of Array ( InlineKeyboardButton[]
)IDictionary<string, string>
to get text and callback dataEvery IEnumerable<InlineKeyboardButton>
Is single row, This means you should pass list of rows.
You shouldn't use 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:
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()
As per documentation: https://telegrambots.github.io/book/2/reply-markup.html
You need to put each button in array. Reply markup: keyboard & inline keyboard
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.