Skip to content

Commit

Permalink
chore: Ignore specific JIDs in Typebot integration
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dgcode-tec committed Jul 12, 2024
1 parent 480cc67 commit b2bf5d2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "Typebot" ADD COLUMN "ignoreJids" JSONB;

-- AlterTable
ALTER TABLE "TypebotSetting" ADD COLUMN "ignoreJids" JSONB;
2 changes: 2 additions & 0 deletions prisma/postgresql-schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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])
Expand Down
2 changes: 2 additions & 0 deletions src/api/integrations/typebot/dto/typebot.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class TypebotDto {
triggerType?: TriggerType;
triggerOperator?: TriggerOperator;
triggerValue?: string;
ignoreJids?: any;
}

export class TypebotSettingDto {
Expand All @@ -43,4 +44,5 @@ export class TypebotSettingDto {
keepOpen?: boolean;
debounceTime?: number;
typebotIdFallback?: string;
ignoreJids?: any;
}
64 changes: 24 additions & 40 deletions src/api/integrations/typebot/services/typebot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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, {
Expand All @@ -64,6 +66,7 @@ export class TypebotService {
stopBotFromMe: data.stopBotFromMe,
keepOpen: data.keepOpen,
debounceTime: data.debounceTime,
ignoreJids: data.ignoreJids,
});
}
}
Expand Down Expand Up @@ -128,6 +131,7 @@ export class TypebotService {
triggerType: data.triggerType,
triggerOperator: data.triggerOperator,
triggerValue: data.triggerValue,
ignoreJids: data.ignoreJids,
},
});

Expand Down Expand Up @@ -264,6 +268,7 @@ export class TypebotService {
triggerType: data.triggerType,
triggerOperator: data.triggerOperator,
triggerValue: data.triggerValue,
ignoreJids: data.ignoreJids,
},
});

Expand Down Expand Up @@ -374,6 +379,7 @@ export class TypebotService {
keepOpen: data.keepOpen,
debounceTime: data.debounceTime,
typebotIdFallback: data.typebotIdFallback,
ignoreJids: data.ignoreJids,
},
});

Expand All @@ -387,6 +393,7 @@ export class TypebotService {
keepOpen: updateSettings.keepOpen,
debounceTime: updateSettings.debounceTime,
typebotIdFallback: updateSettings.typebotIdFallback,
ignoreJids: updateSettings.ignoreJids,
};
}

Expand All @@ -401,6 +408,7 @@ export class TypebotService {
keepOpen: data.keepOpen,
debounceTime: data.debounceTime,
typebotIdFallback: data.typebotIdFallback,
ignoreJids: data.ignoreJids,
instanceId: instanceId,
},
});
Expand All @@ -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);
Expand Down Expand Up @@ -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,
};
Expand Down Expand Up @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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: {
Expand All @@ -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;
}

Expand Down Expand Up @@ -1261,24 +1239,31 @@ export class TypebotService {

public async sendTypebot(instance: InstanceDto, remoteJid: string, msg: Message) {
try {
const session = await this.prismaRepository.typebotSession.findFirst({
const settings = await this.prismaRepository.typebotSetting.findFirst({
where: {
remoteJid: remoteJid,
instanceId: instance.instanceId,
},
});

const settings = await this.prismaRepository.typebotSetting.findFirst({
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: {
instanceId: instance.instanceId,
remoteJid: remoteJid,
},
});

const content = this.getConversationMessage(msg);

let findTypebot = null;

console.log('content', content);

if (!session) {
findTypebot = await this.findTypebotByTrigger(content, instance.instanceId);

Expand Down Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions src/api/integrations/typebot/validate/typebot.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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'),
Expand Down

0 comments on commit b2bf5d2

Please sign in to comment.