Skip to content

Commit 2ec0b84

Browse files
committed
refactor: integrations folder
1 parent 884362e commit 2ec0b84

38 files changed

Lines changed: 212 additions & 259 deletions

‎src/api/dto/instance.dto.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
import { IntegrationDto } from '@api/integrations/integration.dto';
12
import { WAPresence } from 'baileys';
23

3-
import { IntegrationDto } from './integration.dto';
4-
54
export class InstanceDto extends IntegrationDto {
65
instanceName: string;
76
instanceId?: string;

‎src/api/integrations/channel/whatsapp/baileys/whatsapp.baileys.service.ts‎

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import { chatwootImport } from '@api/integrations/chatbot/chatwoot/utils/chatwoo
4848
import * as s3Service from '@api/integrations/storage/s3/libs/minio.server';
4949
import { ProviderFiles } from '@api/provider/sessions';
5050
import { PrismaRepository } from '@api/repository/repository.service';
51-
import { waMonitor } from '@api/server.module';
51+
import { chatbotController, waMonitor } from '@api/server.module';
5252
import { CacheService } from '@api/services/cache.service';
5353
import { ChannelStartupService } from '@api/services/channel.service';
5454
import { Events, MessageSubtype, TypeMediaMessage, wa } from '@api/types/wa.types';
@@ -1223,39 +1223,12 @@ export class BaileysStartupService extends ChannelStartupService {
12231223

12241224
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
12251225

1226-
if (this.configService.get<Typebot>('TYPEBOT').ENABLED) {
1227-
if (type === 'notify') {
1228-
if (messageRaw.messageType !== 'reactionMessage')
1229-
await this.typebotService.sendTypebot(
1230-
{ instanceName: this.instance.name, instanceId: this.instanceId },
1231-
messageRaw.key.remoteJid,
1232-
messageRaw,
1233-
);
1234-
}
1235-
}
1236-
1237-
if (this.configService.get<Openai>('OPENAI').ENABLED) {
1238-
if (type === 'notify') {
1239-
if (messageRaw.messageType !== 'reactionMessage')
1240-
await this.openaiService.sendOpenai(
1241-
{ instanceName: this.instance.name, instanceId: this.instanceId },
1242-
messageRaw.key.remoteJid,
1243-
messageRaw.pushName,
1244-
messageRaw,
1245-
);
1246-
}
1247-
}
1248-
1249-
if (this.configService.get<Dify>('DIFY').ENABLED) {
1250-
if (type === 'notify') {
1251-
if (messageRaw.messageType !== 'reactionMessage')
1252-
await this.difyService.sendDify(
1253-
{ instanceName: this.instance.name, instanceId: this.instanceId },
1254-
messageRaw.key.remoteJid,
1255-
messageRaw,
1256-
);
1257-
}
1258-
}
1226+
await chatbotController.emit({
1227+
instance: { instanceName: this.instance.name, instanceId: this.instanceId },
1228+
remoteJid: messageRaw.key.remoteJid,
1229+
msg: messageRaw,
1230+
pushName: messageRaw.pushName,
1231+
});
12591232

12601233
const contact = await this.prismaRepository.contact.findFirst({
12611234
where: { remoteJid: received.key.remoteJid, instanceId: this.instanceId },

‎src/api/integrations/channel/whatsapp/business/whatsapp.business.service.ts‎

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import * as s3Service from '@api/integrations/storage/s3/libs/minio.server';
1717
import { ProviderFiles } from '@api/provider/sessions';
1818
import { PrismaRepository } from '@api/repository/repository.service';
19+
import { chatbotController } from '@api/server.module';
1920
import { CacheService } from '@api/services/cache.service';
2021
import { ChannelStartupService } from '@api/services/channel.service';
2122
import { Events, wa } from '@api/types/wa.types';
@@ -481,6 +482,13 @@ export class BusinessStartupService extends ChannelStartupService {
481482

482483
this.sendDataWebhook(Events.MESSAGES_UPSERT, messageRaw);
483484

485+
await chatbotController.emit({
486+
instance: { instanceName: this.instance.name, instanceId: this.instanceId },
487+
remoteJid: messageRaw.key.remoteJid,
488+
msg: messageRaw,
489+
pushName: messageRaw.pushName,
490+
});
491+
484492
if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED && this.localChatwoot.enabled) {
485493
const chatwootSentMessage = await this.chatwootService.eventWhatsapp(
486494
Events.MESSAGES_UPSERT,
@@ -495,34 +503,6 @@ export class BusinessStartupService extends ChannelStartupService {
495503
}
496504
}
497505

498-
if (this.configService.get<Typebot>('TYPEBOT').ENABLED) {
499-
if (messageRaw.messageType !== 'reactionMessage')
500-
await this.typebotService.sendTypebot(
501-
{ instanceName: this.instance.name, instanceId: this.instanceId },
502-
messageRaw.key.remoteJid,
503-
messageRaw,
504-
);
505-
}
506-
507-
if (this.configService.get<Openai>('OPENAI').ENABLED) {
508-
if (messageRaw.messageType !== 'reactionMessage')
509-
await this.openaiService.sendOpenai(
510-
{ instanceName: this.instance.name, instanceId: this.instanceId },
511-
messageRaw.key.remoteJid,
512-
pushName,
513-
messageRaw,
514-
);
515-
}
516-
517-
if (this.configService.get<Dify>('DIFY').ENABLED) {
518-
if (messageRaw.messageType !== 'reactionMessage')
519-
await this.difyService.sendDify(
520-
{ instanceName: this.instance.name, instanceId: this.instanceId },
521-
messageRaw.key.remoteJid,
522-
messageRaw,
523-
);
524-
}
525-
526506
await this.prismaRepository.message.create({
527507
data: messageRaw,
528508
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { InstanceDto } from '@api/dto/instance.dto';
2+
import {
3+
difyController,
4+
openaiController,
5+
typebotController,
6+
websocketController,
7+
} from '@api/integrations/integration.module';
8+
import { PrismaRepository } from '@api/repository/repository.service';
9+
import { WAMonitoringService } from '@api/services/monitor.service';
10+
11+
export class ChatbotController {
12+
public prismaRepository: PrismaRepository;
13+
public waMonitor: WAMonitoringService;
14+
15+
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
16+
this.prisma = prismaRepository;
17+
this.monitor = waMonitor;
18+
}
19+
20+
public set prisma(prisma: PrismaRepository) {
21+
this.prismaRepository = prisma;
22+
}
23+
24+
public get prisma() {
25+
return this.prismaRepository;
26+
}
27+
28+
public set monitor(waMonitor: WAMonitoringService) {
29+
this.waMonitor = waMonitor;
30+
}
31+
32+
public get monitor() {
33+
return this.waMonitor;
34+
}
35+
36+
public async emit({
37+
instance,
38+
remoteJid,
39+
msg,
40+
pushName,
41+
}: {
42+
instance: InstanceDto;
43+
remoteJid: string;
44+
msg: any;
45+
pushName?: string;
46+
}): Promise<void> {
47+
const emitData = {
48+
instance,
49+
remoteJid,
50+
msg,
51+
pushName,
52+
};
53+
// typebot
54+
await typebotController.emit(emitData);
55+
56+
// openai
57+
await openaiController.emit(emitData);
58+
59+
// dify
60+
await difyController.emit(emitData);
61+
}
62+
63+
public async setInstance(instanceName: string, data: any): Promise<any> {
64+
// chatwoot
65+
if (data.websocketEnabled)
66+
await websocketController.set(instanceName, {
67+
enabled: true,
68+
events: data.websocketEvents,
69+
});
70+
}
71+
}
File renamed without changes.
File renamed without changes.

‎src/api/integrations/chatbot/chatwoot/dto/chatwoot.dto.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Constructor } from '@api/dto/integration.dto';
1+
import { Constructor } from '@api/integrations/integration.dto';
22

33
export class ChatwootDto {
44
enabled?: boolean;

‎src/api/integrations/chatbot/dify/controllers/dify.controller.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,19 @@ export class DifyController {
6666

6767
return this.difyService.ignoreJid(instance, data);
6868
}
69+
70+
public async emit({
71+
instance,
72+
remoteJid,
73+
msg,
74+
}: {
75+
instance: InstanceDto;
76+
remoteJid: string;
77+
msg: any;
78+
pushName?: string;
79+
}) {
80+
if (!configService.get<Dify>('DIFY').ENABLED) return;
81+
82+
await this.difyService.sendDify(instance, remoteJid, msg);
83+
}
6984
}

‎src/api/integrations/chatbot/dify/routes/dify.router.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { RouterBroker } from '@api/abstract/abstract.router';
22
import { InstanceDto } from '@api/dto/instance.dto';
33
import { DifyDto, DifyIgnoreJidDto, DifySettingDto } from '@api/integrations/chatbot/dify/dto/dify.dto';
4+
import { difyController } from '@api/integrations/integration.module';
45
import { HttpStatus } from '@api/routes/index.router';
5-
import { difyController } from '@api/server.module';
66
import {
77
difyIgnoreJidSchema,
88
difySchema,

‎src/api/integrations/chatbot/openai/controllers/openai.controller.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,20 @@ export class OpenaiController {
9090

9191
return this.openaiService.ignoreJid(instance, data);
9292
}
93+
94+
public async emit({
95+
instance,
96+
remoteJid,
97+
msg,
98+
pushName,
99+
}: {
100+
instance: InstanceDto;
101+
remoteJid: string;
102+
msg: any;
103+
pushName?: string;
104+
}) {
105+
if (!configService.get<Openai>('OPENAI').ENABLED) return;
106+
107+
await this.openaiService.sendOpenai(instance, remoteJid, pushName, msg);
108+
}
93109
}

0 commit comments

Comments
 (0)