mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-20 12:22:21 -06:00
refactor: openai integration
This commit is contained in:
12
src/api/dto/chatbot.dto.ts
Normal file
12
src/api/dto/chatbot.dto.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export class Session {
|
||||
remoteJid?: string;
|
||||
sessionId?: string;
|
||||
status?: string;
|
||||
createdAt?: number;
|
||||
updateAt?: number;
|
||||
}
|
||||
|
||||
export class IgnoreJidDto {
|
||||
remoteJid?: string;
|
||||
action?: string;
|
||||
}
|
||||
@@ -2,11 +2,16 @@ import { InstanceDto } from '@api/dto/instance.dto';
|
||||
import { PrismaRepository } from '@api/repository/repository.service';
|
||||
import { difyController, openaiController, typebotController, websocketController } from '@api/server.module';
|
||||
import { WAMonitoringService } from '@api/services/monitor.service';
|
||||
import { Logger } from '@config/logger.config';
|
||||
import { IntegrationSession } from '@prisma/client';
|
||||
import { findBotByTrigger } from '@utils/findBotByTrigger';
|
||||
|
||||
export class ChatbotController {
|
||||
public prismaRepository: PrismaRepository;
|
||||
public waMonitor: WAMonitoringService;
|
||||
|
||||
public readonly logger = new Logger(ChatbotController.name);
|
||||
|
||||
constructor(prismaRepository: PrismaRepository, waMonitor: WAMonitoringService) {
|
||||
this.prisma = prismaRepository;
|
||||
this.monitor = waMonitor;
|
||||
@@ -63,4 +68,112 @@ export class ChatbotController {
|
||||
events: data.websocketEvents,
|
||||
});
|
||||
}
|
||||
|
||||
public processDebounce(
|
||||
userMessageDebounce: any,
|
||||
content: string,
|
||||
remoteJid: string,
|
||||
debounceTime: number,
|
||||
callback: any,
|
||||
) {
|
||||
if (userMessageDebounce[remoteJid]) {
|
||||
userMessageDebounce[remoteJid].message += ` ${content}`;
|
||||
this.logger.log('message debounced: ' + userMessageDebounce[remoteJid].message);
|
||||
clearTimeout(userMessageDebounce[remoteJid].timeoutId);
|
||||
} else {
|
||||
userMessageDebounce[remoteJid] = {
|
||||
message: content,
|
||||
timeoutId: null,
|
||||
};
|
||||
}
|
||||
|
||||
userMessageDebounce[remoteJid].timeoutId = setTimeout(() => {
|
||||
const myQuestion = userMessageDebounce[remoteJid].message;
|
||||
this.logger.log('Debounce complete. Processing message: ' + myQuestion);
|
||||
|
||||
delete userMessageDebounce[remoteJid];
|
||||
callback(myQuestion);
|
||||
}, debounceTime * 1000);
|
||||
}
|
||||
|
||||
public checkIgnoreJids(ignoreJids: any, remoteJid: string) {
|
||||
if (ignoreJids && ignoreJids.length > 0) {
|
||||
let ignoreGroups = false;
|
||||
let ignoreContacts = false;
|
||||
|
||||
if (ignoreJids.includes('@g.us')) {
|
||||
ignoreGroups = true;
|
||||
}
|
||||
|
||||
if (ignoreJids.includes('@s.whatsapp.net')) {
|
||||
ignoreContacts = true;
|
||||
}
|
||||
|
||||
if (ignoreGroups && remoteJid.endsWith('@g.us')) {
|
||||
this.logger.warn('Ignoring message from group: ' + remoteJid);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ignoreContacts && remoteJid.endsWith('@s.whatsapp.net')) {
|
||||
this.logger.warn('Ignoring message from contact: ' + remoteJid);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (ignoreJids.includes(remoteJid)) {
|
||||
this.logger.warn('Ignoring message from jid: ' + remoteJid);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async getSession(remoteJid: string, instance: InstanceDto) {
|
||||
let session = await this.prismaRepository.integrationSession.findFirst({
|
||||
where: {
|
||||
remoteJid: remoteJid,
|
||||
instanceId: instance.instanceId,
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
|
||||
if (session) {
|
||||
if (session.status !== 'closed' && !session.botId) {
|
||||
this.logger.warn('Session is already opened in another integration');
|
||||
return;
|
||||
} else if (!session.botId) {
|
||||
session = null;
|
||||
}
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
public async findBotTrigger(
|
||||
botRepository: any,
|
||||
settingsRepository: any,
|
||||
content: string,
|
||||
instance: InstanceDto,
|
||||
session?: IntegrationSession,
|
||||
) {
|
||||
let findBot = null;
|
||||
|
||||
if (!session) {
|
||||
findBot = await findBotByTrigger(botRepository, settingsRepository, content, instance.instanceId);
|
||||
|
||||
if (!findBot) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
findBot = await botRepository.findFirst({
|
||||
where: {
|
||||
id: session.botId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return findBot;
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,5 @@
|
||||
import { TriggerOperator, TriggerType } from '@prisma/client';
|
||||
|
||||
export class Session {
|
||||
remoteJid?: string;
|
||||
sessionId?: string;
|
||||
status?: string;
|
||||
createdAt?: number;
|
||||
updateAt?: number;
|
||||
}
|
||||
|
||||
export class OpenaiCredsDto {
|
||||
name: string;
|
||||
apiKey: string;
|
||||
@@ -53,8 +45,3 @@ export class OpenaiSettingDto {
|
||||
ignoreJids?: any;
|
||||
speechToText?: boolean;
|
||||
}
|
||||
|
||||
export class OpenaiIgnoreJidDto {
|
||||
remoteJid?: string;
|
||||
action?: string;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
import { RouterBroker } from '@api/abstract/abstract.router';
|
||||
import { IgnoreJidDto } from '@api/dto/chatbot.dto';
|
||||
import { InstanceDto } from '@api/dto/instance.dto';
|
||||
import {
|
||||
OpenaiCredsDto,
|
||||
OpenaiDto,
|
||||
OpenaiIgnoreJidDto,
|
||||
OpenaiSettingDto,
|
||||
} from '@api/integrations/chatbot/openai/dto/openai.dto';
|
||||
import { OpenaiCredsDto, OpenaiDto, OpenaiSettingDto } from '@api/integrations/chatbot/openai/dto/openai.dto';
|
||||
import { HttpStatus } from '@api/routes/index.router';
|
||||
import { openaiController } from '@api/server.module';
|
||||
import {
|
||||
@@ -57,7 +53,7 @@ export class OpenaiRouter extends RouterBroker {
|
||||
request: req,
|
||||
schema: openaiSchema,
|
||||
ClassRef: OpenaiDto,
|
||||
execute: (instance, data) => openaiController.createOpenai(instance, data),
|
||||
execute: (instance, data) => openaiController.createBot(instance, data),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.CREATED).json(response);
|
||||
@@ -67,7 +63,7 @@ export class OpenaiRouter extends RouterBroker {
|
||||
request: req,
|
||||
schema: instanceSchema,
|
||||
ClassRef: InstanceDto,
|
||||
execute: (instance) => openaiController.findOpenai(instance),
|
||||
execute: (instance) => openaiController.findBot(instance),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
@@ -77,7 +73,7 @@ export class OpenaiRouter extends RouterBroker {
|
||||
request: req,
|
||||
schema: instanceSchema,
|
||||
ClassRef: InstanceDto,
|
||||
execute: (instance) => openaiController.fetchOpenai(instance, req.params.openaiBotId),
|
||||
execute: (instance) => openaiController.fetchBot(instance, req.params.openaiBotId),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
@@ -87,7 +83,7 @@ export class OpenaiRouter extends RouterBroker {
|
||||
request: req,
|
||||
schema: openaiSchema,
|
||||
ClassRef: OpenaiDto,
|
||||
execute: (instance, data) => openaiController.updateOpenai(instance, req.params.openaiBotId, data),
|
||||
execute: (instance, data) => openaiController.updateBot(instance, req.params.openaiBotId, data),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
@@ -97,7 +93,7 @@ export class OpenaiRouter extends RouterBroker {
|
||||
request: req,
|
||||
schema: instanceSchema,
|
||||
ClassRef: InstanceDto,
|
||||
execute: (instance) => openaiController.deleteOpenai(instance, req.params.openaiBotId),
|
||||
execute: (instance) => openaiController.deleteBot(instance, req.params.openaiBotId),
|
||||
});
|
||||
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
@@ -143,10 +139,10 @@ export class OpenaiRouter extends RouterBroker {
|
||||
res.status(HttpStatus.OK).json(response);
|
||||
})
|
||||
.post(this.routerPath('ignoreJid'), ...guards, async (req, res) => {
|
||||
const response = await this.dataValidate<OpenaiIgnoreJidDto>({
|
||||
const response = await this.dataValidate<IgnoreJidDto>({
|
||||
request: req,
|
||||
schema: openaiIgnoreJidSchema,
|
||||
ClassRef: OpenaiIgnoreJidDto,
|
||||
ClassRef: IgnoreJidDto,
|
||||
execute: (instance, data) => openaiController.ignoreJid(instance, data),
|
||||
});
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
||||
import { PrismaRepository } from '@api/repository/repository.service';
|
||||
import { WAMonitoringService } from '@api/services/monitor.service';
|
||||
import { wa } from '@api/types/wa.types';
|
||||
import { configService, Log, Webhook, Websocket } from '@config/env.config';
|
||||
import { configService, Log, Webhook } from '@config/env.config';
|
||||
import { Logger } from '@config/logger.config';
|
||||
import { BadRequestException, NotFoundException } from '@exceptions';
|
||||
import axios from 'axios';
|
||||
@@ -31,7 +31,7 @@ export class WebhookController extends EventController {
|
||||
}
|
||||
|
||||
await this.get(instanceName);
|
||||
|
||||
|
||||
return this.prisma.webhook.upsert({
|
||||
where: {
|
||||
instanceId: this.monitor.waInstances[instanceName].instanceId,
|
||||
@@ -89,10 +89,6 @@ export class WebhookController extends EventController {
|
||||
apiKey?: string;
|
||||
local?: boolean;
|
||||
}): Promise<void> {
|
||||
if (!configService.get<Websocket>('WEBSOCKET')?.ENABLED) {
|
||||
return;
|
||||
}
|
||||
|
||||
const instanceWebhook = await this.get(instanceName);
|
||||
const webhookGlobal = configService.get<Webhook>('WEBHOOK');
|
||||
const webhookLocal = instanceWebhook?.events;
|
||||
@@ -110,6 +106,7 @@ export class WebhookController extends EventController {
|
||||
server_url: serverUrl,
|
||||
apikey: apiKey,
|
||||
};
|
||||
|
||||
if (local) {
|
||||
if (Array.isArray(webhookLocal) && webhookLocal.includes(we)) {
|
||||
let baseURL: string;
|
||||
|
||||
Reference in New Issue
Block a user