简体   繁体   English

在c#,MrRoundRobin API中创建动态键盘电报机器人

[英]create dynamic Keyboard telegram bot in c# , MrRoundRobin API

I want to create custom keyboard in telegram.bot 我想在telegram.bot创建自定义键盘

For example: 例如:
We have an array of string that gets from the database or other recurses how we can push data from the array to InlineKeyboardMarkup in for loop or function 我们有一个从数据库获取的字符串数组或其他的recurses如何将数据从数组推送到InlineKeyboardMarkup for for循环或函数

//array  of Button
string[] ButtonItem= new string[] { "one", "two", "three", "Four" };

//function or solution to create keyboard like this 
var keyboard = new InlineKeyboardMarkup(new[]
    {
        new[] 
        {
            new InlineKeyboardButton("one"),
            new InlineKeyboardButton("two"),
        },
        new[] 
        {
            new InlineKeyboardButton("three"),
            new InlineKeyboardButton("Four"),
        }
    });

You could use a separate function to get an array of InlineKeyboardButton 您可以使用单独的函数来获取InlineKeyboardButton数组

private static InlineKeyboardButton[][] GetInlineKeyboard(string [] stringArray)
{
    var keyboardInline = new InlineKeyboardButton[1][];
    var keyboardButtons = new InlineKeyboardButton[stringArray.Length];
    for (var i = 0; i < stringArray.Length; i++)
    {
        keyboardButtons[i] = new InlineKeyboardButton
        {
            Text = stringArray[i],
            CallbackData = "Some Callback Data",
        };
    }
    keyboardInline[0] = keyboardButtons;
    return keyboardInline;
}

And then call the function: 然后调用函数:

var buttonItem = new[] { "one", "two", "three", "Four" };
var keyboardMarkup = new InlineKeyboardMarkup(GetInlineKeyboard(buttonItem));

Create InlineKeyboardMarkup in a method: 在方法中创建InlineKeyboardMarkup:

public static InlineKeyboardMarkup InlineKeyboardMarkupMaker(Dictionary<int, string> items)
{
     InlineKeyboardButton[][] ik = items.Select(item => new[]
     {
           new InlineKeyboardButton(item.Key, item.Value)
     }).ToArray();
     return new InlineKeyboardMarkup(ik);
}

Then use it like this: 然后像这样使用它:

var items=new Dictionary<int,string>()
{
     {0 , "True" }
     {1 , "False" }
};
var inlineKeyboardMarkup = InlineKeyboardMarkupMaker(items);
Bot.SendTextMessageAsync(message.Chat.Id, messageText, replyMarkup: inlineKeyboardMarkup);

Selecting True or False make an update with Update.CallbackQuery.Data equal to selected item key (0 or 1). 选择True或False会使Update.CallbackQuery.Data等于所选项密钥(0或1)进行更新。

Create InlineKeyboardButton with specific columns by below method. 通过以下方法创建具有特定列的InlineKeyboardButton。

                public static IReplyMarkup CreateInlineKeyboardButton(Dictionary<string, string> buttonList, int columns)
    {
        int rows = (int)Math.Ceiling((double)buttonList.Count / (double)columns);
        InlineKeyboardButton[][] buttons = new InlineKeyboardButton[rows][];

        for (int i = 0; i < buttons.Length; i++)
        {
            buttons[i] = buttonList
                .Skip(i * columns)
                .Take(columns)
                .Select(direction => new InlineKeyboardCallbackButton(
                    direction.Key, direction.Value
                ) as InlineKeyboardCallbackButton)
                .ToArray();
        }
        return new InlineKeyboardMarkup(buttons);
    }

Use method like this: 使用这样的方法:

        public static IReplyMarkup CreateInLineMainMenuMarkup()
    {
        Dictionary<string, string> buttonsList = new Dictionary<string, string>();
        buttonsList.Add("one", "DATA1");
        buttonsList.Add("two", "DATA2");
        buttonsList.Add("three", "DATA3");

        return CreateInlineKeyboardButton(buttonsList, 2);
    }

Thanks pouladpld to create this function. 感谢pouladpld创建此功能。

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

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