Функция CallbackQuery не работает с InlineKeyboard - PullRequest
0 голосов
/ 25 марта 2020

Я хочу использовать InlineKeyboard в моем Telegram Bot.

Этот класс для его построения:

public class InlineKeyboardBuilder {

    private Long chatId;
    private String text;

    private List<List<InlineKeyboardButton>> keyboard = new ArrayList<>();
    private List<InlineKeyboardButton> row = null;

    private InlineKeyboardBuilder() {}

    public static InlineKeyboardBuilder create() {
        InlineKeyboardBuilder builder = new InlineKeyboardBuilder();
        return builder;
    }

    public static InlineKeyboardBuilder create(Long chatId) {
        InlineKeyboardBuilder builder = new InlineKeyboardBuilder();
        builder.setChatId(chatId);
        return builder;
    }

    public InlineKeyboardBuilder setText(String text) {
        this.text = text;
        return this;
    }

    public InlineKeyboardBuilder setChatId(Long chatId) {
        this.chatId = chatId;
        return this;
    }

    public InlineKeyboardBuilder row() {
        this.row = new ArrayList<>();
        return this;
    }

    public InlineKeyboardBuilder button(String text, String callbackData) {
        row.add(new InlineKeyboardButton().setText(text).setCallbackData(callbackData));
        return this;
    }

    public InlineKeyboardBuilder endRow() {
        this.keyboard.add(this.row);
        this.row = null;
        return this;
    }


    public SendMessage build() {
        SendMessage message = new SendMessage();

        message.setChatId(chatId);
        message.setText(text);

        InlineKeyboardMarkup keyboardMarkup = new InlineKeyboardMarkup();

        keyboardMarkup.setKeyboard(keyboard);
        message.setReplyMarkup(keyboardMarkup);

        return message;
    }

}

Затем я отправляю эту клавиатуру и жду обратного вызова:

 @Override
    public void onUpdateReceived(Update update) {
        long chat_id = update.getMessage().getChatId();
        if (update.hasMessage() && update.getMessage().hasText()) {
            SendMessage message = InlineKeyboardBuilder.create(chat_id)
                    .setText("Menu:")
                    .row()
                    .button("Action 1", "action-1")
                    .button("Action 2", "action-2")
                    .endRow()
                    .row()
                    .button("Action 3", "action-3")
                    .endRow()
                    .build();
            try {
                // Send the message
                sendApiMethod(message);
                //execute(message);
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        } else if (update.hasCallbackQuery()) {
            // Set variables
            String call_data = update.getCallbackQuery().getData();
            long message_id = update.getCallbackQuery().getMessage().getMessageId();

            if (call_data.equals("action-1")) {
                String answer = "Updated message text";
                EditMessageText new_message = new EditMessageText()
                        .setChatId(chat_id)
                        .setMessageId(toIntExact(message_id))
                        .setText(answer);
                try {
                    execute(new_message);
                } catch (TelegramApiException e) {
                    e.printStackTrace();
                }
            }

Но ничего не происходит. Почему функция обратного вызова не работает с этой клавиатурой? Если я отправлю сообщение боту, он будет время от времени возвращать клавиатуру. Я должен получить ответ, когда нажата кнопка. Похоже, я не могу получить результат запроса обратного вызова, но почему?

...