chore: Ignore specific JIDs in Typebot integration

Implements the ability to ignore specific JIDs in the Typebot integration, improving the flexibility of the feature. This change includes modifications in the Prisma schema, DTOs, services, and validation schema.

- Adds 'ignoreJids' field to the PostgreSQL schema (prisma/postgresql-schema.prisma).
- Updates TypebotDto and TypebotSettingDto to include 'ignoreJids' (src/api/integrations/typebot/dto/typebot.dto.ts).
- Modifies TypebotService to handle 'ignoreJids' when creating and updating Typebot instances (src/api/integrations/typebot/services/typebot.service.ts).
- Adds 'ignoreJids' to the Typebot validation schema (src/api/integrations/typebot/validate/typebot.schema.ts).

This update allows for more precise control over which JIDs are processed by the Typebot integration, reducing unnecessary processing and improving performance.
This commit is contained in:
Davidson Gomes 2024-07-12 20:14:57 -03:00
parent 480cc67927
commit b2bf5d2318
5 changed files with 37 additions and 42 deletions

View File

@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "Typebot" ADD COLUMN "ignoreJids" JSONB;
-- AlterTable
ALTER TABLE "TypebotSetting" ADD COLUMN "ignoreJids" JSONB;

View File

@ -266,6 +266,7 @@ model Typebot {
debounceTime Int? @db.Integer
createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime? @updatedAt @db.Timestamp
ignoreJids Json?
triggerType TriggerType?
triggerOperator TriggerOperator?
triggerValue String?
@ -303,6 +304,7 @@ model TypebotSetting {
keepOpen Boolean? @default(false) @db.Boolean
debounceTime Int? @db.Integer
typebotIdFallback String? @db.VarChar(100)
ignoreJids Json?
createdAt DateTime? @default(now()) @db.Timestamp
updatedAt DateTime @updatedAt @db.Timestamp
Fallback Typebot? @relation(fields: [typebotIdFallback], references: [id])

View File

@ -31,6 +31,7 @@ export class TypebotDto {
triggerType?: TriggerType;
triggerOperator?: TriggerOperator;
triggerValue?: string;
ignoreJids?: any;
}
export class TypebotSettingDto {
@ -43,4 +44,5 @@ export class TypebotSettingDto {
keepOpen?: boolean;
debounceTime?: number;
typebotIdFallback?: string;
ignoreJids?: any;
}

View File

@ -37,7 +37,8 @@ export class TypebotService {
!data.listeningFromMe ||
!data.stopBotFromMe ||
!data.keepOpen ||
!data.debounceTime
!data.debounceTime ||
!data.ignoreJids
) {
const defaultSettingCheck = await this.prismaRepository.typebotSetting.findFirst({
where: {
@ -53,6 +54,7 @@ export class TypebotService {
if (!data.stopBotFromMe) data.stopBotFromMe = defaultSettingCheck?.stopBotFromMe || false;
if (!data.keepOpen) data.keepOpen = defaultSettingCheck?.keepOpen || false;
if (!data.debounceTime) data.debounceTime = defaultSettingCheck?.debounceTime || 0;
if (!data.ignoreJids) data.ignoreJids = defaultSettingCheck?.ignoreJids || [];
if (!defaultSettingCheck) {
await this.setDefaultSettings(instance, {
@ -64,6 +66,7 @@ export class TypebotService {
stopBotFromMe: data.stopBotFromMe,
keepOpen: data.keepOpen,
debounceTime: data.debounceTime,
ignoreJids: data.ignoreJids,
});
}
}
@ -128,6 +131,7 @@ export class TypebotService {
triggerType: data.triggerType,
triggerOperator: data.triggerOperator,
triggerValue: data.triggerValue,
ignoreJids: data.ignoreJids,
},
});
@ -264,6 +268,7 @@ export class TypebotService {
triggerType: data.triggerType,
triggerOperator: data.triggerOperator,
triggerValue: data.triggerValue,
ignoreJids: data.ignoreJids,
},
});
@ -374,6 +379,7 @@ export class TypebotService {
keepOpen: data.keepOpen,
debounceTime: data.debounceTime,
typebotIdFallback: data.typebotIdFallback,
ignoreJids: data.ignoreJids,
},
});
@ -387,6 +393,7 @@ export class TypebotService {
keepOpen: updateSettings.keepOpen,
debounceTime: updateSettings.debounceTime,
typebotIdFallback: updateSettings.typebotIdFallback,
ignoreJids: updateSettings.ignoreJids,
};
}
@ -401,6 +408,7 @@ export class TypebotService {
keepOpen: data.keepOpen,
debounceTime: data.debounceTime,
typebotIdFallback: data.typebotIdFallback,
ignoreJids: data.ignoreJids,
instanceId: instanceId,
},
});
@ -415,6 +423,7 @@ export class TypebotService {
keepOpen: newSetttings.keepOpen,
debounceTime: newSetttings.debounceTime,
typebotIdFallback: newSetttings.typebotIdFallback,
ignoreJids: newSetttings.ignoreJids,
};
} catch (error) {
this.logger.error(error);
@ -453,6 +462,7 @@ export class TypebotService {
listeningFromMe: settings.listeningFromMe,
stopBotFromMe: settings.stopBotFromMe,
keepOpen: settings.keepOpen,
ignoreJids: settings.ignoreJids,
typebotIdFallback: settings.typebotIdFallback,
fallback: settings.Fallback,
};
@ -1095,8 +1105,6 @@ export class TypebotService {
}
public async findTypebotByTrigger(content: string, instanceId: string) {
console.log('Check for triggerType all');
// Check for triggerType 'all'
const findTriggerAll = await this.prismaRepository.typebot.findFirst({
where: {
@ -1106,12 +1114,8 @@ export class TypebotService {
},
});
console.log('findTriggerAll', findTriggerAll);
if (findTriggerAll) return findTriggerAll;
console.log('Check for exact match');
// Check for exact match
const findTriggerEquals = await this.prismaRepository.typebot.findFirst({
where: {
@ -1123,12 +1127,8 @@ export class TypebotService {
},
});
console.log('findTriggerEquals', findTriggerEquals);
if (findTriggerEquals) return findTriggerEquals;
console.log('Check for regex match');
// Check for regex match
const findRegex = await this.prismaRepository.typebot.findMany({
where: {
@ -1150,12 +1150,8 @@ export class TypebotService {
}
}
console.log('findTriggerRegex', findTriggerRegex);
if (findTriggerRegex) return findTriggerRegex;
console.log('Check for startsWith match');
// Check for startsWith match
const findTriggerStartsWith = await this.prismaRepository.typebot.findFirst({
where: {
@ -1169,12 +1165,8 @@ export class TypebotService {
},
});
console.log('findTriggerStartsWith', findTriggerStartsWith);
if (findTriggerStartsWith) return findTriggerStartsWith;
console.log('Check for endsWith match');
// Check for endsWith match
const findTriggerEndsWith = await this.prismaRepository.typebot.findFirst({
where: {
@ -1188,12 +1180,8 @@ export class TypebotService {
},
});
console.log('findTriggerEndsWith', findTriggerEndsWith);
if (findTriggerEndsWith) return findTriggerEndsWith;
console.log('Check for contains match');
// Check for contains match
const findTriggerContains = await this.prismaRepository.typebot.findFirst({
where: {
@ -1207,31 +1195,21 @@ export class TypebotService {
},
});
console.log('findTriggerContains', findTriggerContains);
if (findTriggerContains) return findTriggerContains;
console.log('Check for fallback');
const fallback = await this.prismaRepository.typebotSetting.findFirst({
where: {
instanceId: instanceId,
},
});
console.log('fallback', fallback);
if (fallback?.typebotIdFallback) {
console.log('Check for fallback typebot');
const findFallback = await this.prismaRepository.typebot.findFirst({
where: {
id: fallback.typebotIdFallback,
},
});
console.log('findFallback', findFallback);
if (findFallback) return findFallback;
}
@ -1261,24 +1239,31 @@ export class TypebotService {
public async sendTypebot(instance: InstanceDto, remoteJid: string, msg: Message) {
try {
const session = await this.prismaRepository.typebotSession.findFirst({
where: {
remoteJid: remoteJid,
},
});
const settings = await this.prismaRepository.typebotSetting.findFirst({
where: {
instanceId: instance.instanceId,
},
});
if (settings.ignoreJids) {
const ignoreJids: any = settings.ignoreJids;
if (ignoreJids.includes(remoteJid)) {
this.logger.warn('Ignoring message from jid: ' + remoteJid);
return;
}
}
const session = await this.prismaRepository.typebotSession.findFirst({
where: {
remoteJid: remoteJid,
},
});
const content = this.getConversationMessage(msg);
let findTypebot = null;
console.log('content', content);
if (!session) {
findTypebot = await this.findTypebotByTrigger(content, instance.instanceId);
@ -1351,7 +1336,6 @@ export class TypebotService {
return;
}
console.log(debounceTime);
if (debounceTime && debounceTime > 0) {
this.processDebounce(content, remoteJid, debounceTime, async (debouncedContent) => {
await this.processTypebot(

View File

@ -36,6 +36,7 @@ export const typebotSchema: JSONSchema7 = {
unknownMessage: { type: 'string' },
listeningFromMe: { type: 'boolean' },
stopBotFromMe: { type: 'boolean' },
ignoreJids: { type: 'array', items: { type: 'string' } },
},
required: ['enabled', 'url', 'typebot', 'triggerType'],
...isNotEmpty('enabled', 'url', 'typebot', 'triggerType'),
@ -77,6 +78,7 @@ export const typebotSettingSchema: JSONSchema7 = {
keepOpen: { type: 'boolean' },
debounceTime: { type: 'integer' },
typebotIdFallback: { type: 'string' },
ignoreJids: { type: 'array', items: { type: 'string' } },
},
required: ['expire', 'keywordFinish', 'delayMessage', 'unknownMessage', 'listeningFromMe', 'stopBotFromMe'],
...isNotEmpty('expire', 'keywordFinish', 'delayMessage', 'unknownMessage', 'listeningFromMe', 'stopBotFromMe'),