简体   繁体   English

使用C#中的键盘按钮而非内联按钮在C#中创建动态键盘电报机器人

[英]create dynamic Keyboard telegram bot in c# with keyboard button not inline button in c#

How can I create a dynamic keyboard button I found some codes but they are for inline buttons? 我如何创建一个动态键盘按钮,我发现了一些代码,但它们是用于嵌入式按钮的? I want to get data from database and create one button in each row with keyboard button 我想从数据库中获取数据并使用键盘按钮在每一行中创建一个按钮

var keyboard = new ReplyKeyboardMarkup(
    new[] {
        new[]{
            new KeyboardButton("a"),
        },
        new[]{
            new KeyboardButton("b"),
        },
    });

The first part (ie adding the buttons from db) is OK with the same code you wrote. 第一部分(即从db添加按钮)可以使用您编写的相同代码来确定。

The second part (ie changing button layout) I'm afraid, is not possible as far as I know. 据我所知,第二部分(即更改按钮布局)恐怕是不可能的。 Because layout is done by Telegram app in client device and based on many things including device screen size and may change in different app versions. 因为布局是由客户端设备中的Telegram应用程序完成的,并且基于包括设备屏幕尺寸在内的许多因素,因此在不同的应用程序版本中可能会发生变化。

Use InlineKeyboardMarkup and InlineKeyboardButton instead of it, you can refer to this example . 使用InlineKeyboardMarkupInlineKeyboardButton代替它,您可以参考此示例

Please look up your library document next time. 请下次查询您的图书馆文件。

private static ReplyKeyboardMarkup calendarMenu;

SqlDataAdapter sc3 = new SqlDataAdapter("select KeyboardName from  dbo.Keyboards", SqlConnection);
DataTable dt3 = new DataTable();
sc3.Fill(dt3);

int keyboardRows = 0;
if (dt3.Rows.Count % 2 == 0)
{
    keyboardRows = dt3.Rows.Count / 2;
}
else
{
    keyboardRows = (dt3.Rows.Count / 2) + 1;
}

KeyboardButton[][] kbc = new KeyboardButton[(keyboardRows + 1)][];

KeyboardButton[] keys = new KeyboardButton[dt3.Rows.Count];

var i = 0;
foreach (DataRow cn3 in dt3.Rows)
{
    keys[i] = new KeyboardButton(cn3["KeyboardName"].ToString());
    i++;
}

for (int r = 0, s = 0; r < keyboardRows; r++, s++)
{
    if (dt3.Rows.Count % 2 == 0)
    {
        kbc[r] = new KeyboardButton[] {keys[r + s], keys[r + s + 1]};
    }
    else
    {
        if ((r + s) != keys.Length)
        {
            kbc[r] = new KeyboardButton[] { keys[r + s], keys[r + s + 1] };
        }
        else
        {
            kbc[r] = new KeyboardButton[] { keys[r + s] };
        }
     }
 }

 kbc[keyboardRows] = new KeyboardButton[] { new KeyboardButton("Return to Main Menu"), };

 calendarMenu = new ReplyKeyboardMarkup
 {
     Keyboard = kbc
 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM