You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在用户选择后移除Telegram内联键盘按钮

How to Remove InlineKeyboardMarkup After Button Tap in Telegram Bot (C#)

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 EditMessageReplyMarkupAsync instead of EditMessageTextAsync, and pass null as the ReplyMarkup parameter.
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:14:27