Skip to content

Commit

Permalink
removed TelegramNotifierConfig.java
Browse files Browse the repository at this point in the history
  • Loading branch information
yvasyliev committed Oct 20, 2023
1 parent ff8f344 commit e709209
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.yvasyliev.appenders;

import com.github.yvasyliev.bots.telegram.notifier.TelegramNotifier;
import com.github.yvasyliev.config.TelegramNotifierConfig;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.Filter;
Expand All @@ -15,7 +14,6 @@
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

import java.io.IOException;
import java.io.PrintWriter;
Expand All @@ -25,13 +23,11 @@
@Plugin(name = "TelegramBotAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE)
public class TelegramBotAppender extends AbstractAppender {
private final TelegramNotifier telegramNotifier;
private final int charactersLimit;

protected TelegramBotAppender(String name, Filter filter, Layout<? extends Serializable> layout, boolean ignoreExceptions, Property[] properties) {
super(name, filter, layout, ignoreExceptions, properties);
var applicationContext = new AnnotationConfigApplicationContext(TelegramNotifierConfig.class);
var applicationContext = new AnnotationConfigApplicationContext("com.github.yvasyliev.bots.telegram.notifier");
this.telegramNotifier = applicationContext.getBean(TelegramNotifier.class);
this.charactersLimit = applicationContext.getBean("charactersLimit", int.class);
}

@PluginFactory
Expand All @@ -50,13 +46,9 @@ public void append(LogEvent event) {
%s""".formatted(formattedMessage, stackTrace);
}

if (formattedMessage.length() > charactersLimit) {
formattedMessage = formattedMessage.substring(0, charactersLimit);
}

try {
telegramNotifier.notify(formattedMessage);
} catch (TelegramApiException e) {
telegramNotifier.applyWithException(formattedMessage);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ public class AdminNotifier extends AbstractRedTelBot implements TelegramNotifier
```""")
private String messageTemplate;

@Value("4096")
private int charactersLimit;

public AdminNotifier(@Value("${BOT_TOKEN}") String botToken) {
super(botToken);
}

@Override
public Message notify(String text) throws TelegramApiException {
public Message applyWithException(String text) throws TelegramApiException {
if (text.length() > charactersLimit) {
text = text.substring(0, charactersLimit);
}
SendMessage sendMessage = SendMessage.builder()
.chatId(getAdminId())
.text(messageTemplate.formatted(text))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.github.yvasyliev.bots.telegram.notifier;

import org.springframework.util.function.ThrowingFunction;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

@FunctionalInterface
public interface TelegramNotifier {
Message notify(String text) throws TelegramApiException;
public interface TelegramNotifier extends ThrowingFunction<String, Message> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

@Configuration
@ComponentScan("com.github.yvasyliev.service")
public class RedTelBotConfiguration extends TelegramNotifierConfig {
public class RedTelBotConfiguration {
@Autowired
private JsonDeserializer<Post> postJsonDeserializer;

Expand Down Expand Up @@ -111,4 +114,14 @@ public Map<Long, String> longStringMap(@Value("16") int maxSize) {
public Map<Long, ExternalMessageData> longExternalMessageDataMap(@Value("16") int maxSize) {
return synchronizedFixedSizeMap(maxSize);
}

@Bean
public Executor delayedExecutor() {
return CompletableFuture.delayedExecutor(10, TimeUnit.SECONDS, singleThreadExecutor());
}

@Bean
public Executor singleThreadExecutor() {
return Executors.newSingleThreadExecutor();
}
}

This file was deleted.

0 comments on commit e709209

Please sign in to comment.