|
|
|
@ -39,22 +39,23 @@ export class WebhookServer {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (process.env.NODE_ENV !== 'test') {
|
|
|
|
|
console.log('ℹ️ Incoming webhook request:', {
|
|
|
|
|
method: request.method,
|
|
|
|
|
path: url.pathname,
|
|
|
|
|
time: new Date().toISOString()
|
|
|
|
|
});
|
|
|
|
|
console.log('ℹ️ Incoming webhook request:')
|
|
|
|
|
// console.log('ℹ️ Incoming webhook request:', {
|
|
|
|
|
// method: request.method,
|
|
|
|
|
// path: url.pathname,
|
|
|
|
|
// time: new Date().toISOString()
|
|
|
|
|
// });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 1. Method validation
|
|
|
|
|
if (request.method !== 'POST') {
|
|
|
|
|
return new Response('Method not allowed', { status: 405 });
|
|
|
|
|
return new Response('🚫 Method not allowed', { status: 405 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Content-Type validation
|
|
|
|
|
const contentType = request.headers.get('content-type');
|
|
|
|
|
if (!contentType?.includes('application/json')) {
|
|
|
|
|
return new Response('Invalid content type', { status: 400 });
|
|
|
|
|
return new Response('🚫 Invalid content type', { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
@ -62,13 +63,13 @@ export class WebhookServer {
|
|
|
|
|
const payload = await request.json() as WebhookPayload;
|
|
|
|
|
|
|
|
|
|
if (!payload.event || !payload.instance) {
|
|
|
|
|
return new Response('Invalid payload', { status: 400 });
|
|
|
|
|
return new Response('🚫 Invalid payload', { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 4. Verify instance matches (skip in test environment unless TEST_VERIFY_INSTANCE is set)
|
|
|
|
|
if ((process.env.NODE_ENV !== 'test' || process.env.TEST_VERIFY_INSTANCE) &&
|
|
|
|
|
payload.instance !== process.env.EVOLUTION_API_INSTANCE) {
|
|
|
|
|
return new Response('Invalid instance', { status: 403 });
|
|
|
|
|
return new Response('🚫 Invalid instance', { status: 403 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 5. Route events
|
|
|
|
|