Добавляю логирование

This commit is contained in:
Struchkov Mark 2023-03-30 22:32:53 +03:00
parent f916f7aa09
commit ee2024d37f
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
3 changed files with 31 additions and 32 deletions

View File

@ -17,11 +17,6 @@ import java.net.PasswordAuthentication;
import static dev.struchkov.haiti.utils.Checker.checkNotNull; import static dev.struchkov.haiti.utils.Checker.checkNotNull;
/**
* TODO: Добавить описание класса.
*
* @author upagge [30.01.2020]
*/
@Slf4j @Slf4j
public class TelegramPollingConnect implements TelegramConnect { public class TelegramPollingConnect implements TelegramConnect {
@ -32,6 +27,7 @@ public class TelegramPollingConnect implements TelegramConnect {
} }
private void initLongPolling(TelegramBotConfig telegramBotConfig) { private void initLongPolling(TelegramBotConfig telegramBotConfig) {
log.info("Initializing Telegram Long Polling...");
final ProxyConfig proxyConfig = telegramBotConfig.getProxyConfig(); final ProxyConfig proxyConfig = telegramBotConfig.getProxyConfig();
if (checkNotNull(proxyConfig) && proxyConfig.isEnable() && checkNotNull(proxyConfig.getPassword()) && !"".equals(proxyConfig.getPassword())) { if (checkNotNull(proxyConfig) && proxyConfig.isEnable() && checkNotNull(proxyConfig.getPassword()) && !"".equals(proxyConfig.getPassword())) {
try { try {
@ -45,8 +41,9 @@ public class TelegramPollingConnect implements TelegramConnect {
} }
}); });
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error("Error setting default authenticator for telegram proxy", e);
} }
log.info("Telegram proxy with authentication enabled");
} }
final TelegramBotsApi botapi; final TelegramBotsApi botapi;
@ -54,26 +51,29 @@ public class TelegramPollingConnect implements TelegramConnect {
if (checkNotNull(proxyConfig) && proxyConfig.isEnable() && checkNotNull(proxyConfig.getHost()) && !"".equals(proxyConfig.getHost())) { if (checkNotNull(proxyConfig) && proxyConfig.isEnable() && checkNotNull(proxyConfig.getHost()) && !"".equals(proxyConfig.getHost())) {
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
// System.setProperty("javax.net.debug", "all"); // System.setProperty("javax.net.debug", "all");
log.info(System.getProperty("https.protocols")); // log.info(System.getProperty("https.protocols"));
DefaultBotOptions botOptions = new DefaultBotOptions(); DefaultBotOptions botOptions = new DefaultBotOptions();
botOptions.setProxyHost(proxyConfig.getHost()); botOptions.setProxyHost(proxyConfig.getHost());
botOptions.setProxyPort(proxyConfig.getPort()); botOptions.setProxyPort(proxyConfig.getPort());
botOptions.setProxyType(convertProxyType(proxyConfig.getType())); botOptions.setProxyType(convertProxyType(proxyConfig.getType()));
log.info("Telegram proxy configuration set for bot");
final TelegramPollingBot bot = new TelegramPollingBot(telegramBotConfig, botOptions); final TelegramPollingBot bot = new TelegramPollingBot(telegramBotConfig, botOptions);
botapi = new TelegramBotsApi(DefaultBotSession.class); botapi = new TelegramBotsApi(DefaultBotSession.class);
botapi.registerBot(bot); botapi.registerBot(bot);
this.pollingBot = bot; this.pollingBot = bot;
log.info("Telegram Bot registered with proxy settings");
} else { } else {
final TelegramPollingBot bot = new TelegramPollingBot(telegramBotConfig); final TelegramPollingBot bot = new TelegramPollingBot(telegramBotConfig);
botapi = new TelegramBotsApi(DefaultBotSession.class); botapi = new TelegramBotsApi(DefaultBotSession.class);
botapi.registerBot(bot); botapi.registerBot(bot);
this.pollingBot = bot; this.pollingBot = bot;
log.info("Telegram Bot registered without proxy settings");
} }
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
log.error(e.getMessage()); log.error("Error registering telegram bot", e);
} }
} }

View File

@ -15,6 +15,7 @@ public class TelegramWebhookConnect implements TelegramConnect {
private TelegramWebhookBot webhookBot; private TelegramWebhookBot webhookBot;
public TelegramWebhookConnect(TelegramBotConfig telegramBotConfig) { public TelegramWebhookConnect(TelegramBotConfig telegramBotConfig) {
log.info("Initializing Webhook Polling...");
try { try {
final TelegramWebhookBot bot = new TelegramWebhookBot(telegramBotConfig); final TelegramWebhookBot bot = new TelegramWebhookBot(telegramBotConfig);
final WebhookConfig webhookConfig = telegramBotConfig.getWebhookConfig(); final WebhookConfig webhookConfig = telegramBotConfig.getWebhookConfig();

View File

@ -4,10 +4,8 @@ import dev.struchkov.godfather.telegram.domain.config.ProxyConfig;
import dev.struchkov.godfather.telegram.domain.config.ProxyConfig.Type; import dev.struchkov.godfather.telegram.domain.config.ProxyConfig.Type;
import dev.struchkov.godfather.telegram.domain.config.TelegramBotConfig; import dev.struchkov.godfather.telegram.domain.config.TelegramBotConfig;
import dev.struchkov.godfather.telegram.simple.context.service.EventDistributor; import dev.struchkov.godfather.telegram.simple.context.service.EventDistributor;
import dev.struchkov.godfather.telegram.simple.context.service.TelegramBot;
import dev.struchkov.godfather.telegram.simple.context.service.TelegramConnect; import dev.struchkov.godfather.telegram.simple.context.service.TelegramConnect;
import org.slf4j.Logger; import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.meta.TelegramBotsApi; import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.bots.AbsSender;
@ -19,26 +17,19 @@ import java.net.PasswordAuthentication;
import static dev.struchkov.haiti.utils.Checker.checkNotNull; import static dev.struchkov.haiti.utils.Checker.checkNotNull;
/** @Slf4j
* TODO: Добавить описание класса.
*
* @author upagge [30.01.2020]
*/
public class TelegramPollingConnect implements TelegramConnect { public class TelegramPollingConnect implements TelegramConnect {
private static final Logger log = LoggerFactory.getLogger(TelegramPollingConnect.class); private TelegramPollingBot pollingBot;
private TelegramBot telegramBot;
private final TelegramBotConfig telegramBotConfig;
public TelegramPollingConnect(TelegramBotConfig telegramBotConfig) { public TelegramPollingConnect(TelegramBotConfig telegramBotConfig) {
this.telegramBotConfig = telegramBotConfig;
initLongPolling(telegramBotConfig); initLongPolling(telegramBotConfig);
} }
private void initLongPolling(TelegramBotConfig telegramBotConfig) { private void initLongPolling(TelegramBotConfig telegramBotConfig) {
log.info("Initializing Telegram Long Polling...");
final ProxyConfig proxyConfig = telegramBotConfig.getProxyConfig(); final ProxyConfig proxyConfig = telegramBotConfig.getProxyConfig();
if (checkNotNull(proxyConfig) && checkNotNull(proxyConfig.getPassword()) && !"".equals(proxyConfig.getPassword())) { if (checkNotNull(proxyConfig) && proxyConfig.isEnable() && checkNotNull(proxyConfig.getPassword()) && !"".equals(proxyConfig.getPassword())) {
try { try {
Authenticator.setDefault(new Authenticator() { Authenticator.setDefault(new Authenticator() {
@Override @Override
@ -50,8 +41,9 @@ public class TelegramPollingConnect implements TelegramConnect {
} }
}); });
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error("Error setting default authenticator for telegram proxy", e);
} }
log.info("Telegram proxy with authentication enabled");
} }
final TelegramBotsApi botapi; final TelegramBotsApi botapi;
@ -59,25 +51,29 @@ public class TelegramPollingConnect implements TelegramConnect {
if (checkNotNull(proxyConfig) && proxyConfig.isEnable() && checkNotNull(proxyConfig.getHost()) && !"".equals(proxyConfig.getHost())) { if (checkNotNull(proxyConfig) && proxyConfig.isEnable() && checkNotNull(proxyConfig.getHost()) && !"".equals(proxyConfig.getHost())) {
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2"); System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
// System.setProperty("javax.net.debug", "all"); // System.setProperty("javax.net.debug", "all");
log.info(System.getProperty("https.protocols")); // log.info(System.getProperty("https.protocols"));
DefaultBotOptions botOptions = new DefaultBotOptions(); DefaultBotOptions botOptions = new DefaultBotOptions();
botOptions.setProxyHost(proxyConfig.getHost()); botOptions.setProxyHost(proxyConfig.getHost());
botOptions.setProxyPort(proxyConfig.getPort()); botOptions.setProxyPort(proxyConfig.getPort());
botOptions.setProxyType(convertProxyType(proxyConfig.getType())); botOptions.setProxyType(convertProxyType(proxyConfig.getType()));
log.info("Telegram proxy configuration set for bot");
final TelegramPollingBot bot = new TelegramPollingBot(telegramBotConfig, botOptions); final TelegramPollingBot bot = new TelegramPollingBot(telegramBotConfig, botOptions);
botapi = new TelegramBotsApi(DefaultBotSession.class); botapi = new TelegramBotsApi(DefaultBotSession.class);
botapi.registerBot(bot); botapi.registerBot(bot);
this.telegramBot = bot; this.pollingBot = bot;
log.info("Telegram Bot registered with proxy settings");
} else { } else {
final TelegramPollingBot bot = new TelegramPollingBot(telegramBotConfig); final TelegramPollingBot bot = new TelegramPollingBot(telegramBotConfig);
botapi = new TelegramBotsApi(DefaultBotSession.class); botapi = new TelegramBotsApi(DefaultBotSession.class);
botapi.registerBot(bot); botapi.registerBot(bot);
this.telegramBot = bot; this.pollingBot = bot;
log.info("Telegram Bot registered without proxy settings");
} }
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
log.error(e.getMessage()); log.error("Error registering telegram bot", e);
} }
} }
@ -89,17 +85,19 @@ public class TelegramPollingConnect implements TelegramConnect {
}; };
} }
public void initEventDistributor(EventDistributor eventDistributor) { @Override
telegramBot.initEventDistributor(eventDistributor); public String getToken() {
return pollingBot.getBotToken();
} }
public String getToken() { @Override
return telegramBotConfig.getToken(); public void initEventDistributor(EventDistributor eventDistributorService) {
pollingBot.initEventDistributor(eventDistributorService);
} }
@Override @Override
public AbsSender getAbsSender() { public AbsSender getAbsSender() {
return telegramBot.getAdsSender(); return pollingBot.getAdsSender();
} }
} }