How to add Inline button in asp net telegram bot?

2019-08-11 18:13发布

how can I add inline buttons, cause this code examle doesn't work?

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

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

1条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-11 19:02

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:

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. 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. 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.

查看更多
登录 后发表回答