How to get the response from the keyboard input in

2020-01-20 04:46发布

问题:

I want to use custom keyboard to get the selected option.

How to get the selected option ? Is there any example?

my question is answered by "node-telegram-bot-api"

here: How to get the response of the keyboard selection?

Is there any solution for c#?

回答1:

When you call SendTextMessageAsync, you pass an IReplyMarkup object which specifies a "custom reply keyboard". I don't know much about the Telegram Bot API, but this looks to be the same feature referred to by the GitHub issue you linked.

There appear to be several implementations listed in the API documentation. I suspect either InlineKeyboardMarkup or ReplyKeyboardMarkup is what you're looking for.



回答2:

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!