Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions handler/PostMessageSentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}
12 changes: 10 additions & 2 deletions lib/Room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down
6 changes: 4 additions & 2 deletions lib/SynchronousHandover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand All @@ -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();
}
};

Expand Down