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();
}