Skip to content

Commit d7ddb99

Browse files
committed
fix: Fixed bot fallback not working on integrations
1 parent 2d732c8 commit d7ddb99

File tree

7 files changed

+101
-26
lines changed

7 files changed

+101
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
* Fixed getBase64FromMediaMessage with convertToMp4
2929
* Fixed bug when send message when don't have mentionsEveryOne on payload
3030
* Does not search message without chatwoot Message Id for reply
31+
* Fixed bot fallback not working on integrations
3132

3233
# 2.1.1 (2024-09-22 10:31)
3334

src/api/integrations/chatbot/dify/controllers/dify.controller.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -726,15 +726,33 @@ export class DifyController extends ChatbotController implements ChatbotControll
726726

727727
const content = getConversationMessage(msg);
728728

729-
const findBot = (await this.findBotTrigger(
729+
let findBot = (await this.findBotTrigger(
730730
this.botRepository,
731731
this.settingsRepository,
732732
content,
733733
instance,
734734
session,
735735
)) as DifyModel;
736736

737-
if (!findBot) return;
737+
if (!findBot) {
738+
const fallback = await this.settingsRepository.findFirst({
739+
where: {
740+
instanceId: instance.instanceId,
741+
},
742+
});
743+
744+
if (fallback?.difyIdFallback) {
745+
const findFallback = await this.botRepository.findFirst({
746+
where: {
747+
id: fallback.difyIdFallback,
748+
},
749+
});
750+
751+
findBot = findFallback;
752+
} else {
753+
return;
754+
}
755+
}
738756

739757
let expire = findBot?.expire;
740758
let keywordFinish = findBot?.keywordFinish;

src/api/integrations/chatbot/evolutionBot/controllers/evolutionBot.controller.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,15 +698,33 @@ export class EvolutionBotController extends ChatbotController implements Chatbot
698698

699699
const content = getConversationMessage(msg);
700700

701-
const findBot = (await this.findBotTrigger(
701+
let findBot = (await this.findBotTrigger(
702702
this.botRepository,
703703
this.settingsRepository,
704704
content,
705705
instance,
706706
session,
707707
)) as EvolutionBot;
708708

709-
if (!findBot) return;
709+
if (!findBot) {
710+
const fallback = await this.settingsRepository.findFirst({
711+
where: {
712+
instanceId: instance.instanceId,
713+
},
714+
});
715+
716+
if (fallback?.botIdFallback) {
717+
const findFallback = await this.botRepository.findFirst({
718+
where: {
719+
id: fallback.botIdFallback,
720+
},
721+
});
722+
723+
findBot = findFallback;
724+
} else {
725+
return;
726+
}
727+
}
710728

711729
let expire = findBot?.expire;
712730
let keywordFinish = findBot?.keywordFinish;

src/api/integrations/chatbot/flowise/controllers/flowise.controller.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,15 +698,33 @@ export class FlowiseController extends ChatbotController implements ChatbotContr
698698

699699
const content = getConversationMessage(msg);
700700

701-
const findBot = (await this.findBotTrigger(
701+
let findBot = (await this.findBotTrigger(
702702
this.botRepository,
703703
this.settingsRepository,
704704
content,
705705
instance,
706706
session,
707707
)) as Flowise;
708708

709-
if (!findBot) return;
709+
if (!findBot) {
710+
const fallback = await this.settingsRepository.findFirst({
711+
where: {
712+
instanceId: instance.instanceId,
713+
},
714+
});
715+
716+
if (fallback?.flowiseIdFallback) {
717+
const findFallback = await this.botRepository.findFirst({
718+
where: {
719+
id: fallback.flowiseIdFallback,
720+
},
721+
});
722+
723+
findBot = findFallback;
724+
} else {
725+
return;
726+
}
727+
}
710728

711729
let expire = findBot?.expire;
712730
let keywordFinish = findBot?.keywordFinish;

src/api/integrations/chatbot/openai/controllers/openai.controller.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -935,15 +935,33 @@ export class OpenaiController extends ChatbotController implements ChatbotContro
935935

936936
const content = getConversationMessage(msg);
937937

938-
const findBot = (await this.findBotTrigger(
938+
let findBot = (await this.findBotTrigger(
939939
this.botRepository,
940940
this.settingsRepository,
941941
content,
942942
instance,
943943
session,
944944
)) as OpenaiBot;
945945

946-
if (!findBot) return;
946+
if (!findBot) {
947+
const fallback = await this.settingsRepository.findFirst({
948+
where: {
949+
instanceId: instance.instanceId,
950+
},
951+
});
952+
953+
if (fallback?.openaiIdFallback) {
954+
const findFallback = await this.botRepository.findFirst({
955+
where: {
956+
id: fallback.openaiIdFallback,
957+
},
958+
});
959+
960+
findBot = findFallback;
961+
} else {
962+
return;
963+
}
964+
}
947965

948966
let expire = findBot?.expire;
949967
let keywordFinish = findBot?.keywordFinish;

src/api/integrations/chatbot/typebot/controllers/typebot.controller.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,15 +985,33 @@ export class TypebotController extends ChatbotController implements ChatbotContr
985985

986986
const content = getConversationMessage(msg);
987987

988-
const findBot = (await this.findBotTrigger(
988+
let findBot = (await this.findBotTrigger(
989989
this.botRepository,
990990
this.settingsRepository,
991991
content,
992992
instance,
993993
session,
994994
)) as TypebotModel;
995995

996-
if (!findBot) return;
996+
if (!findBot) {
997+
const fallback = await this.settingsRepository.findFirst({
998+
where: {
999+
instanceId: instance.instanceId,
1000+
},
1001+
});
1002+
1003+
if (fallback?.typebotIdFallback) {
1004+
const findFallback = await this.botRepository.findFirst({
1005+
where: {
1006+
id: fallback.typebotIdFallback,
1007+
},
1008+
});
1009+
1010+
findBot = findFallback;
1011+
} else {
1012+
return;
1013+
}
1014+
}
9971015

9981016
const settings = await this.prismaRepository.typebotSetting.findFirst({
9991017
where: {

src/utils/findBotByTrigger.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,5 @@ export const findBotByTrigger = async (
129129

130130
if (findTriggerContains) return findTriggerContains;
131131

132-
const fallback = await settingsRepository.findFirst({
133-
where: {
134-
instanceId: instanceId,
135-
},
136-
});
137-
138-
if (fallback?.openaiIdFallback) {
139-
const findFallback = await botRepository.findFirst({
140-
where: {
141-
id: fallback.openaiIdFallback,
142-
},
143-
});
144-
145-
if (findFallback) return findFallback;
146-
}
147-
148132
return null;
149133
};

0 commit comments

Comments
 (0)