David Russel
David Russel

Reputation: 47

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

Upvotes: 1

Views: 4511

Answers (2)

Lungdrache
Lungdrache

Reputation: 21

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

Upvotes: 2

Naser.Sadeghi
Naser.Sadeghi

Reputation: 1302

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:

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

Upvotes: 1

Related Questions