diff --git a/build.gradle b/build.gradle index 6c7424a..a4e5f41 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { } group 'cc.sukazyo' -version '0.1' +version '0.1.1' repositories { mavenCentral() diff --git a/src/main/java/cc/sukazyo/cono/morny/Logger.java b/src/main/java/cc/sukazyo/cono/morny/Logger.java index 7df971b..d9b6514 100644 --- a/src/main/java/cc/sukazyo/cono/morny/Logger.java +++ b/src/main/java/cc/sukazyo/cono/morny/Logger.java @@ -5,10 +5,7 @@ public class Logger { public static final Logger logger = new Logger(); public void info(String message) { - System.out.println( - "[INFO]" + - message.replaceAll("\\n", "\n[INFO]") - ); + System.out.println(formatMessage(message, "INFO")); } public void warn (String message) { @@ -16,9 +13,17 @@ public class Logger { } public void waring (String message) { - System.out.println( - "[WARN]" + - message.replaceAll("\\n", "\n[WARN]") + System.out.println(formatMessage(message, "WARN")); + } + + private String formatMessage (String message, String level) { + String levelStr = "\n["+level+"]"; + return String.format( + "[%d][%s][%s]%s", + System.currentTimeMillis(), + Thread.currentThread().getName(), + level, + message.replaceAll("\\n", levelStr) ); } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index 18db6d8..20e3c34 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -1,6 +1,7 @@ package cc.sukazyo.cono.morny; -import cc.sukazyo.cono.morny.bot.OnUpdate; +import cc.sukazyo.cono.morny.bot.api.OnUpdate; +import cc.sukazyo.cono.morny.bot.event.EventListeners; import com.pengrad.telegrambot.TelegramBot; import com.pengrad.telegrambot.request.GetMe; @@ -20,6 +21,7 @@ public class MornyCoeur { logger.info("Bot login succeed."); + EventListeners.registerAllListeners(); account.setUpdatesListener(OnUpdate::onNormalUpdate); logger.info("System start complete"); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/OnUpdate.java b/src/main/java/cc/sukazyo/cono/morny/bot/OnUpdate.java deleted file mode 100644 index 3dfb108..0000000 --- a/src/main/java/cc/sukazyo/cono/morny/bot/OnUpdate.java +++ /dev/null @@ -1,21 +0,0 @@ -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; - } - -} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListener.java b/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListener.java new file mode 100644 index 0000000..6c5114d --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListener.java @@ -0,0 +1,60 @@ +package cc.sukazyo.cono.morny.bot.api; + +import com.pengrad.telegrambot.model.Update; + +@SuppressWarnings("unused") +public abstract class EventListener { + + public boolean onMessage (Update update) { + return false; + } + + public boolean onEditedMessage (Update update) { + return false; + } + + public boolean onChannelPost (Update update) { + return false; + } + + public boolean onEditedChannelPost (Update update) { + return false; + } + + public boolean onInlineQuery (Update update) { + return false; + } + + public boolean onChosenInlineResult (Update update) { + return false; + } + + public boolean onCallbackQuery (Update update) { + return false; + } + + public boolean onShippingQuery (Update update) { + return false; + } + + public boolean onPreCheckoutQuery (Update update) { + return false; + } + + public boolean onPoll (Update update) { + return false; + } + + public boolean onPollAnswer (Update update) { + return false; + } + + public boolean onMyChatMemberUpdated (Update update) { + return false; + } + + public boolean onChatMemberUpdated (Update update) { + return false; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java b/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java new file mode 100644 index 0000000..5b1eeba --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/api/EventListenerManager.java @@ -0,0 +1,91 @@ +package cc.sukazyo.cono.morny.bot.api; + +import com.pengrad.telegrambot.model.Update; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.function.Function; + +import static cc.sukazyo.cono.morny.Logger.logger; + +public class EventListenerManager { + + private static final List listeners = new ArrayList<>(); + + private static class EventPublisher extends Thread { + + private final Function exec; + + public EventPublisher(Update update, Function exec) { + this.setName("EVT"+update.updateId()); + this.exec = exec; + } + + @Override + public void run () { + for (EventListener x : listeners) { + if (exec.apply(x)) return; + } + logger.info("event exited undone"); + } + + } + + public static void addListener (EventListener... listeners) { + EventListenerManager.listeners.addAll(Arrays.asList(listeners)); + } + + public static void publishMessageEvent (Update update) { + new EventPublisher(update, x -> x.onMessage(update)).start(); + } + + public static void publishEditedMessageEvent (Update update) { + new EventPublisher(update, x -> x.onEditedMessage(update)).start(); + } + + public static void publishChannelPostEvent (Update update) { + new EventPublisher(update, x -> x.onChannelPost(update)).start(); + } + + public static void publishEditedChannelPostEvent (Update update) { + new EventPublisher(update, x -> x.onEditedChannelPost(update)).start(); + } + + public static void publishInlineQueryEvent (Update update) { + new EventPublisher(update, x -> x.onInlineQuery(update)).start(); + } + + public static void publishChosenInlineResultEvent (Update update) { + new EventPublisher(update, x -> x.onChosenInlineResult(update)).start(); + } + + public static void publishCallbackQueryEvent (Update update) { + new EventPublisher(update, x -> x.onCallbackQuery(update)).start(); + } + + public static void publishShippingQueryEvent (Update update) { + new EventPublisher(update, x -> x.onShippingQuery(update)).start(); + } + + public static void publishPreCheckoutQueryEvent (Update update) { + new EventPublisher(update, x -> x.onPreCheckoutQuery(update)).start(); + } + + public static void publishPollEvent (Update update) { + new EventPublisher(update, x -> x.onPoll(update)).start(); + } + + public static void publishPollAnswerEvent (Update update) { + new EventPublisher(update, x -> x.onPollAnswer(update)).start(); + } + + public static void publishMyChatMemberUpdatedEvent (Update update) { + new EventPublisher(update, x -> x.onMyChatMemberUpdated(update)).start(); + } + + public static void publishChatMemberUpdatedEvent (Update update) { + new EventPublisher(update, x -> x.onChatMemberUpdated(update)).start(); + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/api/OnUpdate.java b/src/main/java/cc/sukazyo/cono/morny/bot/api/OnUpdate.java new file mode 100644 index 0000000..f45671e --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/api/OnUpdate.java @@ -0,0 +1,55 @@ +package cc.sukazyo.cono.morny.bot.api; + +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) { + EventListenerManager.publishMessageEvent(update); + } + if (update.editedMessage() != null) { + EventListenerManager.publishEditedMessageEvent(update); + } + if (update.channelPost() != null) { + EventListenerManager.publishChannelPostEvent(update); + } + if (update.editedChannelPost() != null) { + EventListenerManager.publishEditedChannelPostEvent(update); + } + if (update.inlineQuery() != null) { + EventListenerManager.publishInlineQueryEvent(update); + } + if (update.chosenInlineResult() != null) { + EventListenerManager.publishChosenInlineResultEvent(update); + } + if (update.callbackQuery() != null) { + EventListenerManager.publishCallbackQueryEvent(update); + } + if (update.shippingQuery() != null) { + EventListenerManager.publishShippingQueryEvent(update); + } + if (update.preCheckoutQuery() != null) { + EventListenerManager.publishPreCheckoutQueryEvent(update); + } + if (update.poll() != null) { + EventListenerManager.publishPollEvent(update); + } + if (update.pollAnswer() != null) { + EventListenerManager.publishPollAnswerEvent(update); + } + if (update.myChatMember() != null) { + EventListenerManager.publishMyChatMemberUpdatedEvent(update); + } + if (update.chatMember() != null) { + EventListenerManager.publishChatMemberUpdatedEvent(update); + } + } + return UpdatesListener.CONFIRMED_UPDATES_ALL; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java new file mode 100644 index 0000000..aaeefb4 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java @@ -0,0 +1,15 @@ +package cc.sukazyo.cono.morny.bot.event; + +import cc.sukazyo.cono.morny.bot.api.EventListenerManager; + +public class EventListeners { + + public static final OnCommandExecute COMMANDS_LISTENER = new OnCommandExecute(); + + public static void registerAllListeners () { + EventListenerManager.addListener( + COMMANDS_LISTENER + ); + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/OnCommandExecute.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java similarity index 78% rename from src/main/java/cc/sukazyo/cono/morny/bot/OnCommandExecute.java rename to src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java index 8ca897b..4f2b9ea 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/OnCommandExecute.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java @@ -1,20 +1,25 @@ -package cc.sukazyo.cono.morny.bot; +package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.MornyTrusted; +import cc.sukazyo.cono.morny.bot.api.EventListener; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.request.SendSticker; import static cc.sukazyo.cono.morny.Logger.logger; -public class OnCommandExecute { +public class OnCommandExecute extends EventListener { 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) { + @Override + public boolean onMessage (Update event) { + if (event.message().text() == null) { + return false; + } switch (event.message().text()) { case "/o": onCommandOnExec(event); @@ -27,10 +32,12 @@ public class OnCommandExecute { onCommandExitExec(event); break; default: + return false; } + return true; } - private static void onCommandOnExec (Update event) { + private void onCommandOnExec (Update event) { MornyCoeur.getAccount().execute(new SendSticker( event.message().chat().id(), ONLINE_STATUS_RETURN_STICKER_ID @@ -38,7 +45,7 @@ public class OnCommandExecute { ); } - private static void onCommandHelloExec (Update event) { + private void onCommandHelloExec (Update event) { MornyCoeur.getAccount().execute(new SendSticker( event.message().chat().id(), HELLO_STICKER_ID @@ -46,7 +53,7 @@ public class OnCommandExecute { ); } - private static void onCommandExitExec (Update event) { + private void onCommandExitExec (Update event) { if (MornyTrusted.isTrusted(event.message().from().id())) { MornyCoeur.getAccount().execute(new SendSticker( event.message().chat().id(), @@ -61,6 +68,7 @@ public class OnCommandExecute { EXIT_403_STICKER_ID ).replyToMessageId(event.message().messageId()) ); + logger.info("403 exited tag from user @" + event.message().from().username()); } }