Java Telegram Bot Inline Keyboard Button Removal Issue

I’m working on a Java Telegram bot that uses inline keyboards for user interaction. Here’s my problem:

I have a bot that shows text with a button to start placing an order. When users click it, the bot prompts for their first name and displays a cancel button underneath. After the user types their name, I update the message to ask for their last name with new buttons, but the previous cancel button from the name input step stays visible on screen.

I need help removing the old button after the user enters their text input.

Here’s my code for requesting the first name:

String prompt = "Please enter your first name";

EditMessageText updatedMessage = new EditMessageText()
        .setChatId(chatId)
        .setMessageId(msgId)
        .setText(prompt).setParseMode("HTML");

inlineMarkup = new InlineKeyboardMarkup();
List<List<InlineKeyboardButton>> allButtons = new ArrayList<>();
List<InlineKeyboardButton> firstRow = new ArrayList<>();

firstRow.add(buildButton("cancel_action", cancelIcon+" Cancel Order"));

allButtons.add(firstRow);

inlineMarkup.setKeyboard(allButtons);
updatedMessage.setReplyMarkup(inlineMarkup);

try {
    execute(updatedMessage); 
} catch (TelegramApiException ex) {
    ex.printStackTrace();
}

When the user provides their first name, I display the surname request, but the cancel button from before remains visible:

String prompt = "Please provide your last name";

newMessage = new SendMessage().setChatId(update.getMessage().getChatId());
newMessage.setText(prompt).setParseMode("HTML");

inlineMarkup = new InlineKeyboardMarkup();
List<List<InlineKeyboardButton>> allButtons = new ArrayList<>();
List<InlineKeyboardButton> firstRow = new ArrayList<>();

firstRow.add(buildButton("cancel_action", cancelIcon+" Cancel Order"));

allButtons.add(firstRow);

inlineMarkup.setKeyboard(allButtons);
newMessage.setReplyMarkup(inlineMarkup);

try {
    execute(newMessage);
} catch (TelegramApiException ex) {
    ex.printStackTrace();
}

I encountered a similar issue when developing my bot. The problem lies in using SendMessage after you’ve already sent a message with InlineKeyboardMarkup. Instead, you should stick with EditMessageText for updating your messages. After the first name is entered, store the messageId you received from the first EditMessageText and use it again for the surname request. This way, you can effectively replace the old InlineKeyboardMarkup with the new one, thus removing the old cancel button. Alternatively, you can update the existing message first by setting the reply markup to an empty InlineKeyboardMarkup to clear the previous buttons before sending the new prompt.

yeah, you’re mixing SendMessage with EditMessageText. when the user types their first name, clear the old keyboard by editing that original message with empty markup, then send the surname request. or just stick with EditMessageText for the whole flow instead of switching to SendMessage.

It seems you’re experiencing issues due to the mix of EditMessageText and SendMessage in your code. To ensure that the previous cancel button disappears, it’s best to consistently use EditMessageText for all updates. After asking for the first name, retain the message ID and reuse it for your subsequent requests. Alternatively, you could clear the old buttons first by using EditMessageText to set an empty InlineKeyboardMarkup before sending your surname prompt. However, sticking with EditMessageText is the cleaner approach.