create dynamic Keyboard telegram bot in c# , MrRoundRobin API
Asked Answered
C

3

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"),
        }
    });
Caddish answered 5/10, 2016 at 22:59 Comment(0)
S
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));
Stymie answered 8/10, 2016 at 3:58 Comment(1)
help me this link:#41519995Octillion
F
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).

Fornix answered 9/10, 2016 at 8:52 Comment(3)
hi i dont understand this one .are you using a JSON array ?Caddish
I have updated my solution if you see that again you probably found it.Fornix
help me this link:#41519995Octillion
G
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.

Gin answered 9/10, 2017 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.