Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenlagus committed Mar 14, 2024
1 parent 2df5f3e commit 0952b27
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 57 deletions.
4 changes: 2 additions & 2 deletions Writerside/topics/Lesson-1.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Now, when you are in the project, create files `MyAmazingBot.java` and `Main.jav
```java
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
import org.telegram.telegrambots.exceptions.TelegramApiException;

public class MyAmazingBot implements LongPollingSingleThreadUpdateConsumer {
Expand Down Expand Up @@ -162,7 +162,7 @@ public class Main {
```java
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
import org.telegram.telegrambots.exceptions.TelegramApiException;

public class MyAmazingBot implements LongPollingSingleThreadUpdateConsumer {
Expand Down
4 changes: 2 additions & 2 deletions Writerside/topics/Lesson-2.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ This code will register our bot print "PhotoBot successfully started!" when it i
```java
import org.telegram.telegrambots.api.methods.send.SendMessage;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
import org.telegram.telegrambots.exceptions.TelegramApiException;

public class PhotoBot implements LongPollingSingleThreadUpdateConsumer {
Expand Down Expand Up @@ -295,7 +295,7 @@ import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboardMarkup;
import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboardRemove;
import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardRow;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.longpolling.util.LongPollingSingleThreadUpdateConsumer;
import org.telegram.telegrambots.exceptions.TelegramApiException;

import java.util.ArrayList;
Expand Down
92 changes: 39 additions & 53 deletions Writerside/topics/Lesson-3.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# 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!
Expand All @@ -10,39 +9,30 @@ As always, open `IntelliJ Idea` and create new project. Within the `src` folder
> src/LoggingTestBot.java
```java
public class LoggingTestBot extends TelegramLongPollingBot {
public class LoggingTestBot implements LongPollingSingleThreadUpdateConsumer {
private TelegramClient telegramClient = new OkHttpTelegramClient("YOUR_BOT_TOKEN");

@Override
public void onUpdateReceived(Update update) {
public void consume(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);
SendMessage message = SendMessage // Create a message object object
.builder()
.chatId(chat_id)
.text(message_text)
.build()
try {
execute(message); // Sending our message object to user
telegramClient.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";
}
}
```

Expand All @@ -53,15 +43,9 @@ And our startup file:
```java
public class Main {
public static void main(String[] args) {
// Initialize Api Context
ApiContextInitializer.init();

// Instantiate Telegram Bots API
TelegramBotsApi botsApi = new TelegramBotsApi();

// Register our bot
try {
botsApi.registerBot(new LoggingTestBot());
TelegramBotsLongPollingApplication botsApplication = new TelegramBotsLongPollingApplication();
botsApplication.registerBot(new LoggingTestBot());
} catch (TelegramApiException e) {
e.printStackTrace();
}
Expand All @@ -75,7 +59,7 @@ public class Main {
Lets set additional variables for logging:

```java
public void onUpdateReceived(Update update) {
public void consume(Update update) {
// We check if the update has a message and the message has text
if (update.hasMessage() && update.getMessage().hasText()) {
// Set variables
Expand All @@ -86,11 +70,13 @@ public void onUpdateReceived(Update update) {
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);
SendMessage message = SendMessage // Create a message object object
.builder()
.chatId(chat_id)
.text(message_text)
.build()
try {
execute(message); // Sending our message object to user
telegramClient.execute(message); // Sending our message object to user
} catch (TelegramApiException e) {
e.printStackTrace();
}
Expand All @@ -100,7 +86,7 @@ public void onUpdateReceived(Update update) {

Create `logging` function:

> Dont forget to import:
> Don't forget to import:
>
> ```java
> import java.text.DateFormat;
Expand Down Expand Up @@ -135,12 +121,14 @@ public void onUpdateReceived(Update update) {
String message_text = update.getMessage().getText();
long chat_id = update.getMessage().getChatId();
String answer = message_text;
SendMessage message = new SendMessage() // Create a message object object
.setChatId(chat_id)
.setText(answer);
SendMessage message = SendMessage // Create a message object object
.builder()
.chatId(chat_id)
.text(message_text)
.build()
log(user_first_name, user_last_name, Long.toString(user_id), message_text, answer);
try {
execute(message); // Sending our message object to user
telegramClient.execute(message); // Sending our message object to user
} catch (TelegramApiException e) {
e.printStackTrace();
}
Expand All @@ -159,15 +147,9 @@ import org.telegram.telegrambots.exceptions.TelegramApiException;

public class Main {
public static void main(String[] args) {
// Initialize Api Context
ApiContextInitializer.init();

// Instantiate Telegram Bots API
TelegramBotsApi botsApi = new TelegramBotsApi();

// Register our bot
try {
botsApi.registerBot(new LoggingTestBot());
TelegramBotsLongPollingApplication botsApplication = new TelegramBotsLongPollingApplication();
botsApplication.registerBot(new PhotoBot());
} catch (TelegramApiException e) {
e.printStackTrace();
}
Expand All @@ -189,8 +171,10 @@ import java.text.SimpleDateFormat;
import java.util.Date;

public class LoggingTestBot extends TelegramLongPollingBot {
private TelegramClient telegramClient = new OkHttpTelegramClient("YOUR_BOT_TOKEN");

@Override
public void onUpdateReceived(Update update) {
public void consume(Update update) {

// We check if the update has a message and the message has text
if (update.hasMessage() && update.getMessage().hasText()) {
Expand All @@ -202,12 +186,14 @@ public class LoggingTestBot extends TelegramLongPollingBot {
String message_text = update.getMessage().getText();
long chat_id = update.getMessage().getChatId();
String answer = message_text;
SendMessage message = new SendMessage() // Create a message object object
.setChatId(chat_id)
.setText(answer);
SendMessage message = SendMessage // Create a message object object
.builder()
.chatId(chat_id)
.text(message_text)
.build()
log(user_first_name, user_last_name, Long.toString(user_id), message_text, answer);
try {
execute(message); // Sending our message object to user
telegramClient.execute(message); // Sending our message object to user
} catch (TelegramApiException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -241,11 +227,11 @@ public class LoggingTestBot extends TelegramLongPollingBot {

You can also find all sources at [GitHub repository](https://github.com/MonsterDeveloper/java-telegram-bot-tutorial/).

Now it will do ~~ugly~~ log for us:\)
Now it will do ~~ugly~~ log for us.

![Beautiful Logging 1](https://github.com/MonsterDeveloper/java-telegram-bot-tutorial/raw/master/media/Bot_Logging_1.png)

![Beautiful Logging 2](https://github.com/MonsterDeveloper/java-telegram-bot-tutorial/raw/master/media/Bot_Logging_2.png)

Well, that's all for now. In the next lesson we will learn how to make your messages more beautiful with [unicode emojis:\)](https://en.wikipedia.org/wiki/Emoji).
Well, that's all for now. In the next lesson we will learn how to make your messages more beautiful with [unicode emojis](https://en.wikipedia.org/wiki/Emoji).

0 comments on commit 0952b27

Please sign in to comment.