如何在用户点击时更改InlineKeyboardButton标签?

时间:2018-02-24 20:16:51

标签: java telegram telegram-bot

我想在点击后更改由setText()方法初始化的InlineKeyboardButton对象的标签

例如,将“label”字符串更改为“label-1”单击按钮。

这是我的一段代码:

if (update.hasMessage() && update.getMessage().hasText()) {
    if (update.getMessage().getText().equals("/start")) {
        long chat_id = update.getMessage().getChatId();
        message = new SendMessage() 
                .setChatId(chat_id)
                .setText("You send /start");
        InlineKeyboardButton dk1=new InlineKeyboardButton().setText("labe1").setCallbackData("change_the_label");
        InlineKeyboardMarkup markupInline = new InlineKeyboardMarkup();
        List<List<InlineKeyboardButton>> rowsInline = new ArrayList<>();
        List<InlineKeyboardButton> rowInline = new ArrayList<>();
        rowInline.add(dk1);
        rowsInline.add(rowInline);
        markupInline.setKeyboard(rowsInline);
        message.setReplyMarkup(markupInline);
        try {execute(message); catch (TelegramApiException e) {e.printStackTrace();}}}

1 个答案:

答案 0 :(得分:0)

我通过添加这段代码解决了这个问题 它使用EditMessageReplyMarkup对象  编辑现有的内联键盘:

   String call_data = update0.getCallbackQuery().getData();
            long message_id = update0.getCallbackQuery().getMessage().getMessageId();
            long chat_id = update0.getCallbackQuery().getMessage().getChatId();
            String inline_message_id = update0.getCallbackQuery().getInlineMessageId();
            if (call_data.equals("change_the_label")) {
                String answer = "Updated message text";
                EditMessageReplyMarkup new_message = new EditMessageReplyMarkup()
                        .setChatId(chat_id).setMessageId(toIntExact(message_id))
                        .setInlineMessageId(inline_message_id);
                InlineKeyboardButton dk1=new InlineKeyboardButton();
                dk1.setText("label1");
                dk1.setCallbackData("change_the_label");
                InlineKeyboardMarkup markupInline = new InlineKeyboardMarkup();
                List<List<InlineKeyboardButton>> rowsInline = new ArrayList<>();
                List<InlineKeyboardButton> rowInline = new ArrayList<>();

                rowInline.add(dk1);

                rowsInline.add(rowInline);

                markupInline.setKeyboard(rowsInline);
                new_message.setReplyMarkup(markupInline);

                                try {editMessageReplyMarkup(new_message);

                } catch (TelegramApiException e) {
                    e.printStackTrace();
                }