Lesson 3. Logging

Good afternoon everyone! Did you look into console? Kinda empty ya? Now, we want to see something, isn't it? Let's make a logging function!

Creating project

As always, open IntelliJ Idea and create new project. Within the src folder create 2 files: Main.java and LoggingTestBot.java. Let's create a body of our bot:

src/LoggingTestBot.java

public class LoggingTestBot extends TelegramLongPollingBot {
    @Override
    public void onUpdateReceived(Update update) {

        // We check if the update has a message and the message has text
        if (update.hasMessage() && update.getMessage().hasText()) {
            // Set variables
            String message_text = update.getMessage().getText();
            long chat_id = update.getMessage().getChatId();

            SendMessage message = new SendMessage() // Create a message object object
                .setChatId(chat_id)
                .setText(message_text);
            try {
                execute(message); // Sending our message object to user
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public String getBotUsername() {
        // Return bot username
        // If bot username is @MyAmazingBot, it must return 'MyAmazingBot'
        return "LoggingTestBot";
    }

    @Override
    public String getBotToken() {
        // Return bot token from BotFather
        return "12345:qwertyuiopASDGFHKMK";
    }
}

And our startup file:

src/Main.java

Logs, where are you?

Lets set additional variables for logging:

Create logging function:

Dont forget to import:

Add new private function:

Now we need just call this function when we want to log

Our files:

src/Main.java

src/LoggingTestBot.java

You can also find all sources at GitHub repository.

Now it will do ugly log for us:)

Beautiful Logging 1
Beautiful Logging 2

Well, thats all for now. In the next lesson we will learn how to make your messages more beautiful with unicode emojis:).

Last updated

Was this helpful?