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

This commit is contained in:
Struchkov Mark 2023-03-19 16:34:53 +03:00
parent 8f8b524b0e
commit be8d9a3c82
Signed by: upagge
GPG Key ID: D3018BE7BA428CA6
6 changed files with 63 additions and 72 deletions

View File

@ -1,6 +1,7 @@
package dev.struchkov.godfather.telegram.quarkus.core; package dev.struchkov.godfather.telegram.quarkus.core;
import dev.struchkov.godfather.telegram.domain.config.TelegramBotConfig; import dev.struchkov.godfather.telegram.domain.config.TelegramBotConfig;
import dev.struchkov.godfather.telegram.domain.config.WebhookConfig;
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.TelegramConnect; import dev.struchkov.godfather.telegram.quarkus.context.service.TelegramConnect;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -18,16 +19,19 @@ public class TelegramWebhookConnect implements TelegramConnect {
} }
private void initWebHook(TelegramBotConfig telegramBotConfig) { private void initWebHook(TelegramBotConfig telegramBotConfig) {
// try { try {
final TelegramWebhookBot bot = new TelegramWebhookBot(telegramBotConfig); final TelegramWebhookBot bot = new TelegramWebhookBot(telegramBotConfig);
final SetWebhook setWebhook = SetWebhook.builder() final WebhookConfig webhookConfig = telegramBotConfig.getWebhookConfig();
.url(telegramBotConfig.getWebHookUrl()) if (webhookConfig.isEnable()) {
.build(); final SetWebhook setWebhook = SetWebhook.builder()
// bot.setWebhook(setWebhook); .url(webhookConfig.getRootUrl() + "/" + webhookConfig.getRootUrl() + "?webhookAccessKey=" + webhookConfig.getAccessKey())
webhookBot = bot; .build();
// } catch (TelegramApiException e) { bot.setWebhook(setWebhook);
// log.error(e.getMessage()); webhookBot = bot;
// } }
} catch (TelegramApiException e) {
log.error(e.getMessage());
}
} }
@Override @Override

View File

@ -1,6 +1,7 @@
package dev.struchkov.godfather.telegram.simple.core; package dev.struchkov.godfather.telegram.simple.core;
import dev.struchkov.godfather.telegram.domain.config.TelegramBotConfig; import dev.struchkov.godfather.telegram.domain.config.TelegramBotConfig;
import dev.struchkov.godfather.telegram.domain.config.WebhookConfig;
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.TelegramConnect; import dev.struchkov.godfather.telegram.simple.context.service.TelegramConnect;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -20,11 +21,14 @@ public class TelegramWebhookConnect implements TelegramConnect {
private void initWebHook(TelegramBotConfig telegramBotConfig) { private void initWebHook(TelegramBotConfig telegramBotConfig) {
try { try {
final TelegramWebhookBot bot = new TelegramWebhookBot(telegramBotConfig); final TelegramWebhookBot bot = new TelegramWebhookBot(telegramBotConfig);
final SetWebhook setWebhook = SetWebhook.builder() final WebhookConfig webhookConfig = telegramBotConfig.getWebhookConfig();
.url(telegramBotConfig.getWebHookUrl()) if (webhookConfig.isEnable()) {
.build(); final SetWebhook setWebhook = SetWebhook.builder()
bot.setWebhook(setWebhook); .url(webhookConfig.getRootUrl() + "/" + webhookConfig.getRootUrl() + "?webhookAccessKey=" + webhookConfig.getAccessKey())
webhookBot = bot; .build();
bot.setWebhook(setWebhook);
webhookBot = bot;
}
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
log.error(e.getMessage()); log.error(e.getMessage());
} }

View File

@ -1,67 +1,24 @@
package dev.struchkov.godfather.telegram.domain.config; package dev.struchkov.godfather.telegram.domain.config;
import lombok.Getter;
import lombok.Setter;
/** /**
* TODO: Добавить описание класса. * TODO: Добавить описание класса.
* *
* @author upagge [30.01.2020] * @author upagge [30.01.2020]
*/ */
@Getter
@Setter
public class ProxyConfig { public class ProxyConfig {
private boolean enable = true; private boolean enable = false;
private String host; private String host;
private Integer port; private Integer port;
private String user; private String user;
private String password; private String password;
private Type type; private Type type;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Type getType() {
return type;
}
public void setType(Type type) {
this.type = type;
}
public boolean isEnable() {
return enable;
}
public void setEnable(boolean enable) {
this.enable = enable;
}
public enum Type { public enum Type {
SOCKS5, SOCKS4, HTTP SOCKS5, SOCKS4, HTTP
} }

View File

@ -16,9 +16,9 @@ public class TelegramBotConfig {
private String username; private String username;
private String token; private String token;
private String webHookUrl;
private ProxyConfig proxyConfig; private ProxyConfig proxyConfig;
private WebhookConfig webhookConfig;
public TelegramBotConfig(String username, String token) { public TelegramBotConfig(String username, String token) {
this.username = username; this.username = username;

View File

@ -0,0 +1,15 @@
package dev.struchkov.godfather.telegram.domain.config;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class WebhookConfig {
private boolean enable = false;
private String rootUrl;
private String path;
private String accessKey;
}

View File

@ -12,38 +12,49 @@ 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;
import static dev.struchkov.haiti.context.exception.AccessException.accessException;
import static dev.struchkov.haiti.utils.Inspector.isTrue;
@Slf4j @Slf4j
@Path("callback")
public class WebhookController { public class WebhookController {
private final String pathKey; private final String pathKey;
private final String accessKey;
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.eventDistributor = eventDistributor; this.eventDistributor = eventDistributor;
this.pathKey = telegramBotConfig.getWebHookUrl().split("callback")[1]; this.pathKey = telegramBotConfig.getWebhookConfig().getPath();
} }
@POST @POST
@Path("/{botPath}") @Path("{webhookPath}")
@Consumes(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Uni<Response> updateReceived(@PathParam("botPath") String botPath, Update update) { public Uni<Response> updateReceived(@PathParam("webhookPath") String botPath, @QueryParam("webhookAccessKey") String webhookAccessKey, Update update) {
return Uni.createFrom().voidItem() return Uni.createFrom().voidItem()
// .onItem().invoke(() -> isTrue(pathKey.equals(botPath), accessException("В доступе отказано!"))) .onItem().invoke(() -> {
isTrue(pathKey.equals(botPath), accessException("В доступе отказано!"));
isTrue(accessKey.equals(webhookAccessKey), accessException("В доступе отказано!"));
})
.onItem().ignore().andSwitchTo(() -> eventDistributor.processing(update)) .onItem().ignore().andSwitchTo(() -> eventDistributor.processing(update))
.onItem().transform(ignore -> Response.ok().build()); .onItem().transform(ignore -> Response.ok().build());
} }
@GET @GET
@Path("/{botPath}") @Path("{webhookPath}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Uni<String> testReceived(@PathParam("botPath") String botPath) { public Uni<String> testReceived(@PathParam("webhookPath") String botPath, @QueryParam("webhookAccessKey") String webhookAccessKey) {
return Uni.createFrom().voidItem() return Uni.createFrom().voidItem()
// .onItem().invoke(() -> isTrue(pathKey.equals(botPath), accessException("В доступе отказано!"))) .onItem().invoke(() -> {
isTrue(pathKey.equals(botPath), accessException("В доступе отказано!"));
isTrue(accessKey.equals(webhookAccessKey), accessException("В доступе отказано!"));
})
.onItem().transform(ignore -> "Hi there " + botPath + "!"); .onItem().transform(ignore -> "Hi there " + botPath + "!");
} }