如何通过c#向电报机器人添加两个内联按钮?
我不熟悉电报机器人编程。我想添加两个内联按钮,但我只知道如何添加一个:如何通过c#向电报机器人添加两个内联按钮?,c#,telegram-bot,C#,Telegram Bot,我不熟悉电报机器人编程。我想添加两个内联按钮,但我只知道如何添加一个: StringBuilder sb = new StringBuilder(); sb.AppendLine("Some websites:"); InlineKeyboardButton urlButton = new InlineKeyboardButton(); InlineKeyboardMarkup inline = new InlineKeyboardMarkup(urlButton); url
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);
如何添加一个或多个按钮?您可以通过将
IEnumerable
传递到InlineKeyboardMarkup
类似于不需要传递的InlineKeyboardButton
这样做:
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);
}
您可以通过将IEnumerable
传递到InlineKeyboardMarkup
构造函数和每个IEnumerable来控制每一行中的按钮数,每一个IEnumerable都是一行,如下所示:
// 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);
这是第二次的结果:如何才能在新行中添加另一个按钮?您可以从行中放入所需内容,只需定义
InlineKeyboardButton[]
并将其添加到按钮