behzad razzaqi
behzad razzaqi

Reputation: 147

How can i create custome keyboard in telegram bot with c#?

I'm new in telegram bot, and want to create the custom keyboard for my bot, for that purpose write this code:

var rkm = new ReplyKeyboardMarkup();

rkm.Keyboard =
    new KeyboardButton[][]
    {
        new KeyboardButton[]
        {
            new KeyboardButton("1-1"),
            new KeyboardButton("1-2")
        },

        new KeyboardButton[]
        {
            new KeyboardButton("2")
        },
        new KeyboardButton[]
        {
            new KeyboardButton("3-1"),
            new KeyboardButton("3-2"),
            new KeyboardButton("3-3")
        }
    };

WebRequest req = WebRequest.Create("https://api.telegram.org/bot" + "282189232:AAGdsdsdsdVOrsxy2rzU75QUAnWL_F2vo" + "/sendMessage?chat_id=" + chat_id + "&text=" + message + "&reply_markup=" + rkm);
req.UseDefaultCredentials = true;
var result = req.GetResponse();
req.Abort();

but when i run that code in this line:

var result = req.GetResponse();

get this error:

The remote server returned an error: (400) Bad Request.

How can I solve that problem?

Upvotes: 0

Views: 324

Answers (1)

Mohamed Sohail
Mohamed Sohail

Reputation: 1867

As suggested use the library by MrRoundRobin. here is how you would do it:

var keyboard = new ReplyKeyboardMarkup(new [] {
    new [] // 1st row
    {
        new KeyboardButton("1"),
        new KeyboardButton("2"),
    },
    new [] // 2nd row
    {
        new KeyboardButton("3"),
        new KeyboardButton("4"),
    }
});

await Bot.SendTextMessageAsync(message.Chat.Id, "Choose", replyMarkup: keyboard);;

Upvotes: 0

Related Questions