From aed6212ff1217ed644b54fa3f983fbf3388f13c7 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Sat, 6 Nov 2021 20:43:56 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8D=95log=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=EF=BC=8C=E5=AE=8C=E6=88=90tg=E8=BF=9E=E7=BA=BF=EF=BC=8C?= =?UTF-8?q?=E5=93=8D=E5=BA=94=E5=9C=A8=E7=BA=BF=E5=92=8C=E9=80=80=E5=87=BA?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=EF=BC=8C=E9=80=80=E5=87=BA=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E7=9A=84=E7=AE=80=E5=8D=95=E8=BA=AB=E4=BB=BD=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 6 +- .../java/cc/sukazyo/cono/morny/Logger.java | 25 +++++++ .../cc/sukazyo/cono/morny/MornyCoeur.java | 49 ++++++++++++++ .../cc/sukazyo/cono/morny/MornyTrusted.java | 20 ++++++ .../cono/morny/bot/OnCommandExecute.java | 67 +++++++++++++++++++ .../cc/sukazyo/cono/morny/bot/OnUpdate.java | 21 ++++++ 6 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/Logger.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/OnCommandExecute.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/OnUpdate.java diff --git a/build.gradle b/build.gradle index d05aea8..6c7424a 100644 --- a/build.gradle +++ b/build.gradle @@ -3,15 +3,19 @@ plugins { } group 'cc.sukazyo' -version '1.0-dev' +version '0.1' repositories { mavenCentral() } dependencies { + + implementation 'com.github.pengrad:java-telegram-bot-api:5.3.0' + testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' + } test { diff --git a/src/main/java/cc/sukazyo/cono/morny/Logger.java b/src/main/java/cc/sukazyo/cono/morny/Logger.java new file mode 100644 index 0000000..7df971b --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/Logger.java @@ -0,0 +1,25 @@ +package cc.sukazyo.cono.morny; + +public class Logger { + + public static final Logger logger = new Logger(); + + public void info(String message) { + System.out.println( + "[INFO]" + + message.replaceAll("\\n", "\n[INFO]") + ); + } + + public void warn (String message) { + waring(message); + } + + public void waring (String message) { + System.out.println( + "[WARN]" + + message.replaceAll("\\n", "\n[WARN]") + ); + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java new file mode 100644 index 0000000..18db6d8 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -0,0 +1,49 @@ +package cc.sukazyo.cono.morny; + +import cc.sukazyo.cono.morny.bot.OnUpdate; +import com.pengrad.telegrambot.TelegramBot; +import com.pengrad.telegrambot.request.GetMe; + +import static cc.sukazyo.cono.morny.Logger.logger; + +public class MornyCoeur { + + private static TelegramBot account; + + public static void main (String[] args) { + + logger.info("System Starting"); + + logger.info("args key: " + args[0]); + + account = login(args[0]); + + logger.info("Bot login succeed."); + + account.setUpdatesListener(OnUpdate::onNormalUpdate); + + logger.info("System start complete"); + + } + + public static TelegramBot login (String key) { + TelegramBot account = new TelegramBot(key); + logger.info("Trying to login..."); + for (int i = 1; i < 4; i++) { + if (i != 1) logger.info("retrying..."); + try { + logger.info("Succeed login to @" + account.execute(new GetMe()).user().username()); + return account; + } catch (Exception e) { + e.printStackTrace(System.out); + logger.info("login failed."); + } + } + throw new RuntimeException("Login failed.."); + } + + public static TelegramBot getAccount () { + return account; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java b/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java new file mode 100644 index 0000000..a1bd6d5 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/MornyTrusted.java @@ -0,0 +1,20 @@ +package cc.sukazyo.cono.morny; + +import com.pengrad.telegrambot.model.ChatMember; +import com.pengrad.telegrambot.request.GetChatMember; + +public class MornyTrusted { + + public static final long TRUSTED_CHAT_ID = -1001541451710L; + + public static boolean isTrusted (long userId) { + ChatMember chatMember = MornyCoeur.getAccount().execute(new GetChatMember(TRUSTED_CHAT_ID, userId)).chatMember(); + return ( + chatMember != null && ( + chatMember.status() == ChatMember.Status.administrator || + chatMember.status() == ChatMember.Status.creator + ) + ); + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/OnCommandExecute.java b/src/main/java/cc/sukazyo/cono/morny/bot/OnCommandExecute.java new file mode 100644 index 0000000..8ca897b --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/OnCommandExecute.java @@ -0,0 +1,67 @@ +package cc.sukazyo.cono.morny.bot; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.MornyTrusted; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.request.SendSticker; + +import static cc.sukazyo.cono.morny.Logger.logger; + +public class OnCommandExecute { + + private static final String ONLINE_STATUS_RETURN_STICKER_ID = "CAACAgEAAx0CW-CvvgAC5eBhhhODGRuu0pxKLwoQ3yMsowjviAACcycAAnj8xgVVU666si1utiIE"; + private static final String HELLO_STICKER_ID = "CAACAgEAAxkBAAMnYYYWKNXO4ibo9dlsmDctHhhV6fIAAqooAAJ4_MYFJJhrHS74xUAiBA"; + private static final String EXIT_STICKER_ID = "CAACAgEAAxkBAAMoYYYWt8UjvP0N405SAyvg2SQZmokAAkMiAAJ4_MYFw6yZLu06b-MiBA"; + private static final String EXIT_403_STICKER_ID = "CAACAgEAAxkBAAMqYYYa_7hpXH6hMOYMX4Nh8AVYd74AAnQnAAJ4_MYFRdmmsQKLDZgiBA"; + + public static void searchForCommands (Update event) { + switch (event.message().text()) { + case "/o": + onCommandOnExec(event); + break; + case "/hi": + case "/hello": + onCommandHelloExec(event); + break; + case "/exit": + onCommandExitExec(event); + break; + default: + } + } + + private static void onCommandOnExec (Update event) { + MornyCoeur.getAccount().execute(new SendSticker( + event.message().chat().id(), + ONLINE_STATUS_RETURN_STICKER_ID + ).replyToMessageId(event.message().messageId()) + ); + } + + private static void onCommandHelloExec (Update event) { + MornyCoeur.getAccount().execute(new SendSticker( + event.message().chat().id(), + HELLO_STICKER_ID + ).replyToMessageId(event.message().messageId()) + ); + } + + private static void onCommandExitExec (Update event) { + if (MornyTrusted.isTrusted(event.message().from().id())) { + MornyCoeur.getAccount().execute(new SendSticker( + event.message().chat().id(), + EXIT_STICKER_ID + ).replyToMessageId(event.message().messageId()) + ); + logger.info("Morny exited by user @" + event.message().from().username()); + System.exit(0); + } else { + MornyCoeur.getAccount().execute(new SendSticker( + event.message().chat().id(), + EXIT_403_STICKER_ID + ).replyToMessageId(event.message().messageId()) + ); + } + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/OnUpdate.java b/src/main/java/cc/sukazyo/cono/morny/bot/OnUpdate.java new file mode 100644 index 0000000..3dfb108 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/OnUpdate.java @@ -0,0 +1,21 @@ +package cc.sukazyo.cono.morny.bot; + +import com.pengrad.telegrambot.UpdatesListener; +import com.pengrad.telegrambot.model.Update; + +import java.util.List; + +public class OnUpdate { + + public static int onNormalUpdate (List updates) { + for (Update update : updates) { + if (update.message() != null) { + if (update.message().text() != null) { + new Thread(() -> OnCommandExecute.searchForCommands(update)).start(); + } + } + } + return UpdatesListener.CONFIRMED_UPDATES_ALL; + } + +}