Добавил логирование вебхуков
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-03-19 18:21:48 +03:00
parent c2d9b5db78
commit 2f3d7b9b10
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
3 changed files with 16 additions and 9 deletions

View File

@ -15,17 +15,19 @@ public class TelegramWebhookConnect implements TelegramConnect {
private TelegramWebhookBot webhookBot; private TelegramWebhookBot webhookBot;
public TelegramWebhookConnect(TelegramBotConfig telegramBotConfig) { public TelegramWebhookConnect(TelegramBotConfig telegramBotConfig) {
log.info("Инициализация webhook соединения. {}", telegramBotConfig.getWebhookConfig());
try { try {
final TelegramWebhookBot bot = new TelegramWebhookBot(telegramBotConfig); final TelegramWebhookBot bot = new TelegramWebhookBot(telegramBotConfig);
final WebhookConfig webhookConfig = telegramBotConfig.getWebhookConfig(); final WebhookConfig webhookConfig = telegramBotConfig.getWebhookConfig();
if (webhookConfig.isEnable()) { if (webhookConfig.isEnable()) {
log.info("Инициализация webhook соединения. {}", telegramBotConfig.getWebhookConfig());
final SetWebhook setWebhook = SetWebhook.builder() final SetWebhook setWebhook = SetWebhook.builder()
.url(webhookConfig.getRootUrl() + "/" + webhookConfig.getRootUrl() + "?webhookAccessKey=" + webhookConfig.getAccessKey()) .url(webhookConfig.getRootUrl() + "/" + webhookConfig.getRootUrl() + "?webhookAccessKey=" + webhookConfig.getAccessKey())
.build(); .build();
bot.setWebhook(setWebhook); bot.setWebhook(setWebhook);
webhookBot = bot; webhookBot = bot;
log.info("Инициализация webhook соединения прошла успешно."); log.info("Инициализация webhook соединения прошла успешно.");
} else {
log.debug("Webhook соединение не устанавливалось.");
} }
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
log.error(e.getMessage()); log.error(e.getMessage());

View File

@ -19,17 +19,19 @@ public class TelegramWebhookConnect implements TelegramConnect {
} }
private void initWebHook(TelegramBotConfig telegramBotConfig) { private void initWebHook(TelegramBotConfig telegramBotConfig) {
log.info("Инициализация webhook соединения. {}", telegramBotConfig.getWebhookConfig());
try { try {
final TelegramWebhookBot bot = new TelegramWebhookBot(telegramBotConfig); final TelegramWebhookBot bot = new TelegramWebhookBot(telegramBotConfig);
final WebhookConfig webhookConfig = telegramBotConfig.getWebhookConfig(); final WebhookConfig webhookConfig = telegramBotConfig.getWebhookConfig();
if (webhookConfig.isEnable()) { if (webhookConfig.isEnable()) {
log.info("Инициализация webhook соединения. {}", telegramBotConfig.getWebhookConfig());
final SetWebhook setWebhook = SetWebhook.builder() final SetWebhook setWebhook = SetWebhook.builder()
.url(webhookConfig.getRootUrl() + "/" + webhookConfig.getRootUrl() + "?webhookAccessKey=" + webhookConfig.getAccessKey()) .url(webhookConfig.getRootUrl() + "/" + webhookConfig.getRootUrl() + "callback" + "?webhookAccessKey=" + webhookConfig.getAccessKey())
.build(); .build();
bot.setWebhook(setWebhook); bot.setWebhook(setWebhook);
webhookBot = bot; webhookBot = bot;
log.info("Инициализация webhook соединения прошла успешно."); log.info("Инициализация webhook соединения прошла успешно.");
} else {
log.debug("Webhook соединение не устанавливалось.");
} }
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
log.error(e.getMessage()); log.error(e.getMessage());

View File

@ -22,6 +22,7 @@ import static dev.struchkov.haiti.utils.Inspector.isTrue;
@Slf4j @Slf4j
public class WebhookController { public class WebhookController {
public static final String ERROR_ACCESS = "В доступе отказано!";
private final String pathKey; private final String pathKey;
private final String accessKey; private final String accessKey;
private final EventDistributor eventDistributor; private final EventDistributor eventDistributor;
@ -38,12 +39,14 @@ public class WebhookController {
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Uni<Response> updateReceived(@PathParam("webhookPath") String botPath, @QueryParam("webhookAccessKey") String webhookAccessKey, Update update) { public Uni<Response> updateReceived(@PathParam("webhookPath") String botPath, @QueryParam("webhookAccessKey") String webhookAccessKey, Update update) {
return Uni.createFrom().voidItem() return Uni.createFrom().voidItem()
.onItem().invoke(() -> { .invoke(() -> log.trace("Получено webhook событие"))
isTrue(pathKey.equals(botPath), accessException("В доступе отказано!")); .invoke(() -> {
isTrue(accessKey.equals(webhookAccessKey), accessException("В доступе отказано!")); isTrue(pathKey.equals(botPath), accessException(ERROR_ACCESS));
isTrue(accessKey.equals(webhookAccessKey), accessException(ERROR_ACCESS));
}) })
.onItem().ignore().andSwitchTo(() -> eventDistributor.processing(update)) .onItem().ignore().andSwitchTo(() -> eventDistributor.processing(update))
.onItem().transform(ignore -> Response.ok().build()); .invoke(() -> log.trace("Webhook событие успешно обработано"))
.replaceWith(Response.ok().build());
} }
@GET @GET
@ -52,8 +55,8 @@ public class WebhookController {
public Uni<String> testReceived(@PathParam("webhookPath") String botPath, @QueryParam("webhookAccessKey") String webhookAccessKey) { public Uni<String> testReceived(@PathParam("webhookPath") String botPath, @QueryParam("webhookAccessKey") String webhookAccessKey) {
return Uni.createFrom().voidItem() return Uni.createFrom().voidItem()
.onItem().invoke(() -> { .onItem().invoke(() -> {
isTrue(pathKey.equals(botPath), accessException("В доступе отказано!")); isTrue(pathKey.equals(botPath), accessException(ERROR_ACCESS));
isTrue(accessKey.equals(webhookAccessKey), accessException("В доступе отказано!")); isTrue(accessKey.equals(webhookAccessKey), accessException(ERROR_ACCESS));
}) })
.onItem().transform(ignore -> "Hi there " + botPath + "!"); .onItem().transform(ignore -> "Hi there " + botPath + "!");
} }