skip to Main Content

I’m writing a Java bot with https://github.com/rubenlagus/TelegramBots, and I have a problem, when I click inline keyboard button, this little clock:
clock appears and after some time it says that my bot is not responding. My bot is actually fully functional except this one thing. Here is how I receive callbacks:

@Override
public void onUpdateReceived(Update update) {
    var messagesToSend = updateReceiver.handle(update);

    if (messagesToSend != null && !messagesToSend.isEmpty()) {

        messagesToSend.forEach(response -> {
            if (response instanceof SendMessage) {
                try {
                    execute((SendMessage) response);
                } catch (TelegramApiException e) {
                    e.printStackTrace();
                }
            } else if (response instanceof SendPhoto) {
                try {
                    execute((SendPhoto) response);
                } catch (TelegramApiException e) {
                    e.printStackTrace();
                }
            } else if (response instanceof FileSaveRequest) {
                FileSaveRequest request = (FileSaveRequest) response;
                try {
                    saveFile(request);
                } catch (TelegramApiException | IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}

=
This is only part of the whole code

 } else if (update.hasCallbackQuery()) {
            final CallbackQuery callbackQuery = update.getCallbackQuery();
            final long chatId = callbackQuery.getFrom().getId();
            final User user = userRepository.findById(chatId)
                    .orElseGet(() -> userRepository.save(new User(chatId)));

            AnswerCallbackQuery acceptCallback = new AnswerCallbackQuery();
            acceptCallback.setShowAlert(false);
            acceptCallback.setCallbackQueryId(String.valueOf(update.getCallbackQuery().getId()));
            acceptCallback.setCacheTime(1000);
            List<PartialBotApiMethod<? extends Serializable>> resultList =
                    new ArrayList<>(
                            getHandlerByCallBackQuery(callbackQuery.getData())
                            .handle(user, callbackQuery.getData()));
            resultList.add(acceptCallback);
            return resultList;
        }

As you can see, I still attach AnswerCallbackQuery but it still doesent work. What’s wrong?

2

Answers


  1. I just already solve that issue. It’s not problem on Library but it could error in some exceptions.

    var messagesToSend = updateReceiver.handle(update);
    if (messagesToSend != null && !messagesToSend.isEmpty()) {
    

    I dont have full your code but I think there’s some confused written and happen exception before if (update.callbackQuery())

    Here is my sample:

    @Override
        public void onUpdateReceived(Update update) {
            // I have error, cannot getCallbackQuery because of print which call method getMessage.getText() is null -> happen exception error on the println
            // -> System.out.println(update.getMessage.getText());
            if (update.hasMessage() && !update.getMessage().getText().isEmpty()) {
                String chat_id = update.getMessage().getChatId().toString();
                if (update.getMessage().getText().equals("/start")) {
                    SendMessage sendMessage = new SendMessage();
                    sendMessage.setText("Here is option:");
                    sendMessage.setChatId(chat_id);
                    sendMessage.setParseMode(ParseMode.MARKDOWN);
    
                    InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
                    List<List<InlineKeyboardButton>> listInlineButton = new ArrayList<>();
                    List<InlineKeyboardButton> reportSaleBtn = new ArrayList<>();
                    List<InlineKeyboardButton> reportBuyBtn = new ArrayList<>();
                    List<InlineKeyboardButton> reportPriceBtn = new ArrayList<>();
                    InlineKeyboardButton saleBtn = new InlineKeyboardButton();
                    InlineKeyboardButton buyBtn = new InlineKeyboardButton();
                    InlineKeyboardButton priceBtn = new InlineKeyboardButton();
    
                    saleBtn.setText(Constant.SALE_REPORT_TEXT);
                    saleBtn.setCallbackData(Constant.SALE_REPORT);
    
                    buyBtn.setText(Constant.BUY_REPORT_TEXT);
                    buyBtn.setCallbackData(Constant.BUY_REPORT);
    
                    priceBtn.setText(Constant.PRICE_TEXT);
                    priceBtn.setCallbackData(Constant.PRICE_REPORT);
    
                    reportSaleBtn.add(saleBtn);
                    reportBuyBtn.add(buyBtn);
                    reportPriceBtn.add(priceBtn);
                    listInlineButton.add(reportSaleBtn);
                    listInlineButton.add(reportBuyBtn);
                    listInlineButton.add(reportPriceBtn);
                    inlineKeyboardMarkup.setKeyboard(listInlineButton);
                    sendMessage.setReplyMarkup(inlineKeyboardMarkup);
                    try {
                        execute(sendMessage);
                    } catch (TelegramApiException e) {
                        e.printStackTrace();
                    }
                }
          }
          else if (update.hasCallbackQuery()) {
                CallbackQuery callbackQuery = update.getCallbackQuery();
                String data = callbackQuery.getData();
                String chat_id = callbackQuery.getMessage().getChat().getId().toString();
    
                SendChatAction sendChatAction = new SendChatAction();
                if (data.equals(Constant.SALE_REPORT)) {
                    sendChatAction.setChatId(chat_id);
                    SendMessage sendMessage = new SendMessage();
                    sendMessage.setText("Generating report, please wait!");
                    sendMessage.setChatId(chat_id);
                    try {
                        sendChatAction.setAction(ActionType.TYPING);
                        execute(sendChatAction);
                        execute(sendMessage);
                    } catch (TelegramApiException e) {
                        e.printStackTrace();
                    }
          }
    }
    

    Why it got an error. Click we click on /start Bot will display all inlinekeyboard.
    In the button you only setText() and setCallbackData(). So update.GetMessage() is null.
    In while update.getMessage().getText() is null cannot print out. So it is error and it skip the else if (update.hasCallbackQuery()) {…}

    I think you can check again your code below:

    @Override
    public void onUpdateReceived(Update update) {
     //check carefully before if may there's exception error before if
    }
    

    I hope what I explain may solve your problems too.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search