Skip to content

Commit

Permalink
🔇 silent changes: update function wrapper for telegram and slack #2
Browse files Browse the repository at this point in the history
  • Loading branch information
pnguyen215 committed Jun 22, 2024
1 parent 1f8c259 commit 35306ad
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 1 deletion.
5 changes: 4 additions & 1 deletion plugin/src/main/groovy/org/botwrap4j/common/BotWrap4j.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ public interface SlackWrapService {

void sendMessageSilent(String key, Map<String, Object> message);

void sendMessageSilentAfterWaitSec(String key, long seconds, Map<String, Object> message);

void sendMessageSilent(String key, String clusterId, Map<String, Object> message);

void sendMessageSilentAfterWaitSec(String key, String clusterId, long seconds, Map<String, Object> message);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,6 +90,33 @@ public void sendMessageSilent(String key, Map<String, Object> 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<String, Object> message) {
if (!botWrapService.isEnabled()) {
return;
}
new Thread(() -> {
try {
Optional<TelegramClustersRequest> 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
Expand All @@ -104,4 +133,32 @@ public void sendMessageSilent(String key, String clusterId, Map<String, Object>
}
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<String, Object> message) {
if (!botWrapService.isEnabled()) {
return;
}
new Thread(() -> {
try {
Optional<TelegramClustersRequest> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<TelegramClustersRequest> 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
Expand Down Expand Up @@ -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<TelegramClustersRequest> 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
Expand Down

0 comments on commit 35306ad

Please sign in to comment.