diff --git a/plugin/src/main/groovy/org/botwrap4j/common/BotWrap4j.java b/plugin/src/main/groovy/org/botwrap4j/common/BotWrap4j.java index 8ec90c4..47cc15e 100644 --- a/plugin/src/main/groovy/org/botwrap4j/common/BotWrap4j.java +++ b/plugin/src/main/groovy/org/botwrap4j/common/BotWrap4j.java @@ -43,7 +43,7 @@ public static String getCurrentSessionId() { ServletRequestAttributes s = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); return s.getSessionId(); } catch (IllegalStateException e) { - return String.valueOf(UniqueId4j.getUniqueId()); + return String.valueOf(UniqueId4j.getUniqueId19()); } } @@ -58,6 +58,9 @@ public static String getCurrentSessionId() { * @return the session ID, or null if there is no current session */ public static String getSessionId(HttpServletRequest request) { + if (request == null) { + return String.valueOf(UniqueId4j.getUniqueId19()); + } HttpSession session = request.getSession(false); // Pass false to prevent creating a new session if one does not exist return (session != null) ? session.getId() : null; } diff --git a/plugin/src/main/groovy/org/botwrap4j/service/SlackWrapService.java b/plugin/src/main/groovy/org/botwrap4j/service/SlackWrapService.java index a2f9e6c..ad00d0a 100644 --- a/plugin/src/main/groovy/org/botwrap4j/service/SlackWrapService.java +++ b/plugin/src/main/groovy/org/botwrap4j/service/SlackWrapService.java @@ -15,5 +15,9 @@ public interface SlackWrapService { void sendMessageSilent(String key, Map message); + void sendMessageSilentAfterWaitSec(String key, long seconds, Map message); + void sendMessageSilent(String key, String clusterId, Map message); + + void sendMessageSilentAfterWaitSec(String key, String clusterId, long seconds, Map message); } diff --git a/plugin/src/main/groovy/org/botwrap4j/service/TelegramWrapService.java b/plugin/src/main/groovy/org/botwrap4j/service/TelegramWrapService.java index f73dc54..d40e75e 100644 --- a/plugin/src/main/groovy/org/botwrap4j/service/TelegramWrapService.java +++ b/plugin/src/main/groovy/org/botwrap4j/service/TelegramWrapService.java @@ -13,12 +13,16 @@ public interface TelegramWrapService { void sendMessageSilent(String key, String message); + void sendMessageSilentAfterWaitSec(String key, long seconds, String message); + void sendMessageSilent(String key, StringBuilder message); void sendMessageSilent(String key, Object message); void sendMessageSilent(String key, String clusterId, String message); + void sendMessageSilentAfterWaitSec(String key, String clusterId, long seconds, String message); + void sendMessageSilent(String key, String clusterId, StringBuilder message); void sendMessageSilent(String key, String clusterId, Object message); diff --git a/plugin/src/main/groovy/org/botwrap4j/service/impl/SlackWrapServiceImpl.java b/plugin/src/main/groovy/org/botwrap4j/service/impl/SlackWrapServiceImpl.java index 534f4dd..7e9590c 100644 --- a/plugin/src/main/groovy/org/botwrap4j/service/impl/SlackWrapServiceImpl.java +++ b/plugin/src/main/groovy/org/botwrap4j/service/impl/SlackWrapServiceImpl.java @@ -4,12 +4,14 @@ import org.bot4j.slack.model.builder.SlackConnectionBuilder; import org.botwrap4j.common.BotWrap4j; import org.botwrap4j.model.request.SlackClustersRequest; +import org.botwrap4j.model.request.TelegramClustersRequest; import org.botwrap4j.service.BotWrapService; import org.botwrap4j.service.SlackWrapService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.unify4j.common.Time4j; import java.util.Map; import java.util.Optional; @@ -88,6 +90,33 @@ public void sendMessageSilent(String key, Map message) { slack4j.requestId(BotWrap4j.getCurrentSessionId()).sendMessageSilent(); } + /** + * @param key - the key for slack cluster configs + * @param seconds - the second before sending + * @param message - the message will be sent, class {@link Map} + */ + @Override + public void sendMessageSilentAfterWaitSec(String key, long seconds, Map message) { + if (!botWrapService.isEnabled()) { + return; + } + new Thread(() -> { + try { + Optional node = botWrapService.findTelegramNode(key); + if (node.isPresent()) { + if (node.get().isDebugging()) { + logger.info("Schedule '{}' to run {} second(s) before sending message: {}", node.get().getDesc(), seconds, message); + } + Thread.sleep(Time4j.fromSecondToMillis(seconds)); // Wait for N seconds before sending + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + logger.error("Schedule to run {} second(s) before sending message to Slack failure: {} by key: {}", seconds, e.getMessage(), key, e); + } + this.sendMessageSilent(key, message); + }).start(); + } + /** * @param key - the key for slack cluster * @param clusterId - the cluster_id @@ -104,4 +133,32 @@ public void sendMessageSilent(String key, String clusterId, Map } slack4j.requestId(BotWrap4j.getCurrentSessionId()).sendMessageSilent(); } + + /** + * @param key - the key for slack cluster + * @param clusterId - the cluster_id + * @param seconds - the second waiting before sending + * @param message - the message will be sent, class {@link Map} + */ + @Override + public void sendMessageSilentAfterWaitSec(String key, String clusterId, long seconds, Map message) { + if (!botWrapService.isEnabled()) { + return; + } + new Thread(() -> { + try { + Optional node = botWrapService.findTelegramNode(key, clusterId); + if (node.isPresent()) { + if (node.get().isDebugging()) { + logger.info("Schedule '{}' to run {} second(s) before sending message: {}", node.get().getDesc(), seconds, message); + } + Thread.sleep(Time4j.fromSecondToMillis(seconds)); // Wait for N seconds before sending + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + logger.error("Schedule to run {} second(s) before sending message to Slack failure: {} by key: {}", seconds, e.getMessage(), key, e); + } + this.sendMessageSilent(key, clusterId, message); + }).start(); + } } diff --git a/plugin/src/main/groovy/org/botwrap4j/service/impl/TelegramWrapServiceImpl.java b/plugin/src/main/groovy/org/botwrap4j/service/impl/TelegramWrapServiceImpl.java index 51e74df..eaa0b03 100644 --- a/plugin/src/main/groovy/org/botwrap4j/service/impl/TelegramWrapServiceImpl.java +++ b/plugin/src/main/groovy/org/botwrap4j/service/impl/TelegramWrapServiceImpl.java @@ -12,6 +12,7 @@ import org.springframework.stereotype.Service; import org.unify4j.common.Class4j; import org.unify4j.common.Json4j; +import org.unify4j.common.Time4j; import java.util.Optional; @@ -90,6 +91,33 @@ public void sendMessageSilent(String key, String message) { telegram4j.requestId(BotWrap4j.getCurrentSessionId()).sendMessageSilent(); } + /** + * @param key - the key configured for telegram clusters, + * @param seconds - the second after waiting + * @param message - the message will be sent + */ + @Override + public void sendMessageSilentAfterWaitSec(String key, long seconds, String message) { + if (!botWrapService.isEnabled()) { + return; + } + new Thread(() -> { + try { + Optional node = botWrapService.findTelegramNode(key); + if (node.isPresent()) { + if (node.get().isDebugging()) { + logger.info("Schedule '{}' to run {} second(s) before sending message: {}", node.get().getDesc(), seconds, message); + } + Thread.sleep(Time4j.fromSecondToMillis(seconds)); // Wait for N seconds before sending + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + logger.error("Schedule to run {} second(s) before sending message to Telegram failure: {} by key: {}", seconds, e.getMessage(), key, e); + } + this.sendMessageSilent(key, message); + }).start(); + } + /** * @param key - the key configured for telegram clusters, * @param message - the message will be sent @@ -125,6 +153,34 @@ public void sendMessageSilent(String key, String clusterId, String message) { telegram4j.requestId(BotWrap4j.getCurrentSessionId()).sendMessageSilent(); } + /** + * @param key - the key configured for telegram clusters, + * @param message - the message will be sent + * @param clusterId - the cluster_id + * @param seconds - the second after waiting + */ + @Override + public void sendMessageSilentAfterWaitSec(String key, String clusterId, long seconds, String message) { + if (!botWrapService.isEnabled()) { + return; + } + new Thread(() -> { + try { + Optional node = botWrapService.findTelegramNode(key, clusterId); + if (node.isPresent()) { + if (node.get().isDebugging()) { + logger.info("Schedule '{}' to run {} second(s) before sending message: {}", node.get().getDesc(), seconds, message); + } + Thread.sleep(Time4j.fromSecondToMillis(seconds)); // Wait for N seconds before sending + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + logger.error("Schedule to run {} second(s) before sending message to Telegram failure: {} by key: {}", seconds, e.getMessage(), key, e); + } + this.sendMessageSilent(key, clusterId, message); + }).start(); + } + /** * @param key - the key configured for telegram clusters, * @param message - the message will be sent