如何通过单击InlineKeyboardButton打开ReplyKeyboardMarkup?

时间:2018-08-18 19:04:56

标签: java telegram-bot

我需要通过使用library单击ReplyKeyboardMarkup来打开InlineKeyboardButton

就我而言,请点击Create post按钮 enter image description here

然后打开这种类型的键盘 enter image description here

因此,我试图单击InlineKeyboardButton并像此snippet一样使用ReplyKeyboardMarkup打开CallbackQuery(以处理单击)。

单击按钮时,我只能看到屏幕上的时钟(但是我有CallbackQuery可以处理此按钮):

enter image description here

   else if (call_data.equals("correcting_post")) {
                ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
                List<KeyboardRow> keyboard = new ArrayList<>();
                KeyboardRow row = new KeyboardRow();
                row.add("Clear");
                row.add("Preview");
                keyboard.add(row);
                row = new KeyboardRow();
                row.add("Cancel");
                row.add("Next");
                keyboard.add(row);
                keyboardMarkup.setKeyboard(keyboard);
                AnswerCallbackQuery a = new AnswerCallbackQuery()
                        .setCallbackQueryId(update.getCallbackQuery().getId());
                try {
                    execute(a);
                } catch (TelegramApiException e) {
                    e.printStackTrace();
                }
            }

我需要更正或添加什么?感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

如果我做对了,您想单击InlineKeyboardButton按钮之一,然后打开ReplyKeyboardMarkup。为此,您可以使用如下代码:

public class YourClass extends TelegramLongPollingBot {

    @Override
    public void onUpdateReceived(Update update) {
        if (update.hasCallbackQuery()) {
            String data = update.getCallbackQuery().getData();
            if (data.equals("correcting_post")) {
                try {
                    ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
                    List<KeyboardRow> keyboard = new ArrayList<>();
                    KeyboardRow row = new KeyboardRow();
                    row.add("Test button");
                    keyboard.add(row);
                    keyboardMarkup.setKeyboard(keyboard);

                    // Create a message object
                    SendMessage message = new SendMessage()
                            .setChatId(update.getCallbackQuery().getMessage().getChatId())
                            .enableMarkdown(true)
                            .setText("Message text");
                    message.setReplyMarkup(keyboardMarkup);
                    execute(message);
                } catch (TelegramApiException e) {
                    //exception handling
                }
            }
            //Check another options for data
        }
    }

    ...
}

该库可能提供了一种更方便的方法,但是至少这种代码的和平是可行的。