O
O
okeyndell2022-03-22 23:45:47
Java
okeyndell, 2022-03-22 23:45:47

How to check button click in JAVA Longpoll API (yvasiliev)?

Hello , I have a question.
When using Java VK Bots Long Poll API from yvasiliev, I can't figure out how to process inline button clicks. When I try, it throws an error and closes.
I can’t attach the code, except perhaps my own implementation of the buttons.

public class Bot extends LongPollBot {

    private final String token;

    public Bot(String token) {
        this.token = token;
    }

    @SneakyThrows
    @Override
    public void onMessageNew(MessageNew messageNew) {
        Message message = messageNew.getMessage();
        if (!message.hasText()) return;
        if (!message.getText().startsWith(Constants.PREFIX)) return;
        if (!Utils.contains(message.getFromId(), Constants.adminList)) return;
        String[] cmd =  message.getText().replaceAll("!", "").split(" ");
        if (cmd.length < 3) {
            vk.messages.send()
                    .setPeerId(message.getPeerId())
                    .setMessage("Вы не указали важные параметры! \n Использование: !send {server} команда")
                    .execute();
            return;
        }

        Send.Response response = vk.messages.send()
                .setPeerId(message.getPeerId())
                .setKeyboard(getKeyboard())
                .execute();

    }

    @Override
    public String getAccessToken() {
        return token;
    }

    @SneakyThrows
    private Keyboard getKeyboard() {
        // button 1
        AtomicReference<JsonObject> payload = new AtomicReference<>(new JsonObject());
        payload.get().addProperty("order", "send");
        payload.get().addProperty("quantity", 2);
        Button sendBTN = new TextButton(Button.Color.POSITIVE, new TextButton.Action(
                "Отправить",
                payload.get()
        ));
        
        // button 2
        payload.set(new JsonObject());
        payload.get().addProperty("order", "cancel");
        Button cancelBTN = new TextButton(Button.Color.NEGATIVE, new TextButton.Action("Отменить", payload.get()));

        var row1 = Collections.singletonList(sendBTN);
        var row2 = Collections.singletonList(cancelBTN);

        return new Keyboard(Collections.unmodifiableList(Arrays.asList(row1, row2))).setInline(true);
    }

    @SneakyThrows
    public static void main(String[] args) {
        Constants.adminList.addAll(Arrays.asList("713491127", "428819897"));
        Constants.serverList.put("default", "127.0.0.1:25565");
        Constants.serverList.put("auth", "127.0.0.1:27756");

        new Bot(Constants.TOKEN).startPolling();
    }
}


Thanks for the help!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question