mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-19 03:42:23 -06:00
refactor: change webhook reception for all instances
This commit refactors the webhook reception to handle all instances at once. Previously, each instance had its own webhook endpoint, but now there is a single endpoint for all instances. This change simplifies the codebase and reduces the potential for errors. The main changes include: - Modifying the `InstanceController` to update the webhook URL for all instances. - Modifying the `WebhookController` to handle the reception of webhooks for all instances. - Modifying the `IndexRouter` and `WebhookRouter` to add a new route for the webhook reception endpoint. - Modifying the `ServerModule` to inject the `PrismaRepository` into the `WebhookService`. - Modifying the `WebhookService` to handle the reception of webhooks for all instances. These changes improve the maintainability and scalability of the application, as there is no longer a need to manage individual webhook endpoints for each instance.
This commit is contained in:
@@ -187,7 +187,7 @@ export class WAMonitoringService {
|
||||
data: {
|
||||
id: data.instanceId,
|
||||
name: data.instanceName,
|
||||
connectionStatus: 'close',
|
||||
connectionStatus: data.integration && data.integration === Integration.WHATSAPP_BUSINESS ? 'open' : 'close',
|
||||
number: data.number,
|
||||
integration: data.integration || Integration.WHATSAPP_BAILEYS,
|
||||
token: data.hash,
|
||||
|
||||
@@ -3,10 +3,11 @@ import { Webhook } from '@prisma/client';
|
||||
import { Logger } from '../../config/logger.config';
|
||||
import { InstanceDto } from '../dto/instance.dto';
|
||||
import { WebhookDto } from '../dto/webhook.dto';
|
||||
import { PrismaRepository } from '../repository/repository.service';
|
||||
import { WAMonitoringService } from './monitor.service';
|
||||
|
||||
export class WebhookService {
|
||||
constructor(private readonly waMonitor: WAMonitoringService) {}
|
||||
constructor(private readonly waMonitor: WAMonitoringService, public readonly prismaRepository: PrismaRepository) {}
|
||||
|
||||
private readonly logger = new Logger(WebhookService.name);
|
||||
|
||||
@@ -29,4 +30,32 @@ export class WebhookService {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public async receiveWebhook(data: any) {
|
||||
if (data.object === 'whatsapp_business_account') {
|
||||
data.entry?.forEach(async (entry: any) => {
|
||||
const numberId = entry.changes[0].value.metadata.phone_number_id;
|
||||
|
||||
if (!numberId) {
|
||||
this.logger.error('WebhookService -> receiveWebhook -> numberId not found');
|
||||
return;
|
||||
}
|
||||
|
||||
const instance = await this.prismaRepository.instance.findFirst({
|
||||
where: { number: numberId },
|
||||
});
|
||||
|
||||
if (!instance) {
|
||||
this.logger.error('WebhookService -> receiveWebhook -> instance not found');
|
||||
return;
|
||||
}
|
||||
|
||||
await this.waMonitor.waInstances[instance.name].connectToWhatsapp(data);
|
||||
|
||||
return;
|
||||
});
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user