I’m working on a Telegram bot and I need help with custom keyboards. How can I capture the option a user selects? I’ve been looking for examples but haven’t found anything useful yet.
I know this has been addressed for the node-telegram-bot-api, but I’m actually using C#. Does anyone know if there’s a similar solution for C# Telegram bots?
Here’s what I’m trying to do:
- Display a custom keyboard to the user
- Wait for them to pick an option
- Get the selected option in my code
Has anyone done this before? Any tips or code snippets would be super helpful. Thanks!
I’ve implemented custom keyboards in a C# Telegram bot before, and it’s definitely doable. The key is to use the Telegram.Bot library and handle the OnMessage event.
First, create your custom keyboard using ReplyKeyboardMarkup. Then, in your OnMessage event handler, check for the CallbackQuery property. This contains the data from the button press.
Here’s a basic outline:
bot.OnMessage += (sender, args) =>
{
if (args.Message.Text != null)
{
// Handle the selected option here
switch (args.Message.Text)
{
case "Option 1":
// Do something
break;
case "Option 2":
// Do something else
break;
}
}
};
Remember to remove the keyboard after selection using ReplyKeyboardRemove(). Hope this helps point you in the right direction!
Working with C# Telegram bots and custom keyboards can be quite straightforward when using the Telegram.Bot library. In my experience, you begin by creating a custom keyboard with ReplyKeyboardMarkup and sending it along with a message to the user. Once the user responds, the bot’s update handler checks incoming messages to see if the text matches one of the specified options. When a match is found, you process the input accordingly and remove the custom keyboard using ReplyKeyboardRemove. This approach has proven effective in several projects.
hey mate, i’ve worked with c# and telegram.bot library. its simple: use ReplyKeyboardMarkup to send a custom kb, then in the update handler check Message.Text for the selected option. a switch statement works well. remv keybd after the choice!