mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-12-24 06:07:45 -06:00
refactor: openai integration
This commit is contained in:
149
src/utils/findBotByTrigger.ts
Normal file
149
src/utils/findBotByTrigger.ts
Normal file
@@ -0,0 +1,149 @@
|
||||
import { advancedOperatorsSearch } from './advancedOperatorsSearch';
|
||||
|
||||
export const findBotByTrigger = async (
|
||||
botRepository: any,
|
||||
settingsRepository: any,
|
||||
content: string,
|
||||
instanceId: string,
|
||||
) => {
|
||||
// Check for triggerType 'all'
|
||||
const findTriggerAll = await botRepository.findFirst({
|
||||
where: {
|
||||
enabled: true,
|
||||
triggerType: 'all',
|
||||
instanceId: instanceId,
|
||||
},
|
||||
});
|
||||
|
||||
if (findTriggerAll) return findTriggerAll;
|
||||
|
||||
const findTriggerAdvanced = await botRepository.findMany({
|
||||
where: {
|
||||
enabled: true,
|
||||
triggerType: 'advanced',
|
||||
instanceId: instanceId,
|
||||
},
|
||||
});
|
||||
for (const advanced of findTriggerAdvanced) {
|
||||
if (advancedOperatorsSearch(content, advanced.triggerValue)) {
|
||||
return advanced;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for exact match
|
||||
const findTriggerEquals = await botRepository.findFirst({
|
||||
where: {
|
||||
enabled: true,
|
||||
triggerType: 'keyword',
|
||||
triggerOperator: 'equals',
|
||||
triggerValue: content,
|
||||
instanceId: instanceId,
|
||||
},
|
||||
});
|
||||
|
||||
if (findTriggerEquals) return findTriggerEquals;
|
||||
|
||||
// Check for regex match
|
||||
const findRegex = await botRepository.findMany({
|
||||
where: {
|
||||
enabled: true,
|
||||
triggerType: 'keyword',
|
||||
triggerOperator: 'regex',
|
||||
instanceId: instanceId,
|
||||
},
|
||||
});
|
||||
|
||||
let findTriggerRegex = null;
|
||||
|
||||
for (const regex of findRegex) {
|
||||
const regexValue = new RegExp(regex.triggerValue);
|
||||
|
||||
if (regexValue.test(content)) {
|
||||
findTriggerRegex = regex;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (findTriggerRegex) return findTriggerRegex;
|
||||
|
||||
// Check for startsWith match
|
||||
const findStartsWith = await botRepository.findMany({
|
||||
where: {
|
||||
enabled: true,
|
||||
triggerType: 'keyword',
|
||||
triggerOperator: 'startsWith',
|
||||
instanceId: instanceId,
|
||||
},
|
||||
});
|
||||
|
||||
let findTriggerStartsWith = null;
|
||||
|
||||
for (const startsWith of findStartsWith) {
|
||||
if (content.startsWith(startsWith.triggerValue)) {
|
||||
findTriggerStartsWith = startsWith;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (findTriggerStartsWith) return findTriggerStartsWith;
|
||||
|
||||
// Check for endsWith match
|
||||
const findEndsWith = await botRepository.findMany({
|
||||
where: {
|
||||
enabled: true,
|
||||
triggerType: 'keyword',
|
||||
triggerOperator: 'endsWith',
|
||||
instanceId: instanceId,
|
||||
},
|
||||
});
|
||||
|
||||
let findTriggerEndsWith = null;
|
||||
|
||||
for (const endsWith of findEndsWith) {
|
||||
if (content.endsWith(endsWith.triggerValue)) {
|
||||
findTriggerEndsWith = endsWith;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (findTriggerEndsWith) return findTriggerEndsWith;
|
||||
|
||||
// Check for contains match
|
||||
const findContains = await botRepository.findMany({
|
||||
where: {
|
||||
enabled: true,
|
||||
triggerType: 'keyword',
|
||||
triggerOperator: 'contains',
|
||||
instanceId: instanceId,
|
||||
},
|
||||
});
|
||||
|
||||
let findTriggerContains = null;
|
||||
|
||||
for (const contains of findContains) {
|
||||
if (content.includes(contains.triggerValue)) {
|
||||
findTriggerContains = contains;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (findTriggerContains) return findTriggerContains;
|
||||
|
||||
const fallback = await settingsRepository.findFirst({
|
||||
where: {
|
||||
instanceId: instanceId,
|
||||
},
|
||||
});
|
||||
|
||||
if (fallback?.openaiIdFallback) {
|
||||
const findFallback = await botRepository.findFirst({
|
||||
where: {
|
||||
id: fallback.openaiIdFallback,
|
||||
},
|
||||
});
|
||||
|
||||
if (findFallback) return findFallback;
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
65
src/utils/getConversationMessage.ts
Normal file
65
src/utils/getConversationMessage.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { configService, S3 } from '@config/env.config';
|
||||
|
||||
const getTypeMessage = (msg: any) => {
|
||||
let mediaId: string;
|
||||
|
||||
if (configService.get<S3>('S3').ENABLE) mediaId = msg.message.mediaUrl;
|
||||
else mediaId = msg.key.id;
|
||||
|
||||
const types = {
|
||||
conversation: msg?.message?.conversation,
|
||||
extendedTextMessage: msg?.message?.extendedTextMessage?.text,
|
||||
contactMessage: msg?.message?.contactMessage?.displayName,
|
||||
locationMessage: msg?.message?.locationMessage?.degreesLatitude,
|
||||
viewOnceMessageV2:
|
||||
msg?.message?.viewOnceMessageV2?.message?.imageMessage?.url ||
|
||||
msg?.message?.viewOnceMessageV2?.message?.videoMessage?.url ||
|
||||
msg?.message?.viewOnceMessageV2?.message?.audioMessage?.url,
|
||||
listResponseMessage: msg?.message?.listResponseMessage?.singleSelectReply?.selectedRowId,
|
||||
responseRowId: msg?.message?.listResponseMessage?.singleSelectReply?.selectedRowId,
|
||||
// Medias
|
||||
audioMessage: msg?.message?.speechToText
|
||||
? msg?.message?.speechToText
|
||||
: msg?.message?.audioMessage
|
||||
? `audioMessage|${mediaId}`
|
||||
: undefined,
|
||||
imageMessage: msg?.message?.imageMessage
|
||||
? `imageMessage|${mediaId}${msg?.message?.imageMessage?.caption ? `|${msg?.message?.imageMessage?.caption}` : ''}`
|
||||
: undefined,
|
||||
videoMessage: msg?.message?.videoMessage
|
||||
? `videoMessage|${mediaId}${msg?.message?.videoMessage?.caption ? `|${msg?.message?.videoMessage?.caption}` : ''}`
|
||||
: undefined,
|
||||
documentMessage: msg?.message?.documentMessage
|
||||
? `documentMessage|${mediaId}${
|
||||
msg?.message?.documentMessage?.caption ? `|${msg?.message?.documentMessage?.caption}` : ''
|
||||
}`
|
||||
: undefined,
|
||||
documentWithCaptionMessage: msg?.message?.documentWithCaptionMessage?.message?.documentMessage
|
||||
? `documentWithCaptionMessage|${mediaId}${
|
||||
msg?.message?.documentWithCaptionMessage?.message?.documentMessage?.caption
|
||||
? `|${msg?.message?.documentWithCaptionMessage?.message?.documentMessage?.caption}`
|
||||
: ''
|
||||
}`
|
||||
: undefined,
|
||||
};
|
||||
|
||||
const messageType = Object.keys(types).find((key) => types[key] !== undefined) || 'unknown';
|
||||
|
||||
return { ...types, messageType };
|
||||
};
|
||||
|
||||
const getMessageContent = (types: any) => {
|
||||
const typeKey = Object.keys(types).find((key) => types[key] !== undefined);
|
||||
|
||||
const result = typeKey ? types[typeKey] : undefined;
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
export const getConversationMessage = (msg: any) => {
|
||||
const types = getTypeMessage(msg);
|
||||
|
||||
const messageContent = getMessageContent(types);
|
||||
|
||||
return messageContent;
|
||||
};
|
||||
Reference in New Issue
Block a user