【问题标题】:create dynamic Keyboard telegram bot in c# , MrRoundRobin API在 C# 中创建动态键盘电报机器人,MrRoundRobin API
【发布时间】:2017-02-14 13:08:29
【问题描述】:

我想在telegram.bot 中创建自定义键盘

例如:
我们有一个从数据库或其他递归中获取的字符串数组 我们如何在 for 循环或函数中将数据从数组推送到 InlineKeyboardMarkup

//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"),
        }
    });

【问题讨论】:

    标签: c# keyboard telegram telegram-bot


    【解决方案1】:

    您可以使用单独的函数来获取 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;
    }
    

    然后调用函数:

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

    【讨论】:

    【解决方案2】:

    在方法中创建 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);
    }
    

    然后像这样使用它:

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

    选择 True 或 False 使用 Update.CallbackQuery.Dataequal 更新所选项目键(0 或 1)。

    【讨论】:

    • 嗨,我不明白这个。您使用的是 JSON 数组吗?
    • 我已经更新了我的解决方案,如果你再次看到,你可能已经找到了。
    【解决方案3】:

    通过以下方法创建具有特定列的 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);
        }
    

    使用方法如下:

            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);
        }
    

    感谢pouladpld创建这个函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-22
      • 2017-04-11
      • 2018-03-23
      • 2018-02-09
      • 2023-03-20
      • 1970-01-01
      • 2020-10-08
      • 1970-01-01
      相关资源
      最近更新 更多