mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-24 14:17:47 -06:00
refactor: integrations folder structure
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import { InstanceDto } from '@api/dto/instance.dto';
|
||||
import { DifyDto, DifyIgnoreJidDto } from '@api/integrations/chatbot/dify/dto/dify.dto';
|
||||
import { DifyService } from '@api/integrations/chatbot/dify/services/dify.service';
|
||||
import { configService, Dify } from '@config/env.config';
|
||||
import { BadRequestException } from '@exceptions';
|
||||
|
||||
export class DifyController {
|
||||
constructor(private readonly difyService: DifyService) {}
|
||||
|
||||
public async createDify(instance: InstanceDto, data: DifyDto) {
|
||||
if (!configService.get<Dify>('DIFY').ENABLED) throw new BadRequestException('Dify is disabled');
|
||||
|
||||
return this.difyService.create(instance, data);
|
||||
}
|
||||
|
||||
public async findDify(instance: InstanceDto) {
|
||||
if (!configService.get<Dify>('DIFY').ENABLED) throw new BadRequestException('Dify is disabled');
|
||||
|
||||
return this.difyService.find(instance);
|
||||
}
|
||||
|
||||
public async fetchDify(instance: InstanceDto, difyId: string) {
|
||||
if (!configService.get<Dify>('DIFY').ENABLED) throw new BadRequestException('Dify is disabled');
|
||||
|
||||
return this.difyService.fetch(instance, difyId);
|
||||
}
|
||||
|
||||
public async updateDify(instance: InstanceDto, difyId: string, data: DifyDto) {
|
||||
if (!configService.get<Dify>('DIFY').ENABLED) throw new BadRequestException('Dify is disabled');
|
||||
|
||||
return this.difyService.update(instance, difyId, data);
|
||||
}
|
||||
|
||||
public async deleteDify(instance: InstanceDto, difyId: string) {
|
||||
if (!configService.get<Dify>('DIFY').ENABLED) throw new BadRequestException('Dify is disabled');
|
||||
|
||||
return this.difyService.delete(instance, difyId);
|
||||
}
|
||||
|
||||
public async settings(instance: InstanceDto, data: any) {
|
||||
if (!configService.get<Dify>('DIFY').ENABLED) throw new BadRequestException('Dify is disabled');
|
||||
|
||||
return this.difyService.setDefaultSettings(instance, data);
|
||||
}
|
||||
|
||||
public async fetchSettings(instance: InstanceDto) {
|
||||
if (!configService.get<Dify>('DIFY').ENABLED) throw new BadRequestException('Dify is disabled');
|
||||
|
||||
return this.difyService.fetchDefaultSettings(instance);
|
||||
}
|
||||
|
||||
public async changeStatus(instance: InstanceDto, data: any) {
|
||||
if (!configService.get<Dify>('DIFY').ENABLED) throw new BadRequestException('Dify is disabled');
|
||||
|
||||
return this.difyService.changeStatus(instance, data);
|
||||
}
|
||||
|
||||
public async fetchSessions(instance: InstanceDto, difyId: string) {
|
||||
if (!configService.get<Dify>('DIFY').ENABLED) throw new BadRequestException('Dify is disabled');
|
||||
|
||||
return this.difyService.fetchSessions(instance, difyId);
|
||||
}
|
||||
|
||||
public async ignoreJid(instance: InstanceDto, data: DifyIgnoreJidDto) {
|
||||
if (!configService.get<Dify>('DIFY').ENABLED) throw new BadRequestException('Dify is disabled');
|
||||
|
||||
return this.difyService.ignoreJid(instance, data);
|
||||
}
|
||||
}
|
||||
47
src/api/integrations/chatbot/dify/dto/dify.dto.ts
Normal file
47
src/api/integrations/chatbot/dify/dto/dify.dto.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { $Enums, TriggerOperator, TriggerType } from '@prisma/client';
|
||||
|
||||
export class Session {
|
||||
remoteJid?: string;
|
||||
sessionId?: string;
|
||||
status?: string;
|
||||
createdAt?: number;
|
||||
updateAt?: number;
|
||||
}
|
||||
|
||||
export class DifyDto {
|
||||
enabled?: boolean;
|
||||
description?: string;
|
||||
botType?: $Enums.DifyBotType;
|
||||
apiUrl?: string;
|
||||
apiKey?: string;
|
||||
expire?: number;
|
||||
keywordFinish?: string;
|
||||
delayMessage?: number;
|
||||
unknownMessage?: string;
|
||||
listeningFromMe?: boolean;
|
||||
stopBotFromMe?: boolean;
|
||||
keepOpen?: boolean;
|
||||
debounceTime?: number;
|
||||
triggerType?: TriggerType;
|
||||
triggerOperator?: TriggerOperator;
|
||||
triggerValue?: string;
|
||||
ignoreJids?: any;
|
||||
}
|
||||
|
||||
export class DifySettingDto {
|
||||
expire?: number;
|
||||
keywordFinish?: string;
|
||||
delayMessage?: number;
|
||||
unknownMessage?: string;
|
||||
listeningFromMe?: boolean;
|
||||
stopBotFromMe?: boolean;
|
||||
keepOpen?: boolean;
|
||||
debounceTime?: number;
|
||||
difyIdFallback?: string;
|
||||
ignoreJids?: any;
|
||||
}
|
||||
|
||||
export class DifyIgnoreJidDto {
|
||||
remoteJid?: string;
|
||||
action?: string;
|
||||
}
|
||||
122
src/api/integrations/chatbot/dify/routes/dify.router.ts
Normal file
122
src/api/integrations/chatbot/dify/routes/dify.router.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
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 { HttpStatus } from '@api/routes/index.router';
|
||||
import { difyController } from '@api/server.module';
|
||||
import {
|
||||
difyIgnoreJidSchema,
|
||||
difySchema,
|
||||
difySettingSchema,
|
||||
difyStatusSchema,
|
||||
instanceSchema,
|
||||
} from '@validate/validate.schema';
|
||||
import { RequestHandler, Router } from 'express';
|
||||
|
||||
export class DifyRouter extends RouterBroker {
|
||||
constructor(...guards: RequestHandler[]) {
|
||||
super();
|
||||
this.router
|
||||
.post(this.routerPath('create'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<DifyDto>({
|
||||
request: req,
|
||||
schema: difySchema,
|
||||
ClassRef: DifyDto,
|
||||
execute: (instance, data) => difyController.createDify(instance, data),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.CREATED).json(response);
|
||||
})
|
||||
.get(this.routerPath('find'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<InstanceDto>({
|
||||
request: req,
|
||||
schema: instanceSchema,
|
||||
ClassRef: InstanceDto,
|
||||
execute: (instance) => difyController.findDify(instance),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.get(this.routerPath('fetch/:difyId'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<InstanceDto>({
|
||||
request: req,
|
||||
schema: instanceSchema,
|
||||
ClassRef: InstanceDto,
|
||||
execute: (instance) => difyController.fetchDify(instance, req.params.difyId),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.put(this.routerPath('update/:difyId'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<DifyDto>({
|
||||
request: req,
|
||||
schema: difySchema,
|
||||
ClassRef: DifyDto,
|
||||
execute: (instance, data) => difyController.updateDify(instance, req.params.difyId, data),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.delete(this.routerPath('delete/:difyId'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<InstanceDto>({
|
||||
request: req,
|
||||
schema: instanceSchema,
|
||||
ClassRef: InstanceDto,
|
||||
execute: (instance) => difyController.deleteDify(instance, req.params.difyId),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.post(this.routerPath('settings'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<DifySettingDto>({
|
||||
request: req,
|
||||
schema: difySettingSchema,
|
||||
ClassRef: DifySettingDto,
|
||||
execute: (instance, data) => difyController.settings(instance, data),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.get(this.routerPath('fetchSettings'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<InstanceDto>({
|
||||
request: req,
|
||||
schema: instanceSchema,
|
||||
ClassRef: InstanceDto,
|
||||
execute: (instance) => difyController.fetchSettings(instance),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.post(this.routerPath('changeStatus'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<InstanceDto>({
|
||||
request: req,
|
||||
schema: difyStatusSchema,
|
||||
ClassRef: InstanceDto,
|
||||
execute: (instance, data) => difyController.changeStatus(instance, data),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.get(this.routerPath('fetchSessions/:difyId'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<InstanceDto>({
|
||||
request: req,
|
||||
schema: instanceSchema,
|
||||
ClassRef: InstanceDto,
|
||||
execute: (instance) => difyController.fetchSessions(instance, req.params.difyId),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.post(this.routerPath('ignoreJid'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<DifyIgnoreJidDto>({
|
||||
request: req,
|
||||
schema: difyIgnoreJidSchema,
|
||||
ClassRef: DifyIgnoreJidDto,
|
||||
execute: (instance, data) => difyController.ignoreJid(instance, data),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
});
|
||||
}
|
||||
|
||||
public readonly router: Router = Router();
|
||||
}
|
||||
2100
src/api/integrations/chatbot/dify/services/dify.service.ts
Normal file
2100
src/api/integrations/chatbot/dify/services/dify.service.ts
Normal file
File diff suppressed because it is too large
Load Diff
108
src/api/integrations/chatbot/dify/validate/dify.schema.ts
Normal file
108
src/api/integrations/chatbot/dify/validate/dify.schema.ts
Normal file
@@ -0,0 +1,108 @@
|
||||
import { JSONSchema7 } from 'json-schema';
|
||||
import { v4 } from 'uuid';
|
||||
|
||||
const isNotEmpty = (...propertyNames: string[]): JSONSchema7 => {
|
||||
const properties = {};
|
||||
propertyNames.forEach(
|
||||
(property) =>
|
||||
(properties[property] = {
|
||||
minLength: 1,
|
||||
description: `The "${property}" cannot be empty`,
|
||||
}),
|
||||
);
|
||||
return {
|
||||
if: {
|
||||
propertyNames: {
|
||||
enum: [...propertyNames],
|
||||
},
|
||||
},
|
||||
then: { properties },
|
||||
};
|
||||
};
|
||||
|
||||
export const difySchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
enabled: { type: 'boolean' },
|
||||
description: { type: 'string' },
|
||||
botType: { type: 'string', enum: ['chatBot', 'textGenerator', 'agent', 'workflow'] },
|
||||
apiUrl: { type: 'string' },
|
||||
apiKey: { type: 'string' },
|
||||
triggerType: { type: 'string', enum: ['all', 'keyword', 'none', 'advanced'] },
|
||||
triggerOperator: { type: 'string', enum: ['equals', 'contains', 'startsWith', 'endsWith', 'regex'] },
|
||||
triggerValue: { type: 'string' },
|
||||
expire: { type: 'integer' },
|
||||
keywordFinish: { type: 'string' },
|
||||
delayMessage: { type: 'integer' },
|
||||
unknownMessage: { type: 'string' },
|
||||
listeningFromMe: { type: 'boolean' },
|
||||
stopBotFromMe: { type: 'boolean' },
|
||||
keepOpen: { type: 'boolean' },
|
||||
debounceTime: { type: 'integer' },
|
||||
ignoreJids: { type: 'array', items: { type: 'string' } },
|
||||
},
|
||||
required: ['enabled', 'botType', 'triggerType'],
|
||||
...isNotEmpty('enabled', 'botType', 'triggerType'),
|
||||
};
|
||||
|
||||
export const difyStatusSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
remoteJid: { type: 'string' },
|
||||
status: { type: 'string', enum: ['opened', 'closed', 'paused', 'delete'] },
|
||||
},
|
||||
required: ['remoteJid', 'status'],
|
||||
...isNotEmpty('remoteJid', 'status'),
|
||||
};
|
||||
|
||||
export const difySettingSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
expire: { type: 'integer' },
|
||||
keywordFinish: { type: 'string' },
|
||||
delayMessage: { type: 'integer' },
|
||||
unknownMessage: { type: 'string' },
|
||||
listeningFromMe: { type: 'boolean' },
|
||||
stopBotFromMe: { type: 'boolean' },
|
||||
keepOpen: { type: 'boolean' },
|
||||
debounceTime: { type: 'integer' },
|
||||
ignoreJids: { type: 'array', items: { type: 'string' } },
|
||||
difyIdFallback: { type: 'string' },
|
||||
},
|
||||
required: [
|
||||
'expire',
|
||||
'keywordFinish',
|
||||
'delayMessage',
|
||||
'unknownMessage',
|
||||
'listeningFromMe',
|
||||
'stopBotFromMe',
|
||||
'keepOpen',
|
||||
'debounceTime',
|
||||
'ignoreJids',
|
||||
],
|
||||
...isNotEmpty(
|
||||
'expire',
|
||||
'keywordFinish',
|
||||
'delayMessage',
|
||||
'unknownMessage',
|
||||
'listeningFromMe',
|
||||
'stopBotFromMe',
|
||||
'keepOpen',
|
||||
'debounceTime',
|
||||
'ignoreJids',
|
||||
),
|
||||
};
|
||||
|
||||
export const difyIgnoreJidSchema: JSONSchema7 = {
|
||||
$id: v4(),
|
||||
type: 'object',
|
||||
properties: {
|
||||
remoteJid: { type: 'string' },
|
||||
action: { type: 'string', enum: ['add', 'remove'] },
|
||||
},
|
||||
required: ['remoteJid', 'action'],
|
||||
...isNotEmpty('remoteJid', 'action'),
|
||||
};
|
||||
Reference in New Issue
Block a user