Ошибка компеляции setType в Vk api
Пишу бота на Java с vk api. Сам код:
TransportClient transportClient = new HttpTransportClient();
VkApiClient vk = new VkApiClient(transportClient);
Random random = new Random();
Keyboard keyboard = new Keyboard();
List<List<KeyboardButton>> allKey = new ArrayList<>();
List<Keyboard> 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));
allKey.add(line1);
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<Message> 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);
}
}
}
Ошибку выдаёт такую
'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)'
Ответы (1 шт):
Автор решения: NyashMyash99
→ Ссылка
Так вам же программа разработки и говорит, в чём именно проблема - неправильный тип данных используете. Если вам не указывает на ошибку сразу, а только после попытки сборки - скачайте нормальную IDE, например IntelliJ IDEA от JetBrains, версия Community у них абсолютно бесплатная.
Теперь по ошибкам:
- Лист
line1у вас почему-то имеет типKeyboard, хотя вы пытаетесь добавить тудаKeyboardButton. setTypeуKeyboardButtonActionимеет типTemplateActionTypeNames, как и было указано в ошибке.
Тест работы приложения с этой частью кода: https://i.imgur.com/g7HG7MM.png
List<KeyboardButton> line1 = new ArrayList<>();
line1.add(
new KeyboardButton()
.setAction(
new KeyboardButtonAction()
.setLabel("Привет")
.setType(TemplateActionTypeNames.TEXT)
)
.setColor(KeyboardButtonColor.POSITIVE)
);