Telegram 机器人中的 CallbackQuery 问题

Issue with CallbackQuery in Telegram bot

提问人:Ahmed Mohamed 提问时间:6/24/2023 最后编辑:Federico klez CullocaAhmed Mohamed 更新时间:7/8/2023 访问量:170

问:

我正在尝试使用 java 在我的电报机器人中创建内联键盘标记(使用 TelegramBotsApi v 6.7.0 的 Maven 项目)。这包括处理用户的回调查询,以便稍后做出一些决定。

内联键盘代码是

 private InlineKeyboardMarkup createMenuMarkup() {


        List<List<InlineKeyboardButton>> inlineButtons = new ArrayList<>();
        List<InlineKeyboardButton> inlineKeyboardButtonList1 = new ArrayList<>();
        List<InlineKeyboardButton> inlineKeyboardButtonList2 = new ArrayList<>();

        InlineKeyboardButton inlineKeyboardButton1 = InlineKeyboardButton.builder()
                .text("Choice 1")
                .callbackData("1")
                .url("https://www.google.com")
                .build();

        InlineKeyboardButton inlineKeyboardButton2 = InlineKeyboardButton.builder()
                .text("Choice 2")
                .callbackData("2")
                .build();

        InlineKeyboardButton inlineKeyboardButton3 = InlineKeyboardButton.builder()
                .text("Choice 3")
                .callbackData("3")
                .build();
        InlineKeyboardButton inlineKeyboardButton4 = InlineKeyboardButton.builder()
                .text("Choice 4")
                .callbackData("4")
                .build();


        inlineKeyboardButtonList1.add(inlineKeyboardButton1);
        inlineKeyboardButtonList1.add(inlineKeyboardButton2);
        inlineKeyboardButtonList2.add(inlineKeyboardButton3);
        inlineKeyboardButtonList2.add(inlineKeyboardButton4);
        inlineButtons.add(inlineKeyboardButtonList1);
        inlineButtons.add(inlineKeyboardButtonList2);

        return InlineKeyboardMarkup
                .builder()
                .keyboard(inlineButtons)
                .build();
    }

为了处理回调,我使用了带有处理函数的 if 语句

else if(update.getMessage().getText().equals("/menu"))
        {
            sendMarkup(createMenuMarkup());
        }
        else if(update.hasCallbackQuery())
        {
            **System.out.println("Callback Received");
            handleCallback(update);**
        }
    private void handleCallback(Update update)
    {
        String callData = update.getCallbackQuery().getData();
        System.out.println(callData);
        switch (callData)
        {
            case "1":
                sendMessage("You chose 1");
                break;
            case "2":
                sendMessage("You chose 2");
                break;
            case "3":
                sendMessage("You chose 3");
                break;
            case "4":
                sendMessage("You chose 4");
                break;
            default:
                break;
        }
    }

问题在于回调查询未被识别为并更新,因此它根本无法发挥作用,就好像菜单中的按钮从未被单击过一样。onUpdateReceivedonUpdateReceived

我该怎么办?

我尝试将我正在使用的版本更改为 5.4.0,但仍然遇到同样的问题。

我还尝试使用按钮功能,效果很好ReplyKeyboardMarkup

java 回调 电报机器人

评论


答:

0赞 Samreach 7/8/2023 #1

请使用 TelegramApiv 6.7.0,它支持 telegram API 的新功能。 阅读您的代码后,此遗嘱错误中缺少一些要点。我们应该首先检查以防止异常。if(update.getMessage()...)update.hasMessage()


无论如何,你可以查看我的代码,我按照你的结构写了。


public class InlineBot extends TelegramLongPollingBot {
    @Override
    public void onUpdateReceived(Update update) {
        if(update.hasMessage() && update.getMessage().getText().equals("/menu")){ // check hasMessage first! if not check hasMessage(), it call getMessage(), it breaks into runtimeException
            String chatId = String.valueOf(update.getMessage().getChatId());
            sendMarkup(createMenuMarkup(), chatId);
        } else if(update.hasCallbackQuery()){
            System.out.println("Callback Received");
            handleCallback(update);
        }
    }

    private void sendMarkup(InlineKeyboardMarkup menuMarkup, String chatId) {
        SendMessage sendMessage = new SendMessage();
        sendMessage.setChatId(chatId);
        sendMessage.setText("Please choose button below:"); // if not set Text message, it won't send
        sendMessage.setReplyMarkup(menuMarkup);
        try {
            execute(sendMessage);
        } catch (TelegramApiException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getBotToken() {
        return "...";
    }

    @Override
    public String getBotUsername() {
        return "...";
    }

    private InlineKeyboardMarkup createMenuMarkup() {
        List<List<InlineKeyboardButton>> inlineButtons = new ArrayList<>();
        List<InlineKeyboardButton> inlineKeyboardButtonList1 = new ArrayList<>();
        List<InlineKeyboardButton> inlineKeyboardButtonList2 = new ArrayList<>();

        InlineKeyboardButton inlineKeyboardButton1 = InlineKeyboardButton.builder()
                .text("Choice 1")
                .callbackData("1")
                .url("https://www.google.com")
                .build();

        InlineKeyboardButton inlineKeyboardButton2 = InlineKeyboardButton.builder()
                .text("Choice 2")
                .callbackData("2")
                .build();

        InlineKeyboardButton inlineKeyboardButton3 = InlineKeyboardButton.builder()
                .text("Choice 3")
                .callbackData("3")
                .build();
        InlineKeyboardButton inlineKeyboardButton4 = InlineKeyboardButton.builder()
                .text("Choice 4")
                .callbackData("4")
                .build();


        inlineKeyboardButtonList1.add(inlineKeyboardButton1);
        inlineKeyboardButtonList1.add(inlineKeyboardButton2);
        inlineKeyboardButtonList2.add(inlineKeyboardButton3);
        inlineKeyboardButtonList2.add(inlineKeyboardButton4);
        inlineButtons.add(inlineKeyboardButtonList1);
        inlineButtons.add(inlineKeyboardButtonList2);

        return InlineKeyboardMarkup
                .builder()
                .keyboard(inlineButtons)
                .build();
    }

    private void handleCallback(Update update) {
        String callData = update.getCallbackQuery().getData();
        String chatId = String.valueOf(update.getCallbackQuery().getMessage().getChatId());
        System.out.println(callData);
        switch (callData) {
            case "1":
                sendMessage("You chose 1", chatId);
                break;
            case "2":
                sendMessage("You chose 2", chatId);
                break;
            case "3":
                sendMessage("You chose 3", chatId);
                break;
            case "4":
                sendMessage("You chose 4", chatId);
                break;
            default:
                break;
        }
    }

    private void sendMessage(String msg, String chatId) {
        SendMessage sendMessage = new SendMessage(chatId, msg);
        try {
            execute(sendMessage);
        } catch (TelegramApiException e) {
            throw new RuntimeException(e);
        }
    }
}

我希望我的代码,它可以帮助您改进代码并处理错误。请阅读这个简短的博客教程并尝试,您将了解有关该框架的更多信息。

评论

0赞 Ahmed Mohamed 7/8/2023
谢谢。我已经使用 v6.7.0 了。我认为额外的检查将解决我的问题,因为在您回复之前进行了一些尝试后,我发现将调用作为第一行功能工作正常,我不知道为什么。我想现在我知道为什么了。再次感谢您的考虑。update.hasMessage()handleCallback(update)onUpdateReceived(Update update)