How to get the response from the keyboard input in

2020-01-20 04:26发布

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#?

2条回答
迷人小祖宗
2楼-- · 2020-01-20 05:14

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.

查看更多
虎瘦雄心在
3楼-- · 2020-01-20 05:24

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:
enter image description here

Here is a chat where I clicked the first button:
enter image description here

I hope this helps!

查看更多
登录 后发表回答