Бета версия вебхука и бета версия оплаты
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Struchkov Mark 2023-03-17 03:16:18 +03:00
parent 2d240152fa
commit 90c54f0d76
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
26 changed files with 678 additions and 123 deletions

24
pom.xml
View File

@ -14,6 +14,7 @@
<module>telegram-consumer</module> <module>telegram-consumer</module>
<module>telegram-context</module> <module>telegram-context</module>
<module>telegram-simple</module> <module>telegram-simple</module>
<module>telegram-webhook</module>
</modules> </modules>
<name>Telegram Bot</name> <name>Telegram Bot</name>
@ -37,11 +38,14 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<godfather.core.ver>0.0.53</godfather.core.ver> <godfather.core.ver>0.0.54-SNAPSHOT</godfather.core.ver>
<!-- https://mvnrepository.com/artifact/org.telegram/telegrambots --> <!-- https://mvnrepository.com/artifact/org.telegram/telegrambots -->
<telegrambots.ver>6.3.0</telegrambots.ver> <telegrambots.ver>6.3.0</telegrambots.ver>
<!-- https://mvnrepository.com/artifact/io.smallrye.reactive/smallrye-mutiny-vertx-core -->
<smallrye.mutiny.vertx.core.version>2.30.1</smallrye.mutiny.vertx.core.version>
<haiti.version>2.6.0</haiti.version> <haiti.version>2.6.0</haiti.version>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin --> <!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
@ -245,6 +249,24 @@
<artifactId>telegrambotsextensions</artifactId> <artifactId>telegrambotsextensions</artifactId>
<version>${telegrambots.ver}</version> <version>${telegrambots.ver}</version>
</dependency> </dependency>
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>io.smallrye.reactive</groupId>
<artifactId>smallrye-mutiny-vertx-core</artifactId>
<version>${smallrye.mutiny.vertx.core.version}</version>
</dependency>
<dependency>
<groupId>io.smallrye.reactive</groupId>
<artifactId>smallrye-mutiny-vertx-core</artifactId>
<version>${smallrye.mutiny.vertx.core.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>

View File

@ -9,7 +9,7 @@ import dev.struchkov.godfather.telegram.main.consumer.MessageMailConvert;
import dev.struchkov.godfather.telegram.main.consumer.SubscribeConvert; import dev.struchkov.godfather.telegram.main.consumer.SubscribeConvert;
import dev.struchkov.godfather.telegram.main.consumer.UnsubscribeConvert; import dev.struchkov.godfather.telegram.main.consumer.UnsubscribeConvert;
import dev.struchkov.godfather.telegram.quarkus.context.service.EventDistributor; import dev.struchkov.godfather.telegram.quarkus.context.service.EventDistributor;
import dev.struchkov.godfather.telegram.quarkus.core.TelegramConnectBot; import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramConnect;
import io.smallrye.mutiny.Multi; import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -18,6 +18,7 @@ import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.User; import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.api.objects.payments.PreCheckoutQuery;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -35,9 +36,9 @@ public class EventDistributorService implements EventDistributor {
private final Map<String, List<EventHandler>> eventHandlerMap; private final Map<String, List<EventHandler>> eventHandlerMap;
public EventDistributorService(TelegramConnectBot telegramConnectBot, List<EventHandler> eventProviders) { public EventDistributorService(TelegramConnect telegramConnect, List<EventHandler> eventProviders) {
this.eventHandlerMap = eventProviders.stream().collect(Collectors.groupingBy(EventHandler::getEventType)); this.eventHandlerMap = eventProviders.stream().collect(Collectors.groupingBy(EventHandler::getEventType));
telegramConnectBot.initEventDistributor(this); telegramConnect.initEventDistributor(this);
} }
@Override @Override
@ -47,52 +48,66 @@ public class EventDistributorService implements EventDistributor {
v -> { v -> {
final Message message = update.getMessage(); final Message message = update.getMessage();
final CallbackQuery callbackQuery = update.getCallbackQuery(); final CallbackQuery callbackQuery = update.getCallbackQuery();
if (checkNotNull(message)) { final PreCheckoutQuery preCheckoutQuery = update.getPreCheckoutQuery();
if (!isEvent(message)) {
final Optional<List<EventHandler>> optHandlers = getHandler(Mail.TYPE); if (checkNotNull(preCheckoutQuery)) {
final Optional<List<EventHandler>> optHandlers = getHandler(preCheckoutQuery.getClass().getName());
if (optHandlers.isPresent()) {
return Multi.createFrom().iterable(optHandlers.get())
.onItem().transformToUni(
eventHandler -> eventHandler.handle(preCheckoutQuery)
).concatenate().collect().asList().replaceWithVoid();
}
return Uni.createFrom().voidItem();
}
if (checkNotNull(message) && (!isEvent(message))) {
final Optional<List<EventHandler>> optHandlers = getHandler(Mail.class.getName());
if (optHandlers.isPresent()) { if (optHandlers.isPresent()) {
return Multi.createFrom().iterable(optHandlers.get()) return Multi.createFrom().iterable(optHandlers.get())
.onItem().transformToUni( .onItem().transformToUni(
eventHandler -> eventHandler.handle(MessageMailConvert.apply(message)) eventHandler -> eventHandler.handle(MessageMailConvert.apply(message))
).concatenate().toUni().replaceWithVoid(); ).concatenate().collect().asList().replaceWithVoid();
} }
return Uni.createFrom().voidItem(); return Uni.createFrom().voidItem();
} }
}
if (checkNotNull(callbackQuery)) { if (checkNotNull(callbackQuery)) {
final Optional<List<EventHandler>> optHandlers = getHandler(Mail.TYPE); final Optional<List<EventHandler>> optHandlers = getHandler(Mail.class.getName());
if (optHandlers.isPresent()) { if (optHandlers.isPresent()) {
return Multi.createFrom().iterable(optHandlers.get()) return Multi.createFrom().iterable(optHandlers.get())
.onItem().transformToUni( .onItem().transformToUni(
eventHandler -> eventHandler.handle(CallbackQueryConvert.apply(callbackQuery)) eventHandler -> eventHandler.handle(CallbackQueryConvert.apply(callbackQuery))
).concatenate().toUni().replaceWithVoid(); ).concatenate().collect().asList().replaceWithVoid();
} }
return Uni.createFrom().voidItem(); return Uni.createFrom().voidItem();
} }
if (checkNotNull(update.getMyChatMember())) { if (checkNotNull(update.getMyChatMember())) {
final ChatMemberUpdated chatMember = update.getMyChatMember(); final ChatMemberUpdated chatMember = update.getMyChatMember();
if ("kicked".equals(chatMember.getNewChatMember().getStatus())) { if ("kicked".equals(chatMember.getNewChatMember().getStatus())) {
final Optional<List<EventHandler>> optHandlers = getHandler(Unsubscribe.TYPE); final Optional<List<EventHandler>> optHandlers = getHandler(Unsubscribe.class.getName());
if (optHandlers.isPresent()) { if (optHandlers.isPresent()) {
return Multi.createFrom().iterable(optHandlers.get()) return Multi.createFrom().iterable(optHandlers.get())
.onItem().transformToUni( .onItem().transformToUni(
eventHandler -> eventHandler.handle(UnsubscribeConvert.apply(chatMember)) eventHandler -> eventHandler.handle(UnsubscribeConvert.apply(chatMember))
).concatenate().toUni().replaceWithVoid(); ).concatenate().collect().asList().replaceWithVoid();
} }
return Uni.createFrom().voidItem(); return Uni.createFrom().voidItem();
} }
if ("member".equals(chatMember.getNewChatMember().getStatus())) { if ("member".equals(chatMember.getNewChatMember().getStatus())) {
final Optional<List<EventHandler>> optHandlers = getHandler(Subscribe.TYPE); final Optional<List<EventHandler>> optHandlers = getHandler(Subscribe.class.getName());
if (optHandlers.isPresent()) { if (optHandlers.isPresent()) {
return Multi.createFrom().iterable(optHandlers.get()) return Multi.createFrom().iterable(optHandlers.get())
.onItem().transformToUni( .onItem().transformToUni(
eventHandler -> eventHandler.handle(SubscribeConvert.apply(chatMember)) eventHandler -> eventHandler.handle(SubscribeConvert.apply(chatMember))
).concatenate().toUni().replaceWithVoid(); ).concatenate().collect().asList().replaceWithVoid();
} }
return Uni.createFrom().voidItem(); return Uni.createFrom().voidItem();
} }
} }
return Uni.createFrom().voidItem(); return Uni.createFrom().voidItem();
} }
); );

View File

@ -11,13 +11,15 @@ import dev.struchkov.godfather.telegram.main.consumer.MessageMailConvert;
import dev.struchkov.godfather.telegram.main.consumer.SubscribeConvert; import dev.struchkov.godfather.telegram.main.consumer.SubscribeConvert;
import dev.struchkov.godfather.telegram.main.consumer.UnsubscribeConvert; import dev.struchkov.godfather.telegram.main.consumer.UnsubscribeConvert;
import dev.struchkov.godfather.telegram.simple.context.service.EventDistributor; import dev.struchkov.godfather.telegram.simple.context.service.EventDistributor;
import dev.struchkov.godfather.telegram.simple.core.TelegramConnectBot; import dev.struchkov.godfather.telegram.simple.context.service.TelegramConnect;
import dev.struchkov.haiti.utils.Checker;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery; import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated; import org.telegram.telegrambots.meta.api.objects.ChatMemberUpdated;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.User; import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.api.objects.payments.PreCheckoutQuery;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -33,20 +35,25 @@ public class EventDistributorService implements EventDistributor {
private final Map<String, List<EventHandler>> eventProviderMap; private final Map<String, List<EventHandler>> eventProviderMap;
public EventDistributorService(TelegramConnectBot telegramConnectBot, List<EventHandler> eventProviders) { public EventDistributorService(TelegramConnect telegramConnect, List<EventHandler> eventProviders) {
this.eventProviderMap = eventProviders.stream().collect(Collectors.groupingBy(EventHandler::getEventType)); this.eventProviderMap = eventProviders.stream().collect(Collectors.groupingBy(EventHandler::getEventType));
telegramConnectBot.initEventDistributor(this); telegramConnect.initEventDistributor(this);
} }
@Override @Override
public void processing(@NotNull Update update) { public void processing(@NotNull Update update) {
final Message message = update.getMessage(); final Message message = update.getMessage();
final CallbackQuery callbackQuery = update.getCallbackQuery(); final CallbackQuery callbackQuery = update.getCallbackQuery();
if (message != null) { final PreCheckoutQuery preCheckoutQuery = update.getPreCheckoutQuery();
if (!isEvent(message)) {
processionMessage(message); if (Checker.checkNotNull(preCheckoutQuery)) {
getHandler(preCheckoutQuery.getClass().getName()).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(preCheckoutQuery)));
return; return;
} }
if (message != null && (!isEvent(message))) {
processionMessage(message);
return;
} }
if (callbackQuery != null) { if (callbackQuery != null) {
processionCallback(callbackQuery); processionCallback(callbackQuery);
@ -55,11 +62,11 @@ public class EventDistributorService implements EventDistributor {
if (update.getMyChatMember() != null) { if (update.getMyChatMember() != null) {
final ChatMemberUpdated chatMember = update.getMyChatMember(); final ChatMemberUpdated chatMember = update.getMyChatMember();
if ("kicked".equals(chatMember.getNewChatMember().getStatus())) { if ("kicked".equals(chatMember.getNewChatMember().getStatus())) {
getHandler(Unsubscribe.TYPE).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(UnsubscribeConvert.apply(chatMember)))); getHandler(Unsubscribe.class.getName()).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(UnsubscribeConvert.apply(chatMember))));
return; return;
} }
if ("member".equals(chatMember.getNewChatMember().getStatus())) { if ("member".equals(chatMember.getNewChatMember().getStatus())) {
getHandler(Subscribe.TYPE).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(SubscribeConvert.apply(chatMember)))); getHandler(Subscribe.class.getName()).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(SubscribeConvert.apply(chatMember))));
return; return;
} }
} }
@ -71,7 +78,7 @@ public class EventDistributorService implements EventDistributor {
} else { } else {
final Mail mail = CallbackQueryConvert.apply(callbackQuery); final Mail mail = CallbackQueryConvert.apply(callbackQuery);
getHandler(Mail.TYPE).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(mail))); getHandler(Mail.class.getName()).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(mail)));
} }
} }
@ -79,10 +86,10 @@ public class EventDistributorService implements EventDistributor {
final Long fromId = message.getChat().getId(); final Long fromId = message.getChat().getId();
if (fromId < 0) { if (fromId < 0) {
final ChatMail chatMail = MessageChatMailConvert.apply(message); final ChatMail chatMail = MessageChatMailConvert.apply(message);
getHandler(ChatMail.TYPE).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(chatMail))); getHandler(ChatMail.class.getName()).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(chatMail)));
} else { } else {
final Mail mail = MessageMailConvert.apply(message); final Mail mail = MessageMailConvert.apply(message);
getHandler(Mail.TYPE).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(mail))); getHandler(Mail.class.getName()).ifPresent(handlers -> handlers.forEach(handler -> handler.handle(mail)));
} }
} }

View File

@ -2,11 +2,13 @@ package dev.struchkov.godfather.telegram.main.context;
import dev.struchkov.haiti.utils.Exceptions; import dev.struchkov.haiti.utils.Exceptions;
import dev.struchkov.haiti.utils.container.ContextKey; import dev.struchkov.haiti.utils.container.ContextKey;
import org.telegram.telegrambots.meta.api.methods.invoices.SendInvoice;
public final class BoxAnswerPayload { public final class BoxAnswerPayload {
public static final ContextKey<Boolean> DISABLE_WEB_PAGE_PREVIEW = ContextKey.of("DISABLE_WEB_PAGE_PREVIEW", Boolean.class); public static final ContextKey<Boolean> DISABLE_WEB_PAGE_PREVIEW = ContextKey.of("DISABLE_WEB_PAGE_PREVIEW", Boolean.class);
public static final ContextKey<Boolean> DISABLE_NOTIFICATION = ContextKey.of("DISABLE_NOTIFICATION", Boolean.class); public static final ContextKey<Boolean> DISABLE_NOTIFICATION = ContextKey.of("DISABLE_NOTIFICATION", Boolean.class);
public static final ContextKey<SendInvoice> INVOICE = ContextKey.of("INVOICE", SendInvoice.class);
private BoxAnswerPayload() { private BoxAnswerPayload() {
Exceptions.utilityClass(); Exceptions.utilityClass();

View File

@ -0,0 +1,13 @@
package dev.struchkov.godfather.telegram.quarkus.context.service;
import org.telegram.telegrambots.meta.bots.AbsSender;
public interface TelegramConnect {
AbsSender getAbsSender();
String getToken();
void initEventDistributor(EventDistributor eventDistributorService);
}

View File

@ -1,4 +1,4 @@
package dev.struchkov.godfather.telegram.main.context; package dev.struchkov.godfather.telegram.simple.context.service;
import org.telegram.telegrambots.meta.bots.AbsSender; import org.telegram.telegrambots.meta.bots.AbsSender;
@ -8,4 +8,6 @@ public interface TelegramConnect {
String getToken(); String getToken();
void initEventDistributor(EventDistributor eventDistributorService);
} }

View File

@ -0,0 +1,89 @@
package dev.struchkov.godfather.telegram.quarkus.core;
import dev.struchkov.godfather.telegram.domain.config.ProxyConfig;
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 org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.bots.DefaultAbsSender;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.meta.bots.AbsSender;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import static dev.struchkov.haiti.utils.Checker.checkNotNull;
public class TelegramDefaultConnect implements TelegramConnect {
private final String botToken;
private final AbsSender absSender;
public TelegramDefaultConnect(TelegramBotConfig connectConfig) {
this.botToken = connectConfig.getToken();
this.absSender = createAbsSender(connectConfig);
}
@NotNull
private DefaultAbsSender createAbsSender(TelegramBotConfig connectConfig) {
final DefaultBotOptions botOptions = new DefaultBotOptions();
final ProxyConfig proxyConfig = connectConfig.getProxyConfig();
if (checkNotNull(proxyConfig) && proxyConfig.isEnable()) {
if (checkNotNull(proxyConfig.getPassword())) {
try {
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
proxyConfig.getUser(),
proxyConfig.getPassword().toCharArray()
);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
if (checkNotNull(proxyConfig.getHost())) {
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
// System.setProperty("javax.net.debug", "all");
botOptions.setProxyHost(proxyConfig.getHost());
botOptions.setProxyPort(proxyConfig.getPort());
botOptions.setProxyType(convertProxyType(proxyConfig.getType()));
}
}
return new DefaultAbsSender(botOptions) {
@Override
public String getBotToken() {
return botToken;
}
};
}
private DefaultBotOptions.ProxyType convertProxyType(ProxyConfig.Type type) {
return switch (type) {
case SOCKS5 -> DefaultBotOptions.ProxyType.SOCKS5;
case SOCKS4 -> DefaultBotOptions.ProxyType.SOCKS4;
case HTTP -> DefaultBotOptions.ProxyType.HTTP;
};
}
@Override
public AbsSender getAbsSender() {
return absSender;
}
@Override
public String getToken() {
return botToken;
}
@Override
public void initEventDistributor(EventDistributor eventDistributorService) {
throw new IllegalStateException();
}
}

View File

@ -3,11 +3,9 @@ package dev.struchkov.godfather.telegram.quarkus.core;
import dev.struchkov.godfather.telegram.domain.config.ProxyConfig; 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.main.context.TelegramConnect;
import dev.struchkov.godfather.telegram.quarkus.context.service.EventDistributor; import dev.struchkov.godfather.telegram.quarkus.context.service.EventDistributor;
import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramBot; import dev.struchkov.godfather.telegram.quarkus.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;
@ -24,35 +22,16 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull;
* *
* @author upagge [30.01.2020] * @author upagge [30.01.2020]
*/ */
public class TelegramConnectBot implements TelegramConnect { @Slf4j
public class TelegramPollingConnect implements TelegramConnect {
private static final Logger log = LoggerFactory.getLogger(TelegramConnectBot.class); private TelegramPollingBot pollingBot;
private TelegramBot telegramBot; public TelegramPollingConnect(TelegramBotConfig telegramBotConfig) {
private final TelegramBotConfig telegramBotConfig;
public TelegramConnectBot(TelegramBotConfig telegramBotConfig) {
this.telegramBotConfig = telegramBotConfig;
initLongPolling(telegramBotConfig); initLongPolling(telegramBotConfig);
} }
// public TelegramConnect(TelegramWebHookConfig telegramWebHookConfig) {
// initWebHook(telegramWebHookConfig);
// }
//
// private void initWebHook(TelegramWebHookConfig telegramWebHookConfig) {
// TelegramBotsApi botapi = new TelegramBotsApi();
// final TelegramWebhookBot telegramWebhookBot = new TelegramHookBot(telegramWebHookConfig);
// try {
// botapi.registerBot(telegramWebhookBot);
// this.telegramBot = (TelegramBot) telegramWebhookBot;
// } catch (TelegramApiRequestException e) {
// e.printStackTrace();
// }
// }
private void initLongPolling(TelegramBotConfig telegramBotConfig) { private void initLongPolling(TelegramBotConfig telegramBotConfig) {
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 {
@ -86,12 +65,12 @@ public class TelegramConnectBot implements TelegramConnect {
botapi = new TelegramBotsApi(DefaultBotSession.class); botapi = new TelegramBotsApi(DefaultBotSession.class);
botapi.registerBot(bot); botapi.registerBot(bot);
this.telegramBot = bot; this.pollingBot = bot;
} 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;
} }
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
log.error(e.getMessage()); log.error(e.getMessage());
@ -106,17 +85,19 @@ public class TelegramConnectBot 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();
} }
} }

View File

@ -0,0 +1,53 @@
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 lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.bots.AbsSender;
@Slf4j
public class TelegramWebhookBot extends org.telegram.telegrambots.bots.TelegramWebhookBot implements TelegramBot {
private final TelegramBotConfig telegramBotConfig;
private EventDistributor eventDistributor;
public TelegramWebhookBot(TelegramBotConfig telegramBotConfig) {
this.telegramBotConfig = telegramBotConfig;
}
@Override
public String getBotUsername() {
return telegramBotConfig.getUsername();
}
@Override
public String getBotToken() {
return telegramBotConfig.getToken();
}
@Override
public BotApiMethod<?> onWebhookUpdateReceived(Update update) {
eventDistributor.processing(update).subscribe().with(v -> {
});
return null;
}
@Override
public String getBotPath() {
return "bot";
}
@Override
public AbsSender getAdsSender() {
return this;
}
@Override
public void initEventDistributor(EventDistributor eventDistributor) {
this.eventDistributor = eventDistributor;
}
}

View File

@ -0,0 +1,48 @@
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.TelegramConnect;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.meta.bots.AbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
@Slf4j
public class TelegramWebhookConnect implements TelegramConnect {
private TelegramWebhookBot webhookBot;
public TelegramWebhookConnect(TelegramBotConfig telegramBotConfig) {
initWebHook(telegramBotConfig);
}
private void initWebHook(TelegramBotConfig telegramBotConfig) {
// try {
final TelegramWebhookBot bot = new TelegramWebhookBot(telegramBotConfig);
final SetWebhook setWebhook = SetWebhook.builder()
.url(telegramBotConfig.getWebHookUrl())
.build();
// bot.setWebhook(setWebhook);
webhookBot = bot;
// } catch (TelegramApiException e) {
// log.error(e.getMessage());
// }
}
@Override
public AbsSender getAbsSender() {
return webhookBot.getAdsSender();
}
@Override
public String getToken() {
return webhookBot.getBotToken();
}
@Override
public void initEventDistributor(EventDistributor eventDistributorService) {
webhookBot.initEventDistributor(eventDistributorService);
}
}

View File

@ -0,0 +1,52 @@
package dev.struchkov.godfather.telegram.quarkus.core.handler;
import dev.struchkov.godfather.quarkus.context.service.EventHandler;
import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramConnect;
import io.smallrye.mutiny.Uni;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.meta.api.methods.AnswerPreCheckoutQuery;
import org.telegram.telegrambots.meta.api.methods.AnswerShippingQuery;
import org.telegram.telegrambots.meta.api.objects.payments.PreCheckoutQuery;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
@Slf4j
@RequiredArgsConstructor
public class PreCheckoutQueryHandler implements EventHandler<PreCheckoutQuery> {
private final TelegramConnect telegramConnect;
@Override
public Uni<Void> handle(PreCheckoutQuery event) {
final AnswerPreCheckoutQuery answerPreCheckoutQuery = new AnswerPreCheckoutQuery();
answerPreCheckoutQuery.setPreCheckoutQueryId(event.getId());
answerPreCheckoutQuery.setOk(true);
try {
answerPreCheckoutQuery.validate();
} catch (TelegramApiValidationException e) {
log.error(e.getMessage(), e);
}
return Uni.createFrom().completionStage(
execAsync(answerPreCheckoutQuery)
).replaceWithVoid();
}
public CompletionStage<Object> execAsync(AnswerPreCheckoutQuery answerShippingQuery) {
try {
telegramConnect.getAbsSender().executeAsync(answerShippingQuery);
} catch (TelegramApiException e) {
log.error(e.getMessage(), e);
}
return CompletableFuture.completedStage(null);
}
@Override
public String getEventType() {
return PreCheckoutQuery.class.getName();
}
}

View File

@ -4,8 +4,8 @@ import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment;
import dev.struchkov.godfather.telegram.domain.attachment.Picture; import dev.struchkov.godfather.telegram.domain.attachment.Picture;
import dev.struchkov.godfather.telegram.domain.files.ByteContainer; import dev.struchkov.godfather.telegram.domain.files.ByteContainer;
import dev.struchkov.godfather.telegram.domain.files.FileContainer; import dev.struchkov.godfather.telegram.domain.files.FileContainer;
import dev.struchkov.godfather.telegram.main.context.TelegramConnect;
import dev.struchkov.godfather.telegram.quarkus.context.service.AttachmentService; import dev.struchkov.godfather.telegram.quarkus.context.service.AttachmentService;
import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramConnect;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;

View File

@ -2,7 +2,7 @@ package dev.struchkov.godfather.telegram.quarkus.core.service;
import dev.struchkov.godfather.telegram.domain.ChatAction; import dev.struchkov.godfather.telegram.domain.ChatAction;
import dev.struchkov.godfather.telegram.domain.ClientBotCommand; import dev.struchkov.godfather.telegram.domain.ClientBotCommand;
import dev.struchkov.godfather.telegram.main.context.TelegramConnect; import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramConnect;
import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramService; import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramService;
import io.smallrye.mutiny.Multi; import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;

View File

@ -1,8 +1,9 @@
package dev.struchkov.godfather.telegram.main.core; package dev.struchkov.godfather.telegram.simple.core;
import dev.struchkov.godfather.telegram.domain.config.ProxyConfig; import dev.struchkov.godfather.telegram.domain.config.ProxyConfig;
import dev.struchkov.godfather.telegram.domain.config.TelegramBotConfig; import dev.struchkov.godfather.telegram.domain.config.TelegramBotConfig;
import dev.struchkov.godfather.telegram.main.context.TelegramConnect; import dev.struchkov.godfather.telegram.simple.context.service.EventDistributor;
import dev.struchkov.godfather.telegram.simple.context.service.TelegramConnect;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.bots.DefaultAbsSender; import org.telegram.telegrambots.bots.DefaultAbsSender;
import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.bots.DefaultBotOptions;
@ -80,4 +81,9 @@ public class TelegramDefaultConnect implements TelegramConnect {
return botToken; return botToken;
} }
@Override
public void initEventDistributor(EventDistributor eventDistributorService) {
throw new IllegalStateException();
}
} }

View File

@ -3,9 +3,9 @@ package dev.struchkov.godfather.telegram.simple.core;
import dev.struchkov.godfather.telegram.domain.config.ProxyConfig; 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.main.context.TelegramConnect;
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.TelegramBot;
import dev.struchkov.godfather.telegram.simple.context.service.TelegramConnect;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.bots.DefaultBotOptions;
@ -24,14 +24,14 @@ import static dev.struchkov.haiti.utils.Checker.checkNotNull;
* *
* @author upagge [30.01.2020] * @author upagge [30.01.2020]
*/ */
public class TelegramConnectBot implements TelegramConnect { public class TelegramPollingConnect implements TelegramConnect {
private static final Logger log = LoggerFactory.getLogger(TelegramConnectBot.class); private static final Logger log = LoggerFactory.getLogger(TelegramPollingConnect.class);
private TelegramBot telegramBot; private TelegramBot telegramBot;
private final TelegramBotConfig telegramBotConfig; private final TelegramBotConfig telegramBotConfig;
public TelegramConnectBot(TelegramBotConfig telegramBotConfig) { public TelegramPollingConnect(TelegramBotConfig telegramBotConfig) {
this.telegramBotConfig = telegramBotConfig; this.telegramBotConfig = telegramBotConfig;
initLongPolling(telegramBotConfig); initLongPolling(telegramBotConfig);
} }

View File

@ -0,0 +1,52 @@
package dev.struchkov.godfather.telegram.simple.core;
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.TelegramBot;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.bots.AbsSender;
@Slf4j
public class TelegramWebhookBot extends org.telegram.telegrambots.bots.TelegramWebhookBot implements TelegramBot {
private final TelegramBotConfig telegramBotConfig;
private EventDistributor eventDistributor;
public TelegramWebhookBot(TelegramBotConfig telegramBotConfig) {
this.telegramBotConfig = telegramBotConfig;
}
@Override
public String getBotUsername() {
return telegramBotConfig.getUsername();
}
@Override
public String getBotToken() {
return telegramBotConfig.getToken();
}
@Override
public BotApiMethod<?> onWebhookUpdateReceived(Update update) {
eventDistributor.processing(update);
return null;
}
@Override
public String getBotPath() {
return null;
}
@Override
public AbsSender getAdsSender() {
return this;
}
@Override
public void initEventDistributor(EventDistributor eventDistributor) {
this.eventDistributor = eventDistributor;
}
}

View File

@ -0,0 +1,48 @@
package dev.struchkov.godfather.telegram.simple.core;
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.TelegramConnect;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.meta.bots.AbsSender;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
@Slf4j
public class TelegramWebhookConnect implements TelegramConnect {
private TelegramWebhookBot webhookBot;
public TelegramWebhookConnect(TelegramBotConfig telegramBotConfig) {
initWebHook(telegramBotConfig);
}
private void initWebHook(TelegramBotConfig telegramBotConfig) {
try {
final TelegramWebhookBot bot = new TelegramWebhookBot(telegramBotConfig);
final SetWebhook setWebhook = SetWebhook.builder()
.url(telegramBotConfig.getWebHookUrl())
.build();
bot.setWebhook(setWebhook);
webhookBot = bot;
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
}
@Override
public AbsSender getAbsSender() {
return webhookBot.getAdsSender();
}
@Override
public String getToken() {
return webhookBot.getBotToken();
}
@Override
public void initEventDistributor(EventDistributor eventDistributorService) {
webhookBot.initEventDistributor(eventDistributorService);
}
}

View File

@ -0,0 +1,36 @@
package dev.struchkov.godfather.telegram.simple.core.handler;
import dev.struchkov.godfather.simple.context.service.EventHandler;
import dev.struchkov.godfather.telegram.simple.context.service.TelegramConnect;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.meta.api.methods.AnswerPreCheckoutQuery;
import org.telegram.telegrambots.meta.api.objects.payments.PreCheckoutQuery;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
@Slf4j
@RequiredArgsConstructor
public class PreCheckoutQueryHandler implements EventHandler<PreCheckoutQuery> {
private final TelegramConnect telegramConnect;
@Override
public void handle(PreCheckoutQuery event) {
final AnswerPreCheckoutQuery answerPreCheckoutQuery = new AnswerPreCheckoutQuery();
answerPreCheckoutQuery.setPreCheckoutQueryId(event.getId());
answerPreCheckoutQuery.setOk(true);
try {
answerPreCheckoutQuery.validate();
telegramConnect.getAbsSender().execute(answerPreCheckoutQuery);
} catch (TelegramApiException e) {
log.error(e.getMessage(), e);
}
}
@Override
public String getEventType() {
return PreCheckoutQuery.class.getName();
}
}

View File

@ -4,7 +4,7 @@ import dev.struchkov.godfather.telegram.domain.attachment.DocumentAttachment;
import dev.struchkov.godfather.telegram.domain.attachment.Picture; import dev.struchkov.godfather.telegram.domain.attachment.Picture;
import dev.struchkov.godfather.telegram.domain.files.ByteContainer; import dev.struchkov.godfather.telegram.domain.files.ByteContainer;
import dev.struchkov.godfather.telegram.domain.files.FileContainer; import dev.struchkov.godfather.telegram.domain.files.FileContainer;
import dev.struchkov.godfather.telegram.main.context.TelegramConnect; import dev.struchkov.godfather.telegram.simple.context.service.TelegramConnect;
import dev.struchkov.godfather.telegram.simple.context.service.AttachmentService; import dev.struchkov.godfather.telegram.simple.context.service.AttachmentService;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;

View File

@ -2,7 +2,7 @@ package dev.struchkov.godfather.telegram.simple.core.service;
import dev.struchkov.godfather.telegram.domain.ChatAction; import dev.struchkov.godfather.telegram.domain.ChatAction;
import dev.struchkov.godfather.telegram.domain.ClientBotCommand; import dev.struchkov.godfather.telegram.domain.ClientBotCommand;
import dev.struchkov.godfather.telegram.main.context.TelegramConnect; import dev.struchkov.godfather.telegram.simple.context.service.TelegramConnect;
import dev.struchkov.godfather.telegram.simple.context.service.TelegramService; import dev.struchkov.godfather.telegram.simple.context.service.TelegramService;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;

View File

@ -1,14 +1,22 @@
package dev.struchkov.godfather.telegram.domain.config; package dev.struchkov.godfather.telegram.domain.config;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
/** /**
* TODO: Добавить описание класса. * TODO: Добавить описание класса.
* *
* @author upagge [18.08.2019] * @author upagge [18.08.2019]
*/ */
@Getter
@Setter
@NoArgsConstructor
public class TelegramBotConfig { public class TelegramBotConfig {
private String username; private String username;
private String token; private String token;
private String webHookUrl;
private ProxyConfig proxyConfig; private ProxyConfig proxyConfig;
@ -17,31 +25,4 @@ public class TelegramBotConfig {
this.token = token; this.token = token;
} }
public TelegramBotConfig() {
}
public void setUsername(String botUsername) {
this.username = botUsername;
}
public void setToken(String botToken) {
this.token = botToken;
}
public String getUsername() {
return username;
}
public String getToken() {
return token;
}
public ProxyConfig getProxyConfig() {
return proxyConfig;
}
public void setProxyConfig(ProxyConfig proxyConfig) {
this.proxyConfig = proxyConfig;
}
} }

View File

@ -5,14 +5,16 @@ import dev.struchkov.godfather.quarkus.domain.BoxAnswer;
import dev.struchkov.godfather.quarkus.domain.SentBox; import dev.struchkov.godfather.quarkus.domain.SentBox;
import dev.struchkov.godfather.quarkus.domain.action.PreSendProcessing; import dev.struchkov.godfather.quarkus.domain.action.PreSendProcessing;
import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
import dev.struchkov.godfather.telegram.main.context.TelegramConnect; import dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload;
import dev.struchkov.godfather.telegram.main.sender.util.KeyBoardConvert; import dev.struchkov.godfather.telegram.main.sender.util.KeyBoardConvert;
import dev.struchkov.godfather.telegram.quarkus.context.repository.SenderRepository; import dev.struchkov.godfather.telegram.quarkus.context.repository.SenderRepository;
import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramConnect;
import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramSending; import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramSending;
import io.smallrye.mutiny.Uni; import io.smallrye.mutiny.Uni;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.api.methods.invoices.SendInvoice;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage; import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText; import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
@ -23,6 +25,7 @@ import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import java.io.Serializable; import java.io.Serializable;
import java.util.List; import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.DISABLE_NOTIFICATION; import static dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload.DISABLE_NOTIFICATION;
@ -95,6 +98,13 @@ public class TelegramSender implements TelegramSending {
.onItem().transformToUni( .onItem().transformToUni(
v -> { v -> {
final String recipientTelegramId = boxAnswer.getRecipientPersonId(); final String recipientTelegramId = boxAnswer.getRecipientPersonId();
final Optional<SendInvoice> optInvoice = boxAnswer.getPayLoad(BoxAnswerPayload.INVOICE);
if (optInvoice.isPresent()) {
final SendInvoice sendInvoice = optInvoice.get();
return Uni.createFrom().completionStage(executeAsync(sendInvoice))
.onItem().transform(ignore -> null);
} else {
if (boxAnswer.isReplace()) { if (boxAnswer.isReplace()) {
final String replaceMessageId = boxAnswer.getReplaceMessageId(); final String replaceMessageId = boxAnswer.getReplaceMessageId();
if (checkNotNull(replaceMessageId)) { if (checkNotNull(replaceMessageId)) {
@ -116,6 +126,7 @@ public class TelegramSender implements TelegramSending {
} }
return sendMessage(recipientTelegramId, boxAnswer, saveMessageId); return sendMessage(recipientTelegramId, boxAnswer, saveMessageId);
} }
}
); );
} }
@ -201,6 +212,15 @@ public class TelegramSender implements TelegramSending {
return completedFuture(null); return completedFuture(null);
} }
private CompletableFuture<Message> executeAsync(SendInvoice sendInvoice) {
try {
return absSender.executeAsync(sendInvoice);
} catch (TelegramApiException e) {
log.error(e.getMessage(), e);
}
return completedFuture(null);
}
@Override @Override
public SendType getType() { public SendType getType() {
return SendType.PRIVATE; return SendType.PRIVATE;

View File

@ -5,12 +5,14 @@ import dev.struchkov.godfather.simple.domain.BoxAnswer;
import dev.struchkov.godfather.simple.domain.SentBox; import dev.struchkov.godfather.simple.domain.SentBox;
import dev.struchkov.godfather.simple.domain.action.PreSendProcessing; import dev.struchkov.godfather.simple.domain.action.PreSendProcessing;
import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard; import dev.struchkov.godfather.telegram.domain.keyboard.InlineKeyBoard;
import dev.struchkov.godfather.telegram.main.context.TelegramConnect; import dev.struchkov.godfather.telegram.main.context.BoxAnswerPayload;
import dev.struchkov.godfather.telegram.simple.context.repository.SenderRepository; import dev.struchkov.godfather.telegram.simple.context.repository.SenderRepository;
import dev.struchkov.godfather.telegram.simple.context.service.TelegramConnect;
import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending; import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.api.methods.invoices.SendInvoice;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage; import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText; import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageText;
@ -93,6 +95,19 @@ public class TelegramSender implements TelegramSending {
final String recipientTelegramId = boxAnswer.getRecipientPersonId(); final String recipientTelegramId = boxAnswer.getRecipientPersonId();
isNotNull(recipientTelegramId); isNotNull(recipientTelegramId);
final Optional<SendInvoice> optInvoice = boxAnswer.getPayLoad(BoxAnswerPayload.INVOICE);
if (optInvoice.isPresent()) {
final SendInvoice sendInvoice = optInvoice.get();
try {
sendInvoice.validate();
absSender.execute(sendInvoice);
return Optional.empty();
} catch (TelegramApiException e) {
log.error(e.getMessage(), e);
return Optional.empty();
}
}
BoxAnswer preparedAnswer = boxAnswer; BoxAnswer preparedAnswer = boxAnswer;
for (PreSendProcessing preSendProcessor : preSendProcessors) { for (PreSendProcessing preSendProcessor : preSendProcessors) {
preparedAnswer = preSendProcessor.pretreatment(boxAnswer); preparedAnswer = preSendProcessor.pretreatment(boxAnswer);

24
telegram-webhook/pom.xml Normal file
View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-bot</artifactId>
<version>0.0.53-SNAPSHOT</version>
</parent>
<artifactId>telegram-webhook</artifactId>
<packaging>pom</packaging>
<modules>
<module>telegram-webhook-quarkus</module>
</modules>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-webhook</artifactId>
<version>0.0.53-SNAPSHOT</version>
</parent>
<artifactId>telegram-webhook-quarkus</artifactId>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>dev.struchkov.godfather.telegram</groupId>
<artifactId>telegram-context-quarkus</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.reactive</groupId>
<artifactId>smallrye-mutiny-vertx-core</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-meta</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,50 @@
package dev.struchkov.godfather.telegram.webhook;
import dev.struchkov.godfather.telegram.domain.config.TelegramBotConfig;
import dev.struchkov.godfather.telegram.quarkus.context.service.EventDistributor;
import io.smallrye.mutiny.Uni;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.meta.api.objects.Update;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Slf4j
@Path("callback")
public class WebhookController {
private final String pathKey;
private final EventDistributor eventDistributor;
public WebhookController(TelegramBotConfig telegramBotConfig, EventDistributor eventDistributor) {
this.eventDistributor = eventDistributor;
this.pathKey = telegramBotConfig.getWebHookUrl().split("callback")[1];
}
@POST
@Path("/{botPath}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Uni<Response> updateReceived(@PathParam("botPath") String botPath, Update update) {
return Uni.createFrom().voidItem()
// .onItem().invoke(() -> isTrue(pathKey.equals(botPath), accessException("В доступе отказано!")))
.onItem().ignore().andSwitchTo(() -> eventDistributor.processing(update))
.onItem().transform(ignore -> Response.ok().build());
}
@GET
@Path("/{botPath}")
@Produces(MediaType.APPLICATION_JSON)
public Uni<String> testReceived(@PathParam("botPath") String botPath) {
return Uni.createFrom().voidItem()
// .onItem().invoke(() -> isTrue(pathKey.equals(botPath), accessException("В доступе отказано!")))
.onItem().transform(ignore -> "Hi there " + botPath + "!");
}
}