To create a custom keyboard you have to sent a text message and pass a IReplyMarkup
. The selected option is sent as a message which can be handled in the OnMessage
event. You can hide the custom keyboard when you set a ReplyKeyboardHide
as reply markup.
Here is an example:
private const string FirstOptionText = "First option";
private const string SecondOptionText = "Second option";
private async void BotClientOnMessage(object sender, MessageEventArgs e)
{
switch (e.Message.Text)
{
case FirstOptionText:
await BotClient.SendTextMessageAsync(e.Message.Chat.Id, "You chose the first option", replyMarkup:new ReplyKeyboardHide());
break;
case SecondOptionText:
await BotClient.SendTextMessageAsync(e.Message.Chat.Id, "You chose the second option", replyMarkup:new ReplyKeyboardHide());
break;
default:
await BotClient.SendTextMessageAsync(e.Message.Chat.Id, "Hi, select an option!",
replyMarkup: new ReplyKeyboardMarkup(new[]
{
new KeyboardButton(FirstOptionText),
new KeyboardButton(SecondOptionText),
}));
break;
}
}
Here is a chat with a custom keyboard:
Here is a chat where I clicked the first button:
I hope this helps!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…