如何在用户选择后移除Telegram内联键盘按钮
Got it, let's solve this problem where you want to get rid of the inline keyboard as soon as a user selects one of its options. Here's exactly how to implement this with the Telegram Bot API in C#:
1. Handle Callback Queries (Not Just Regular Messages)
First, remember that tapping an inline button triggers a CallbackQuery, not a regular Message. So you'll need to add an event handler for OnCallbackQuery (in addition to your existing Bot_OnMessage method).
Here's the full callback handler code:
private void Bot_OnCallbackQuery(object sender, CallbackQueryEventArgs e) { var callbackQuery = e.CallbackQuery; var botClient = (ITelegramBotClient)sender; // Skip if there's no associated message if (callbackQuery.Message == null) return; // Process the user's language selection (customize this to your needs) string feedbackText = callbackQuery.Data switch { "Lan_EN" => "Successfully switched to English!", "Lan_SP" => "¡Cambiado al español exitosamente!", _ => "Sorry, that selection isn't recognized." }; // Edit the original message to REMOVE the inline keyboard var editRequest = new EditMessageTextRequest { ChatId = callbackQuery.Message.Chat.Id, MessageId = callbackQuery.Message.MessageId, Text = feedbackText, // Replace with your desired message, or keep the original text ReplyMarkup = null // THIS IS THE KEY: setting to null removes the keyboard }; // Send the edited message botClient.EditMessageTextAsync(editRequest); // Confirm the callback to stop the loading spinner on the user's end botClient.AnswerCallbackQueryAsync(new AnswerCallbackQueryRequest { CallbackQueryId = callbackQuery.Id, Text = feedbackText, ShowAlert = false // Set to true if you want a pop-up alert instead of a toast }); }
2. Register the Callback Event
Don't forget to hook up this event when initializing your bot, alongside your existing OnMessage handler:
var bot = new TelegramBotClient("YOUR_BOT_API_TOKEN"); bot.OnMessage += Bot_OnMessage; bot.OnCallbackQuery += Bot_OnCallbackQuery; // Add this line bot.StartReceiving();
3. (Optional) Polish Your Message Handler
If you haven't already, here's how to send the initial language selection keyboard in your Bot_OnMessage method (assuming you trigger it with a command like /language):
private void Bot_OnMessage(object sender, MessageEventArgs messageEventArgs) { var message = messageEventArgs.Message; var botClient = (ITelegramBotClient)sender; if (message == null || message.Type != MessageType.Text) return; // Trigger language selection when user sends /language if (message.Text.Equals("/language", StringComparison.OrdinalIgnoreCase)) { // Your existing keyboard code (tweaked to use the official method) InlineKeyboardMarkup keyLanguage = new InlineKeyboardMarkup( new InlineKeyboardButton[][] { new InlineKeyboardButton[] { InlineKeyboardButton.WithCallbackData("English", "Lan_EN"), InlineKeyboardButton.WithCallbackData("Spain", "Lan_SP") } }); botClient.SendTextMessageAsync( chatId: message.Chat.Id, text: "Please choose your preferred language:", replyMarkup: keyLanguage); } }
Key Notes
- If you don't want to change the original message text, use
EditMessageReplyMarkupAsyncinstead ofEditMessageTextAsync, and passnullas theReplyMarkupparameter. - Always call
AnswerCallbackQueryAsync—this tells Telegram the button tap was processed, so the user doesn't see a permanent loading state.
内容的提问来源于stack exchange,提问作者Mahdi Yousef

