Добрый день всем.
Задался я сделать значит простенького телеграм бота для общего саморазвития, залез в
туториал телеги по-копипастил немного и дошел до момента навигации меню...
Дело в том что я не пойму почему не отрабатывают кнопки, хоть убей.
Если кто может подсказать где, что, куда переставить, подскажите пожалуйста
Код:
package tutorial;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.AnswerCallbackQuery;
import org.telegram.telegrambots.meta.api.methods.CopyMessage;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageReplyMarkup;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import java.util.List;
public class Bot extends TelegramLongPollingBot {
private boolean screaming = false;
private InlineKeyboardMarkup keyboardM1;
private InlineKeyboardMarkup keyboardM2;
@Override
public String getBotUsername() {
return "mybotname";
}
@Override
public String getBotToken() {
return "mytoken";
}
@Override
public void onUpdateReceived(Update update) {
System.out.println(update);
var msg = update.getMessage();
var user = msg.getFrom();
var id = user.getId();
var next = InlineKeyboardButton.builder()
.text("Next").callbackData("next")
.build();
var back = InlineKeyboardButton.builder()
.text("Back").callbackData("back")
.build();
var url = InlineKeyboardButton.builder()
.text("Tutorial")
.url("https://core.telegram.org/bots/api")
.build();
keyboardM1 = InlineKeyboardMarkup.builder()
.keyboardRow(List.of(next)).build();
//Buttons are wrapped in lists since each keyboard is a set of button rows
keyboardM2 = InlineKeyboardMarkup.builder()
.keyboardRow(List.of(back))
.keyboardRow(List.of(url))
.build();
if (msg.isCommand()) {
if (msg.getText().equals("/scream")) //If the command was /scream, we switch gears
screaming = true;
else if (msg.getText().equals("/whisper")) //Otherwise, we return to normal
screaming = false;
else if (msg.getText().equals("/start")) {
sendMenu(user.getId(), "MENU 2", keyboardM2);
}
return; //We don't want to echo commands, so we exit
}
else
if (screaming) //If we are screaming
scream(id, update.getMessage()); //Call a custom method
else
copyMessage(id, msg.getMessageId()); //Else proceed normally
System.out.println(user.getLastName() + " "+ user.getFirstName() + " wrote " + msg.getText());
if (update.hasCallbackQuery()) {
// The update is a callback query (menu button click)
var callbackQuery = update.getCallbackQuery();
var data = callbackQuery.getData();
var msgId = callbackQuery.getMessage().getMessageId();
try {
buttonTap(id, callbackQuery.getId(), data, msgId); //Вызов здесь!!!
} catch (TelegramApiException e) {
throw new RuntimeException(e);
}}
}
private void scream(Long id, Message msg) {
if(msg.hasText())
sendText(id, msg.getText().toUpperCase());
else
copyMessage(id, msg.getMessageId()); //We can't really scream a sticker
}
public void sendText(Long who, String what){
SendMessage sm = SendMessage.builder()
.chatId(who.toString()) //Who are we sending a message to
.text(what).build(); //Message content
try {
execute(sm); //Actually sending the message
} catch (TelegramApiException e) {
throw new RuntimeException(e); //Any error will be printed here
}
}
public void copyMessage(Long who, Integer msgId){
CopyMessage cm = CopyMessage.builder()
.fromChatId(who.toString()) //We copy from the user
.chatId(who.toString()) //And send it back to him
.messageId(msgId) //Specifying what message
.build();
try {
execute(cm);
} catch (TelegramApiException e) {
throw new RuntimeException(e);
}
}
public void sendMenu(Long who, String txt, InlineKeyboardMarkup kb){
SendMessage sm = SendMessage.builder().chatId(who.toString())
.parseMode("HTML").text(txt)
.replyMarkup(kb).build();
try {
execute(sm);
} catch (TelegramApiException e) {
throw new RuntimeException(e);
}
}
private void buttonTap(Long id, String queryId, String data, int msgId) throws TelegramApiException { //Функция здесь!!!
EditMessageText newTxt = EditMessageText.builder()
.chatId(id.toString())
.messageId(msgId).text("").build();
EditMessageReplyMarkup newKb = EditMessageReplyMarkup.builder()
.chatId(id.toString()).messageId(msgId).build();
if(data.equals("next")) {
newTxt.setText("MENU 2");
newKb.setReplyMarkup(keyboardM2);
} else if(data.equals("back")) {
newTxt.setText("MENU 1");
newKb.setReplyMarkup(keyboardM1);
}
AnswerCallbackQuery close = AnswerCallbackQuery.builder()
.callbackQueryId(queryId).build();
execute(close);
execute(newTxt);
execute(newKb);
}
}