Добавил Vertex для LongPolling
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Struchkov Mark 2023-04-02 12:58:20 +03:00
parent e960c273a0
commit 6414f92904
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
3 changed files with 18 additions and 9 deletions

View File

@ -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();

View File

@ -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;

View File

@ -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<String, String> lastMessageId = new HashMap<>();
private final Map<String, String> lastMessageId = new ConcurrentHashMap<>();
@Override
public Uni<String> getLastSendMessage(String telegramId) {