|
|
|
@ -77,6 +77,12 @@ export class WebhookManager {
|
|
|
|
|
static async registerWebhook(): Promise<WebhookResponse> {
|
|
|
|
|
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<boolean> {
|
|
|
|
|
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<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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|