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"),
        }
    });
Sumithran
  • 6,217
  • 4
  • 40
  • 54
Pooria.Shariatzadeh
  • 291
  • 2
  • 4
  • 16

3 Answers3

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));
Nathan
  • 1,303
  • 12
  • 26
  • help me this link:http://stackoverflow.com/questions/41519495/how-to-solve-disturbance-in-my-bot-in-c?noredirect=1#comment70247673_41519495 – cyrus2500 Jan 07 '17 at 12:23
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).

n.y
  • 3,343
  • 3
  • 35
  • 54
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.

Majid gharaei
  • 281
  • 3
  • 11