E
E
etozhesano2021-01-21 21:54:32
Java
etozhesano, 2021-01-21 21:54:32

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.

Bot.java
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;
    }


}


Initially, the problem is in the line:
SendMessage sendMessage = new SendMessage()
            .setChatId(chat_id)
            .setText(Menu.getMessage(update.getMessage().getText()));


Due to the fact that I still didn’t figure out how to pass information between classes and objects normally, I decided to do everything through static (because then you can directly access methods and variables without an object). As a result, the Menu class (where I planned to put all the logic for processing the message) looks like this:
Menu.java
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 "Выберите пункт меню";
}
/* ну и так далее... */



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.

PS I wanted to look at the code on github from someone, but basically I found noodles from if-else.

Therefore, I ask for advice on where I should move and what should be changed. I saw examples on Spring, but now for me core is the maximum.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-01-22
@etozhesano

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.

Alternatively, you can do without spring for now... For DI, you can use Guice or Dagger
https://www.baeldung.com/guice

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.

Well, here you can create a Service interface, and then create an implementation of this service layer.
Further, for example, if statement can be simplified by using switch. And since switch cases can be grouped, it is very convenient and probably more readable.
It would be nice to learn Spring Boot

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question