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

This commit is contained in:
Struchkov Mark 2023-03-19 19:28:11 +03:00
parent c1febc78e9
commit 77f2c67540
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
4 changed files with 15 additions and 19 deletions

View File

@ -21,7 +21,8 @@ public class TelegramWebhookConnect implements TelegramConnect {
if (webhookConfig.isEnable()) {
log.info("Инициализация webhook соединения. {}", telegramBotConfig.getWebhookConfig());
final SetWebhook setWebhook = SetWebhook.builder()
.url(webhookConfig.getRootUrl() + "/" + webhookConfig.getControllerPath() + "/" + webhookConfig.getPath() + "?webhookAccessKey=" + webhookConfig.getAccessKey())
.secretToken(webhookConfig.getSecretToken())
.url(webhookConfig.getUrl())
.build();
bot.setWebhook(setWebhook);
webhookBot = bot;

View File

@ -25,7 +25,8 @@ public class TelegramWebhookConnect implements TelegramConnect {
if (webhookConfig.isEnable()) {
log.info("Инициализация webhook соединения. {}", telegramBotConfig.getWebhookConfig());
final SetWebhook setWebhook = SetWebhook.builder()
.url(webhookConfig.getRootUrl() + "/" + webhookConfig.getControllerPath() + "/" + webhookConfig.getPath() + "?webhookAccessKey=" + webhookConfig.getAccessKey())
.secretToken(webhookConfig.getSecretToken())
.url(webhookConfig.getUrl())
.build();
bot.setWebhook(setWebhook);
webhookBot = bot;

View File

@ -12,15 +12,9 @@ public class WebhookConfig {
private boolean enable = false;
@ToString.Exclude
private String rootUrl;
private String url;
@ToString.Exclude
private String controllerPath;
@ToString.Exclude
private String path;
@ToString.Exclude
private String accessKey;
private String secretToken;
}

View File

@ -8,11 +8,11 @@ import org.telegram.telegrambots.meta.api.objects.Update;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
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;
@Slf4j
@Path("callback")
public class WebhookController {
public static final String ERROR_ACCESS = "В доступе отказано!";
private final String pathKey;
private final String accessKey;
private final String pathKey = "bot";
private final String secretToken;
private final EventDistributor eventDistributor;
public WebhookController(TelegramBotConfig telegramBotConfig, EventDistributor eventDistributor) {
this.accessKey = telegramBotConfig.getWebhookConfig().getAccessKey();
this.secretToken = telegramBotConfig.getWebhookConfig().getSecretToken();
this.eventDistributor = eventDistributor;
this.pathKey = telegramBotConfig.getWebhookConfig().getPath();
}
@POST
@Path("{webhookPath}")
@Consumes(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()
.invoke(() -> log.trace("Получено webhook событие"))
.invoke(() -> {
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))
.invoke(() -> log.trace("Webhook событие успешно обработано"))
@ -52,11 +52,11 @@ public class WebhookController {
@GET
@Path("{webhookPath}")
@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()
.onItem().invoke(() -> {
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 + "!");
}