|
| 1 | +import { IHttp, IModify, IPersistence, IRead } from '@rocket.chat/apps-engine/definition/accessors'; |
| 2 | +import { IApp } from '@rocket.chat/apps-engine/definition/IApp'; |
| 3 | +import { IMessage } from '@rocket.chat/apps-engine/definition/messages'; |
| 4 | +import { RoomType } from '@rocket.chat/apps-engine/definition/rooms'; |
| 5 | +import { AppSetting } from '../config/Settings'; |
| 6 | +import { getAppSettingValue } from '../lib/Settings'; |
| 7 | + |
| 8 | +export const handleTimeout = async (app: IApp, message: IMessage, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify ) => { |
| 9 | + |
| 10 | + if (message.room.type !== RoomType.LIVE_CHAT || (message.customFields && message.customFields.idleTimeoutConfig)) { |
| 11 | + return; |
| 12 | + } |
| 13 | + |
| 14 | + const dialogflowBotUsername: string = (await getAppSettingValue(read, AppSetting.DialogflowBotUsername)); |
| 15 | + const chasitorIdleTimeoutIsEnabled: string = (await getAppSettingValue(read, AppSetting.DialogflowEnableCustomerTimeout)); |
| 16 | + |
| 17 | + if (chasitorIdleTimeoutIsEnabled) { |
| 18 | + |
| 19 | + /** |
| 20 | + * Sets the amount of time that a customer has to respond to an agent message before a warning appears and a timer begins a countdown. |
| 21 | + * The warning disappears (and the timer stops) each time the customer sends a message. |
| 22 | + * The warning disappears (and the timer resets to 0) each time the agent sends message. |
| 23 | + * The warning value must be shorter than the time-out value (we recommend at least 30 seconds). |
| 24 | + */ |
| 25 | + const warningTime: string = (await getAppSettingValue(read, AppSetting.DialogflowCustomerTimeoutWarningTime)); |
| 26 | + |
| 27 | + /** |
| 28 | + * Sets the amount of time that a customer has to respond to an agent message before the session ends. |
| 29 | + * The timer stops when the customer sends a message and starts again from 0 on the next agent's message. |
| 30 | + */ |
| 31 | + const timeoutTime: string = (await getAppSettingValue(read, AppSetting.DialogflowCustomerTimeoutTime)); |
| 32 | + |
| 33 | + // ------ When agent sends message ----- |
| 34 | + // Send new timeout msg and reset previous timeout |
| 35 | + |
| 36 | + // ------ When customer sends message ----- |
| 37 | + // Send timeout msg to cancel previous timeout |
| 38 | + |
| 39 | + // On Timeout : Close chat |
| 40 | + // On Warning : Show Countdown Popup in Livechat Widget |
| 41 | + |
| 42 | + const timeoutWarningMessage: string = (await getAppSettingValue(read, AppSetting.DialogflowCustomerTimeoutWarningMessage)); |
| 43 | + |
| 44 | + if (message.sender.username === dialogflowBotUsername) { |
| 45 | + // Agent sent message |
| 46 | + if (!message.id) { |
| 47 | + return; |
| 48 | + } |
| 49 | + const user = await read.getUserReader().getByUsername(dialogflowBotUsername); |
| 50 | + const msgExtender = modify.getExtender().extendMessage(message.id, user); |
| 51 | + (await msgExtender).addCustomField('idleTimeoutConfig', { |
| 52 | + idleTimeoutAction: 'start', |
| 53 | + idleTimeoutWarningTime: warningTime, |
| 54 | + idleTimeoutTimeoutTime: timeoutTime, |
| 55 | + idleTimeoutMessage: timeoutWarningMessage, |
| 56 | + }); |
| 57 | + modify.getExtender().finish(await msgExtender); |
| 58 | + } else { |
| 59 | + // Guest sent message |
| 60 | + |
| 61 | + if (!message.id) { |
| 62 | + return; |
| 63 | + } |
| 64 | + const user = await read.getUserReader().getByUsername(dialogflowBotUsername); |
| 65 | + const msgExtender = modify.getExtender().extendMessage(message.id, user); |
| 66 | + (await msgExtender).addCustomField('idleTimeoutConfig', { |
| 67 | + idleTimeoutAction: 'stop', |
| 68 | + idleTimeoutWarningTime: warningTime, |
| 69 | + idleTimeoutTimeoutTime: timeoutTime, |
| 70 | + idleTimeoutMessage: timeoutWarningMessage, |
| 71 | + }); |
| 72 | + modify.getExtender().finish(await msgExtender); |
| 73 | + } |
| 74 | + } |
| 75 | +}; |
0 commit comments