Skip to content

Commit 5015cfb

Browse files
Merge pull request #1927 from josiasmaceda/hotfix/change-typebot-status-webhook
fix: integrate Typebot status change events for webhook in chatbot controller e service
2 parents 21502b9 + b9ae401 commit 5015cfb

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { IgnoreJidDto } from '@api/dto/chatbot.dto';
22
import { InstanceDto } from '@api/dto/instance.dto';
33
import { PrismaRepository } from '@api/repository/repository.service';
44
import { WAMonitoringService } from '@api/services/monitor.service';
5+
import { Events } from '@api/types/wa.types';
56
import { Logger } from '@config/logger.config';
67
import { BadRequestException } from '@exceptions';
78
import { TriggerOperator, TriggerType } from '@prisma/client';
@@ -446,6 +447,16 @@ export abstract class BaseChatbotController<BotType = any, BotData extends BaseC
446447

447448
const remoteJid = data.remoteJid;
448449
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({
@@ -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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PrismaRepository } from '@api/repository/repository.service';
22
import { WAMonitoringService } from '@api/services/monitor.service';
3+
import { Events } from '@api/types/wa.types';
34
import { Auth, ConfigService, HttpServer, Typebot } from '@config/env.config';
45
import { Instance, IntegrationSession, Message, Typebot as TypebotModel } from '@prisma/client';
56
import { getConversationMessage } from '@utils/getConversationMessage';
@@ -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,13 @@ 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);
418436
}
419437
}
420438

@@ -639,6 +657,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
639657
}
640658

641659
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
660+
let statusChange = 'closed';
642661
if (keepOpen) {
643662
await this.prismaRepository.integrationSession.update({
644663
where: {
@@ -649,13 +668,22 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
649668
},
650669
});
651670
} else {
671+
statusChange = 'delete';
652672
await this.prismaRepository.integrationSession.deleteMany({
653673
where: {
654674
botId: findTypebot.id,
655675
remoteJid: remoteJid,
656676
},
657677
});
658678
}
679+
680+
const typebotData = {
681+
remoteJid: remoteJid,
682+
status: statusChange,
683+
session,
684+
};
685+
waInstance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
686+
659687
return;
660688
}
661689

@@ -788,6 +816,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
788816
}
789817

790818
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
819+
let statusChange = 'closed';
791820
if (keepOpen) {
792821
await this.prismaRepository.integrationSession.update({
793822
where: {
@@ -798,6 +827,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
798827
},
799828
});
800829
} else {
830+
statusChange = 'delete';
801831
await this.prismaRepository.integrationSession.deleteMany({
802832
where: {
803833
botId: findTypebot.id,
@@ -806,6 +836,13 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
806836
});
807837
}
808838

839+
const typebotData = {
840+
remoteJid: remoteJid,
841+
status: statusChange,
842+
session,
843+
};
844+
waInstance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
845+
809846
return;
810847
}
811848

@@ -881,6 +918,7 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
881918
}
882919

883920
if (keywordFinish && content.toLowerCase() === keywordFinish.toLowerCase()) {
921+
let statusChange = 'closed';
884922
if (keepOpen) {
885923
await this.prismaRepository.integrationSession.update({
886924
where: {
@@ -891,13 +929,23 @@ export class TypebotService extends BaseChatbotService<TypebotModel, any> {
891929
},
892930
});
893931
} else {
932+
statusChange = 'delete';
894933
await this.prismaRepository.integrationSession.deleteMany({
895934
where: {
896935
botId: findTypebot.id,
897936
remoteJid: remoteJid,
898937
},
899938
});
900939
}
940+
941+
const typebotData = {
942+
remoteJid: remoteJid,
943+
status: statusChange,
944+
session,
945+
};
946+
947+
waInstance.sendDataWebhook(Events.TYPEBOT_CHANGE_STATUS, typebotData);
948+
901949
return;
902950
}
903951

0 commit comments

Comments
 (0)