K
March 5, 2022, 10:34 p.m.

Vk api на java. Как сделать кнопки

Java, VK, API

Вот код:

public class Bot {
public static void main(String[] args) throws ClientException, ApiException, InterruptedException {
TransportClient transportClient = new HttpTransportClient();
VkApiClient vk = new VkApiClient(transportClient);
Random random = new Random();
Keyboard keyboard = new Keyboard();
List > allKey = new ArrayList<>();
List line1 = new ArrayList<>();
line1.add(new KeyboardButton()).setAction(new KeyboardButtonAction().setLabel("Привет").setType(KeyboardButtonActionType.TEXT).setColor(KeyboardButtonColor.POSITIVE));
line1.add(new KeyboardButton()).setAction(new KeyboardButtonAction().setLabel("Кто я?").setType(KeyboardButtonActionType.TEXT).setColor(KeyboardButtonColor.POSITIVE));
keyboard.setButtons(allKey);
GroupActor actor = new GroupActor(211045078, "Key");
Integer ts = vk.messages().getLongPollServer(actor).execute().getTs();
while(true){
MessagesGetLongPollHistoryQuery historyQuery = vk.messages().getLongPollHistory(actor).ts(ts);
List messages = historyQuery.execute().getMessages().getItems();
if(!messages.isEmpty()){
messages.forEach(message -> {
System.out.println(message.toString());
try {
if (message.getText().equals("Привет")){
vk.messages().send(actor).message("Привет").userId(message.getFromId()).randomId(random.nextInt(10000)).execute();
}
else if (message.getText().equals("Кто я?")) {
vk.messages().send(actor).message("Ты хороший человек.").userId(message.getFromId()).randomId(random.nextInt(10000)).execute();
}
else if (message.getText().equals("Кнопки")){
vk.messages().send(actor).message("А вот и они").userId(message.getFromId()).randomId(random.nextInt(10000)).keyboard(keyboard).execute();
}
else {
vk.messages().send(actor).message("Я тебя не понял").userId(message.getFromId()).randomId(random.nextInt(10000)).execute();
}
}catch (ApiException | ClientException e) {e.printStackTrace();}
});
}
ts = vk.messages().getLongPollServer(actor).execute().getTs();
Thread.sleep(500);
}
}
}

И на строке---
line1.add(new KeyboardButton()).setAction(new KeyboardButtonAction().setLabel("Привет").setType(KeyboardButtonActionType.TEXT).setColor(KeyboardButtonColor.POSITIVE));

Выдаёт ошибку 'setType(com.vk.api.sdk.objects.messages.TemplateActionTypeNames)' in 'com.vk.api.sdk.objects.messages.KeyboardButtonAction' cannot be applied to '(com.vk.api.sdk.objects.messages.KeyboardButtonActionType)'
Как это решить? Для java докуменация просто идиотия:|

2

Do you like it? Share on social networks!

1
АМ
  • March 24, 2022, 12:12 p.m.

Првиет
line1.add(new KeyboardButton().setAction(new KeyboardButtonAction().setLabel("Текст кнопки").setType(TemplateActionTypeNames.TEXT)).setColor(KeyboardButtonColor.POSITIVE));

А вообще там градл уже другой

plugins {
id 'java'
id 'application'
}
apply plugin: 'java'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

repositories {
mavenCentral()
}

mainClassName = 'class.name'

dependencies {
implementation 'com.vk.api:sdk:1.0.7'
}

    Comments

    Only authorized users can post comments.
    Please, Log in or Sign up
    • Last comments
    • Evgenii Legotckoi
      March 9, 2025, 9:02 p.m.
      К сожалению, я этого подсказать не могу, поскольку у меня нет необходимости в обходе блокировок и т.д. Поэтому я и не задавался решением этой проблемы. Ну выглядит так, что вам действитель…
    • VP
      March 9, 2025, 4:14 p.m.
      Здравствуйте! Я устанавливал Qt6 из исходников а также Qt Creator по отдельности. Все компоненты, связанные с разработкой для Android, установлены. Кроме одного... Когда пытаюсь скомпилиров…
    • ИМ
      Nov. 22, 2024, 9:51 p.m.
      Добрый вечер Евгений! Я сделал себе авторизацию аналогичную вашей, все работает, кроме возврата к предидущей странице. Редеректит всегда на главную, хотя в логах сервера вижу запросы на правильн…
    • Evgenii Legotckoi
      Oct. 31, 2024, 11:37 p.m.
      Добрый день. Да, можно. Либо через такие же плагины, либо с постобработкой через python библиотеку Beautiful Soup
    • A
      Oct. 19, 2024, 5:19 p.m.
      Подскажите как это запустить? Я не шарю в программировании и кодинге. Скачал и установаил Qt, но куча ошибок выдается и не запустить. А очень надо fb3 переконвертировать в html