简体   繁体   English

如何在asp.net电报机器人中添加内联按钮?

[英]How to add Inline button in asp.net telegram bot?

How can I add inline buttons, cause this code example doesn't work?如何添加内联按钮,导致此代码示例不起作用?

var keyboard = new InlineKeyboardMarkup
            (
                new InlineKeyboardButton[][]
                {
                    // First row
                    new InlineKeyboardButton[] {
                        // First column (Button)
                        InlineKeyboardButton("one", "callback1"),

                        // Second column (Button)
                        InlineKeyboardButton("two", "callback2"),
                    },
                }
            );

All you need to do is defining the Inline Keyboard first and then use it when you send any kind of message to the user.您需要做的就是首先定义内联键盘,然后在向用户发送任何类型的消息时使用它。 To define an inline keyboard you should use the code below right inside your Program class:要定义内联键盘,您应该在 Program 类中使用下面的代码:

private static InlineKeyboardMarkup myInlineKeyboard;

Then inside your main function, you have to use a code like below:然后在你的主函数中,你必须使用如下代码:

myInlineKeyboard = new InlineKeyboardMarkup()
{
 InlineKeyboard = new InlineKeyboardButton[][]    
 {
     new InlineKeyboardButton[] // First row
     {
         new InlineKeyboardButton("option1","CallbackQuery1"), // First column
         new InlineKeyboardButton("option2","CallbackQuery2")  //Second column
     }
 };

Finally in order to see your inline keyboard you should use it when you send a message, Or Edit message, For example if your message is a Text Message you could use this code:最后,为了看到您的内联键盘,您应该在发送消息或编辑消息时使用它,例如,如果您的消息是文本消息,则可以使用以下代码:

await Bot.SendTextMessageAsync(chatId, "Your_Text", replyMarkup: myInlineKeyboard);

I have used this code with Telegram.Bot API Version 13.0.0-beta-01 and it works very well.我已将此代码与Telegram.Bot API 版本 13.0.0-beta-01 一起使用,并且效果很好。 But if you want to use the recent version of this API the code for Inline Keyboards is a bit different but very similar to this.但是如果你想使用这个 API 的最新版本,内联键盘的代码有点不同,但与此非常相似。

i made some hotfixes for the code so you can just copy/paste it我为代码做了一些修补程序,所以你可以复制/粘贴它

InlineKeyboardMarkup myInlineKeyboard = new InlineKeyboardMarkup(

    new InlineKeyboardButton[][]
    {
        new InlineKeyboardButton[] // First row
        {
            InlineKeyboardButton.WithCallbackData( // First Column
                "option1", // Button Name
                "CallbackQuery1" // Answer you'll recieve
            ), 
            InlineKeyboardButton.WithCallbackData( //Second column
                "option2", // Button Name
                "CallbackQuery2" // Answer you'll recieve
            )  
        }
    }
);

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

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