Reputation: 604
I am new to telegram bot programming. I want to add two inline buttons, but I just know how to add one:
StringBuilder sb = new StringBuilder();
sb.AppendLine("Some websites:");
InlineKeyboardButton urlButton = new InlineKeyboardButton();
InlineKeyboardMarkup inline = new InlineKeyboardMarkup(urlButton);
urlButton.Text = "Go URL1";
urlButton.Url = "https://www.google.com/";
bot.SendTextMessageAsync(chatId, sb.ToString(), ParseMode.Html, true, false, 0, inline);
How is it possible add one or more buttons?
Upvotes: 2
Views: 11237
Reputation: 158
More simpler compact syntax:
var inlineKeyboard = new InlineKeyboardMarkup(new[]
{
InlineKeyboardButton.WithUrl("Go url 1", "https://www.google.com/"),
InlineKeyboardButton.WithUrl("Go url 2", "https://www.bing.com/")
});
Message message = await bot.SendTextMessageAsync(
chatId,
text,
replyMarkup: inlineKeyboard,
parseMode: ParseMode.Markdown);
Upvotes: -1
Reputation: 623
You can do it by pass IEnumerable<InlineKeyboardButton>
to InlineKeyboardMarkup
Like ineasted of pass InlineKeyboardButton
like this this:
private void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
// Buttons
InlineKeyboardButton urlButton = new InlineKeyboardButton();
InlineKeyboardButton urlButton2 = new InlineKeyboardButton();
urlButton.Text = "Go URL1";
urlButton.Url = "https://www.google.com/";
urlButton2.Text = "Go URL2";
urlButton2.Url = "https://www.bing.com/"
InlineKeybordButton[] buttons = new InlineKeybordButton[] { urlButton, urlButton2 };
// Keyboard markup
InlineKeyboardMarkup inline = new InlineKeyboardMarkup(buttons);
// Send message!
bot.SendTextMessageAsync(chatId, sb.ToString(), ParseMode.Html, true, false, 0, inline);
}
And you can control How many buttons in every row By Passing IEnumerable<IEnumerable<InlineKeyboardButton>>
to InlineKeyboardMarkup
constructor And Every IEnumerable it will be a single row like this:
// Defining buttons
InlineKeyboardButton urlButton = new InlineKeyboardButton();
InlineKeyboardButton urlButton2 = new InlineKeyboardButton();
InlineKeyboardButton urlButton3 = new InlineKeyboardButton();
InlineKeyboardButton urlButton4 = new InlineKeyboardButton();
urlButton.Text = "Go URL1";
urlButton.Url = "https://www.google.com/";
urlButton2.Text = "Go URL2";
urlButton2.Url = "https://www.bing.com/";
urlButton3.Text = "Go URL3";
urlButton3.Url = "https://www.duckduckgo.com/";
urlButton4.Text = "Go URL4";
urlButton4.Url = "https://stackoverflow.com/";
// Rows, every row is InlineKeyboardButton[], You can put multiple buttons!
InlineKeyboardButton[] row1 = new InlineKeyboardButton[] { urlButton };
InlineKeyboardButton[] row2 = new InlineKeyboardButton[] { urlButton2, urlButton3 };
InlineKeyboardButton[] row3 = new InlineKeyboardButton[] { urlButton4 };
// Buttons by rows
InlineKeyboardButton[][] buttons = new InlineKeyboardButton[][] { row1, row2, row3 };
// Keyboard
InlineKeyboardMarkup keyboard = new InlineKeyboardMarkup(buttons);
// Send Message
await bot.SendTextMessageAsync(chat, "Message", replyMarkup: keyboard);
And this Is the result in second time:
Upvotes: 3