Configure Evolution webhook for bridge replies
This commit is contained in:
@@ -135,6 +135,36 @@ public final class EvolutionApiClient {
|
|||||||
return resp;
|
return resp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void configurarWebhookRespostas(String baseUrl, String instanceName, String webhookUrl, String webhookSecret) throws IOException, InterruptedException {
|
||||||
|
if (baseUrl == null || baseUrl.isBlank()) {
|
||||||
|
baseUrl = defaultBaseUrl;
|
||||||
|
}
|
||||||
|
if (mockMode(baseUrl) || webhookUrl == null || webhookUrl.isBlank()) return;
|
||||||
|
Map<String, Object> headers = new LinkedHashMap<>();
|
||||||
|
if (webhookSecret != null && !webhookSecret.isBlank()) headers.put("X-Webhook-Secret", webhookSecret);
|
||||||
|
Map<String, Object> webhook = new LinkedHashMap<>();
|
||||||
|
webhook.put("enabled", true);
|
||||||
|
webhook.put("url", webhookUrl);
|
||||||
|
webhook.put("webhook_by_events", false);
|
||||||
|
webhook.put("webhook_base64", false);
|
||||||
|
webhook.put("byEvents", false);
|
||||||
|
webhook.put("base64", false);
|
||||||
|
webhook.put("events", java.util.List.of("MESSAGES_UPSERT"));
|
||||||
|
if (!headers.isEmpty()) webhook.put("headers", headers);
|
||||||
|
Map<String, Object> payload = new LinkedHashMap<>();
|
||||||
|
payload.put("webhook", webhook);
|
||||||
|
HttpRequest req = HttpRequest.newBuilder(URI.create(clean(baseUrl) + "/webhook/set/" + instanceName))
|
||||||
|
.timeout(Duration.ofSeconds(20))
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.header("apikey", globalApiKey)
|
||||||
|
.POST(HttpRequest.BodyPublishers.ofString(Json.write(payload)))
|
||||||
|
.build();
|
||||||
|
HttpResponse<String> resp = httpClient.send(req, HttpResponse.BodyHandlers.ofString());
|
||||||
|
if (resp.statusCode() < 200 || resp.statusCode() >= 300) {
|
||||||
|
throw new IOException("Evolution API retornou HTTP " + resp.statusCode() + ": " + resp.body());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public String enviarTexto(String baseUrl, String instanceName, String token, String numero, String texto) throws IOException, InterruptedException {
|
public String enviarTexto(String baseUrl, String instanceName, String token, String numero, String texto) throws IOException, InterruptedException {
|
||||||
if (baseUrl == null || baseUrl.isBlank()) {
|
if (baseUrl == null || baseUrl.isBlank()) {
|
||||||
baseUrl = defaultBaseUrl;
|
baseUrl = defaultBaseUrl;
|
||||||
|
|||||||
@@ -80,6 +80,7 @@ public final class ExternalBridgeHandler implements HttpHandler {
|
|||||||
List<Destinatario> destinatarios = resolverDestinatarios(empresaId, destinatariosRaw);
|
List<Destinatario> destinatarios = resolverDestinatarios(empresaId, destinatariosRaw);
|
||||||
if (destinatarios.isEmpty()) throw new IllegalArgumentException("Nenhum destinatario encontrado.");
|
if (destinatarios.isEmpty()) throw new IllegalArgumentException("Nenhum destinatario encontrado.");
|
||||||
String[] parts = String.valueOf(inst.get("instance_token_enc")).split(":", 2);
|
String[] parts = String.valueOf(inst.get("instance_token_enc")).split(":", 2);
|
||||||
|
configurarWebhookEvolution(inst, parts[0]);
|
||||||
List<Map<String,Object>> resultados = new ArrayList<>();
|
List<Map<String,Object>> resultados = new ArrayList<>();
|
||||||
try (Connection m = masterDs.getMasterConnection()) {
|
try (Connection m = masterDs.getMasterConnection()) {
|
||||||
for (Destinatario d : destinatarios) {
|
for (Destinatario d : destinatarios) {
|
||||||
@@ -198,6 +199,16 @@ public final class ExternalBridgeHandler implements HttpHandler {
|
|||||||
return URLEncoder.encode(value == null ? "" : value, StandardCharsets.UTF_8);
|
return URLEncoder.encode(value == null ? "" : value, StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void configurarWebhookEvolution(Map<String,Object> inst, String instanceName) {
|
||||||
|
try {
|
||||||
|
String publicUrl = firstNonBlank(System.getenv("APP_PUBLIC_URL"), "https://condominio.seconectai.com.br");
|
||||||
|
String url = publicUrl.replaceAll("/+$", "") + "/api/webhooks/evolution/respostas";
|
||||||
|
evolutionApiClient.configurarWebhookRespostas(str(inst.get("instance_url")), instanceName, url, webhookSecret);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Falha ao configurar webhook da Evolution: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void validarWebhookEvolution(HttpExchange ex) {
|
private void validarWebhookEvolution(HttpExchange ex) {
|
||||||
String recebido = ex.getRequestHeaders().getFirst("X-Webhook-Secret");
|
String recebido = ex.getRequestHeaders().getFirst("X-Webhook-Secret");
|
||||||
if (webhookSecret == null || webhookSecret.isBlank() || recebido == null || !MessageDigest.isEqual(webhookSecret.getBytes(StandardCharsets.UTF_8), recebido.getBytes(StandardCharsets.UTF_8))) {
|
if (webhookSecret == null || webhookSecret.isBlank() || recebido == null || !MessageDigest.isEqual(webhookSecret.getBytes(StandardCharsets.UTF_8), recebido.getBytes(StandardCharsets.UTF_8))) {
|
||||||
|
|||||||
Reference in New Issue
Block a user