mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-20 04:12:23 -06:00
refactor: integrations folder
This commit is contained in:
@@ -48,7 +48,7 @@ import { chatwootImport } from '@api/integrations/chatbot/chatwoot/utils/chatwoo
|
||||
import * as s3Service from '@api/integrations/storage/s3/libs/minio.server';
|
||||
import { ProviderFiles } from '@api/provider/sessions';
|
||||
import { PrismaRepository } from '@api/repository/repository.service';
|
||||
import { waMonitor } from '@api/server.module';
|
||||
import { chatbotController, waMonitor } from '@api/server.module';
|
||||
import { CacheService } from '@api/services/cache.service';
|
||||
import { ChannelStartupService } from '@api/services/channel.service';
|
||||
import { Events, MessageSubtype, TypeMediaMessage, wa } from '@api/types/wa.types';
|
||||
@@ -1223,39 +1223,12 @@ export class BaileysStartupService extends ChannelStartupService {
|
||||
|
||||
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
|
||||
|
||||
if (this.configService.get<Typebot>('TYPEBOT').ENABLED) {
|
||||
if (type === 'notify') {
|
||||
if (messageRaw.messageType !== 'reactionMessage')
|
||||
await this.typebotService.sendTypebot(
|
||||
{ instanceName: this.instance.name, instanceId: this.instanceId },
|
||||
messageRaw.key.remoteJid,
|
||||
messageRaw,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.configService.get<Openai>('OPENAI').ENABLED) {
|
||||
if (type === 'notify') {
|
||||
if (messageRaw.messageType !== 'reactionMessage')
|
||||
await this.openaiService.sendOpenai(
|
||||
{ instanceName: this.instance.name, instanceId: this.instanceId },
|
||||
messageRaw.key.remoteJid,
|
||||
messageRaw.pushName,
|
||||
messageRaw,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.configService.get<Dify>('DIFY').ENABLED) {
|
||||
if (type === 'notify') {
|
||||
if (messageRaw.messageType !== 'reactionMessage')
|
||||
await this.difyService.sendDify(
|
||||
{ instanceName: this.instance.name, instanceId: this.instanceId },
|
||||
messageRaw.key.remoteJid,
|
||||
messageRaw,
|
||||
);
|
||||
}
|
||||
}
|
||||
await chatbotController.emit({
|
||||
instance: { instanceName: this.instance.name, instanceId: this.instanceId },
|
||||
remoteJid: messageRaw.key.remoteJid,
|
||||
msg: messageRaw,
|
||||
pushName: messageRaw.pushName,
|
||||
});
|
||||
|
||||
const contact = await this.prismaRepository.contact.findFirst({
|
||||
where: { remoteJid: received.key.remoteJid, instanceId: this.instanceId },
|
||||
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
import * as s3Service from '@api/integrations/storage/s3/libs/minio.server';
|
||||
import { ProviderFiles } from '@api/provider/sessions';
|
||||
import { PrismaRepository } from '@api/repository/repository.service';
|
||||
import { chatbotController } from '@api/server.module';
|
||||
import { CacheService } from '@api/services/cache.service';
|
||||
import { ChannelStartupService } from '@api/services/channel.service';
|
||||
import { Events, wa } from '@api/types/wa.types';
|
||||
@@ -481,6 +482,13 @@ export class BusinessStartupService extends ChannelStartupService {
|
||||
|
||||
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
|
||||
|
||||
await chatbotController.emit({
|
||||
instance: { instanceName: this.instance.name, instanceId: this.instanceId },
|
||||
remoteJid: messageRaw.key.remoteJid,
|
||||
msg: messageRaw,
|
||||
pushName: messageRaw.pushName,
|
||||
});
|
||||
|
||||
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
|
||||
const chatwootSentMessage = await this.chatwootService.eventWhatsapp(
|
||||
Events.MESSAGES_UPSERT,
|
||||
@@ -495,34 +503,6 @@ export class BusinessStartupService extends ChannelStartupService {
|
||||
}
|
||||
}
|
||||
|
||||
if (this.configService.get<Typebot>('TYPEBOT').ENABLED) {
|
||||
if (messageRaw.messageType !== 'reactionMessage')
|
||||
await this.typebotService.sendTypebot(
|
||||
{ instanceName: this.instance.name, instanceId: this.instanceId },
|
||||
messageRaw.key.remoteJid,
|
||||
messageRaw,
|
||||
);
|
||||
}
|
||||
|
||||
if (this.configService.get<Openai>('OPENAI').ENABLED) {
|
||||
if (messageRaw.messageType !== 'reactionMessage')
|
||||
await this.openaiService.sendOpenai(
|
||||
{ instanceName: this.instance.name, instanceId: this.instanceId },
|
||||
messageRaw.key.remoteJid,
|
||||
pushName,
|
||||
messageRaw,
|
||||
);
|
||||
}
|
||||
|
||||
if (this.configService.get<Dify>('DIFY').ENABLED) {
|
||||
if (messageRaw.messageType !== 'reactionMessage')
|
||||
await this.difyService.sendDify(
|
||||
{ instanceName: this.instance.name, instanceId: this.instanceId },
|
||||
messageRaw.key.remoteJid,
|
||||
messageRaw,
|
||||
);
|
||||
}
|
||||
|
||||
await this.prismaRepository.message.create({
|
||||
data: messageRaw,
|
||||
});
|
||||
|
||||
71
src/api/integrations/chatbot/chatbot.controller.ts
Normal file
71
src/api/integrations/chatbot/chatbot.controller.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { InstanceDto } from '@api/dto/instance.dto';
|
||||
import {
|
||||
difyController,
|
||||
openaiController,
|
||||
typebotController,
|
||||
websocketController,
|
||||
} from '@api/integrations/integration.module';
|
||||
import { PrismaRepository } from '@api/repository/repository.service';
|
||||
import { WAMonitoringService } from '@api/services/monitor.service';
|
||||
|
||||
export class ChatbotController {
|
||||
public prismaRepository: PrismaRepository;
|
||||
public waMonitor: WAMonitoringService;
|
||||
|
||||
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
|
||||
this.prisma = prismaRepository;
|
||||
this.monitor = waMonitor;
|
||||
}
|
||||
|
||||
public set prisma(prisma: PrismaRepository) {
|
||||
this.prismaRepository = prisma;
|
||||
}
|
||||
|
||||
public get prisma() {
|
||||
return this.prismaRepository;
|
||||
}
|
||||
|
||||
public set monitor(waMonitor: WAMonitoringService) {
|
||||
this.waMonitor = waMonitor;
|
||||
}
|
||||
|
||||
public get monitor() {
|
||||
return this.waMonitor;
|
||||
}
|
||||
|
||||
public async emit({
|
||||
instance,
|
||||
remoteJid,
|
||||
msg,
|
||||
pushName,
|
||||
}: {
|
||||
instance: InstanceDto;
|
||||
remoteJid: string;
|
||||
msg: any;
|
||||
pushName?: string;
|
||||
}): Promise<void> {
|
||||
const emitData = {
|
||||
instance,
|
||||
remoteJid,
|
||||
msg,
|
||||
pushName,
|
||||
};
|
||||
// typebot
|
||||
await typebotController.emit(emitData);
|
||||
|
||||
// openai
|
||||
await openaiController.emit(emitData);
|
||||
|
||||
// dify
|
||||
await difyController.emit(emitData);
|
||||
}
|
||||
|
||||
public async setInstance(instanceName: string, data: any): Promise<any> {
|
||||
// chatwoot
|
||||
if (data.websocketEnabled)
|
||||
await websocketController.set(instanceName, {
|
||||
enabled: true,
|
||||
events: data.websocketEvents,
|
||||
});
|
||||
}
|
||||
}
|
||||
18
src/api/integrations/chatbot/chatbot.router.ts
Normal file
18
src/api/integrations/chatbot/chatbot.router.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { ChatwootRouter } from '@api/integrations/chatbot/chatwoot/routes/chatwoot.router';
|
||||
import { DifyRouter } from '@api/integrations/chatbot/dify/routes/dify.router';
|
||||
import { OpenaiRouter } from '@api/integrations/chatbot/openai/routes/openai.router';
|
||||
import { TypebotRouter } from '@api/integrations/chatbot/typebot/routes/typebot.router';
|
||||
import { Router } from 'express';
|
||||
|
||||
export class ChatbotRouter {
|
||||
public readonly router: Router;
|
||||
|
||||
constructor(...guards: any[]) {
|
||||
this.router = Router();
|
||||
|
||||
this.router.use('/chatwoot', new ChatwootRouter(...guards).router);
|
||||
this.router.use('/typebot', new TypebotRouter(...guards).router);
|
||||
this.router.use('/openai', new OpenaiRouter(...guards).router);
|
||||
this.router.use('/dify', new DifyRouter(...guards).router);
|
||||
}
|
||||
}
|
||||
4
src/api/integrations/chatbot/chatbot.schema.ts
Normal file
4
src/api/integrations/chatbot/chatbot.schema.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from '@api/integrations/chatbot/chatwoot/validate/chatwoot.schema';
|
||||
export * from '@api/integrations/chatbot/dify/validate/dify.schema';
|
||||
export * from '@api/integrations/chatbot/openai/validate/openai.schema';
|
||||
export * from '@api/integrations/chatbot/typebot/validate/typebot.schema';
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Constructor } from '@api/dto/integration.dto';
|
||||
import { Constructor } from '@api/integrations/integration.dto';
|
||||
|
||||
export class ChatwootDto {
|
||||
enabled?: boolean;
|
||||
|
||||
@@ -66,4 +66,19 @@ export class DifyController {
|
||||
|
||||
return this.difyService.ignoreJid(instance, data);
|
||||
}
|
||||
|
||||
public async emit({
|
||||
instance,
|
||||
remoteJid,
|
||||
msg,
|
||||
}: {
|
||||
instance: InstanceDto;
|
||||
remoteJid: string;
|
||||
msg: any;
|
||||
pushName?: string;
|
||||
}) {
|
||||
if (!configService.get<Dify>('DIFY').ENABLED) return;
|
||||
|
||||
await this.difyService.sendDify(instance, remoteJid, msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { RouterBroker } from '@api/abstract/abstract.router';
|
||||
import { InstanceDto } from '@api/dto/instance.dto';
|
||||
import { DifyDto, DifyIgnoreJidDto, DifySettingDto } from '@api/integrations/chatbot/dify/dto/dify.dto';
|
||||
import { difyController } from '@api/integrations/integration.module';
|
||||
import { HttpStatus } from '@api/routes/index.router';
|
||||
import { difyController } from '@api/server.module';
|
||||
import {
|
||||
difyIgnoreJidSchema,
|
||||
difySchema,
|
||||
|
||||
@@ -90,4 +90,20 @@ export class OpenaiController {
|
||||
|
||||
return this.openaiService.ignoreJid(instance, data);
|
||||
}
|
||||
|
||||
public async emit({
|
||||
instance,
|
||||
remoteJid,
|
||||
msg,
|
||||
pushName,
|
||||
}: {
|
||||
instance: InstanceDto;
|
||||
remoteJid: string;
|
||||
msg: any;
|
||||
pushName?: string;
|
||||
}) {
|
||||
if (!configService.get<Openai>('OPENAI').ENABLED) return;
|
||||
|
||||
await this.openaiService.sendOpenai(instance, remoteJid, pushName, msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ import {
|
||||
OpenaiIgnoreJidDto,
|
||||
OpenaiSettingDto,
|
||||
} from '@api/integrations/chatbot/openai/dto/openai.dto';
|
||||
import { openaiController } from '@api/integrations/integration.module';
|
||||
import { HttpStatus } from '@api/routes/index.router';
|
||||
import { openaiController } from '@api/server.module';
|
||||
import {
|
||||
instanceSchema,
|
||||
openaiCredsSchema,
|
||||
|
||||
@@ -72,4 +72,19 @@ export class TypebotController {
|
||||
|
||||
return this.typebotService.ignoreJid(instance, data);
|
||||
}
|
||||
|
||||
public async emit({
|
||||
instance,
|
||||
remoteJid,
|
||||
msg,
|
||||
}: {
|
||||
instance: InstanceDto;
|
||||
remoteJid: string;
|
||||
msg: any;
|
||||
pushName?: string;
|
||||
}) {
|
||||
if (!configService.get<Typebot>('TYPEBOT').ENABLED) return;
|
||||
|
||||
await this.typebotService.sendTypebot(instance, remoteJid, msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { RouterBroker } from '@api/abstract/abstract.router';
|
||||
import { InstanceDto } from '@api/dto/instance.dto';
|
||||
import { TypebotDto, TypebotIgnoreJidDto, TypebotSettingDto } from '@api/integrations/chatbot/typebot/dto/typebot.dto';
|
||||
import { typebotController } from '@api/integrations/integration.module';
|
||||
import { HttpStatus } from '@api/routes/index.router';
|
||||
import { typebotController } from '@api/server.module';
|
||||
import {
|
||||
instanceSchema,
|
||||
typebotIgnoreJidSchema,
|
||||
|
||||
154
src/api/integrations/event/event.controller.ts
Normal file
154
src/api/integrations/event/event.controller.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
import {
|
||||
rabbitmqController,
|
||||
sqsController,
|
||||
webhookController,
|
||||
websocketController,
|
||||
} from '@api/integrations/integration.module';
|
||||
import { PrismaRepository } from '@api/repository/repository.service';
|
||||
import { WAMonitoringService } from '@api/services/monitor.service';
|
||||
import { Server } from 'http';
|
||||
|
||||
export class EventController {
|
||||
public prismaRepository: PrismaRepository;
|
||||
public waMonitor: WAMonitoringService;
|
||||
|
||||
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
|
||||
this.prisma = prismaRepository;
|
||||
this.monitor = waMonitor;
|
||||
}
|
||||
|
||||
public set prisma(prisma: PrismaRepository) {
|
||||
this.prismaRepository = prisma;
|
||||
}
|
||||
|
||||
public get prisma() {
|
||||
return this.prismaRepository;
|
||||
}
|
||||
|
||||
public set monitor(waMonitor: WAMonitoringService) {
|
||||
this.waMonitor = waMonitor;
|
||||
}
|
||||
|
||||
public get monitor() {
|
||||
return this.waMonitor;
|
||||
}
|
||||
|
||||
public readonly events = [
|
||||
'APPLICATION_STARTUP',
|
||||
'QRCODE_UPDATED',
|
||||
'MESSAGES_SET',
|
||||
'MESSAGES_UPSERT',
|
||||
'MESSAGES_EDITED',
|
||||
'MESSAGES_UPDATE',
|
||||
'MESSAGES_DELETE',
|
||||
'SEND_MESSAGE',
|
||||
'CONTACTS_SET',
|
||||
'CONTACTS_UPSERT',
|
||||
'CONTACTS_UPDATE',
|
||||
'PRESENCE_UPDATE',
|
||||
'CHATS_SET',
|
||||
'CHATS_UPSERT',
|
||||
'CHATS_UPDATE',
|
||||
'CHATS_DELETE',
|
||||
'GROUPS_UPSERT',
|
||||
'GROUP_UPDATE',
|
||||
'GROUP_PARTICIPANTS_UPDATE',
|
||||
'CONNECTION_UPDATE',
|
||||
'LABELS_EDIT',
|
||||
'LABELS_ASSOCIATION',
|
||||
'CALL',
|
||||
'TYPEBOT_START',
|
||||
'TYPEBOT_CHANGE_STATUS',
|
||||
'REMOVE_INSTANCE',
|
||||
'LOGOUT_INSTANCE',
|
||||
];
|
||||
|
||||
public init(httpServer: Server): void {
|
||||
// websocket
|
||||
websocketController.init(httpServer);
|
||||
|
||||
// rabbitmq
|
||||
rabbitmqController.init();
|
||||
|
||||
// sqs
|
||||
sqsController.init();
|
||||
}
|
||||
|
||||
public async emit({
|
||||
instanceName,
|
||||
origin,
|
||||
event,
|
||||
data,
|
||||
serverUrl,
|
||||
dateTime,
|
||||
sender,
|
||||
apiKey,
|
||||
local,
|
||||
}: {
|
||||
instanceName: string;
|
||||
origin: string;
|
||||
event: string;
|
||||
data: Object;
|
||||
serverUrl: string;
|
||||
dateTime: string;
|
||||
sender: string;
|
||||
apiKey?: string;
|
||||
local?: boolean;
|
||||
}): Promise<void> {
|
||||
const emitData = {
|
||||
instanceName,
|
||||
origin,
|
||||
event,
|
||||
data,
|
||||
serverUrl,
|
||||
dateTime,
|
||||
sender,
|
||||
apiKey,
|
||||
local,
|
||||
};
|
||||
// websocket
|
||||
await websocketController.emit(emitData);
|
||||
|
||||
// rabbitmq
|
||||
await rabbitmqController.emit(emitData);
|
||||
|
||||
// sqs
|
||||
await sqsController.emit(emitData);
|
||||
|
||||
// webhook
|
||||
await webhookController.emit(emitData);
|
||||
}
|
||||
|
||||
public async setInstance(instanceName: string, data: any): Promise<any> {
|
||||
// websocket
|
||||
if (data.websocketEnabled)
|
||||
await websocketController.set(instanceName, {
|
||||
enabled: true,
|
||||
events: data.websocketEvents,
|
||||
});
|
||||
|
||||
// rabbitmq
|
||||
if (data.rabbitmqEnabled)
|
||||
await rabbitmqController.set(instanceName, {
|
||||
enabled: true,
|
||||
events: data.rabbitmqEvents,
|
||||
});
|
||||
|
||||
// sqs
|
||||
if (data.sqsEnabled)
|
||||
await sqsController.set(instanceName, {
|
||||
enabled: true,
|
||||
events: data.sqsEvents,
|
||||
});
|
||||
|
||||
// webhook
|
||||
if (data.webhookEnabled)
|
||||
await webhookController.set(instanceName, {
|
||||
enabled: true,
|
||||
events: data.webhookEvents,
|
||||
url: data.webhookUrl,
|
||||
webhookBase64: data.webhookBase64,
|
||||
webhookByEvents: data.webhookByEvents,
|
||||
});
|
||||
}
|
||||
}
|
||||
18
src/api/integrations/event/event.router.ts
Normal file
18
src/api/integrations/event/event.router.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { RabbitmqRouter } from '@api/integrations/event/rabbitmq/routes/rabbitmq.router';
|
||||
import { SqsRouter } from '@api/integrations/event/sqs/routes/sqs.router';
|
||||
import { WebhookRouter } from '@api/integrations/event/webhook/routes/webhook.router';
|
||||
import { WebsocketRouter } from '@api/integrations/event/websocket/routes/websocket.router';
|
||||
import { Router } from 'express';
|
||||
|
||||
export class EventRouter {
|
||||
public readonly router: Router;
|
||||
|
||||
constructor(configService: any, ...guards: any[]) {
|
||||
this.router = Router();
|
||||
|
||||
this.router.use('/webhook', new WebhookRouter(configService, ...guards).router);
|
||||
this.router.use('/websocket', new WebsocketRouter(...guards).router);
|
||||
this.router.use('/rabbitmq', new RabbitmqRouter(...guards).router);
|
||||
this.router.use('/sqs', new SqsRouter(...guards).router);
|
||||
}
|
||||
}
|
||||
4
src/api/integrations/event/event.schema.ts
Normal file
4
src/api/integrations/event/event.schema.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from '@api/integrations/event/rabbitmq/validate/rabbitmq.schema';
|
||||
export * from '@api/integrations/event/sqs/validate/sqs.schema';
|
||||
export * from '@api/integrations/event/webhook/validate/webhook.schema';
|
||||
export * from '@api/integrations/event/websocket/validate/websocket.schema';
|
||||
@@ -1,4 +1,3 @@
|
||||
import { EventController } from '@api/controllers/event.controller';
|
||||
import { RabbitmqDto } from '@api/integrations/event/rabbitmq/dto/rabbitmq.dto';
|
||||
import { PrismaRepository } from '@api/repository/repository.service';
|
||||
import { WAMonitoringService } from '@api/services/monitor.service';
|
||||
@@ -8,6 +7,8 @@ import { Logger } from '@config/logger.config';
|
||||
import { NotFoundException } from '@exceptions';
|
||||
import * as amqp from 'amqplib/callback_api';
|
||||
|
||||
import { EventController } from '../../event.controller';
|
||||
|
||||
export class RabbitmqController extends EventController {
|
||||
public amqpChannel: amqp.Channel | null = null;
|
||||
private readonly logger = new Logger(RabbitmqController.name);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Constructor } from '@api/dto/integration.dto';
|
||||
import { Constructor } from '@api/integrations/integration.dto';
|
||||
|
||||
export class RabbitmqDto {
|
||||
enabled: boolean;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { RouterBroker } from '@api/abstract/abstract.router';
|
||||
import { InstanceDto } from '@api/dto/instance.dto';
|
||||
import { RabbitmqDto } from '@api/integrations/event/rabbitmq/dto/rabbitmq.dto';
|
||||
import { rabbitmqController } from '@api/integrations/integration.module';
|
||||
import { HttpStatus } from '@api/routes/index.router';
|
||||
import { rabbitmqController } from '@api/server.module';
|
||||
import { instanceSchema, rabbitmqSchema } from '@validate/validate.schema';
|
||||
import { RequestHandler, Router } from 'express';
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { EventController } from '@api/controllers/event.controller';
|
||||
import { SqsDto } from '@api/integrations/event/sqs/dto/sqs.dto';
|
||||
import { PrismaRepository } from '@api/repository/repository.service';
|
||||
import { WAMonitoringService } from '@api/services/monitor.service';
|
||||
@@ -8,6 +7,8 @@ import { configService, Log, Sqs } from '@config/env.config';
|
||||
import { Logger } from '@config/logger.config';
|
||||
import { NotFoundException } from '@exceptions';
|
||||
|
||||
import { EventController } from '../../event.controller';
|
||||
|
||||
export class SqsController extends EventController {
|
||||
private sqs: SQS;
|
||||
private readonly logger = new Logger(SqsController.name);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Constructor } from '@api/dto/integration.dto';
|
||||
import { Constructor } from '@api/integrations/integration.dto';
|
||||
|
||||
export class SqsDto {
|
||||
enabled: boolean;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { RouterBroker } from '@api/abstract/abstract.router';
|
||||
import { InstanceDto } from '@api/dto/instance.dto';
|
||||
import { SqsDto } from '@api/integrations/event/sqs/dto/sqs.dto';
|
||||
import { sqsController } from '@api/integrations/integration.module';
|
||||
import { HttpStatus } from '@api/routes/index.router';
|
||||
import { sqsController } from '@api/server.module';
|
||||
import { instanceSchema, sqsSchema } from '@validate/validate.schema';
|
||||
import { RequestHandler, Router } from 'express';
|
||||
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { EventController } from '@api/controllers/event.controller';
|
||||
import { PrismaRepository } from '@api/repository/repository.service';
|
||||
import { WAMonitoringService } from '@api/services/monitor.service';
|
||||
import { wa } from '@api/types/wa.types';
|
||||
@@ -8,6 +7,7 @@ import { BadRequestException, NotFoundException } from '@exceptions';
|
||||
import axios from 'axios';
|
||||
import { isURL } from 'class-validator';
|
||||
|
||||
import { EventController } from '../../event.controller';
|
||||
import { WebhookDto } from '../dto/webhook.dto';
|
||||
|
||||
export class WebhookController extends EventController {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Constructor } from '@api/dto/integration.dto';
|
||||
import { Constructor } from '@api/integrations/integration.dto';
|
||||
|
||||
export class WebhookDto {
|
||||
enabled?: boolean;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { RouterBroker } from '@api/abstract/abstract.router';
|
||||
import { InstanceDto } from '@api/dto/instance.dto';
|
||||
import { webhookController } from '@api/integrations/integration.module';
|
||||
import { HttpStatus } from '@api/routes/index.router';
|
||||
import { webhookController } from '@api/server.module';
|
||||
import { ConfigService } from '@config/env.config';
|
||||
import { ConfigService, WaBusiness } from '@config/env.config';
|
||||
import { instanceSchema, webhookSchema } from '@validate/validate.schema';
|
||||
import { RequestHandler, Router } from 'express';
|
||||
|
||||
@@ -31,6 +31,17 @@ export class WebhookRouter extends RouterBroker {
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.get('meta', 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('meta', async (req, res) => {
|
||||
const { body } = req;
|
||||
const response = await webhookController.receiveWebhook(body);
|
||||
|
||||
return res.status(200).json(response);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -20,11 +20,14 @@ const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
|
||||
};
|
||||
};
|
||||
|
||||
export const websocketSchema: JSONSchema7 = {
|
||||
export const webhookSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
enabled: { type: 'boolean', enum: [true, false] },
|
||||
enabled: { type: 'boolean' },
|
||||
url: { type: 'string' },
|
||||
webhookByEvents: { type: 'boolean' },
|
||||
webhookBase64: { type: 'boolean' },
|
||||
events: {
|
||||
type: 'array',
|
||||
minItems: 0,
|
||||
@@ -60,6 +63,6 @@ export const websocketSchema: JSONSchema7 = {
|
||||
},
|
||||
},
|
||||
},
|
||||
required: ['enabled'],
|
||||
...isNotEmpty('enabled'),
|
||||
required: ['enabled', 'url'],
|
||||
...isNotEmpty('enabled', 'url'),
|
||||
};
|
||||
@@ -1,4 +1,3 @@
|
||||
import { EventController } from '@api/controllers/event.controller';
|
||||
import { WebsocketDto } from '@api/integrations/event/websocket/dto/websocket.dto';
|
||||
import { PrismaRepository } from '@api/repository/repository.service';
|
||||
import { WAMonitoringService } from '@api/services/monitor.service';
|
||||
@@ -9,6 +8,8 @@ import { NotFoundException } from '@exceptions';
|
||||
import { Server } from 'http';
|
||||
import { Server as SocketIO } from 'socket.io';
|
||||
|
||||
import { EventController } from '../../event.controller';
|
||||
|
||||
export class WebsocketController extends EventController {
|
||||
private io: SocketIO;
|
||||
private corsConfig: Array<any>;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Constructor } from '@api/dto/integration.dto';
|
||||
import { Constructor } from '@api/integrations/integration.dto';
|
||||
|
||||
export class WebsocketDto {
|
||||
enabled: boolean;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { RouterBroker } from '@api/abstract/abstract.router';
|
||||
import { InstanceDto } from '@api/dto/instance.dto';
|
||||
import { WebsocketDto } from '@api/integrations/event/websocket/dto/websocket.dto';
|
||||
import { websocketController } from '@api/integrations/integration.module';
|
||||
import { HttpStatus } from '@api/routes/index.router';
|
||||
import { websocketController } from '@api/server.module';
|
||||
import { instanceSchema, websocketSchema } from '@validate/validate.schema';
|
||||
import { RequestHandler, Router } from 'express';
|
||||
|
||||
|
||||
11
src/api/integrations/integration.dto.ts
Normal file
11
src/api/integrations/integration.dto.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { ChatwootInstanceMixin } from '@api/integrations/chatbot/chatwoot/dto/chatwoot.dto';
|
||||
import { RabbitMQInstanceMixin } from '@api/integrations/event/rabbitmq/dto/rabbitmq.dto';
|
||||
import { SQSInstanceMixin } from '@api/integrations/event/sqs/dto/sqs.dto';
|
||||
import { WebhookInstanceMixin } from '@api/integrations/event/webhook/dto/webhook.dto';
|
||||
import { WebsocketInstanceMixin } from '@api/integrations/event/websocket/dto/websocket.dto';
|
||||
|
||||
export type Constructor<T = {}> = new (...args: any[]) => T;
|
||||
|
||||
export class IntegrationDto extends WebhookInstanceMixin(
|
||||
WebsocketInstanceMixin(RabbitMQInstanceMixin(SQSInstanceMixin(ChatwootInstanceMixin(class {})))),
|
||||
) {}
|
||||
29
src/api/integrations/integration.module.ts
Normal file
29
src/api/integrations/integration.module.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { prismaRepository, waMonitor } from '@api/server.module';
|
||||
import { configService } from '@config/env.config';
|
||||
|
||||
import { DifyController } from './chatbot/dify/controllers/dify.controller';
|
||||
import { DifyService } from './chatbot/dify/services/dify.service';
|
||||
import { OpenaiController } from './chatbot/openai/controllers/openai.controller';
|
||||
import { OpenaiService } from './chatbot/openai/services/openai.service';
|
||||
import { TypebotController } from './chatbot/typebot/controllers/typebot.controller';
|
||||
import { TypebotService } from './chatbot/typebot/services/typebot.service';
|
||||
import { RabbitmqController } from './event/rabbitmq/controllers/rabbitmq.controller';
|
||||
import { SqsController } from './event/sqs/controllers/sqs.controller';
|
||||
import { WebhookController } from './event/webhook/controllers/webhook.controller';
|
||||
import { WebsocketController } from './event/websocket/controllers/websocket.controller';
|
||||
|
||||
// events
|
||||
export const websocketController = new WebsocketController(prismaRepository, waMonitor);
|
||||
export const rabbitmqController = new RabbitmqController(prismaRepository, waMonitor);
|
||||
export const sqsController = new SqsController(prismaRepository, waMonitor);
|
||||
export const webhookController = new WebhookController(prismaRepository, waMonitor);
|
||||
|
||||
// chatbots
|
||||
const typebotService = new TypebotService(waMonitor, configService, prismaRepository);
|
||||
export const typebotController = new TypebotController(typebotService);
|
||||
|
||||
const openaiService = new OpenaiService(waMonitor, configService, prismaRepository);
|
||||
export const openaiController = new OpenaiController(openaiService);
|
||||
|
||||
const difyService = new DifyService(waMonitor, configService, prismaRepository);
|
||||
export const difyController = new DifyController(difyService);
|
||||
12
src/api/integrations/storage/storage.router.ts
Normal file
12
src/api/integrations/storage/storage.router.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { S3Router } from '@api/integrations/storage/s3/routes/s3.router';
|
||||
import { Router } from 'express';
|
||||
|
||||
export class StorageRouter {
|
||||
public readonly router: Router;
|
||||
|
||||
constructor(...guards: any[]) {
|
||||
this.router = Router();
|
||||
|
||||
this.router.use('/s3', new S3Router(...guards).router);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user