Reputation: 71
How can I create an inline keyboard with several buttons, one per row?
like:
inlineBtn1
inlineBtn2
inlineBtn3
...
not:
inlineBtn1 inlineBtn2 inlineBtn3 ...
Upvotes: 2
Views: 7969
Reputation: 71
I Solved it
await bot.SendTextMessageAsync(e.Message.Chat.Id, "test", replyMarkup: new InlineKeyboardMarkup(
new InlineKeyboardButton[][]
{
new [] { new InlineKeyboardButton() { Text = "btn 1",CallbackData="Some data1" } }, // buttons in row 1
new [] { new InlineKeyboardButton() { Text = "btn 2", CallbackData = "Some data2" } }, // buttons in row 2
new [] { new InlineKeyboardButton() {Text = "btn 3", CallbackData = "Some data3" } }// buttons in row 3
}));
We must use exactly one of the optional fields. So you have to pass either url, callback_data or switch_inline_query The button would be useless if you don't pass any of those fields.
Upvotes: 3
Reputation: 4408
To send a message to a chat with an inline keyboard use the replyMarkup
parameter of TelegramBot.SendMessage
or TelegramBot.SendMessageAsync
method. It can be either ReplyKeyboardMarkup
or InlineKeyboardMarkup
. A constructor of InlineKeyboardMarkup
accepts an array of button rows (each of them, in turn, comprises an array of buttons in the given row). To create an inline keyboard with several buttons one per row pass an array with several rows with one element each:
TelegramBot bot = ...;
Chat chat = ...;
await bot.SendMessageAsync(chat.Id, "A message. Use the keyboard below.",
replyMarkup: new InlineKeyboardMarkup(
new InlineKeyboardButton[][]
{
new [] { new InlineKeyboardButton("inlineBtn1") }, // buttons in row 1
new [] { new InlineKeyboardButton("inlineBtn2") }, // buttons in row 2
new [] { new InlineKeyboardButton("inlineBtn3") } // buttons in row 3
}));
The message should look like this:
Upvotes: 0