mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-25 22:57:44 -06:00
refactor: channel integration folders
This commit is contained in:
72
src/api/integrations/channel/meta/meta.controller.ts
Normal file
72
src/api/integrations/channel/meta/meta.controller.ts
Normal file
@@ -0,0 +1,72 @@
|
||||
import { PrismaRepository } from '@api/repository/repository.service';
|
||||
import { WAMonitoringService } from '@api/services/monitor.service';
|
||||
import { Logger } from '@config/logger.config';
|
||||
import axios from 'axios';
|
||||
|
||||
import { ChannelController, ChannelControllerInterface } from '../channel.controller';
|
||||
|
||||
export class MetaController extends ChannelController implements ChannelControllerInterface {
|
||||
private readonly logger = new Logger(MetaController.name);
|
||||
|
||||
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
|
||||
super(prismaRepository, waMonitor);
|
||||
}
|
||||
|
||||
integrationEnabled: boolean;
|
||||
|
||||
public async receiveWebhook(data: any) {
|
||||
if (data.object === 'whatsapp_business_account') {
|
||||
if (data.entry[0]?.changes[0]?.field === 'message_template_status_update') {
|
||||
const template = await this.prismaRepository.template.findFirst({
|
||||
where: { templateId: `${data.entry[0].changes[0].value.message_template_id}` },
|
||||
});
|
||||
|
||||
if (!template) {
|
||||
console.log('template not found');
|
||||
return;
|
||||
}
|
||||
|
||||
const { webhookUrl } = template;
|
||||
|
||||
await axios.post(webhookUrl, data.entry[0].changes[0].value, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
data.entry?.forEach(async (entry: any) => {
|
||||
const numberId = entry.changes[0].value.metadata.phone_number_id;
|
||||
|
||||
if (!numberId) {
|
||||
this.logger.error('WebhookService -> receiveWebhookMeta -> numberId not found');
|
||||
return {
|
||||
status: 'success',
|
||||
};
|
||||
}
|
||||
|
||||
const instance = await this.prismaRepository.instance.findFirst({
|
||||
where: { number: numberId },
|
||||
});
|
||||
|
||||
if (!instance) {
|
||||
this.logger.error('WebhookService -> receiveWebhookMeta -> instance not found');
|
||||
return {
|
||||
status: 'success',
|
||||
};
|
||||
}
|
||||
|
||||
await this.waMonitor.waInstances[instance.name].connectToWhatsapp(data);
|
||||
|
||||
return {
|
||||
status: 'success',
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
status: 'success',
|
||||
};
|
||||
}
|
||||
}
|
||||
24
src/api/integrations/channel/meta/meta.router.ts
Normal file
24
src/api/integrations/channel/meta/meta.router.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { RouterBroker } from '@api/abstract/abstract.router';
|
||||
import { metaController } from '@api/server.module';
|
||||
import { ConfigService, WaBusiness } from '@config/env.config';
|
||||
import { Router } from 'express';
|
||||
|
||||
export class MetaRouter extends RouterBroker {
|
||||
constructor(readonly configService: ConfigService) {
|
||||
super();
|
||||
this.router
|
||||
.get(this.routerPath('webhook/meta', false), async (req, res) => {
|
||||
if (req.query['hub.verify_token'] === configService.get<WaBusiness>('WA_BUSINESS').TOKEN_WEBHOOK)
|
||||
res.send(req.query['hub.challenge']);
|
||||
else res.send('Error, wrong validation token');
|
||||
})
|
||||
.post(this.routerPath('webhook/meta', false), async (req, res) => {
|
||||
const { body } = req;
|
||||
const response = await metaController.receiveWebhook(body);
|
||||
|
||||
return res.status(200).json(response);
|
||||
});
|
||||
}
|
||||
|
||||
public readonly router: Router = Router();
|
||||
}
|
||||
1443
src/api/integrations/channel/meta/whatsapp.business.service.ts
Normal file
1443
src/api/integrations/channel/meta/whatsapp.business.service.ts
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user