feat: add webhook endpoint testing and improved logging

main
borja (aider) 3 months ago
parent 7003e50a41
commit 88e0c2edfd

@ -77,6 +77,12 @@ export class WebhookManager {
static async registerWebhook(): Promise<WebhookResponse> { static async registerWebhook(): Promise<WebhookResponse> {
this.validateConfig(); 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 config = this.getConfig();
const apiUrl = this.getApiUrl(); const apiUrl = this.getApiUrl();
@ -88,7 +94,8 @@ export class WebhookManager {
...config, ...config,
webhook: { webhook: {
...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<boolean> { static async verifyWebhook(): Promise<boolean> {
try { 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', method: 'GET',
headers: this.getHeaders(), headers: this.getHeaders(),
}); });
if (!response.ok) { 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; return false;
} }
const data = await response.json(); const data = await response.json();
console.log(' Webhook verification response:', data);
const isEnabled = data?.enabled === true; const isEnabled = data?.enabled === true;
if (!isEnabled) { if (!isEnabled) {
console.error('Webhook not enabled in verification response:', data); console.error('Webhook not enabled in verification response');
} }
return isEnabled; return isEnabled;
} catch (error) { } 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<boolean> {
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; return false;
} }
} }

Loading…
Cancel
Save