Create a dynamic telegram bomb in C #, MrRoundRobin API

I want to create a custom keyboard in telegram.bot For example: We have an array of strings that get from a database or other recurses, how can we transfer data from an array to InlineKeyboardMarkup in for a 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"), } }); 
+4
c # keyboard telegram telegram-bot
source share
3 answers

You can use a separate function to get an InlineKeyboardButton array

 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)); 
+6
source share

Create an InlineKeyboardMarkup in the 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 as follows:

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

Choosing True or False makes the update with Update.CallbackQuery.Data equal to the selected item (0 or 1).

+5
source share

Create an InlineKeyboardButton with specific columns using the method below.

  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 the method as follows:

  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 for creating this feature.

+1
source share

All Articles