6

I want to create custom keyboard in telegram.bot

For example:
We have an array of string that gets from the database or other recurses how we can push data from the array to InlineKeyboardMarkup 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[] 
        {
            new InlineKeyboardButton("one"),
            new InlineKeyboardButton("two"),
        },
        new[] 
        {
            new InlineKeyboardButton("three"),
            new InlineKeyboardButton("Four"),
        }
    });

3 Answers 3

12

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));
1
6

Create InlineKeyboardMarkup in a method:

public static InlineKeyboardMarkup InlineKeyboardMarkupMaker(Dictionary<int, string> items)
{
     InlineKeyboardButton[][] ik = items.Select(item => new[]
     {
           new InlineKeyboardButton(item.Key, item.Value)
     }).ToArray();
     return new InlineKeyboardMarkup(ik);
}

Then use it like this:

var items=new Dictionary<int,string>()
{
     {0 , "True" }
     {1 , "False" }
};
var inlineKeyboardMarkup = InlineKeyboardMarkupMaker(items);
Bot.SendTextMessageAsync(message.Chat.Id, messageText, replyMarkup: inlineKeyboardMarkup);

Selecting True or False make an update with Update.CallbackQuery.Dataequal to selected item key (0 or 1).

3
2

Create InlineKeyboardButton with specific columns by below method.

                public static IReplyMarkup CreateInlineKeyboardButton(Dictionary<string, string> buttonList, int columns)
    {
        int rows = (int)Math.Ceiling((double)buttonList.Count / (double)columns);
        InlineKeyboardButton[][] buttons = new InlineKeyboardButton[rows][];

        for (int i = 0; i < buttons.Length; i++)
        {
            buttons[i] = buttonList
                .Skip(i * columns)
                .Take(columns)
                .Select(direction => new InlineKeyboardCallbackButton(
                    direction.Key, direction.Value
                ) as InlineKeyboardCallbackButton)
                .ToArray();
        }
        return new InlineKeyboardMarkup(buttons);
    }

Use method like this:

        public static IReplyMarkup CreateInLineMainMenuMarkup()
    {
        Dictionary<string, string> buttonsList = new Dictionary<string, string>();
        buttonsList.Add("one", "DATA1");
        buttonsList.Add("two", "DATA2");
        buttonsList.Add("three", "DATA3");

        return CreateInlineKeyboardButton(buttonsList, 2);
    }

Thanks pouladpld to create this function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.