Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

c# - How to get the response from the keyboard input in telegram bot?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...