Answer the question
In order to leave comments, you need to log in
Can't see InlineKeyboardMarkup. What did I do wrong?
For the first time I am doing InlineKeyboard for Telegram and InlineKeyboard is not displayed, where is my error?
public class Kino extends TelegramLongPollingBot {
public Message message;
public String url;
public String per;
@Override
public void onUpdateReceived(Update update) {
Message message = update.getMessage();
this.message = message;
if (message != null && message.hasText()) {
if (message.getText().equals("/start")) {
try {
kino(update);
} catch (Exception ignored) {
}
}
}
}
@SneakyThrows
public void kino(Update update) {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("https://videocdn.tv/api/short?api_token=&id=6089");
HttpResponse response = httpclient.execute(httpget);
String json = EntityUtils.toString(response.getEntity());
JsonNode jsonNode = new JsonMapper().readTree(json);
for (JsonNode data : jsonNode.get("data")) {
String url = String.valueOf(data.get("iframe_src"));
this.url = url;
String per = String.valueOf(data.get("title"));
this.per = per;
}
try {
SendMessage sms = sendMsg(update.getMessage(), per);
sms.setReplyMarkup(getinlineKeyboardMarkup());
} catch (Exception e) {
System.out.println("Can't send");
}
}
public SendMessage sendMsg(Message message, String text) {
SendMessage sendMessage = new SendMessage();
sendMessage.enableMarkdown(true);
sendMessage.setChatId(message.getChatId().toString());
sendMessage.setText(text);
try {
execute(sendMessage);
} catch (TelegramApiException e) {
e.printStackTrace();
}
return sendMessage;
}
public ReplyKeyboard getinlineKeyboardMarkup() {
InlineKeyboardMarkup inlineKeyboardMarkup = new InlineKeyboardMarkup();
InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton();
inlineKeyboardButton.setText("Смотреть");
//inlineKeyboardButton.setUrl(url);
List<InlineKeyboardButton> keyboardButtonsRow = new ArrayList<>();
keyboardButtonsRow.add(inlineKeyboardButton);
List<List<InlineKeyboardButton>> rowList = new ArrayList<>();
rowList.add(keyboardButtonsRow);
inlineKeyboardMarkup.setKeyboard(rowList);
return inlineKeyboardMarkup;
}
@Override
public String getBotUsername() {
return "";
}
@Override
public String getBotToken() {
return "";
}
}
Answer the question
In order to leave comments, you need to log in
If you first send a message, and only then cling to it with a keyboard, then the keyboard will not be sent.
Right here:
// внутри мы вызваем execute, который отправляет сообщение
SendMessage sms = sendMsg(update.getMessage(), per);
// к уже отправленному объекту сообщения мы цепляем клавиатуру
sms.setReplyMarkup(getinlineKeyboardMarkup());
// дальше с переменной sms мы ничего не делаем
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question