From 88e0c2edfd427d771b0706671f2b55f3ae84ac71 Mon Sep 17 00:00:00 2001 From: "borja (aider)" Date: Thu, 27 Mar 2025 17:38:13 +0100 Subject: [PATCH] feat: add webhook endpoint testing and improved logging --- src/services/webhook-manager.ts | 66 ++++++++++++++++++++++++++++++--- 1 file changed, 61 insertions(+), 5 deletions(-) diff --git a/src/services/webhook-manager.ts b/src/services/webhook-manager.ts index e27fd71..90603dd 100644 --- a/src/services/webhook-manager.ts +++ b/src/services/webhook-manager.ts @@ -77,6 +77,12 @@ export class WebhookManager { static async registerWebhook(): Promise { this.validateConfig(); + // First test if our own endpoint is reachable + const endpointTest = await this.testWebhookEndpoint(); + if (!endpointTest) { + throw new Error('Webhook endpoint test failed - check your server logs'); + } + const config = this.getConfig(); const apiUrl = this.getApiUrl(); @@ -88,7 +94,8 @@ export class WebhookManager { ...config, webhook: { ...config.webhook, - url: logSafeUrl(config.webhook.url) + url: logSafeUrl(config.webhook.url), + events: config.webhook.events } } }); @@ -125,24 +132,73 @@ export class WebhookManager { static async verifyWebhook(): Promise { try { - const response = await fetch(`${process.env.EVOLUTION_API_URL}/webhook/find/${process.env.EVOLUTION_API_INSTANCE}`, { + const url = `${process.env.EVOLUTION_API_URL}/webhook/find/${process.env.EVOLUTION_API_INSTANCE}`; + console.log('ℹ️ Verifying webhook at:', url); + + const response = await fetch(url, { method: 'GET', headers: this.getHeaders(), }); if (!response.ok) { - console.error('Webhook verification failed:', response.status, response.statusText); + const body = await response.text(); + console.error('❌ Webhook verification failed:', { + status: response.status, + statusText: response.statusText, + body + }); return false; } const data = await response.json(); + console.log('ℹ️ Webhook verification response:', data); + const isEnabled = data?.enabled === true; if (!isEnabled) { - console.error('Webhook not enabled in verification response:', data); + console.error('❌ Webhook not enabled in verification response'); } return isEnabled; } catch (error) { - console.error('Webhook verification error:', error instanceof Error ? error.message : error); + console.error('❌ Webhook verification error:', error instanceof Error ? error.stack : error); + return false; + } + } + + static async testWebhookEndpoint(): Promise { + try { + if (!process.env.WEBHOOK_URL) { + throw new Error('WEBHOOK_URL is not set'); + } + + console.log('ℹ️ Testing webhook endpoint:', process.env.WEBHOOK_URL); + + const testPayload = { + test: true, + timestamp: new Date().toISOString() + }; + + const response = await fetch(process.env.WEBHOOK_URL, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(testPayload), + }); + + if (!response.ok) { + const body = await response.text(); + console.error('❌ Webhook test failed:', { + status: response.status, + statusText: response.statusText, + body + }); + return false; + } + + console.log('✅ Webhook endpoint test successful'); + return true; + } catch (error) { + console.error('❌ Webhook test error:', error instanceof Error ? error.stack : error); return false; } }