Делаем телеграмм-бот встроенными клавиатурами с нужными номерами строк - PullRequest
1 голос
/ 06 августа 2020

Я хочу сделать встроенные клавиатуры в боте телеграммы в две или более строк, я знаю, как сделать их в одну строку (в следующих кодах):

    StringBuilder sb = new StringBuilder(txtBody.Text);

    int chatId="123456789"; //not real Chat ID


    // Buttons
    InlineKeyboardButton urlButton1 = new InlineKeyboardButton();
    InlineKeyboardButton urlButton2 = new InlineKeyboardButton();
    InlineKeyboardButton urlButton3 = new InlineKeyboardButton();

    urlButton1.Text = "Go URL1";
    urlButton1.Url = "https://www.google.com/";

    urlButton2.Text = "Go URL2";
    urlButton2.Url = "https://www.bing.com/";

    urlButton3.Text = "Go URL3";
    urlButton3.Url = "https://www.yahoo.com/";

    InlineKeyboardButton[] buttons = new InlineKeyboardButton[] { urlButton1, urlButton2 , urlButton3};
    InlineKeyboardMarkup inline = new InlineKeyboardMarkup(buttons);

    // Send message!
    bot.SendTextMessageAsync(chatId, sb.ToString(), ParseMode.Html, true, false, 0, inline);

Но как сделать третью кнопку во втором ряду (или последние две кнопки во втором ряду)?

1 Ответ

1 голос
/ 06 августа 2020

Вы можете передать IEnumerable<IEnumerable<InlineKeyboardButton>> в InlineKeyboardMarkup constuctor, и каждый IEnumerable<InlineKeyboardButton> - это одна строка, вот она:

// Buttons
InlineKeyboardButton urlButton1 = new InlineKeyboardButton();
InlineKeyboardButton urlButton2 = new InlineKeyboardButton();
InlineKeyboardButton urlButton3 = new InlineKeyboardButton();

urlButton1.Text = "Go URL1";
urlButton1.Url = "https://www.google.com/";

urlButton2.Text = "Go URL2";
urlButton2.Url = "https://www.bing.com/";

urlButton3.Text = "Go URL3";
urlButton3.Url = "https://www.yahoo.com/";


// Rows, can put multiple buttons!
InlineKeyboardButton[] row1 = new InlineKeyboardButton[] { urlButton1 };
InlineKeyboardButton[] row2 = new InlineKeyboardButton[] { urlButton2, urlButton3 };

// Set rows in Array of array | IEnamerable<IEnumerable<InlineKeyboardButton>>
InlineKeyboardButton[][] buttons = new InlineKeyboardButton[][] { row1, row2 };

// Keyboard
InlineKeyboardMarkup inline = new InlineKeyboardMarkup(buttons);

// Send message!
bot.SendTextMessageAsync(chatId, sb.ToString(), ParseMode.Html, true, false, 0, inline);
...