From 34cb679e38f76ace696efd069bb01eac077c452f Mon Sep 17 00:00:00 2001 From: kuzdman Date: Tue, 29 Nov 2022 22:30:20 +0500 Subject: [PATCH 01/12] first release of task 1 --- scr/main/resources/config.properties | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 scr/main/resources/config.properties diff --git a/scr/main/resources/config.properties b/scr/main/resources/config.properties new file mode 100644 index 0000000..ac2eafb --- /dev/null +++ b/scr/main/resources/config.properties @@ -0,0 +1,4 @@ +#tg token +#Thu Dec 01 22:46:06 YEKT 2022 +token=5918832145:AAEJLhWvQt6ZNaoJNv_HNBUATaoK36v4W1Q +botName=CryptoTracker From f4b7c77f35d5dd8f36c37ca843ef30b577a0b1d9 Mon Sep 17 00:00:00 2001 From: kuzdman Date: Wed, 30 Nov 2022 22:01:13 +0500 Subject: [PATCH 02/12] added pom.xml --- .idea/JavaTelegramBot.iml | 9 +++-- .idea/compiler.xml | 16 ++++++++ .idea/inspectionProfiles/Project_Default.xml | 34 +++++++++++++++++ .idea/jarRepositories.xml | 20 ++++++++++ ...Maven__org_telegram_telegrambots_6_1_0.xml | 13 +++++++ .idea/misc.xml | 7 ++++ pom.xml | 37 +++++++++++++++++++ 7 files changed, 133 insertions(+), 3 deletions(-) create mode 100644 .idea/compiler.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/jarRepositories.xml create mode 100644 .idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml create mode 100644 pom.xml diff --git a/.idea/JavaTelegramBot.iml b/.idea/JavaTelegramBot.iml index 9cd0610..e7d2e93 100644 --- a/.idea/JavaTelegramBot.iml +++ b/.idea/JavaTelegramBot.iml @@ -1,11 +1,14 @@ - - - + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..1ad8a71 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..9a51f66 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,34 @@ + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..712ab9d --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml b/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml new file mode 100644 index 0000000..4bf6a87 --- /dev/null +++ b/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 13de180..3120b10 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,5 +1,12 @@ + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..9927a7c --- /dev/null +++ b/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + + ru.oopteam.bot + java-tg-bot + 1.0-SNAPSHOT + + + + + org.telegram + telegrambots + 6.1.0 + + + + + + + + Kuznetsov Dmitry + + + Kiselev Egor + + + + + + 18 + 18 + + + \ No newline at end of file From abc2c720187ff837a7470d82f46ff30da96fb3f4 Mon Sep 17 00:00:00 2001 From: Egor Kiselev Date: Wed, 30 Nov 2022 22:22:17 +0500 Subject: [PATCH 03/12] Fixed .gitignore --- .gitignore | 53 +++++++++++++++++++++++++++++++++----------------- .idea/misc.xml | 3 +++ 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index a1c2a23..94252ad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,23 +1,40 @@ -# Compiled class file -*.class - -# Log file -*.log - -# BlueJ files -*.ctxt - -# Mobile Tools for Java (J2ME) +# Java .mtj.tmp/ - -# Package Files # +*.class *.jar *.war -*.nar *.ear -*.zip -*.tar.gz -*.rar - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +*.nar hs_err_pid* +config.properties +*/test + +# Maven +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +pom.xml.bak +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar + +# Gradle +bin/ +build/ +.gradle +.gradletasknamecache +gradle-app.setting +!gradle-wrapper.jar + +# IntelliJ +out/ +.idea/ +.idea_modules/ +*.iml +*.ipr +*.iws + diff --git a/.idea/misc.xml b/.idea/misc.xml index 3120b10..bd70885 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -10,4 +10,7 @@ + + \ No newline at end of file From 35ca88403e0a9b1b36f90504372cd21d83f8d352 Mon Sep 17 00:00:00 2001 From: Egor Kiselev Date: Thu, 1 Dec 2022 22:59:50 +0500 Subject: [PATCH 04/12] added config.properties --- .gitignore | 2 +- .idea/JavaTelegramBot.iml | 44 ++++++++++++++++++++ .idea/inspectionProfiles/Project_Default.xml | 6 +-- scr/main/java/ru/oopteam/bot/Prop.java | 24 +++++++++++ 4 files changed, 70 insertions(+), 6 deletions(-) create mode 100644 scr/main/java/ru/oopteam/bot/Prop.java diff --git a/.gitignore b/.gitignore index 94252ad..36d2a6e 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,7 @@ *.ear *.nar hs_err_pid* -config.properties +scr/main/resources/config.properties */test # Maven diff --git a/.idea/JavaTelegramBot.iml b/.idea/JavaTelegramBot.iml index e7d2e93..41fd40f 100644 --- a/.idea/JavaTelegramBot.iml +++ b/.idea/JavaTelegramBot.iml @@ -10,5 +10,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 9a51f66..6bc67df 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -1,11 +1,7 @@ \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml index 712ab9d..b96e09e 100644 --- a/.idea/jarRepositories.xml +++ b/.idea/jarRepositories.xml @@ -1,6 +1,11 @@ + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml b/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml index 4bf6a87..08ecf6b 100644 --- a/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml +++ b/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml @@ -2,6 +2,9 @@ + + + diff --git a/pom.xml b/pom.xml index 9927a7c..c30a70e 100644 --- a/pom.xml +++ b/pom.xml @@ -16,6 +16,17 @@ 6.1.0 + + org.slf4j + slf4j-api + 2.0.3 + + + org.slf4j + slf4j-simple + 2.0.3 + + diff --git a/scr/main/java/ru/oopteam/bot/Main.java b/scr/main/java/ru/oopteam/bot/Main.java index 5f588cc..7468047 100644 --- a/scr/main/java/ru/oopteam/bot/Main.java +++ b/scr/main/java/ru/oopteam/bot/Main.java @@ -1,19 +1,21 @@ package ru.oopteam.bot; +import org.telegram.telegrambots.meta.TelegramBotsApi; +import org.telegram.telegrambots.meta.exceptions.TelegramApiException; +import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; import ru.oopteam.bot.readers.*; +import ru.oopteam.bot.tgbot.Bot; import ru.oopteam.bot.writers.*; import ru.oopteam.bot.handlers.*; public class Main{ public static void main(String[] args) { - Reader reader = new ConsoleReader(); - Handler handler = new SimpleHandler(); - Writer writer = new ConsoleWriter(); - while (true) { - Request request = reader.read(); - Response response = handler.handle(request); - writer.write(response); + try { + TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class); + botsApi.registerBot(new Bot()); + } catch (TelegramApiException e) { + e.printStackTrace(); } } diff --git a/scr/main/java/ru/oopteam/bot/Prop.java b/scr/main/java/ru/oopteam/bot/Prop.java deleted file mode 100644 index b7f6541..0000000 --- a/scr/main/java/ru/oopteam/bot/Prop.java +++ /dev/null @@ -1,24 +0,0 @@ -package ru.oopteam.bot; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.Properties; - - -public class Prop { - public static void main(String[] args) throws IOException { - Properties properties = new Properties(); -/** - First Step(creating config.properties); - properties.setProperty("token","test"); - System.out.println(properties.getProperty("token")); - FileOutputStream out = new FileOutputStream("config.properties"); - properties.store(out,"tg token"); -**/ - FileInputStream in = new FileInputStream("scr/main/resources/config.properties"); - properties.load(in); - System.out.println(properties.getProperty("token")); - - } -} \ No newline at end of file diff --git a/scr/main/java/ru/oopteam/bot/tgbot/Bot.java b/scr/main/java/ru/oopteam/bot/tgbot/Bot.java new file mode 100644 index 0000000..be9cacc --- /dev/null +++ b/scr/main/java/ru/oopteam/bot/tgbot/Bot.java @@ -0,0 +1,51 @@ +package ru.oopteam.bot.tgbot; + +import org.telegram.telegrambots.bots.TelegramLongPollingBot; +import org.telegram.telegrambots.meta.api.methods.send.SendMessage; +import org.telegram.telegrambots.meta.api.objects.Update; +import org.telegram.telegrambots.meta.exceptions.TelegramApiException; +import ru.oopteam.bot.writers.Response; +import ru.oopteam.bot.writers.Writer; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Properties; + +public class Bot extends TelegramLongPollingBot implements Writer { + @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()) { + SendMessage message = new SendMessage(); // Create a SendMessage object with mandatory fields + message.setChatId(update.getMessage().getChatId().toString()); + message.setText(update.getMessage().getText()); + + try { + execute(message); // Call method to send the message + } catch (TelegramApiException e) { + e.printStackTrace(); + } + } + } + + @Override + public String getBotUsername() { + return "CryptoTracker"; + } + + @Override + public String getBotToken() { + Properties properties = new Properties(); + try { + properties.load(new FileInputStream("scr/main/resources/config.properties")); + return properties.getProperty("token"); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void write(Response response) { + + } +} diff --git a/scr/main/java/ru/oopteam/bot/writers/ConsoleWriter.java b/scr/main/java/ru/oopteam/bot/writers/ConsoleWriter.java index e8647a4..281751b 100644 --- a/scr/main/java/ru/oopteam/bot/writers/ConsoleWriter.java +++ b/scr/main/java/ru/oopteam/bot/writers/ConsoleWriter.java @@ -1,5 +1,6 @@ package ru.oopteam.bot.writers; +import org.telegram.telegrambots.meta.api.objects.Update; import ru.oopteam.bot.writers.*; public class ConsoleWriter implements Writer @@ -9,4 +10,9 @@ public class ConsoleWriter implements Writer public void write(Response response) { System.out.println(response.getMessage()); } + + @Override + public void onUpdateReceived(Update update) { + + } } diff --git a/scr/main/java/ru/oopteam/bot/writers/Writer.java b/scr/main/java/ru/oopteam/bot/writers/Writer.java index 8f0a8cf..c75aa3a 100644 --- a/scr/main/java/ru/oopteam/bot/writers/Writer.java +++ b/scr/main/java/ru/oopteam/bot/writers/Writer.java @@ -1,7 +1,10 @@ package ru.oopteam.bot.writers; +import org.telegram.telegrambots.meta.api.objects.Update; + public interface Writer { void write(Response response); - + + void onUpdateReceived(Update update); } From ebca975fabb2618c7f65f8dd8135253746312104 Mon Sep 17 00:00:00 2001 From: kuzdman Date: Sat, 3 Dec 2022 13:57:43 +0500 Subject: [PATCH 06/12] telegram echo bot configured correctly --- ...Maven__org_telegram_telegrambots_6_1_0.xml | 2 - pom.xml | 12 ----- scr/main/java/ru/oopteam/bot/Main.java | 16 ++++--- .../oopteam/bot/handlers/SimpleHandler.java | 8 +++- .../ru/oopteam/bot/readers/ConsoleReader.java | 13 ------ .../java/ru/oopteam/bot/readers/Request.java | 8 +++- scr/main/java/ru/oopteam/bot/tgbot/Bot.java | 45 ++++++++++--------- .../java/ru/oopteam/bot/tgbot/Converter.java | 24 ++++++++++ .../ru/oopteam/bot/writers/ConsoleWriter.java | 18 -------- .../java/ru/oopteam/bot/writers/Response.java | 21 ++++++--- 10 files changed, 87 insertions(+), 80 deletions(-) delete mode 100644 scr/main/java/ru/oopteam/bot/readers/ConsoleReader.java create mode 100644 scr/main/java/ru/oopteam/bot/tgbot/Converter.java delete mode 100644 scr/main/java/ru/oopteam/bot/writers/ConsoleWriter.java diff --git a/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml b/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml index 08ecf6b..8824b2e 100644 --- a/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml +++ b/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml @@ -3,8 +3,6 @@ - - diff --git a/pom.xml b/pom.xml index c30a70e..417e728 100644 --- a/pom.xml +++ b/pom.xml @@ -15,18 +15,6 @@ telegrambots 6.1.0 - - - org.slf4j - slf4j-api - 2.0.3 - - - org.slf4j - slf4j-simple - 2.0.3 - - diff --git a/scr/main/java/ru/oopteam/bot/Main.java b/scr/main/java/ru/oopteam/bot/Main.java index 7468047..d5c72dc 100644 --- a/scr/main/java/ru/oopteam/bot/Main.java +++ b/scr/main/java/ru/oopteam/bot/Main.java @@ -3,19 +3,25 @@ import org.telegram.telegrambots.meta.TelegramBotsApi; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; -import ru.oopteam.bot.readers.*; import ru.oopteam.bot.tgbot.Bot; -import ru.oopteam.bot.writers.*; -import ru.oopteam.bot.handlers.*; -public class Main{ +import java.io.FileInputStream; +import java.io.IOException; +import java.util.Properties; + +public class Main { public static void main(String[] args) { + Properties properties = new Properties(); + try { TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class); - botsApi.registerBot(new Bot()); + properties.load(new FileInputStream("scr/main/resources/config.properties")); + botsApi.registerBot(new Bot(properties.getProperty("botName"), properties.getProperty("token"))); } catch (TelegramApiException e) { e.printStackTrace(); + } catch (IOException e) { + throw new RuntimeException(e); } } diff --git a/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java b/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java index 408951d..661747f 100644 --- a/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java +++ b/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java @@ -4,8 +4,12 @@ import ru.oopteam.bot.writers.Response; public class SimpleHandler implements Handler { - @Override + public Response handle(Request request) { - return new Response(request.getMessage()); + if (request.getUserID() == null) { + String requestText = request.getMessage(); + return new Response(requestText); + } + return new Response(request.getMessage(), request.getUserID()); } } diff --git a/scr/main/java/ru/oopteam/bot/readers/ConsoleReader.java b/scr/main/java/ru/oopteam/bot/readers/ConsoleReader.java deleted file mode 100644 index e605732..0000000 --- a/scr/main/java/ru/oopteam/bot/readers/ConsoleReader.java +++ /dev/null @@ -1,13 +0,0 @@ -package ru.oopteam.bot.readers; - -import java.util.Scanner; - -public class ConsoleReader implements Reader { - final Scanner scanner = new Scanner(System.in); - - @Override - public Request read() { - String message = scanner.nextLine(); - return new Request(message); - } -} diff --git a/scr/main/java/ru/oopteam/bot/readers/Request.java b/scr/main/java/ru/oopteam/bot/readers/Request.java index f9a2de6..ede77fc 100644 --- a/scr/main/java/ru/oopteam/bot/readers/Request.java +++ b/scr/main/java/ru/oopteam/bot/readers/Request.java @@ -3,12 +3,18 @@ public class Request { private final String message; + private final Long userID; - public Request(String message){ + public Request(String message, Long userID){ this.message = message; + this.userID = userID; } public String getMessage(){ return message; } + + public Long getUserID() { + return this.userID; + } } diff --git a/scr/main/java/ru/oopteam/bot/tgbot/Bot.java b/scr/main/java/ru/oopteam/bot/tgbot/Bot.java index be9cacc..38cb000 100644 --- a/scr/main/java/ru/oopteam/bot/tgbot/Bot.java +++ b/scr/main/java/ru/oopteam/bot/tgbot/Bot.java @@ -4,44 +4,45 @@ import org.telegram.telegrambots.meta.api.methods.send.SendMessage; import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; +import ru.oopteam.bot.handlers.Handler; +import ru.oopteam.bot.handlers.SimpleHandler; +import ru.oopteam.bot.readers.Request; import ru.oopteam.bot.writers.Response; import ru.oopteam.bot.writers.Writer; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.Properties; public class Bot extends TelegramLongPollingBot implements Writer { + + private final String botName; + private final String token; + private final SimpleHandler handler; + private final Converter converter; + + public Bot(String botName, String token) { + this.botName = botName; + this.token = token; + handler = new SimpleHandler(); + converter = new Converter(); + } + @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()) { - SendMessage message = new SendMessage(); // Create a SendMessage object with mandatory fields - message.setChatId(update.getMessage().getChatId().toString()); - message.setText(update.getMessage().getText()); - - try { - execute(message); // Call method to send the message - } catch (TelegramApiException e) { - e.printStackTrace(); - } + if (update.hasMessage()) { + Request request = converter.convertUpdateToRequest(update); + Response response = handler.handle(request); + write(response); } } + @Override public String getBotUsername() { - return "CryptoTracker"; + return this.botName; } @Override public String getBotToken() { - Properties properties = new Properties(); - try { - properties.load(new FileInputStream("scr/main/resources/config.properties")); - return properties.getProperty("token"); - } catch (IOException e) { - throw new RuntimeException(e); - } + return this.token; } @Override diff --git a/scr/main/java/ru/oopteam/bot/tgbot/Converter.java b/scr/main/java/ru/oopteam/bot/tgbot/Converter.java new file mode 100644 index 0000000..5496784 --- /dev/null +++ b/scr/main/java/ru/oopteam/bot/tgbot/Converter.java @@ -0,0 +1,24 @@ +package ru.oopteam.bot.tgbot; + +import org.telegram.telegrambots.meta.api.methods.BotApiMethod; +import org.telegram.telegrambots.meta.api.methods.send.SendMessage; +import org.telegram.telegrambots.meta.api.objects.Update; +import ru.oopteam.bot.readers.Request; +import ru.oopteam.bot.writers.Response; + +public class Converter { + + public Request convertUpdateToRequest(Update update) { + String message = update.getMessage().getText(); + Long userID = update.getMessage().getFrom().getId(); + return new Request(message, userID); + } + + public BotApiMethod convertResponseToMethod(Response response) { + return SendMessage.builder() + .chatId(response.getUserID().toString()) + .text(response.getAnswer()) + .build(); + } + +} diff --git a/scr/main/java/ru/oopteam/bot/writers/ConsoleWriter.java b/scr/main/java/ru/oopteam/bot/writers/ConsoleWriter.java deleted file mode 100644 index 281751b..0000000 --- a/scr/main/java/ru/oopteam/bot/writers/ConsoleWriter.java +++ /dev/null @@ -1,18 +0,0 @@ -package ru.oopteam.bot.writers; - -import org.telegram.telegrambots.meta.api.objects.Update; -import ru.oopteam.bot.writers.*; - -public class ConsoleWriter implements Writer -{ - - @Override - public void write(Response response) { - System.out.println(response.getMessage()); - } - - @Override - public void onUpdateReceived(Update update) { - - } -} diff --git a/scr/main/java/ru/oopteam/bot/writers/Response.java b/scr/main/java/ru/oopteam/bot/writers/Response.java index c4526a2..2d07e70 100644 --- a/scr/main/java/ru/oopteam/bot/writers/Response.java +++ b/scr/main/java/ru/oopteam/bot/writers/Response.java @@ -1,15 +1,26 @@ package ru.oopteam.bot.writers; public class Response { - private final String message; + private final String answer; + private final Long userID; + // Console bot constructor + public Response(String answer) { + this.answer = answer; + this.userID = null; + } - public Response(String text) { - message = text; + // Telegram bot constructor + public Response(String answer, Long userID) { + this.answer = answer; + this.userID = userID; } + public String getAnswer() { + return answer; + } - public String getMessage() { - return message; + public Long getUserID() { + return userID; } } From a5284963e6e6d3c7dc82ab8240e7dfcfcb3fb462 Mon Sep 17 00:00:00 2001 From: kuzdman Date: Sat, 3 Dec 2022 14:11:50 +0500 Subject: [PATCH 07/12] added usage of convertResponseToMethod --- .idea/misc.xml | 2 +- scr/main/java/ru/oopteam/bot/tgbot/Bot.java | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index bd70885..cbafb56 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -7,7 +7,7 @@ - + diff --git a/scr/main/java/ru/oopteam/bot/tgbot/Bot.java b/scr/main/java/ru/oopteam/bot/tgbot/Bot.java index 38cb000..0c4ac42 100644 --- a/scr/main/java/ru/oopteam/bot/tgbot/Bot.java +++ b/scr/main/java/ru/oopteam/bot/tgbot/Bot.java @@ -1,6 +1,7 @@ package ru.oopteam.bot.tgbot; import org.telegram.telegrambots.bots.TelegramLongPollingBot; +import org.telegram.telegrambots.meta.api.methods.BotApiMethod; import org.telegram.telegrambots.meta.api.methods.send.SendMessage; import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.exceptions.TelegramApiException; @@ -47,6 +48,11 @@ public String getBotToken() { @Override public void write(Response response) { - + BotApiMethod sm = converter.convertResponseToMethod(response); + try { + execute(sm); + } catch (TelegramApiException e) { + e.printStackTrace(); + } } } From 1e718745a46e0bfa367bf78fc046d6390d2dd2ac Mon Sep 17 00:00:00 2001 From: Egor Kiselev Date: Sat, 17 Dec 2022 20:59:07 +0500 Subject: [PATCH 08/12] added parsing --- .idea/JavaTelegramBot.iml | 45 +++++++++++++++++++ ...Maven__org_telegram_telegrambots_6_1_0.xml | 1 - .idea/misc.xml | 2 +- pom.xml | 26 +++++++++++ .../java/ru/oopteam/bot/parsing/Data.java | 2 + .../oopteam/bot/parsing/GettingCurrency.java | 40 +++++++++++++++++ 6 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 scr/main/java/ru/oopteam/bot/parsing/Data.java create mode 100644 scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java diff --git a/.idea/JavaTelegramBot.iml b/.idea/JavaTelegramBot.iml index e7d2e93..545041e 100644 --- a/.idea/JavaTelegramBot.iml +++ b/.idea/JavaTelegramBot.iml @@ -10,5 +10,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml b/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml index 8824b2e..4bf6a87 100644 --- a/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml +++ b/.idea/libraries/Maven__org_telegram_telegrambots_6_1_0.xml @@ -2,7 +2,6 @@ - diff --git a/.idea/misc.xml b/.idea/misc.xml index cbafb56..bd70885 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -7,7 +7,7 @@ - + diff --git a/pom.xml b/pom.xml index 417e728..45a7b72 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,32 @@ telegrambots 6.1.0 + + ru.oopteam.bot + java-tg-bot + 1.0-SNAPSHOT + + + + org.slf4j + slf4j-simple + LATEST + test + + + + org.jsoup + jsoup + LATEST + + + + + + org.slf4j + slf4j-api + LATEST + diff --git a/scr/main/java/ru/oopteam/bot/parsing/Data.java b/scr/main/java/ru/oopteam/bot/parsing/Data.java new file mode 100644 index 0000000..486b360 --- /dev/null +++ b/scr/main/java/ru/oopteam/bot/parsing/Data.java @@ -0,0 +1,2 @@ +package ru.oopteam.bot.parsing; + diff --git a/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java b/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java new file mode 100644 index 0000000..19224a3 --- /dev/null +++ b/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java @@ -0,0 +1,40 @@ +package ru.oopteam.bot.parsing; + +import org.jsoup.Jsoup; +import org.jsoup.nodes.Element; +import java.util.HashMap; +import java.util.List; +import java.util.stream.Collectors; + + +public class GettingCurrency { + public static void main(String[] args) { + + try { + + var doc = Jsoup.connect("https://www.binance.com/ru/markets").get(); + var titleElements = doc.getElementsByClass("css-ovtrou"); + var titleElements2 = doc.getElementsByClass("css-1x8dg53"); + + List CryptoName = titleElements2.stream() + .map(Element::text) + .collect(Collectors.toList()); + //Adding Crypto Names to list + List Currency = titleElements.stream() + .map(Element::text) + .collect(Collectors.toList()); + //Adding Crypto Currency to list + + HashMap currencyDictionary = new HashMap<>(); + for (int i = 0; i < CryptoName.size();i++){ + currencyDictionary.put(CryptoName.get(i), Currency.get(i)); + + } + System.out.println(currencyDictionary); + + } + catch (Exception e){ + e.printStackTrace(); + } + } +} \ No newline at end of file From e2250364bddb749e06d82331703a52e3137f5f7d Mon Sep 17 00:00:00 2001 From: Egor Kiselev Date: Sat, 17 Dec 2022 21:24:30 +0500 Subject: [PATCH 09/12] connected exchange rate search to tg --- .../ru/oopteam/bot/handlers/SimpleHandler.java | 17 ++++++++++++++++- .../ru/oopteam/bot/parsing/GettingCurrency.java | 7 ++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java b/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java index 661747f..83e7daa 100644 --- a/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java +++ b/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java @@ -3,13 +3,28 @@ import ru.oopteam.bot.readers.Request; import ru.oopteam.bot.writers.Response; +import java.util.HashMap; + +import static ru.oopteam.bot.parsing.GettingCurrency.getCurrency; + public class SimpleHandler implements Handler { public Response handle(Request request) { if (request.getUserID() == null) { String requestText = request.getMessage(); + System.out.println("QWerty"); return new Response(requestText); + } - return new Response(request.getMessage(), request.getUserID()); + String message = request.getMessage(); + try { + HashMap currencyDictionary = getCurrency(); + assert currencyDictionary != null; + message = currencyDictionary.get(request.getMessage()); + } catch (Exception e) { + throw new RuntimeException(e); + } + + return new Response(message,request.getUserID()); } } diff --git a/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java b/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java index 19224a3..02974ea 100644 --- a/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java +++ b/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java @@ -7,8 +7,8 @@ import java.util.stream.Collectors; -public class GettingCurrency { - public static void main(String[] args) { +public class GettingCurrency{ + public static HashMap getCurrency() { try { @@ -30,11 +30,12 @@ public static void main(String[] args) { currencyDictionary.put(CryptoName.get(i), Currency.get(i)); } - System.out.println(currencyDictionary); + return currencyDictionary; } catch (Exception e){ e.printStackTrace(); } + return null; } } \ No newline at end of file From 5ddb71cb6e6101ef765ca57e07b1521008f1b857 Mon Sep 17 00:00:00 2001 From: kuzdman Date: Mon, 19 Dec 2022 14:08:43 +0500 Subject: [PATCH 10/12] added processing of incorrect input --- .idea/JavaTelegramBot.iml | 4 ++-- scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.idea/JavaTelegramBot.iml b/.idea/JavaTelegramBot.iml index 545041e..774ffb4 100644 --- a/.idea/JavaTelegramBot.iml +++ b/.idea/JavaTelegramBot.iml @@ -52,8 +52,8 @@ - + - + \ No newline at end of file diff --git a/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java b/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java index 83e7daa..3a25e38 100644 --- a/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java +++ b/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java @@ -14,13 +14,15 @@ public Response handle(Request request) { String requestText = request.getMessage(); System.out.println("QWerty"); return new Response(requestText); - } + String message = request.getMessage(); try { HashMap currencyDictionary = getCurrency(); + assert currencyDictionary != null; - message = currencyDictionary.get(request.getMessage()); + message = currencyDictionary.getOrDefault(request.getMessage(), "Не удалось получить заданную валюту"); + } catch (Exception e) { throw new RuntimeException(e); } From 910a516d50272ae7a43bef19a4b1c49ce168d189 Mon Sep 17 00:00:00 2001 From: kuzdman Date: Mon, 19 Dec 2022 15:02:22 +0500 Subject: [PATCH 11/12] implemented reflection --- .idea/JavaTelegramBot.iml | 6 ++- pom.xml | 7 +++- .../ru/oopteam/bot/InitializationManager.java | 29 ++++++++++++++ scr/main/java/ru/oopteam/bot/Main.java | 16 +++++--- .../oopteam/bot/annotations/Initializer.java | 12 ++++++ .../oopteam/bot/parsing/GettingCurrency.java | 39 +++++++++++++------ scr/main/resources/market.properties | 3 ++ 7 files changed, 93 insertions(+), 19 deletions(-) create mode 100644 scr/main/java/ru/oopteam/bot/InitializationManager.java create mode 100644 scr/main/java/ru/oopteam/bot/annotations/Initializer.java create mode 100644 scr/main/resources/market.properties diff --git a/.idea/JavaTelegramBot.iml b/.idea/JavaTelegramBot.iml index 774ffb4..6392d09 100644 --- a/.idea/JavaTelegramBot.iml +++ b/.idea/JavaTelegramBot.iml @@ -5,6 +5,7 @@ + @@ -14,7 +15,6 @@ - @@ -32,7 +32,6 @@ - @@ -54,6 +53,9 @@ + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 45a7b72..f2be31a 100644 --- a/pom.xml +++ b/pom.xml @@ -15,6 +15,7 @@ telegrambots 6.1.0 + ru.oopteam.bot java-tg-bot @@ -34,7 +35,11 @@ LATEST - + + org.reflections + reflections + 0.10.2 + org.slf4j diff --git a/scr/main/java/ru/oopteam/bot/InitializationManager.java b/scr/main/java/ru/oopteam/bot/InitializationManager.java new file mode 100644 index 0000000..11afcda --- /dev/null +++ b/scr/main/java/ru/oopteam/bot/InitializationManager.java @@ -0,0 +1,29 @@ +package ru.oopteam.bot; + +import org.reflections.Reflections; +import ru.oopteam.bot.annotations.Initializer; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Set; + +public class InitializationManager { + public static void initialize(String packageToScan) { + Reflections reflections = new Reflections(packageToScan); + Set> annotated = + reflections.getTypesAnnotatedWith(Initializer.class); + + for (Class annotatedClass : annotated) { + try { + Method method = annotatedClass.getMethod("initialize"); + method.setAccessible(true); + method.invoke(null); + } catch (NoSuchMethodException e) { + System.out.println("Метод не найден в классе: " + annotatedClass.getName()); + } catch (InvocationTargetException | IllegalAccessException ignored) { + } catch (Exception e) { + e.printStackTrace(); + } + } + } +} \ No newline at end of file diff --git a/scr/main/java/ru/oopteam/bot/Main.java b/scr/main/java/ru/oopteam/bot/Main.java index d5c72dc..7351c3a 100644 --- a/scr/main/java/ru/oopteam/bot/Main.java +++ b/scr/main/java/ru/oopteam/bot/Main.java @@ -5,24 +5,30 @@ import org.telegram.telegrambots.updatesreceivers.DefaultBotSession; import ru.oopteam.bot.tgbot.Bot; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; import java.util.Properties; public class Main { public static void main(String[] args) { + InitializationManager.initialize("ru.oopteam.bot"); + Properties properties = new Properties(); try { + URL resource = Main.class.getResource("/config.properties"); + properties.load(new FileInputStream(new File(resource.toURI()))); TelegramBotsApi botsApi = new TelegramBotsApi(DefaultBotSession.class); - properties.load(new FileInputStream("scr/main/resources/config.properties")); botsApi.registerBot(new Bot(properties.getProperty("botName"), properties.getProperty("token"))); - } catch (TelegramApiException e) { - e.printStackTrace(); - } catch (IOException e) { + } catch (IOException | URISyntaxException | TelegramApiException e) { + throw new RuntimeException(e); + } catch (NullPointerException e) { + System.err.println("Файл конфигураций не найден"); throw new RuntimeException(e); } - } } diff --git a/scr/main/java/ru/oopteam/bot/annotations/Initializer.java b/scr/main/java/ru/oopteam/bot/annotations/Initializer.java new file mode 100644 index 0000000..238c0ef --- /dev/null +++ b/scr/main/java/ru/oopteam/bot/annotations/Initializer.java @@ -0,0 +1,12 @@ +package ru.oopteam.bot.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface Initializer { + String info() default ""; +} diff --git a/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java b/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java index 02974ea..b229830 100644 --- a/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java +++ b/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java @@ -2,32 +2,49 @@ import org.jsoup.Jsoup; import org.jsoup.nodes.Element; + +import java.io.FileInputStream; +import java.io.IOException; import java.util.HashMap; import java.util.List; -import java.util.stream.Collectors; +import java.util.Properties; public class GettingCurrency{ + + private static String urlToMarket = "https://www.binance.com/ru/markets"; + private static String currenciesConfig = "css-ovtrou"; + private static String cryptoNamesConfig = "css-1x8dg53"; + + public static void initialize() throws IOException { + Properties properties = new Properties(); + properties.load(new FileInputStream("/market.properties")); + urlToMarket = properties.getProperty("urlToMarket"); + currenciesConfig = properties.getProperty("currencies"); + cryptoNamesConfig = properties.getProperty("cryptoNames"); + } + public static HashMap getCurrency() { try { - var doc = Jsoup.connect("https://www.binance.com/ru/markets").get(); - var titleElements = doc.getElementsByClass("css-ovtrou"); - var titleElements2 = doc.getElementsByClass("css-1x8dg53"); + var doc = Jsoup.connect(urlToMarket).get(); + var currencies = doc.getElementsByClass(currenciesConfig); + var cryptoNames = doc.getElementsByClass(cryptoNamesConfig); - List CryptoName = titleElements2.stream() + List cryptoName = cryptoNames.stream() .map(Element::text) - .collect(Collectors.toList()); + .toList(); + //Adding Crypto Names to list - List Currency = titleElements.stream() + List currency = currencies.stream() .map(Element::text) - .collect(Collectors.toList()); - //Adding Crypto Currency to list + .toList(); + //Adding Crypto Currency to list HashMap currencyDictionary = new HashMap<>(); - for (int i = 0; i < CryptoName.size();i++){ - currencyDictionary.put(CryptoName.get(i), Currency.get(i)); + for (int i = 0; i < cryptoName.size();i++){ + currencyDictionary.put(cryptoName.get(i), currency.get(i)); } return currencyDictionary; diff --git a/scr/main/resources/market.properties b/scr/main/resources/market.properties new file mode 100644 index 0000000..2d3eb66 --- /dev/null +++ b/scr/main/resources/market.properties @@ -0,0 +1,3 @@ +urlToMarket=https://www.binance.com/ru/markets +cryptoNames=css-ovtrou +currencies=css-1x8dg53 \ No newline at end of file From 3824a4a893e4fa9a7610fb1289c2d984fd36be89 Mon Sep 17 00:00:00 2001 From: kuzdman Date: Mon, 19 Dec 2022 15:20:22 +0500 Subject: [PATCH 12/12] added 'all' command --- .../oopteam/bot/handlers/SimpleHandler.java | 21 ++++++++++++++----- .../oopteam/bot/parsing/GettingCurrency.java | 4 ++-- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java b/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java index 3a25e38..c5160a1 100644 --- a/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java +++ b/scr/main/java/ru/oopteam/bot/handlers/SimpleHandler.java @@ -4,6 +4,7 @@ import ru.oopteam.bot.writers.Response; import java.util.HashMap; +import java.util.Objects; import static ru.oopteam.bot.parsing.GettingCurrency.getCurrency; @@ -12,21 +13,31 @@ public class SimpleHandler implements Handler { public Response handle(Request request) { if (request.getUserID() == null) { String requestText = request.getMessage(); - System.out.println("QWerty"); return new Response(requestText); } - String message = request.getMessage(); + StringBuilder message = new StringBuilder(request.getMessage()); + try { HashMap currencyDictionary = getCurrency(); - assert currencyDictionary != null; - message = currencyDictionary.getOrDefault(request.getMessage(), "Не удалось получить заданную валюту"); + + if (Objects.equals(message.toString(), "all")) { + message = new StringBuilder(); + for (HashMap.Entry item : currencyDictionary.entrySet()){ + message.append(item.getKey()); + message.append(" "); + message.append(item.getValue()); + message.append(" \n"); + } + } else { + message = new StringBuilder(currencyDictionary.getOrDefault(request.getMessage(), "Не удалось получить заданную валюту")); + } } catch (Exception e) { throw new RuntimeException(e); } - return new Response(message,request.getUserID()); + return new Response(message.toString(),request.getUserID()); } } diff --git a/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java b/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java index b229830..ac94718 100644 --- a/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java +++ b/scr/main/java/ru/oopteam/bot/parsing/GettingCurrency.java @@ -43,10 +43,10 @@ public static HashMap getCurrency() { //Adding Crypto Currency to list HashMap currencyDictionary = new HashMap<>(); - for (int i = 0; i < cryptoName.size();i++){ + for (int i = 0; i < cryptoName.size(); i++){ currencyDictionary.put(cryptoName.get(i), currency.get(i)); - } + return currencyDictionary; }