From 6414f929049f67887d8916fdfb26179ea2d25477 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Sun, 2 Apr 2023 12:58:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20Ve?= =?UTF-8?q?rtex=20=D0=B4=D0=BB=D1=8F=20LongPolling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../telegram/quarkus/core/TelegramPollingBot.java | 12 ++++++++++-- .../quarkus/core/TelegramPollingConnect.java | 11 ++++++----- .../quarkus/core/service/SenderMapRepository.java | 4 ++-- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/TelegramPollingBot.java b/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/TelegramPollingBot.java index 035c4bb..3b329f7 100644 --- a/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/TelegramPollingBot.java +++ b/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/TelegramPollingBot.java @@ -3,6 +3,7 @@ package dev.struchkov.godfather.telegram.quarkus.core; import dev.struchkov.godfather.telegram.domain.config.TelegramBotConfig; import dev.struchkov.godfather.telegram.quarkus.context.service.EventDistributor; import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramBot; +import io.vertx.core.Vertx; import org.jetbrains.annotations.NotNull; import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.bots.TelegramLongPollingBot; @@ -16,20 +17,27 @@ import org.telegram.telegrambots.meta.bots.AbsSender; */ public class TelegramPollingBot extends TelegramLongPollingBot implements TelegramBot { + private final Vertx vertx; private final TelegramBotConfig telegramBotConfig; private EventDistributor eventDistributor; - public TelegramPollingBot(TelegramBotConfig telegramBotConfig, DefaultBotOptions defaultBotOptions) { + public TelegramPollingBot(Vertx vertx, TelegramBotConfig telegramBotConfig, DefaultBotOptions defaultBotOptions) { super(defaultBotOptions); this.telegramBotConfig = telegramBotConfig; + this.vertx = vertx; } - public TelegramPollingBot(TelegramBotConfig telegramBotConfig) { + public TelegramPollingBot(Vertx vertx, TelegramBotConfig telegramBotConfig) { + this.vertx = vertx; this.telegramBotConfig = telegramBotConfig; } @Override public void onUpdateReceived(Update update) { + vertx.runOnContext(v -> handleUpdate(update)); + } + + private void handleUpdate(Update update) { if (update != null && eventDistributor != null) { eventDistributor.processing(update) .subscribe().asCompletionStage(); diff --git a/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/TelegramPollingConnect.java b/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/TelegramPollingConnect.java index 00e40ae..c9375ea 100644 --- a/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/TelegramPollingConnect.java +++ b/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/TelegramPollingConnect.java @@ -5,6 +5,7 @@ import dev.struchkov.godfather.telegram.domain.config.ProxyConfig.Type; import dev.struchkov.godfather.telegram.domain.config.TelegramBotConfig; import dev.struchkov.godfather.telegram.quarkus.context.service.EventDistributor; import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramConnect; +import io.vertx.core.Vertx; import lombok.extern.slf4j.Slf4j; import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.meta.TelegramBotsApi; @@ -22,11 +23,11 @@ public class TelegramPollingConnect implements TelegramConnect { private TelegramPollingBot pollingBot; - public TelegramPollingConnect(TelegramBotConfig telegramBotConfig) { - initLongPolling(telegramBotConfig); + public TelegramPollingConnect(Vertx vertx, TelegramBotConfig telegramBotConfig) { + initLongPolling(vertx, telegramBotConfig); } - private void initLongPolling(TelegramBotConfig telegramBotConfig) { + private void initLongPolling(Vertx vertx, TelegramBotConfig telegramBotConfig) { log.info("Initializing Telegram Long Polling..."); final ProxyConfig proxyConfig = telegramBotConfig.getProxyConfig(); if (checkNotNull(proxyConfig) && proxyConfig.isEnable() && checkNotNull(proxyConfig.getPassword()) && !"".equals(proxyConfig.getPassword())) { @@ -59,14 +60,14 @@ public class TelegramPollingConnect implements TelegramConnect { log.info("Telegram proxy configuration set for bot"); - final TelegramPollingBot bot = new TelegramPollingBot(telegramBotConfig, botOptions); + final TelegramPollingBot bot = new TelegramPollingBot(vertx, telegramBotConfig, botOptions); botapi = new TelegramBotsApi(DefaultBotSession.class); botapi.registerBot(bot); this.pollingBot = bot; log.info("Telegram Bot registered with proxy settings"); } else { - final TelegramPollingBot bot = new TelegramPollingBot(telegramBotConfig); + final TelegramPollingBot bot = new TelegramPollingBot(vertx, telegramBotConfig); botapi = new TelegramBotsApi(DefaultBotSession.class); botapi.registerBot(bot); this.pollingBot = bot; diff --git a/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/SenderMapRepository.java b/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/SenderMapRepository.java index 9327270..2cb5fd1 100644 --- a/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/SenderMapRepository.java +++ b/telegram-core/telegram-core-quarkus/src/main/java/dev/struchkov/godfather/telegram/quarkus/core/service/SenderMapRepository.java @@ -4,14 +4,14 @@ import dev.struchkov.godfather.telegram.quarkus.context.repository.SenderReposit import io.smallrye.mutiny.Uni; import org.jetbrains.annotations.NotNull; -import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import static dev.struchkov.haiti.utils.Inspector.isNotNull; public class SenderMapRepository implements SenderRepository { - private final Map lastMessageId = new HashMap<>(); + private final Map lastMessageId = new ConcurrentHashMap<>(); @Override public Uni getLastSendMessage(String telegramId) {