This commit is contained in:
parent
c1febc78e9
commit
77f2c67540
@ -21,7 +21,8 @@ public class TelegramWebhookConnect implements TelegramConnect {
|
|||||||
if (webhookConfig.isEnable()) {
|
if (webhookConfig.isEnable()) {
|
||||||
log.info("Инициализация webhook соединения. {}", telegramBotConfig.getWebhookConfig());
|
log.info("Инициализация webhook соединения. {}", telegramBotConfig.getWebhookConfig());
|
||||||
final SetWebhook setWebhook = SetWebhook.builder()
|
final SetWebhook setWebhook = SetWebhook.builder()
|
||||||
.url(webhookConfig.getRootUrl() + "/" + webhookConfig.getControllerPath() + "/" + webhookConfig.getPath() + "?webhookAccessKey=" + webhookConfig.getAccessKey())
|
.secretToken(webhookConfig.getSecretToken())
|
||||||
|
.url(webhookConfig.getUrl())
|
||||||
.build();
|
.build();
|
||||||
bot.setWebhook(setWebhook);
|
bot.setWebhook(setWebhook);
|
||||||
webhookBot = bot;
|
webhookBot = bot;
|
||||||
|
@ -25,7 +25,8 @@ public class TelegramWebhookConnect implements TelegramConnect {
|
|||||||
if (webhookConfig.isEnable()) {
|
if (webhookConfig.isEnable()) {
|
||||||
log.info("Инициализация webhook соединения. {}", telegramBotConfig.getWebhookConfig());
|
log.info("Инициализация webhook соединения. {}", telegramBotConfig.getWebhookConfig());
|
||||||
final SetWebhook setWebhook = SetWebhook.builder()
|
final SetWebhook setWebhook = SetWebhook.builder()
|
||||||
.url(webhookConfig.getRootUrl() + "/" + webhookConfig.getControllerPath() + "/" + webhookConfig.getPath() + "?webhookAccessKey=" + webhookConfig.getAccessKey())
|
.secretToken(webhookConfig.getSecretToken())
|
||||||
|
.url(webhookConfig.getUrl())
|
||||||
.build();
|
.build();
|
||||||
bot.setWebhook(setWebhook);
|
bot.setWebhook(setWebhook);
|
||||||
webhookBot = bot;
|
webhookBot = bot;
|
||||||
|
@ -12,15 +12,9 @@ public class WebhookConfig {
|
|||||||
private boolean enable = false;
|
private boolean enable = false;
|
||||||
|
|
||||||
@ToString.Exclude
|
@ToString.Exclude
|
||||||
private String rootUrl;
|
private String url;
|
||||||
|
|
||||||
@ToString.Exclude
|
@ToString.Exclude
|
||||||
private String controllerPath;
|
private String secretToken;
|
||||||
|
|
||||||
@ToString.Exclude
|
|
||||||
private String path;
|
|
||||||
|
|
||||||
@ToString.Exclude
|
|
||||||
private String accessKey;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -8,11 +8,11 @@ import org.telegram.telegrambots.meta.api.objects.Update;
|
|||||||
|
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.GET;
|
import javax.ws.rs.GET;
|
||||||
|
import javax.ws.rs.HeaderParam;
|
||||||
import javax.ws.rs.POST;
|
import javax.ws.rs.POST;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.PathParam;
|
import javax.ws.rs.PathParam;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.QueryParam;
|
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
|
||||||
@ -20,29 +20,29 @@ import static dev.struchkov.haiti.context.exception.AccessException.accessExcept
|
|||||||
import static dev.struchkov.haiti.utils.Inspector.isTrue;
|
import static dev.struchkov.haiti.utils.Inspector.isTrue;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
|
@Path("callback")
|
||||||
public class WebhookController {
|
public class WebhookController {
|
||||||
|
|
||||||
public static final String ERROR_ACCESS = "В доступе отказано!";
|
public static final String ERROR_ACCESS = "В доступе отказано!";
|
||||||
private final String pathKey;
|
private final String pathKey = "bot";
|
||||||
private final String accessKey;
|
private final String secretToken;
|
||||||
private final EventDistributor eventDistributor;
|
private final EventDistributor eventDistributor;
|
||||||
|
|
||||||
public WebhookController(TelegramBotConfig telegramBotConfig, EventDistributor eventDistributor) {
|
public WebhookController(TelegramBotConfig telegramBotConfig, EventDistributor eventDistributor) {
|
||||||
this.accessKey = telegramBotConfig.getWebhookConfig().getAccessKey();
|
this.secretToken = telegramBotConfig.getWebhookConfig().getSecretToken();
|
||||||
this.eventDistributor = eventDistributor;
|
this.eventDistributor = eventDistributor;
|
||||||
this.pathKey = telegramBotConfig.getWebhookConfig().getPath();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Path("{webhookPath}")
|
@Path("{webhookPath}")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
@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, @HeaderParam("X-Telegram-Bot-Api-Secret-Token") String secretTokenFromTelegram, Update update) {
|
||||||
return Uni.createFrom().voidItem()
|
return Uni.createFrom().voidItem()
|
||||||
.invoke(() -> log.trace("Получено webhook событие"))
|
.invoke(() -> log.trace("Получено webhook событие"))
|
||||||
.invoke(() -> {
|
.invoke(() -> {
|
||||||
isTrue(pathKey.equals(botPath), accessException(ERROR_ACCESS));
|
isTrue(pathKey.equals(botPath), accessException(ERROR_ACCESS));
|
||||||
isTrue(accessKey.equals(webhookAccessKey), accessException(ERROR_ACCESS));
|
isTrue(secretToken.equals(secretTokenFromTelegram), accessException(ERROR_ACCESS));
|
||||||
})
|
})
|
||||||
.onItem().ignore().andSwitchTo(() -> eventDistributor.processing(update))
|
.onItem().ignore().andSwitchTo(() -> eventDistributor.processing(update))
|
||||||
.invoke(() -> log.trace("Webhook событие успешно обработано"))
|
.invoke(() -> log.trace("Webhook событие успешно обработано"))
|
||||||
@ -52,11 +52,11 @@ public class WebhookController {
|
|||||||
@GET
|
@GET
|
||||||
@Path("{webhookPath}")
|
@Path("{webhookPath}")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Uni<String> testReceived(@PathParam("webhookPath") String botPath, @QueryParam("webhookAccessKey") String webhookAccessKey) {
|
public Uni<String> testReceived(@PathParam("webhookPath") String botPath, @HeaderParam("X-Telegram-Bot-Api-Secret-Token") String secretTokenFromTelegram) {
|
||||||
return Uni.createFrom().voidItem()
|
return Uni.createFrom().voidItem()
|
||||||
.onItem().invoke(() -> {
|
.onItem().invoke(() -> {
|
||||||
isTrue(pathKey.equals(botPath), accessException(ERROR_ACCESS));
|
isTrue(pathKey.equals(botPath), accessException(ERROR_ACCESS));
|
||||||
isTrue(accessKey.equals(webhookAccessKey), accessException(ERROR_ACCESS));
|
isTrue(secretToken.equals(secretTokenFromTelegram), accessException(ERROR_ACCESS));
|
||||||
})
|
})
|
||||||
.onItem().transform(ignore -> "Hi there " + botPath + "!");
|
.onItem().transform(ignore -> "Hi there " + botPath + "!");
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user