forked from RocketChat/Apps.Dialogflow
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPostMessageSentHandler.ts
More file actions
224 lines (181 loc) · 9.81 KB
/
PostMessageSentHandler.ts
File metadata and controls
224 lines (181 loc) · 9.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import {
IHttp,
IModify,
IPersistence,
IRead,
} from '@rocket.chat/apps-engine/definition/accessors';
import { IApp } from '@rocket.chat/apps-engine/definition/IApp';
import {
ILivechatMessage,
ILivechatRoom,
} from '@rocket.chat/apps-engine/definition/livechat';
import { RoomType } from '@rocket.chat/apps-engine/definition/rooms';
import { AppSetting } from '../config/Settings';
import { DialogflowRequestType, IDialogflowMessage, IDialogflowQuickReplies, LanguageCode, Message } from '../enum/Dialogflow';
import { Logs } from '../enum/Logs';
import { botTypingListener, removeBotTypingListener } from '../lib//BotTyping';
import { Dialogflow } from '../lib/Dialogflow';
import { getErrorMessage } from '../lib/Helper';
import { createDialogflowMessage, createMessage, removeQuotedMessage } from '../lib/Message';
import { handleResponse } from '../lib/payloadAction';
import { getIsProcessingMessage, getIsQueueWindowActive, getRoomAssoc, retrieveDataByAssociation, setIsProcessingMessage, setQueuedMessage } from '../lib/Persistence';
import { handleParameters } from '../lib/responseParameters';
import { closeChat, performHandover, updateRoomCustomFields } from '../lib/Room';
import { cancelAllEventSchedulerJobForSession, cancelAllSessionMaintenanceJobForSession } from '../lib/Scheduler';
import { sendEventToDialogFlow } from '../lib/sendEventToDialogFlow';
import { agentConfigExists, getLivechatAgentConfig } from '../lib/Settings';
import { incFallbackIntentAndSendResponse, resetFallbackIntent } from '../lib/SynchronousHandover';
import { handleTimeout } from '../lib/Timeout';
export class PostMessageSentHandler {
constructor(private readonly app: IApp,
private readonly message: ILivechatMessage,
private readonly read: IRead,
private readonly http: IHttp,
private readonly persistence: IPersistence,
private readonly modify: IModify) { }
public async run() {
const { text, editedAt, room, token, sender, customFields, file, location } = this.message;
const livechatRoom = room as ILivechatRoom;
const { id: rid, type, servedBy, isOpen, customFields: roomCustomFields,
visitor: { token: visitorToken, username: visitorUsername }} = livechatRoom;
if (!servedBy) {
return;
}
const agentExists = await(agentConfigExists(this.read, servedBy.username));
if (!agentExists) {
return;
}
if (roomCustomFields && roomCustomFields.isHandedOverFromDialogFlow === true) {
return;
}
if (text === Message.CLOSED_BY_VISITOR) {
await cancelAllSessionMaintenanceJobForSession(this.modify, rid);
await this.handleClosedByVisitor(rid, this.read);
}
if (text === Message.CUSTOMER_IDLE_TIMEOUT) {
await this.handleClosedByVisitor(rid, this.read);
await closeChat(this.modify, this.read, rid, this.persistence);
return;
}
if (!type || type !== RoomType.LIVE_CHAT) {
return;
}
if (!isOpen) {
return;
}
if (customFields) {
const { disableInput, displayTyping } = customFields;
if (disableInput === true && displayTyping !== true) {
await removeBotTypingListener(this.modify, rid, servedBy.username);
}
}
if ((file || location) && sender.username === visitorUsername) {
const invalidInputEventName: string = await getLivechatAgentConfig(this.read, rid, AppSetting.DialogflowInvalidInputEventName);
await sendEventToDialogFlow(this.app, this.read, this.modify, this.persistence, this.http, rid, invalidInputEventName);
return;
}
if (!text || (text && text.trim().length === 0)) {
return;
}
if (!text || editedAt) {
return;
}
if (!text || (text && text.trim().length === 0)) {
return;
}
await handleTimeout(this.app, this.message, this.read, this.http, this.persistence, this.modify);
if (sender.username === servedBy.username || sender.username !== visitorUsername || await(agentConfigExists(this.read, sender.username))) {
return;
}
let messageText = text;
messageText = await removeQuotedMessage(this.read, room, messageText);
let response: IDialogflowMessage;
const isProcessingMessage = await getIsProcessingMessage(this.read, rid);
if (isProcessingMessage) {
console.error(`DF is processing previous message in room ${rid}. So dropping this message with id ${this.message.id}`);
return;
}
const isQueueWindowActive = await getIsQueueWindowActive(this.read, rid);
if (isQueueWindowActive) {
console.debug({rid}, `Queue Window is active. So storing this message with id ${this.message.id} ${this.message.text}`);
await setQueuedMessage(this.read, this.persistence, rid, this.message.text ?? '');
return;
}
try {
await setIsProcessingMessage(this.read, this.persistence, rid, true);
await cancelAllEventSchedulerJobForSession(this.modify, rid);
await botTypingListener(this.modify, rid, servedBy.username);
response = (await Dialogflow.sendRequest(this.http, this.read, this.modify, rid, messageText, DialogflowRequestType.MESSAGE));
await setIsProcessingMessage(this.read, this.persistence, rid, false);
} catch (error) {
await setIsProcessingMessage(this.read, this.persistence, rid, false);
const errorContent = `${Logs.DIALOGFLOW_REST_API_ERROR}: { roomID: ${rid} } ${getErrorMessage(error)}`;
this.app.getLogger().error(errorContent);
console.error(errorContent);
const serviceUnavailable: string = await getLivechatAgentConfig(this.read, rid, AppSetting.DialogflowServiceUnavailableMessage);
await createMessage(rid, this.read, this.modify, { text: serviceUnavailable }, this.app);
const targetDepartment: string = await getLivechatAgentConfig(this.read, rid, AppSetting.FallbackTargetDepartment);
if (!targetDepartment) {
console.error(Logs.EMPTY_HANDOVER_DEPARTMENT);
return;
}
updateRoomCustomFields(rid, { isChatBotFunctional: false }, this.read, this.modify);
await performHandover(this.app, this.modify, this.read, rid, visitorToken, targetDepartment);
return;
}
const createResponseMessage = async () => await createDialogflowMessage(rid, this.read, this.modify, response, this.app);
// synchronous handover check
const { isFallback } = response;
if (isFallback) {
await removeBotTypingListener(this.modify, rid, servedBy.username);
return incFallbackIntentAndSendResponse(this.app, this.read, this.modify, rid, createResponseMessage);
}
// await createResponseMessage();
// await handlePayloadActions(this.app, this.read, this.modify, this.http, this.persistence, rid, visitorToken, response);
await handleResponse(this.app, this.read, this.modify, this.http, this.persistence, rid, visitorToken, response);
await handleParameters(this.app, this.read, this.modify, this.persistence, this.http, rid, visitorToken, response);
await this.handleBotTyping(rid, response);
return resetFallbackIntent(this.read, this.modify, rid);
}
private async handleBotTyping(rid: string, dialogflowMessage: IDialogflowMessage) {
const { messages = [] } = dialogflowMessage;
let removeTypingIndicator = true;
for (const message of messages) {
const { customFields = null } = message as IDialogflowQuickReplies;
if (customFields) {
const { disableInput, displayTyping } = customFields;
if (disableInput === true && displayTyping === true) {
removeTypingIndicator = false;
}
}
}
if (removeTypingIndicator) {
await this.removeBotTypingListener(this.read, rid);
}
}
private async handleClosedByVisitor(rid: string, read: IRead) {
const DialogflowEnableChatClosedByVisitorEvent = await getLivechatAgentConfig(this.read, rid, AppSetting.DialogflowEnableChatClosedByVisitorEvent);
const DialogflowChatClosedByVisitorEventName = await getLivechatAgentConfig(this.read, rid, AppSetting.DialogflowChatClosedByVisitorEventName);
await this.removeBotTypingListener(read, rid);
const data = await retrieveDataByAssociation(read, getRoomAssoc(rid));
if (DialogflowEnableChatClosedByVisitorEvent) {
try {
const defaultLanguageCode = await getLivechatAgentConfig(read, rid, AppSetting.DialogflowAgentDefaultLanguage);
let res: IDialogflowMessage;
res = (await Dialogflow.sendRequest(this.http, this.read, this.modify, rid, {
name: DialogflowChatClosedByVisitorEventName,
languageCode: data.custom_languageCode || defaultLanguageCode || LanguageCode.EN,
}, DialogflowRequestType.EVENT));
} catch (error) {
const errorContent = `${Logs.DIALOGFLOW_REST_API_ERROR}: { roomID: ${rid} } ${getErrorMessage(error)}`;
this.app.getLogger().error(errorContent);
console.error(errorContent);
}
}
}
private async removeBotTypingListener(read: IRead, rid: string) {
const room = await read.getRoomReader().getById(rid) as any;
const DialogflowBotUsername = room.servedBy.username;
await removeBotTypingListener(this.modify, rid, DialogflowBotUsername);
}
}