preparado entorno de desarrollo local
All checks were successful
Deploy conversation layer / deploy (push) Successful in 21s

This commit is contained in:
2025-06-04 23:25:57 -06:00
parent 7fa6a73538
commit 41f6ace10a
4 changed files with 611 additions and 13 deletions

View File

@@ -2,6 +2,20 @@ import express from 'express';
import axios from 'axios';
import { WhatsAppMessage } from './types';
import { getHandler, Handler } from './chatHandlers';
import dotenv from 'dotenv';
dotenv.config();
if (process.env.NODE_ENV === 'development') {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
}
if (
process.env.NODE_ENV !== 'development' &&
process.env.NODE_TLS_REJECT_UNAUTHORIZED === '0'
) {
throw new Error('NODE_TLS_REJECT_UNAUTHORIZED está activado en producción. Abortando.');
}
const app = express();
@@ -25,11 +39,11 @@ function log(level: keyof Console | 'info' | 'warn' | 'error' | 'debug', ...args
async function waitForGateway() {
for (let i = 1; i <= config.MAX_ATTEMPTS; i++) {
try {
await axios.get(`${config.API_URL}/api-docs`);
await axios.get(`${config.API_URL}/api-docs/`);
log('info', '🟢 nucleo-whatsapp ready');
return;
} catch {
log('warn', `Gateway not responding`,' connecting to: ', openWaUrl, ` (attempt ${i}/${config.MAX_ATTEMPTS})…`);
} catch(e) {
log('warn', `Gateway not responding`,' connecting to: ', `${config.API_URL}/api-docs/`, ` (attempt ${i}/${config.MAX_ATTEMPTS})…`, e);
await new Promise(r => setTimeout(r, config.RETRY_MS));
}
}
@@ -67,7 +81,7 @@ async function clearWebhooks() {
}
async function registerWebhook() {
const url = `http://whatsapp-router:${port}/webhook`;
const url = process.env.WEBHOOK_URL || `http://whatsapp-router:${port}/webhook`;
const eventConfig = {
onAck: false,
onAddedToGroup: true,
@@ -112,6 +126,7 @@ async function registerWebhook() {
app.use(express.json());
app.post('/webhook', async (req: express.Request, res: express.Response) => {
log('debug', 'Received webhook request:', req.body);
const { message, text, from } = req.body as {
message?: WhatsAppMessage;
text?: string;
@@ -120,6 +135,12 @@ app.post('/webhook', async (req: express.Request, res: express.Response) => {
const incoming = message || text;
log('debug', 'Webhook received:', {
message: incoming,
from,
chatId: message?.chatId,
type: typeof incoming
});
if (incoming) {
const tipo = typeof incoming === 'string' ? 'texto' : 'objeto';
const origen = from || (message?.chatId ?? 'desconocido');