From 2c235c9140d022c2aad998e9863e31c1c73708f4 Mon Sep 17 00:00:00 2001 From: shubhsherl Date: Thu, 17 Sep 2020 12:23:54 +0530 Subject: [PATCH] Suppress dialogflow message on fallback handover --- handler/PostMessageSentHandler.ts | 9 ++++++--- lib/Room.ts | 12 ++++++++++-- lib/SynchronousHandover.ts | 6 ++++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/handler/PostMessageSentHandler.ts b/handler/PostMessageSentHandler.ts index 3c3325e..594fdec 100644 --- a/handler/PostMessageSentHandler.ts +++ b/handler/PostMessageSentHandler.ts @@ -8,7 +8,7 @@ import { Logs } from '../enum/Logs'; import { Dialogflow } from '../lib/Dialogflow'; import { createDialogflowMessage, createMessage } from '../lib/Message'; import { getAppSettingValue } from '../lib/Settings'; -import { incFallbackIntent, resetFallbackIntent } from '../lib/SynchronousHandover'; +import { incFallbackIntentAndSendResponse, resetFallbackIntent } from '../lib/SynchronousHandover'; export class PostMessageSentHandler { constructor(private readonly app: IApp, @@ -62,13 +62,16 @@ export class PostMessageSentHandler { return; } - await createDialogflowMessage(rid, this.read, this.modify, response); + const createResponseMessage = async () => await createDialogflowMessage(rid, this.read, this.modify, response); // synchronous handover check const { isFallback } = response; if (isFallback) { - return incFallbackIntent(this.read, this.modify, rid); + return incFallbackIntentAndSendResponse(this.read, this.modify, rid, createResponseMessage); } + + await createResponseMessage(); + return resetFallbackIntent(this.read, this.modify, rid); } } diff --git a/lib/Room.ts b/lib/Room.ts index 9d6be93..9079148 100644 --- a/lib/Room.ts +++ b/lib/Room.ts @@ -42,10 +42,18 @@ export const closeChat = async (modify: IModify, read: IRead, rid: string) => { if (!result) { throw new Error(Logs.CLOSE_CHAT_REQUEST_FAILED_ERROR); } }; -export const performHandover = async (modify: IModify, read: IRead, rid: string, visitorToken: string, targetDepartmentName?: string) => { +export const performHandover = async (modify: IModify, read: IRead, rid: string, visitorToken: string, targetDepartmentName?: string, dialogflowMessage?: () => any) => { const handoverMessage: string = await getAppSettingValue(read, AppSetting.DialogflowHandoverMessage); - await createMessage(rid, read, modify, { text: handoverMessage ? handoverMessage : DefaultMessage.DEFAULT_DialogflowHandoverMessage }); + + // Use handoverMessage if set + if (handoverMessage) { + await createMessage(rid, read, modify, { text: handoverMessage }); + } else if (dialogflowMessage) { + await dialogflowMessage(); + } else { + await createMessage(rid, read, modify, { text: DefaultMessage.DEFAULT_DialogflowHandoverMessage }); + } const room: ILivechatRoom = (await read.getRoomReader().getById(rid)) as ILivechatRoom; if (!room) { throw new Error(Logs.INVALID_ROOM_ID); } diff --git a/lib/SynchronousHandover.ts b/lib/SynchronousHandover.ts index dd822ee..72b99a7 100644 --- a/lib/SynchronousHandover.ts +++ b/lib/SynchronousHandover.ts @@ -5,7 +5,7 @@ import { Logs } from '../enum/Logs'; import { performHandover, updateRoomCustomFields } from './Room'; import { getAppSettingValue } from './Settings'; -export const incFallbackIntent = async (read: IRead, modify: IModify, sessionId: string) => { +export const incFallbackIntentAndSendResponse = async (read: IRead, modify: IModify, sessionId: string, dialogflowMessage?: () => any) => { const fallbackThreshold = (await getAppSettingValue(read, AppSetting.DialogflowFallbackResponsesLimit)) as number; if (!fallbackThreshold || (fallbackThreshold && fallbackThreshold === 0)) { return; } @@ -26,7 +26,9 @@ export const incFallbackIntent = async (read: IRead, modify: IModify, sessionId: const targetDepartmentName: string | undefined = await getAppSettingValue(read, AppSetting.FallbackTargetDepartment); // Session Id from Dialogflow will be the same as Room id - await performHandover(modify, read, sessionId, visitorToken, targetDepartmentName); + await performHandover(modify, read, sessionId, visitorToken, targetDepartmentName, dialogflowMessage); + } else if (dialogflowMessage) { + await dialogflowMessage(); } };