create dynamic Keyboard telegram bot in c# , MrRoundRobin API

13,132

Solution 1

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

Solution 2

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).

Solution 3

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.

Share:
13,132
Pooria.Shariatzadeh
Author by

Pooria.Shariatzadeh

Updated on June 06, 2022

Comments

  • Pooria.Shariatzadeh
    Pooria.Shariatzadeh almost 2 years

    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"),
            }
        });