@tiledesk/tiledesk-tybot-connector 0.5.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ExtApi.js +4 -83
- package/ExtUtil.js +0 -1
- package/Logger.js +105 -0
- package/TdCache.js +5 -3
- package/TiledeskClientTest.js +5 -28
- package/TiledeskExpression.js +6 -47
- package/{models → engine}/IntentForm.js +30 -44
- package/{models → engine}/IntentsMachineFactory.js +5 -4
- package/{models → engine}/MongodbBotsDataSource.js +31 -55
- package/{models → engine}/MongodbIntentsMachine.js +6 -5
- package/{models → engine}/TiledeskChatbot.js +82 -259
- package/{models → engine}/TiledeskChatbotConst.js +9 -0
- package/{models → engine}/TiledeskIntentsMachine.js +5 -15
- package/{models → engine/mock}/MockBotsDataSource.js +3 -19
- package/{models → engine/mock}/MockTdCache.js +0 -9
- package/index.js +103 -380
- package/logs/app.log +12723 -277
- package/logs/app1.log +62258 -0
- package/logs/app2.log +46280 -0
- package/logs/app3.log +31686 -0
- package/logs/app4.log +46163 -0
- package/logs/app5.log +33173 -0
- package/models/faq.js +2 -5
- package/package.json +5 -2
- package/{TiledeskServices → services}/AIService.js +4 -3
- package/{models/faqKbService.js → services/FaqKbService.js} +1 -2
- package/{models/faqService.js → services/FaqService.js} +4 -3
- package/services/IntegrationService.js +43 -0
- package/services/TilebotService.js +47 -0
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +49 -135
- package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +8 -14
- package/tiledeskChatbotPlugs/Filler.js +0 -1
- package/tiledeskChatbotPlugs/MarkbotChatbotPlug.js +12 -20
- package/tiledeskChatbotPlugs/MessagePipeline.js +5 -15
- package/tiledeskChatbotPlugs/SplitsChatbotPlug.js +4 -13
- package/tiledeskChatbotPlugs/TiledeskRequestVariables.js +1 -1
- package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +21 -89
- package/tiledeskChatbotPlugs/directives/DEPRECATED_DirSetAttribute.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirAddTags.js +23 -140
- package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +42 -128
- package/tiledeskChatbotPlugs/directives/DirAskGPT.js +37 -127
- package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +52 -145
- package/tiledeskChatbotPlugs/directives/DirAssign.js +12 -23
- package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +16 -12
- package/tiledeskChatbotPlugs/directives/DirAssistant.js +56 -198
- package/tiledeskChatbotPlugs/directives/DirBrevo.js +41 -101
- package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +9 -19
- package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirClose.js +4 -2
- package/tiledeskChatbotPlugs/directives/DirCode.js +16 -23
- package/tiledeskChatbotPlugs/directives/DirCondition.js +19 -25
- package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +9 -82
- package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +10 -22
- package/tiledeskChatbotPlugs/directives/DirCustomerio.js +34 -89
- package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +17 -22
- package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +8 -8
- package/tiledeskChatbotPlugs/directives/DirDepartment.js +13 -12
- package/tiledeskChatbotPlugs/directives/DirDisableInputText.js +2 -6
- package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +4 -4
- package/tiledeskChatbotPlugs/directives/DirForm.js +17 -24
- package/tiledeskChatbotPlugs/directives/DirGptTask.js +49 -139
- package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +8 -53
- package/tiledeskChatbotPlugs/directives/DirHubspot.js +31 -91
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +19 -26
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +24 -78
- package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +17 -68
- package/tiledeskChatbotPlugs/directives/DirIntent.js +6 -100
- package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +15 -14
- package/tiledeskChatbotPlugs/directives/DirLockIntent.js +7 -12
- package/tiledeskChatbotPlugs/directives/DirMake.js +21 -53
- package/tiledeskChatbotPlugs/directives/DirMessage.js +35 -28
- package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +8 -87
- package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +6 -4
- package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +6 -4
- package/tiledeskChatbotPlugs/directives/DirQapla.js +26 -120
- package/tiledeskChatbotPlugs/directives/DirRandomReply.js +11 -19
- package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +4 -0
- package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +5 -2
- package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +12 -58
- package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +11 -57
- package/tiledeskChatbotPlugs/directives/DirReply.js +36 -30
- package/tiledeskChatbotPlugs/directives/DirReplyV2.js +30 -72
- package/tiledeskChatbotPlugs/directives/DirSendEmail.js +9 -10
- package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +15 -40
- package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +8 -5
- package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +25 -113
- package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +14 -13
- package/tiledeskChatbotPlugs/directives/DirUnlockIntent.js +3 -3
- package/tiledeskChatbotPlugs/directives/DirWait.js +5 -6
- package/tiledeskChatbotPlugs/directives/DirWebRequest.js +14 -49
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +22 -77
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirWebResponse.js +11 -14
- package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +13 -42
- package/tiledeskChatbotPlugs/directives/Directives.js +0 -3
- package/utils/HttpUtils.js +128 -0
- package/{models → utils}/TiledeskChatbotUtil.js +164 -234
- package/utils/winston.js +42 -0
- package/TiledeskServices/utils.js +0 -99
- /package/{models → engine/mock}/MockIntentsMachine.js +0 -0
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
const { TiledeskExpression } = require('../TiledeskExpression');
|
|
2
2
|
const { Filler } = require('../tiledeskChatbotPlugs/Filler');
|
|
3
|
-
const { TiledeskChatbotConst } = require('
|
|
4
|
-
const { TiledeskChatbot } = require('
|
|
3
|
+
const { TiledeskChatbotConst } = require('../engine/TiledeskChatbotConst');
|
|
4
|
+
const { TiledeskChatbot } = require('../engine/TiledeskChatbot.js');
|
|
5
5
|
let parser = require('accept-language-parser');
|
|
6
6
|
const { Directives } = require('../tiledeskChatbotPlugs/directives/Directives.js');
|
|
7
7
|
require('dotenv').config();
|
|
8
8
|
let axios = require('axios');
|
|
9
|
+
const winston = require('./winston');
|
|
9
10
|
|
|
10
11
|
class TiledeskChatbotUtil {
|
|
11
12
|
|
|
@@ -26,26 +27,20 @@ class TiledeskChatbotUtil {
|
|
|
26
27
|
}
|
|
27
28
|
if (parts.length > 1) {
|
|
28
29
|
let json_string = explicit_intent_name.substring(parts[0].length);
|
|
29
|
-
// console.log("json_string (params)", json_string);
|
|
30
30
|
try {
|
|
31
31
|
intent.parameters = JSON.parse(json_string);
|
|
32
32
|
// if (intent.parameters) {
|
|
33
33
|
// for (const [key, value] of Object.entries(intent.parameters)) {
|
|
34
|
-
// console.log("Checking type of:", key, value);
|
|
35
34
|
// if (typeof value === "object") {
|
|
36
|
-
// console.log("Checking type of is object:", key);
|
|
37
35
|
// intent.parameters["_tdTypeOf:" + key] = "object";
|
|
38
36
|
// }
|
|
39
37
|
// else if (typeof value === "string") {
|
|
40
|
-
// console.log("Checking type of is string:", key);
|
|
41
38
|
// intent.parameters["_tdTypeOf:" + key] = "string";
|
|
42
39
|
// }
|
|
43
40
|
// else if (typeof value === "number") {
|
|
44
|
-
// console.log("Checking type of is number:", key);
|
|
45
41
|
// intent.parameters["_tdTypeOf:" + key] = "number";
|
|
46
42
|
// }
|
|
47
43
|
// else if (typeof value === "boolean") {
|
|
48
|
-
// console.log("Checking type of is boolean:", key);
|
|
49
44
|
// intent.parameters["_tdTypeOf:" + key] = "boolean";
|
|
50
45
|
// }
|
|
51
46
|
// }
|
|
@@ -53,7 +48,7 @@ class TiledeskChatbotUtil {
|
|
|
53
48
|
|
|
54
49
|
}
|
|
55
50
|
catch (err) {
|
|
56
|
-
|
|
51
|
+
winston.error("(TiledeskChatbotUtils) Error on parse json_string ", err)
|
|
57
52
|
}
|
|
58
53
|
}
|
|
59
54
|
return intent;
|
|
@@ -139,7 +134,7 @@ class TiledeskChatbotUtil {
|
|
|
139
134
|
if (message && message.attributes && message.attributes.commands) {
|
|
140
135
|
let commands = message.attributes.commands;
|
|
141
136
|
if (commands.length %2 != 0) {
|
|
142
|
-
|
|
137
|
+
winston.error("(TiledeskChatbotUtils) Error: commands.length cannot be an odd number")
|
|
143
138
|
return null;
|
|
144
139
|
}
|
|
145
140
|
const MAX_VALUE = commands.length - 1;
|
|
@@ -166,27 +161,15 @@ class TiledeskChatbotUtil {
|
|
|
166
161
|
let commands = message.attributes.commands;
|
|
167
162
|
message.text = "";
|
|
168
163
|
for (let i = commands.length - 1; i >= 0; i--) {
|
|
169
|
-
// console.log("...commands[" + i + "]");
|
|
170
164
|
if (commands[i].type === "message") { // is a message, not wait
|
|
171
|
-
// console.log("commands[i]:", commands[i].message.text);
|
|
172
|
-
// console.log("commands[i]:", lang, (commands[i].message["lang"] === lang));
|
|
173
|
-
|
|
174
165
|
// if (commands[i].message["lang"] && !(commands[i].message["lang"] === lang)) { // if there is a filter and the filter is false, remove
|
|
175
166
|
const jsonCondition = commands[i].message["_tdJSONCondition"];
|
|
176
|
-
// console.log("jsonCondition:", jsonCondition);
|
|
177
167
|
if (jsonCondition) {
|
|
178
|
-
// const expression = TiledeskExpression.JSONGroupsToExpression(jsonCondition.groups);
|
|
179
168
|
const expression = TiledeskExpression.JSONGroupToExpression(jsonCondition);
|
|
180
|
-
// console.log("full json condition expression eval on command.message:", expression);
|
|
181
169
|
const conditionResult = new TiledeskExpression().evaluateStaticExpression(expression, variables);
|
|
182
|
-
// console.log("conditionResult:", conditionResult);
|
|
183
|
-
// FALSE
|
|
184
|
-
// console.log("commands[i]lang:", commands[i]);
|
|
185
170
|
if (conditionResult === false) {
|
|
186
|
-
// console.log("deleting command:", commands[i]);
|
|
187
171
|
commands.splice(i, 1);
|
|
188
172
|
if (commands[i-1]) {
|
|
189
|
-
// console.log("commands[i-1]?:", commands[i-1]);
|
|
190
173
|
if (commands[i-1].type === "wait") {
|
|
191
174
|
commands.splice(i-1, 1);
|
|
192
175
|
i--;
|
|
@@ -194,19 +177,13 @@ class TiledeskChatbotUtil {
|
|
|
194
177
|
}
|
|
195
178
|
}
|
|
196
179
|
else {
|
|
197
|
-
// console.log("comands[i]:", commands[i], commands[i].message, commands[i].message.text)
|
|
198
180
|
if (commands[i] && commands[i].message && commands[i].message.text) {
|
|
199
|
-
// console.log("curr text:", message.text)
|
|
200
181
|
if (message.text === "") {
|
|
201
182
|
message.text = commands[i].message.text;
|
|
202
183
|
}
|
|
203
184
|
else {
|
|
204
185
|
message.text = (commands[i].message.text + "\n\n" + message.text).trim();
|
|
205
186
|
}
|
|
206
|
-
// console.log("new text:", message.text)
|
|
207
|
-
}
|
|
208
|
-
else {
|
|
209
|
-
// console.log("commands@", commands[i])
|
|
210
187
|
}
|
|
211
188
|
}
|
|
212
189
|
}
|
|
@@ -215,11 +192,6 @@ class TiledeskChatbotUtil {
|
|
|
215
192
|
}
|
|
216
193
|
}
|
|
217
194
|
}
|
|
218
|
-
// for (let i = 0; i < commands.length; i++) {
|
|
219
|
-
// if (commands[i].type === 'message' && commands[i].message && commands[i].message.text) {
|
|
220
|
-
// if (this.log) {console.log("[" + commands[i].message.lang + "]commands[i].message.text:", commands[i].message.text);}
|
|
221
|
-
// }
|
|
222
|
-
// }
|
|
223
195
|
}
|
|
224
196
|
}
|
|
225
197
|
|
|
@@ -229,20 +201,15 @@ class TiledeskChatbotUtil {
|
|
|
229
201
|
let commands = message.attributes.commands;
|
|
230
202
|
|
|
231
203
|
for (let i = commands.length - 1; i >= 0; i--) {
|
|
232
|
-
// console.log("...commands[" + i + "]");
|
|
233
204
|
if (commands[i].type === "message") { // is a message, not a "wait"
|
|
234
|
-
// console.log("commands[i]:", commands[i].message.text);
|
|
235
|
-
// let textEmpty = false;
|
|
236
205
|
if (commands[i].message) {
|
|
237
206
|
if (commands[i].message.type === "text") { // check text commands
|
|
238
207
|
if (( commands[i].message.text && commands[i].message.text.trim() === "") || !commands[i].message.text) {
|
|
239
|
-
// console.log("deleting command:", commands[i]);
|
|
240
208
|
commands.splice(i, 1);
|
|
241
209
|
if (commands[i-1]) {
|
|
242
210
|
if (commands[i-1].type === "wait") {
|
|
243
211
|
commands.splice(i-1, 1);
|
|
244
212
|
i--;
|
|
245
|
-
// console.log("deleted wait");
|
|
246
213
|
}
|
|
247
214
|
}
|
|
248
215
|
}
|
|
@@ -250,15 +217,10 @@ class TiledeskChatbotUtil {
|
|
|
250
217
|
}
|
|
251
218
|
}
|
|
252
219
|
}
|
|
253
|
-
// for (let i = 0; i < commands.length; i++) {
|
|
254
|
-
// if (commands[i].type === 'message' && commands[i].message && commands[i].message.text) {
|
|
255
|
-
// if (this.log) {console.log("[" + commands[i].message.lang + "]commands[i].message.text:", commands[i].message.text);}
|
|
256
|
-
// }
|
|
257
|
-
// }
|
|
258
220
|
}
|
|
259
221
|
}
|
|
260
222
|
catch(error) {
|
|
261
|
-
|
|
223
|
+
winston.error("(TiledeskChatbotUtils) Error while checking message ", error)
|
|
262
224
|
}
|
|
263
225
|
return message;
|
|
264
226
|
}
|
|
@@ -278,11 +240,8 @@ class TiledeskChatbotUtil {
|
|
|
278
240
|
if (!message) {
|
|
279
241
|
return;
|
|
280
242
|
}
|
|
281
|
-
// console.log("compute delay...", message)
|
|
282
243
|
if (message.attributes.commands.length > 0) {
|
|
283
|
-
// console.log("going on delay")
|
|
284
244
|
let commands = message.attributes.commands;
|
|
285
|
-
// console.log("got commands", commands)
|
|
286
245
|
let totalWaitTime = 0;
|
|
287
246
|
for (let i = commands.length - 1; i >= 0; i--) {
|
|
288
247
|
if (commands[i].type === "wait") { // is a wait
|
|
@@ -294,50 +253,38 @@ class TiledeskChatbotUtil {
|
|
|
294
253
|
}
|
|
295
254
|
|
|
296
255
|
static fillCommandAttachments(command, variables, log) {
|
|
297
|
-
if (log) {
|
|
298
|
-
|
|
299
|
-
}
|
|
300
|
-
if (command.message && command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.buttons && command.message.attributes.attachment.buttons.length > 0){
|
|
256
|
+
if (log) { winston.debug("(TiledeskChatbotUtils) Filling command button: ", command) }
|
|
257
|
+
if (command.message && command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.buttons && command.message.attributes.attachment.buttons.length > 0) {
|
|
301
258
|
let buttons = command.message.attributes.attachment.buttons;
|
|
302
259
|
const filler = new Filler();
|
|
303
260
|
buttons.forEach(button => {
|
|
304
261
|
if (button.link) {
|
|
305
262
|
button.link = filler.fill(button.link, variables);
|
|
306
|
-
if (log) {
|
|
307
|
-
console.log("button.link filled:", button.link)
|
|
308
|
-
}
|
|
263
|
+
if (log) { winston.debug("(TiledeskChatbotUtils) button.link filled: " + button.link) }
|
|
309
264
|
}
|
|
310
265
|
if (button.value) {
|
|
311
266
|
button.value = filler.fill(button.value, variables);
|
|
312
|
-
if (log) {
|
|
313
|
-
console.log("button.value filled:", button.value)
|
|
314
|
-
}
|
|
267
|
+
if (log) { winston.debug("(TiledeskChatbotUtils) button.value filled: " + button.value) }
|
|
315
268
|
}
|
|
316
269
|
});
|
|
317
270
|
}
|
|
318
|
-
else
|
|
319
|
-
|
|
271
|
+
else {
|
|
272
|
+
if (log) { winston.debug("(TiledeskChatbotUtils) No attachments to fill in command") }
|
|
320
273
|
}
|
|
321
274
|
}
|
|
322
275
|
|
|
323
276
|
static allReplyButtons(message) {
|
|
324
277
|
let all_buttons = [];
|
|
325
278
|
if (message.attributes && message.attributes.commands) {
|
|
326
|
-
// console.log("message.attributes ok")
|
|
327
279
|
let commands = message.attributes.commands;
|
|
328
280
|
if (commands.length > 0) {
|
|
329
|
-
// console.log("commands ok", commands.length)
|
|
330
281
|
for (let i = 0; i < commands.length; i++) {
|
|
331
282
|
let command = commands[i];
|
|
332
|
-
// console.log("got command:", command)s
|
|
333
283
|
if (command.type === 'message' && command.message) {
|
|
334
|
-
if (command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.buttons && command.message.attributes.attachment.buttons.length > 0){
|
|
335
|
-
// console.log("command with buttons ok:")
|
|
284
|
+
if (command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.buttons && command.message.attributes.attachment.buttons.length > 0) {
|
|
336
285
|
let buttons = command.message.attributes.attachment.buttons;
|
|
337
|
-
|
|
338
286
|
buttons.forEach(button => {
|
|
339
287
|
if (button.type === "action") {
|
|
340
|
-
// console.log("pushing button:", button);
|
|
341
288
|
all_buttons.push(button);
|
|
342
289
|
}
|
|
343
290
|
});
|
|
@@ -349,6 +296,87 @@ class TiledeskChatbotUtil {
|
|
|
349
296
|
return all_buttons;
|
|
350
297
|
}
|
|
351
298
|
|
|
299
|
+
static replaceJSONButtons(message, flow_attributes) {
|
|
300
|
+
let all_buttons = [];
|
|
301
|
+
if (message.attributes && message.attributes.commands) {
|
|
302
|
+
let commands = message.attributes.commands;
|
|
303
|
+
if (commands.length > 0) {
|
|
304
|
+
for (let i = 0; i < commands.length; i++) {
|
|
305
|
+
let command = commands[i];
|
|
306
|
+
if (command.type === 'message' && command.message) {
|
|
307
|
+
if (command.message.attributes && command.message.attributes.attachment && command.message.attributes.attachment.json_buttons){
|
|
308
|
+
// console.log("command with buttons ok:")
|
|
309
|
+
let json_buttons_string = command.message.attributes.attachment.json_buttons;
|
|
310
|
+
let json_buttons = null;
|
|
311
|
+
let final_buttons = [];
|
|
312
|
+
try {
|
|
313
|
+
// fill buttons
|
|
314
|
+
const filler = new Filler();
|
|
315
|
+
json_buttons_string = filler.fill(json_buttons_string, flow_attributes);
|
|
316
|
+
// console.log("json_buttons_string:", json_buttons_string);
|
|
317
|
+
json_buttons = JSON.parse(json_buttons_string);
|
|
318
|
+
if (Array.isArray(json_buttons)) {
|
|
319
|
+
json_buttons.forEach(button => {
|
|
320
|
+
if (button.value && button.type === "action" && button.action) {
|
|
321
|
+
button.show_echo = true;
|
|
322
|
+
// console.log("pushing:", button)
|
|
323
|
+
final_buttons.push(button);
|
|
324
|
+
}
|
|
325
|
+
else if (button.value && button.type === "text") {
|
|
326
|
+
button.show_echo = true;
|
|
327
|
+
// console.log("pushing:", button)
|
|
328
|
+
final_buttons.push(button);
|
|
329
|
+
}
|
|
330
|
+
else if (button.value && button.type === "url" && button.link) {
|
|
331
|
+
button.show_echo = true;
|
|
332
|
+
// console.log("pushing:", button)
|
|
333
|
+
final_buttons.push(button);
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
console.log("Invalid button. Skipping:", JSON.stringify(button) );
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// "buttons": [
|
|
342
|
+
// {
|
|
343
|
+
// "type": "action",
|
|
344
|
+
// "value": "Button1", // obbligatorio sempre
|
|
345
|
+
// "action": "#bb347206-d639-4926-94c9-e94930623dce", // mandatory
|
|
346
|
+
// "show_echo": true, // lo inserisco sempre
|
|
347
|
+
// "alias": "button1 alias"
|
|
348
|
+
// },
|
|
349
|
+
// {
|
|
350
|
+
// "type": "text",
|
|
351
|
+
// "value": "Button2 text", // obbligatorio sempre
|
|
352
|
+
// "show_echo": true // lo inserisco sempre
|
|
353
|
+
// },
|
|
354
|
+
// {
|
|
355
|
+
// "type": "url",
|
|
356
|
+
// "value": "Button3 link", // obbligatorio sempre
|
|
357
|
+
// "link": "http://", // obbligatorio
|
|
358
|
+
// "show_echo": true // lo inserisco sempre
|
|
359
|
+
// }
|
|
360
|
+
// ]
|
|
361
|
+
}
|
|
362
|
+
catch(error) {
|
|
363
|
+
console.error("Invalid json_buttons:", error)
|
|
364
|
+
}
|
|
365
|
+
if (final_buttons && final_buttons.length > 0) {
|
|
366
|
+
command.message.attributes.attachment.buttons = final_buttons;
|
|
367
|
+
delete command.message.attributes.attachment.json_buttons;
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
console.log("Invalid json_buttons. Skipping")
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return all_buttons;
|
|
378
|
+
}
|
|
379
|
+
|
|
352
380
|
static buttonByText(text, buttons) {
|
|
353
381
|
if (buttons === null || text === null) {
|
|
354
382
|
return null;
|
|
@@ -363,12 +391,10 @@ class TiledeskChatbotUtil {
|
|
|
363
391
|
}
|
|
364
392
|
else if (button.alias && button.alias.trim() !== "") { // search in button alias
|
|
365
393
|
let alias = button.alias.split(",");
|
|
366
|
-
// console.log("alias splitted:", alias);
|
|
367
394
|
if (alias.length > 0) {
|
|
368
395
|
for (let ii = 0; ii < alias.length; ii++) {
|
|
369
396
|
alias[ii] = alias[ii].toLowerCase().trim();
|
|
370
397
|
}
|
|
371
|
-
// console.log("alias proc:", alias);
|
|
372
398
|
if (alias.indexOf(search_text) > -1) {
|
|
373
399
|
selected_button = button;
|
|
374
400
|
break;
|
|
@@ -376,12 +402,10 @@ class TiledeskChatbotUtil {
|
|
|
376
402
|
}
|
|
377
403
|
}
|
|
378
404
|
}
|
|
379
|
-
|
|
380
405
|
return selected_button;
|
|
381
406
|
}
|
|
382
407
|
|
|
383
408
|
static stripEmoji(str) {
|
|
384
|
-
// console.log("checking:", str);
|
|
385
409
|
if (str === null) {
|
|
386
410
|
return str;
|
|
387
411
|
}
|
|
@@ -398,7 +422,6 @@ class TiledeskChatbotUtil {
|
|
|
398
422
|
const chatbot_name = chatbot.bot.name.trim();
|
|
399
423
|
if (message && message.text && message.text.trim() !== "" && message.sender !== "_tdinternal" && !this.isHiddenMessage(message)) {
|
|
400
424
|
let transcript = await chatbot.getParameter("transcript");
|
|
401
|
-
// console.log("transcript got:", transcript);
|
|
402
425
|
const _name_of = name_of(message, chatbot_name);
|
|
403
426
|
if (transcript) {
|
|
404
427
|
transcript = transcript + "\n" + _name_of + message.text;
|
|
@@ -406,10 +429,7 @@ class TiledeskChatbotUtil {
|
|
|
406
429
|
else {
|
|
407
430
|
transcript = _name_of + " " + message.text;
|
|
408
431
|
}
|
|
409
|
-
// console.log("transcript update:", transcript);
|
|
410
432
|
await chatbot.addParameter("transcript", transcript);
|
|
411
|
-
// let transcript2 = await chatbot.getParameter("transcript");
|
|
412
|
-
// console.log("transcript updated:", transcript2);
|
|
413
433
|
}
|
|
414
434
|
|
|
415
435
|
function name_of(message, chatbot_name) {
|
|
@@ -436,39 +456,32 @@ class TiledeskChatbotUtil {
|
|
|
436
456
|
static transcriptJSON(transcript) {
|
|
437
457
|
const regexp = /(<.*>)/gm;
|
|
438
458
|
const parts = transcript.split(regexp);
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
// console.log("length parts:", parts.length);
|
|
442
|
-
// console.log("Array.isArray(parts):", Array.isArray(parts));
|
|
459
|
+
winston.debug("(TiledeskChatbotUtils) transcriptJSON parts: ", parts)
|
|
460
|
+
|
|
443
461
|
let messages = [];
|
|
444
462
|
let current_message;
|
|
445
463
|
try {
|
|
446
464
|
for (let i = 0; i < parts.length; i++) {
|
|
447
465
|
let row = parts[i];
|
|
448
|
-
// console.log("row:", row)
|
|
449
466
|
if (row.startsWith("<bot:")) {
|
|
450
|
-
// console.log("start with", row)
|
|
451
467
|
current_message = {
|
|
452
468
|
"role": "assistant"
|
|
453
469
|
}
|
|
454
470
|
}
|
|
455
471
|
else if (row.startsWith("<user:")) {
|
|
456
|
-
// console.log("start with", row)
|
|
457
472
|
current_message = {
|
|
458
473
|
"role": "user"
|
|
459
474
|
}
|
|
460
475
|
}
|
|
461
476
|
else if (current_message) {
|
|
462
|
-
// console.log("adding text", row)
|
|
463
477
|
current_message["content"] = row.trim();
|
|
464
478
|
messages.push(current_message);
|
|
465
479
|
}
|
|
466
480
|
};
|
|
467
481
|
}
|
|
468
482
|
catch(error) {
|
|
469
|
-
|
|
483
|
+
winston.error("(TiledeskChatbotUtils) transcriptJSON err: ", error);
|
|
470
484
|
}
|
|
471
|
-
// console.log("messages:", messages);
|
|
472
485
|
return messages;
|
|
473
486
|
}
|
|
474
487
|
|
|
@@ -489,41 +502,42 @@ class TiledeskChatbotUtil {
|
|
|
489
502
|
|
|
490
503
|
static lastUserMessageFrom(msg) {
|
|
491
504
|
let message = {};
|
|
492
|
-
message["senderFullname"] = msg["senderFullname"];
|
|
493
|
-
message["type"] = msg["type"];
|
|
494
|
-
message["channel_type"] = msg["channel_type"];
|
|
495
|
-
message["status"] = msg["status"];
|
|
496
|
-
message["id"] = msg["_id"];
|
|
497
|
-
message["sender"] = msg["sender"];
|
|
498
|
-
message["recipient"] = msg["recipient"];
|
|
499
|
-
message["text"] = msg["text"];
|
|
500
|
-
message["createdBy"] = msg["createdBy"];
|
|
501
|
-
message["attributes"] = msg["attributes"];
|
|
505
|
+
message["senderFullname"] = msg["senderFullname"]; // ex. "Bot"
|
|
506
|
+
message["type"] = msg["type"]; // ex. "text",
|
|
507
|
+
message["channel_type"] = msg["channel_type"]; // ex. "group",
|
|
508
|
+
message["status"] = msg["status"]; // ex. 0,
|
|
509
|
+
message["id"] = msg["_id"]; // ex. "6538cda46cb4d8002cf2317a",
|
|
510
|
+
message["sender"] = msg["sender"]; // ex. "system",
|
|
511
|
+
message["recipient"] = msg["recipient"]; // ex. "support-group-65203e12f8c0cf002cf4110b-4066a69c8b464646a3ff25f9f41575bb",
|
|
512
|
+
message["text"] = msg["text"]; // ex. "\\start",
|
|
513
|
+
message["createdBy"] = msg["createdBy"]; // ex. "system",
|
|
514
|
+
message["attributes"] = msg["attributes"]; // ex. { "subtype": "info" }
|
|
502
515
|
message["metadata"] = msg["metadata"];
|
|
503
|
-
message["channel"] = msg["channel"];
|
|
516
|
+
message["channel"] = msg["channel"]; // ex. { "name": "chat21" }
|
|
504
517
|
return message;
|
|
505
518
|
}
|
|
506
519
|
|
|
507
520
|
static async updateRequestAttributes(chatbot, chatbotToken, message, projectId, requestId) {
|
|
508
521
|
// update request context
|
|
509
522
|
try {
|
|
510
|
-
if (chatbot.log) {
|
|
523
|
+
if (chatbot.log) { winston.debug("Updating request variables. Message:", message); }
|
|
511
524
|
const messageId = message._id;
|
|
512
525
|
const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
|
|
513
|
-
|
|
526
|
+
|
|
514
527
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url);
|
|
515
|
-
// console.log("Adding proj_", projectId);
|
|
516
528
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
|
|
517
|
-
// TODO add projectName too
|
|
518
529
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
|
|
530
|
+
|
|
519
531
|
if (chatbot.bot) {
|
|
520
532
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
|
|
521
533
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_ID_KEY, chatbot.bot._id);
|
|
522
534
|
}
|
|
535
|
+
|
|
523
536
|
if (chatbotToken) {
|
|
524
537
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
|
|
525
538
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
|
|
526
539
|
}
|
|
540
|
+
|
|
527
541
|
if (process.env.TILEDESK_API) {
|
|
528
542
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
|
|
529
543
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
|
|
@@ -534,7 +548,6 @@ class TiledeskChatbotUtil {
|
|
|
534
548
|
}
|
|
535
549
|
|
|
536
550
|
if (message.text && message.sender !== "_tdinternal") {
|
|
537
|
-
// await chatbot.addParameter(TiledeskChatbotConst.USER_INPUT, true); // set userInput
|
|
538
551
|
await chatbot.deleteParameter(TiledeskChatbotConst.USER_INPUT); // user wrote, delete userInput, replyv2 will not trigger timeout action
|
|
539
552
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text); // DEPRECATED
|
|
540
553
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_v2_KEY, message.text);
|
|
@@ -552,44 +565,18 @@ class TiledeskChatbotUtil {
|
|
|
552
565
|
|
|
553
566
|
// get image
|
|
554
567
|
if (message.type && message.type === "image" && message.metadata) {
|
|
555
|
-
// "text": "\nimage text",
|
|
556
|
-
// "id_project": "65203e12f8c0cf002cf4110b",
|
|
557
|
-
// "createdBy": "8ac52a30-133f-4ee1-8b4b-96055bb81757",
|
|
558
|
-
// "metadata": {
|
|
559
|
-
// "height": 905,
|
|
560
|
-
// "name": "tiledesk_Open graph_general.png",
|
|
561
|
-
// "src": "https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2Fda5bbc8d-5174-49a8-a041-3d9355242da5%2Ftiledesk_Open%20graph_general.png?alt=media&token=be82fecb-3cd1-45b9-a135-c2c57a932862",
|
|
562
|
-
// "type": "image/png",
|
|
563
|
-
// "uid": "lo68iyq5",
|
|
564
|
-
// "width": 1724
|
|
565
|
-
// }
|
|
566
568
|
if (message.metadata.src) {
|
|
567
|
-
await chatbot.addParameter(
|
|
568
|
-
await chatbot.addParameter(
|
|
569
|
-
await chatbot.addParameter(
|
|
570
|
-
await chatbot.addParameter(
|
|
571
|
-
await chatbot.addParameter(
|
|
572
|
-
}
|
|
573
|
-
}
|
|
574
|
-
|
|
575
|
-
// await chatbot.addParameter("lastUserImageURL", null);
|
|
576
|
-
// await chatbot.addParameter("lastUserImageName", null);
|
|
577
|
-
// await chatbot.addParameter("lastUserImageWidth", null);
|
|
578
|
-
// await chatbot.addParameter("lastUserImageHeight", null);
|
|
579
|
-
// await chatbot.addParameter("lastUserImageType", null);
|
|
580
|
-
// }
|
|
569
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_URL, message.metadata.src);
|
|
570
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_NAME, message.metadata.name);
|
|
571
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_WIDTH, message.metadata.width);
|
|
572
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_HEIGHT, message.metadata.height);
|
|
573
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_TYPE, message.metadata.type);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
581
577
|
// get document
|
|
582
578
|
if (message.type && message.type === "file" && message.metadata) {
|
|
583
|
-
|
|
584
|
-
// "text": "[LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf](https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2F502265ee-4f4a-47a4-9375-172bb0e6bf39%2FLIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf?alt=media&token=a09d065a-9b56-4507-8960-344cc294e4d1)\nistruzioni",
|
|
585
|
-
// "metadata": {
|
|
586
|
-
// "name": "LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf",
|
|
587
|
-
// "src": "https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2F502265ee-4f4a-47a4-9375-172bb0e6bf39%2FLIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf?alt=media&token=a09d065a-9b56-4507-8960-344cc294e4d1",
|
|
588
|
-
// "type": "application/pdf",
|
|
589
|
-
// "uid": "lo68oz8i"
|
|
590
|
-
// }
|
|
591
|
-
if (message.metadata.src) {
|
|
592
|
-
|
|
579
|
+
if (message.metadata.src) {
|
|
593
580
|
await chatbot.addParameter("lastUserDocumentURL", message.metadata.src); // legacy. will be deprecated
|
|
594
581
|
const url_as_attachment = message.metadata.src;
|
|
595
582
|
await chatbot.addParameter("lastUserDocumentAsAttachmentURL", url_as_attachment);
|
|
@@ -602,44 +589,34 @@ class TiledeskChatbotUtil {
|
|
|
602
589
|
await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
|
|
603
590
|
}
|
|
604
591
|
}
|
|
605
|
-
|
|
606
|
-
// await chatbot.addParameter("lastUserDocumentURL", null);
|
|
607
|
-
// await chatbot.addParameter("lastUserDocumentName", null);
|
|
608
|
-
// await chatbot.addParameter("lastUserDocumentType", null);
|
|
609
|
-
// }
|
|
592
|
+
|
|
610
593
|
if (message && message.request && message.request.lead) {
|
|
611
|
-
if (chatbot.log) {
|
|
594
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Lead found with email: " + message.request.lead.email + " and lead.fullname " + message.request.lead.fullname); }
|
|
612
595
|
let currentLeadEmail = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY);
|
|
613
|
-
if (chatbot.log) {
|
|
596
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) You lead email from attributes: " + currentLeadEmail); }
|
|
614
597
|
if (message.request.lead.email && !currentLeadEmail) {
|
|
615
598
|
// worth saving
|
|
616
|
-
if (chatbot.log) {
|
|
599
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) worth saving email"); }
|
|
617
600
|
try {
|
|
618
601
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY, message.request.lead.email);
|
|
619
602
|
}
|
|
620
603
|
catch(error) {
|
|
621
|
-
|
|
604
|
+
winston.error("(TiledeskChatbotUtil) Error on setting userEmail:", error);
|
|
622
605
|
}
|
|
623
606
|
}
|
|
624
607
|
let currentLeadName = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY);
|
|
625
|
-
if (chatbot.log) {
|
|
626
|
-
// if (message.request.lead.fullname && message.request.lead.fullname.startsWith("guest#") && !currentLeadName) {
|
|
627
|
-
// if (message.request.lead.fullname) {
|
|
608
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) You lead email from attributes: " + currentLeadEmail); }
|
|
628
609
|
if (message.request.lead.fullname && !currentLeadName) {
|
|
629
610
|
// worth saving
|
|
630
|
-
if (chatbot.log) {
|
|
611
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) worth saving email"); }
|
|
631
612
|
try {
|
|
632
613
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, message.request.lead.fullname);
|
|
633
614
|
}
|
|
634
615
|
catch(error) {
|
|
635
|
-
|
|
616
|
+
winston.error("(TiledeskChatbotUtil) Error on setting userFullname: ", error);
|
|
636
617
|
}
|
|
637
|
-
// }
|
|
638
|
-
// else {
|
|
639
|
-
// // console.log("!lead.fullname");
|
|
640
|
-
// }
|
|
641
618
|
}
|
|
642
|
-
|
|
619
|
+
|
|
643
620
|
if (message.request.lead.phone) {
|
|
644
621
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_PHONE_KEY, message.request.lead.phone);
|
|
645
622
|
}
|
|
@@ -667,15 +644,12 @@ class TiledeskChatbotUtil {
|
|
|
667
644
|
if (message.request && message.request.location && message.request.location.city) {
|
|
668
645
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
|
|
669
646
|
}
|
|
670
|
-
// console.log("message.request.language", message.request["language"]);
|
|
671
647
|
if (message.request) {
|
|
672
648
|
let user_language = message.request["language"];
|
|
673
649
|
if (message.request["language"]) {
|
|
674
|
-
// console.log("HTTP language:", message.request["language"]);
|
|
675
650
|
var languages = parser.parse(message.request["language"]);
|
|
676
|
-
// console.log("languages:", languages);
|
|
677
651
|
if (languages && languages.length > 0 && languages[0].code) {
|
|
678
|
-
|
|
652
|
+
user_language = languages[0].code;
|
|
679
653
|
}
|
|
680
654
|
}
|
|
681
655
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY, message.request.sourcePage);
|
|
@@ -693,7 +667,6 @@ class TiledeskChatbotUtil {
|
|
|
693
667
|
}
|
|
694
668
|
}
|
|
695
669
|
}
|
|
696
|
-
// console.log("message.request.language", message.request["language"])
|
|
697
670
|
if (message.request && message.request.department) {
|
|
698
671
|
// It was an error when getting this from widget message's attributes
|
|
699
672
|
// await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
|
|
@@ -711,29 +684,24 @@ class TiledeskChatbotUtil {
|
|
|
711
684
|
if (!message.attributes) {
|
|
712
685
|
message.attributes = {}
|
|
713
686
|
}
|
|
714
|
-
message.attributes.payload = { ...message.attributes.payload, ...message.request.attributes.payload}
|
|
715
|
-
if (chatbot.log) {
|
|
716
|
-
// if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
|
|
687
|
+
message.attributes.payload = { ...message.attributes.payload, ...message.request.attributes.payload }
|
|
688
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Forced Set message.attributes.payload ", message.attributes.payload); }
|
|
717
689
|
}
|
|
718
690
|
if (message.attributes) {
|
|
719
|
-
if (chatbot.log) {
|
|
720
|
-
|
|
691
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Ok message.attributes ", message.attributes); }
|
|
692
|
+
|
|
721
693
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
|
|
722
694
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
|
|
723
695
|
if (message.attributes.payload) {
|
|
724
696
|
try {
|
|
725
697
|
for (const [key, value] of Object.entries(message.attributes.payload)) {
|
|
726
|
-
// const value = all_parameters[key];
|
|
727
698
|
const value_type = typeof value;
|
|
728
|
-
//if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
|
|
729
|
-
//await chatbot.addParameter(key, String(value));
|
|
730
|
-
// console.log("Adding from message.attributes:", key, "->", value);
|
|
731
699
|
await chatbot.addParameter(key, value);
|
|
732
700
|
}
|
|
733
701
|
await chatbot.addParameter("payload", message.attributes.payload);
|
|
734
702
|
}
|
|
735
703
|
catch(err) {
|
|
736
|
-
|
|
704
|
+
winston.error("(TiledeskChatbotUtil) Error importing message payload in request variables: ", err);
|
|
737
705
|
}
|
|
738
706
|
}
|
|
739
707
|
|
|
@@ -754,14 +722,12 @@ class TiledeskChatbotUtil {
|
|
|
754
722
|
|
|
755
723
|
|
|
756
724
|
const _bot = chatbot.bot; // aka FaqKB
|
|
757
|
-
if (chatbot.log) {
|
|
758
|
-
console.log("Adding Globals to context..., chatbot.attributes?", JSON.stringify(_bot));
|
|
759
|
-
}
|
|
725
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Adding Globals to context: ", _bot); }
|
|
760
726
|
|
|
761
727
|
if (_bot.attributes && _bot.attributes.globals) {
|
|
762
|
-
if (chatbot.log) {
|
|
728
|
+
if (chatbot.log) { winston.error("(TiledeskChatbotUtil) Got Globals: ", _bot.attributes.globals); }
|
|
763
729
|
_bot.attributes.globals.forEach(async (global_var) => {
|
|
764
|
-
if (chatbot.log) {
|
|
730
|
+
if (chatbot.log) { winston.error("(TiledeskChatbotUtil) Adding global: " + global_var.key + " value: " + global_var.value); }
|
|
765
731
|
await chatbot.addParameter(global_var.key, global_var.value);
|
|
766
732
|
});
|
|
767
733
|
}
|
|
@@ -775,52 +741,23 @@ class TiledeskChatbotUtil {
|
|
|
775
741
|
// );
|
|
776
742
|
|
|
777
743
|
if (chatbot.log) {
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
// console.log("TiledeskChatbot:", TiledeskChatbot);
|
|
744
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) requestId: " + requestId); }
|
|
745
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Key: " + TiledeskChatbotConst.REQ_PROJECT_ID_KEY); }
|
|
746
|
+
|
|
782
747
|
let proj_ = await chatbot.getParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
|
|
783
|
-
|
|
748
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) request parameter proj_: " + proj_); }
|
|
749
|
+
|
|
784
750
|
const all_parameters = await chatbot.allParameters();
|
|
785
751
|
for (const [key, value] of Object.entries(all_parameters)) {
|
|
786
|
-
// const value = all_parameters[key];
|
|
787
752
|
const value_type = typeof value;
|
|
788
|
-
if (chatbot.log) {
|
|
753
|
+
if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Request Attribute: " + key + " value: " + value + " type: " + value_type); }
|
|
754
|
+
|
|
789
755
|
}
|
|
790
756
|
}
|
|
791
757
|
} catch(error) {
|
|
792
|
-
|
|
758
|
+
winston.error("(TiledeskChatbotUtil) updateRequestAttributes Error: ", error);
|
|
793
759
|
process.exit(1)
|
|
794
760
|
}
|
|
795
|
-
// message["attributes"]: {
|
|
796
|
-
// "departmentId": "63c980054f857c00350535bc",
|
|
797
|
-
// "departmentName": "Default Department",
|
|
798
|
-
// "client": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
|
|
799
|
-
// "sourcePage": "https://tiledesk-html-site.tiledesk.repl.co/custom-attributes.html",
|
|
800
|
-
// "projectId": "63c980054f857c00350535b8",
|
|
801
|
-
// "payload": {
|
|
802
|
-
// "user_country": "Italy",
|
|
803
|
-
// "user_code": "E001"
|
|
804
|
-
// },
|
|
805
|
-
// "userFullname": "guest#7216 ",
|
|
806
|
-
// "requester_id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
807
|
-
// "ipAddress": "79.8.190.172",
|
|
808
|
-
// "sourceTitle": "Custom attributes",
|
|
809
|
-
// "widgetVer": "v.5.0.53-rc.4",
|
|
810
|
-
// "subtype": "info",
|
|
811
|
-
// "decoded_jwt": {
|
|
812
|
-
// "_id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
813
|
-
// "firstname": "guest#7216",
|
|
814
|
-
// "id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
815
|
-
// "fullName": "guest#7216 ",
|
|
816
|
-
// "iat": 1674201892,
|
|
817
|
-
// "aud": "https://tiledesk.com",
|
|
818
|
-
// "iss": "https://tiledesk.com",
|
|
819
|
-
// "sub": "guest",
|
|
820
|
-
// "jti": "f053af3d-14ca-411b-9903-78bd74e24218"
|
|
821
|
-
// }
|
|
822
|
-
// let userFullname = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY);
|
|
823
|
-
// console.log("userFullname:", userFullname);
|
|
824
761
|
}
|
|
825
762
|
|
|
826
763
|
static actionsToDirectives(actions) {
|
|
@@ -837,21 +774,17 @@ class TiledeskChatbotUtil {
|
|
|
837
774
|
}
|
|
838
775
|
|
|
839
776
|
static addConnectAction(reply) {
|
|
840
|
-
// console.log("reply foraddConnectAction:", reply);
|
|
841
777
|
if (reply && reply.attributes && reply.attributes.nextBlockAction) {
|
|
842
778
|
if (reply.actions) {
|
|
843
779
|
reply.actions.push(reply.attributes.nextBlockAction);
|
|
844
|
-
// console.log("actions are:", reply.actions)
|
|
845
780
|
}
|
|
846
781
|
}
|
|
847
782
|
}
|
|
848
783
|
|
|
849
784
|
static validateRequestId(requestId, projectId) {
|
|
850
|
-
// console.log("checking requestId:", requestId, projectId)
|
|
851
785
|
let isValid = false;
|
|
852
786
|
if (requestId.startsWith("support-group-")) {
|
|
853
787
|
const parts = requestId.split("-");
|
|
854
|
-
// console.log("parts support request:", parts);
|
|
855
788
|
if (parts.length >= 4) {
|
|
856
789
|
isValid = (parts[0] === "support" && parts[1] === "group" && parts[2] === projectId && parts[3].length > 0);
|
|
857
790
|
}
|
|
@@ -860,7 +793,6 @@ class TiledeskChatbotUtil {
|
|
|
860
793
|
}
|
|
861
794
|
} else if (requestId.startsWith("automation-request-")) {
|
|
862
795
|
const parts = requestId.split("-");
|
|
863
|
-
// console.log("parts automation request:", parts);
|
|
864
796
|
if (parts.length === 4) {
|
|
865
797
|
isValid = (parts[0] === "automation" && parts[1] === "request" && parts[2] === projectId && parts[3].length > 0);
|
|
866
798
|
}
|
|
@@ -895,19 +827,18 @@ class TiledeskChatbotUtil {
|
|
|
895
827
|
TiledeskChatbotConst.REQ_TRANSCRIPT_KEY,
|
|
896
828
|
TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_KEY,
|
|
897
829
|
TiledeskChatbotConst.REQ_DECODED_JWT_KEY,
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
830
|
+
TiledeskChatbotConst.REQ_LAST_USER_IMAGE_URL,
|
|
831
|
+
TiledeskChatbotConst.REQ_LAST_USER_IMAGE_NAME,
|
|
832
|
+
TiledeskChatbotConst.REQ_LAST_USER_IMAGE_WIDTH,
|
|
833
|
+
TiledeskChatbotConst.REQ_LAST_USER_IMAGE_HEIGHT,
|
|
834
|
+
TiledeskChatbotConst.REQ_LAST_USER_IMAGE_TYPE,
|
|
835
|
+
TiledeskChatbotConst.REQ_LAST_USER_DOCUMENT_URL,
|
|
836
|
+
TiledeskChatbotConst.REQ_LAST_USER_DOCUMENT_NAME,
|
|
837
|
+
TiledeskChatbotConst.REQ_LAST_USER_DOCUMENT_TYPE,
|
|
838
|
+
TiledeskChatbotConst.REQ_TICKET_ID_KEY,
|
|
907
839
|
TiledeskChatbotConst.REQ_CHAT_CHANNEL,
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
"lastUserText",
|
|
840
|
+
TiledeskChatbotConst.REQ_USER_LEAD_ID_KEY,
|
|
841
|
+
TiledeskChatbotConst.REQ_LAST_USER_TEXT_v2_KEY,
|
|
911
842
|
TiledeskChatbotConst.REQ_REQUESTER_IS_AUTHENTICATED_KEY,
|
|
912
843
|
TiledeskChatbotConst.USER_INPUT,
|
|
913
844
|
TiledeskChatbotConst.REQ_CHATBOT_TOKEN,
|
|
@@ -916,7 +847,6 @@ class TiledeskChatbotUtil {
|
|
|
916
847
|
let userParams = {};
|
|
917
848
|
if (flowAttributes) {
|
|
918
849
|
for (const [key, value] of Object.entries(flowAttributes)) {
|
|
919
|
-
// console.log(key, value);
|
|
920
850
|
// There is a bug that moves the requestId as a key in request attributes, so: && !key.startsWith("support-group-")
|
|
921
851
|
if (!key.startsWith("_") && !RESERVED.some(e => e === key) && !key.startsWith("support-group-")) {
|
|
922
852
|
userParams[key] = value;
|
|
@@ -960,8 +890,8 @@ class TiledeskChatbotUtil {
|
|
|
960
890
|
|
|
961
891
|
myrequest(options, callback, log) {
|
|
962
892
|
if (log) {
|
|
963
|
-
|
|
964
|
-
|
|
893
|
+
winston.debug("(TiledeskChatbotUtil) myrequest API URL: " + options.url);
|
|
894
|
+
winston.debug("(TiledeskChatbotUtil) myrequest Options URL: ", options);
|
|
965
895
|
}
|
|
966
896
|
axios(
|
|
967
897
|
{
|
|
@@ -973,8 +903,8 @@ class TiledeskChatbotUtil {
|
|
|
973
903
|
})
|
|
974
904
|
.then((res) => {
|
|
975
905
|
if (log) {
|
|
976
|
-
|
|
977
|
-
|
|
906
|
+
winston.debug("(TiledeskChatbotUtil) Response for url: " + options.url);
|
|
907
|
+
winston.debug("(TiledeskChatbotUtil) Response headers:\n", options);
|
|
978
908
|
}
|
|
979
909
|
if (res && res.status == 200 && res.data) {
|
|
980
910
|
if (callback) {
|
|
@@ -988,7 +918,7 @@ class TiledeskChatbotUtil {
|
|
|
988
918
|
}
|
|
989
919
|
})
|
|
990
920
|
.catch((error) => {
|
|
991
|
-
|
|
921
|
+
winston.error("(TiledeskChatbotUtil) Axios error: ", error.response.data);
|
|
992
922
|
if (callback) {
|
|
993
923
|
callback(error, null, null);
|
|
994
924
|
}
|