Pooria.Shariatzadeh Pooria.Shariatzadeh - 10 months ago 546
C# Question

create dynamic Keyboard telegram bot in c# , MrRoundRobin API

i want to create custom keyboard in


For example:
We have array of string that get from database or other recurses
how we can push data from array to
in for loop or function

//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 InlineKeyboardButton("one"),
new InlineKeyboardButton("two"),
new InlineKeyboardButton("three"),
new InlineKeyboardButton("Four"),

Answer Source

You could use a separate function to get an array of 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;

And then call the function:

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