Skip to content

Commit 0116bc4

Browse files
author
Josias Maceda
committed
fix: integrate Typebot status change events for webhook in chatbot controller and service
1 parent c2085b5 commit 0116bc4

File tree

2 files changed

+76
-6
lines changed

2 files changed

+76
-6
lines changed

src/api/integrations/chatbot/base-chatbot.controller.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { getConversationMessage } from '@utils/getConversationMessage';
99

1010
import { BaseChatbotDto } from './base-chatbot.dto';
1111
import { ChatbotController, ChatbotControllerInterface, EmitData } from './chatbot.controller';
12+
import { Events } from '@api/types/wa.types';
1213

1314
// Common settings interface for all chatbot integrations
1415
export interface ChatbotSettings {
@@ -58,7 +59,7 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
5859
settingsRepository: any;
5960
sessionRepository: any;
6061
userMessageDebounce: { [key: string]: { message: string; timeoutId: NodeJS.Timeout } } = {};
61-
62+
6263
// Name of the integration, to be set by the derived class
6364
protected abstract readonly integrationName: string;
6465

@@ -445,7 +446,17 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
445446
});
446447

447448
const remoteJid = data.remoteJid;
448-
const status = data.status;
449+
const status = data.status;
450+
const session = await this.getSession(remoteJid, instance);
451+
452+
if (this.integrationName === 'Typebot') {
453+
const typebotData = {
454+
remoteJid: remoteJid,
455+
status: status,
456+
session
457+
};
458+
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
459+
}
449460

450461
if (status === 'delete') {
451462
await this.sessionRepository.deleteMany({
@@ -496,7 +507,7 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
496507
status: status,
497508
session,
498509
};
499-
510+
500511
return { bot: { ...instance, bot: botData } };
501512
}
502513
} catch (error) {
@@ -867,6 +878,16 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
867878
status: 'paused',
868879
},
869880
});
881+
882+
if (this.integrationName === 'Typebot') {
883+
const typebotData = {
884+
remoteJid: remoteJid,
885+
status: 'paused',
886+
session,
887+
};
888+
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
889+
}
890+
870891
return;
871892
}
872893

src/api/integrations/chatbot/typebot/services/typebot.service.ts

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ import axios from 'axios';
88

99
import { BaseChatbotService } from '../../base-chatbot.service';
1010
import { OpenaiService } from '../../openai/services/openai.service';
11+
import { Events } from '@api/types/wa.types';
1112

1213
export class TypebotService extends BaseChatbotService<TypebotModel, any> {
13-
private openaiService: OpenaiService;
14-
14+
private openaiService: OpenaiService;
15+
1516
constructor(
1617
waMonitor: WAMonitoringService,
1718
configService: ConfigService,
1819
prismaRepository: PrismaRepository,
1920
openaiService: OpenaiService,
2021
) {
2122
super(waMonitor, prismaRepository, 'TypebotService', configService);
22-
this.openaiService = openaiService;
23+
this.openaiService = openaiService;
2324
}
2425

2526
/**
@@ -151,6 +152,14 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
151152
},
152153
});
153154
}
155+
156+
const typebotData = {
157+
remoteJid: data.remoteJid,
158+
status: 'opened',
159+
session,
160+
};
161+
this.waMonitor.waInstances[instance.name].sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
162+
154163
return { ...request.data, session };
155164
} catch (error) {
156165
this.logger.error(error);
@@ -399,12 +408,14 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
399408
},
400409
});
401410
} else {
411+
let statusChange = 'closed';
402412
if (!settings?.keepOpen) {
403413
await prismaRepository.integrationSession.deleteMany({
404414
where: {
405415
id: session.id,
406416
},
407417
});
418+
statusChange = 'delete';
408419
} else {
409420
await prismaRepository.integrationSession.update({
410421
where: {
@@ -415,6 +426,14 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
415426
},
416427
});
417428
}
429+
430+
const typebotData = {
431+
remoteJid: session.remoteJid,
432+
status: statusChange,
433+
session,
434+
};
435+
instance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
436+
418437
}
419438
}
420439

@@ -639,6 +658,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
639658
}
640659

641660
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
661+
let statusChange = 'closed';
642662
if (keepOpen) {
643663
await this.prismaRepository.integrationSession.update({
644664
where: {
@@ -649,13 +669,22 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
649669
},
650670
});
651671
} else {
672+
statusChange = 'delete';
652673
await this.prismaRepository.integrationSession.deleteMany({
653674
where: {
654675
botId: findTypebot.id,
655676
remoteJid: remoteJid,
656677
},
657678
});
658679
}
680+
681+
const typebotData = {
682+
remoteJid: remoteJid,
683+
status: statusChange,
684+
session,
685+
};
686+
waInstance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
687+
659688
return;
660689
}
661690

@@ -788,6 +817,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
788817
}
789818

790819
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
820+
let statusChange = 'closed';
791821
if (keepOpen) {
792822
await this.prismaRepository.integrationSession.update({
793823
where: {
@@ -798,13 +828,21 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
798828
},
799829
});
800830
} else {
831+
statusChange = 'delete';
801832
await this.prismaRepository.integrationSession.deleteMany({
802833
where: {
803834
botId: findTypebot.id,
804835
remoteJid: remoteJid,
805836
},
806837
});
807838
}
839+
840+
const typebotData = {
841+
remoteJid: remoteJid,
842+
status: statusChange,
843+
session,
844+
};
845+
waInstance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
808846

809847
return;
810848
}
@@ -881,6 +919,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
881919
}
882920

883921
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
922+
let statusChange = 'closed';
884923
if (keepOpen) {
885924
await this.prismaRepository.integrationSession.update({
886925
where: {
@@ -891,13 +930,23 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
891930
},
892931
});
893932
} else {
933+
statusChange = 'delete';
894934
await this.prismaRepository.integrationSession.deleteMany({
895935
where: {
896936
botId: findTypebot.id,
897937
remoteJid: remoteJid,
898938
},
899939
});
900940
}
941+
942+
const typebotData = {
943+
remoteJid: remoteJid,
944+
status: statusChange,
945+
session,
946+
};
947+
948+
waInstance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
949+
901950
return;
902951
}
903952

0 commit comments

Comments
 (0)