Answer the question
In order to leave comments, you need to log in
How to do OOP correctly in telegram bot?
Good day to all. Now I'm picking a telegram bot in Java. In the beginning, in order for it all to start, I made noodles from if-else in one class. Then I slowly started refactoring my code and got to the point where I don't know what to do anymore.
The problem is that I can't operate on objects, well, that is. lack of skills. I will show the code and explain along the way how I came to such a life.
public class Bot extends TelegramLongPollingBot {
protected long chat_id;
protected static String lastMessage;
protected static ReplyKeyboardMarkup replyKeyboardMarkup = new ReplyKeyboardMarkup();
@Override
public void onUpdateReceived(Update update) {
chat_id = update.getMessage().getChatId();
SendMessage sendMessage = new SendMessage()
.setChatId(chat_id)
.setText(Menu.getMessage(update.getMessage().getText()));
}
@Override
public String getBotUsername() {
return botUsername;
}
@Override
public String getBotToken() {
return token;
}
}
SendMessage sendMessage = new SendMessage()
.setChatId(chat_id)
.setText(Menu.getMessage(update.getMessage().getText()));
public class Menu extends Bot implements EditKeyboardRow {
static ArrayList<KeyboardRow> keyboard = new ArrayList<>();
static KeyboardRow keyboard1Row = new KeyboardRow();
static KeyboardRow keyboard2Row = new KeyboardRow();
public static String getMessage(String msg) {
replyKeyboardMarkup.setResizeKeyboard(true);
if (msg.equals("Привет") || msg.equals("Меню") || msg.equals("Выйти в главное меню")
|| msg.equals("/start")) {
keyboard.clear();
keyboard1Row.clear();
keyboard2Row.clear();
keyboard1Row.add("Начало");
keyboard1Row.add("Настройки");
keyboard2Row.add("О боте");
keyboard.add(keyboard1Row);
keyboard.add(keyboard2Row);
replyKeyboardMarkup.setKeyboard(keyboard);
return "Выберите пункт меню";
}
/* ну и так далее... */
Answer the question
In order to leave comments, you need to log in
Hello colleague!
I think that the fact that you are writing a telegram bot does not matter here.
I think you need to learn OOP and design patterns.
For example, I would connect a lib for DI and create a service layer that I would already implement in the right places.
I saw examples on Spring, but now for me core is the maximum.
Then I wanted to further improve my code and try to pass the main methods through the interface (we declare what will be implemented, and not how. And then we override it in the class) and then I fell into a trap. Since normal interfaces are not suitable due to static, and a static interface cannot be redefined. And now I am in a stupor and do not know how to get out of the situation correctly. It is definitely necessary to refactor the code in the other direction, but I have no idea where exactly and how to do it. I can clearly see that I am not working with objects, but some kind of functionality, but due to my not high knowledge I can’t find the right solution myself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question