@tiledesk/tiledesk-tybot-connector 0.5.0-rc1 → 0.5.0-rc2

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.
Files changed (83) hide show
  1. package/ExtApi.js +3 -13
  2. package/ExtUtil.js +0 -1
  3. package/TdCache.js +5 -3
  4. package/TiledeskClientTest.js +5 -28
  5. package/TiledeskExpression.js +6 -47
  6. package/index.js +87 -273
  7. package/logs/app.log +12983 -275
  8. package/logs/app1.log +62258 -0
  9. package/logs/app2.log +46280 -0
  10. package/logs/app3.log +28864 -0
  11. package/models/IntentForm.js +30 -44
  12. package/models/IntentsMachineFactory.js +4 -3
  13. package/models/MockBotsDataSource.js +1 -17
  14. package/models/MockTdCache.js +0 -9
  15. package/models/MongodbBotsDataSource.js +29 -53
  16. package/models/MongodbIntentsMachine.js +5 -4
  17. package/models/TiledeskChatbot.js +79 -258
  18. package/models/TiledeskChatbotConst.js +9 -0
  19. package/models/TiledeskChatbotUtil.js +79 -230
  20. package/models/TiledeskIntentsMachine.js +5 -14
  21. package/models/faqKbService.js +0 -1
  22. package/package.json +4 -2
  23. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +54 -140
  24. package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +7 -13
  25. package/tiledeskChatbotPlugs/Filler.js +0 -1
  26. package/tiledeskChatbotPlugs/MarkbotChatbotPlug.js +12 -20
  27. package/tiledeskChatbotPlugs/MessagePipeline.js +5 -15
  28. package/tiledeskChatbotPlugs/SplitsChatbotPlug.js +4 -13
  29. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +21 -89
  30. package/tiledeskChatbotPlugs/directives/DirAddTags.js +15 -98
  31. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +3 -3
  32. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +32 -57
  33. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +46 -70
  34. package/tiledeskChatbotPlugs/directives/DirAssign.js +11 -22
  35. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +15 -11
  36. package/tiledeskChatbotPlugs/directives/DirAssistant.js +48 -97
  37. package/tiledeskChatbotPlugs/directives/DirBrevo.js +37 -68
  38. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +8 -18
  39. package/tiledeskChatbotPlugs/directives/DirClose.js +3 -1
  40. package/tiledeskChatbotPlugs/directives/DirCode.js +15 -22
  41. package/tiledeskChatbotPlugs/directives/DirCondition.js +18 -24
  42. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +9 -13
  43. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +7 -19
  44. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +30 -57
  45. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +15 -20
  46. package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +7 -7
  47. package/tiledeskChatbotPlugs/directives/DirDepartment.js +13 -12
  48. package/tiledeskChatbotPlugs/directives/DirDisableInputText.js +2 -6
  49. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +4 -4
  50. package/tiledeskChatbotPlugs/directives/DirForm.js +15 -22
  51. package/tiledeskChatbotPlugs/directives/DirGptTask.js +38 -65
  52. package/tiledeskChatbotPlugs/directives/DirHubspot.js +27 -58
  53. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +19 -26
  54. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +22 -40
  55. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +17 -32
  56. package/tiledeskChatbotPlugs/directives/DirIntent.js +10 -28
  57. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +14 -13
  58. package/tiledeskChatbotPlugs/directives/DirLockIntent.js +7 -12
  59. package/tiledeskChatbotPlugs/directives/DirMake.js +20 -52
  60. package/tiledeskChatbotPlugs/directives/DirMessage.js +35 -28
  61. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +10 -20
  62. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +4 -2
  63. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +4 -2
  64. package/tiledeskChatbotPlugs/directives/DirQapla.js +21 -50
  65. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +9 -17
  66. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +4 -0
  67. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +4 -1
  68. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +10 -21
  69. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +9 -19
  70. package/tiledeskChatbotPlugs/directives/DirReply.js +18 -34
  71. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +25 -69
  72. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +8 -9
  73. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +13 -12
  74. package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +7 -4
  75. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +22 -75
  76. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +14 -13
  77. package/tiledeskChatbotPlugs/directives/DirUnlockIntent.js +3 -3
  78. package/tiledeskChatbotPlugs/directives/DirWait.js +4 -5
  79. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +13 -48
  80. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +11 -76
  81. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +11 -14
  82. package/tiledeskChatbotPlugs/directives/Directives.js +0 -3
  83. package/utils/winston.js +42 -0
@@ -6,6 +6,7 @@ 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('../utils/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
- console.log("error on intent.parameters = JSON.parse(json_string)", err);
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
- console.log("Error. commands.length cannot be an odd number");
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
- log.error("error while checking", error)
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
- console.log("filling command button:", JSON.stringify(command))
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 if (log) {
319
- console.log("No attachments to fill in command")
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
  });
@@ -462,12 +409,10 @@ class TiledeskChatbotUtil {
462
409
  }
463
410
  else if (button.alias && button.alias.trim() !== "") { // search in button alias
464
411
  let alias = button.alias.split(",");
465
- // console.log("alias splitted:", alias);
466
412
  if (alias.length > 0) {
467
413
  for (let ii = 0; ii < alias.length; ii++) {
468
414
  alias[ii] = alias[ii].toLowerCase().trim();
469
415
  }
470
- // console.log("alias proc:", alias);
471
416
  if (alias.indexOf(search_text) > -1) {
472
417
  selected_button = button;
473
418
  break;
@@ -475,12 +420,10 @@ class TiledeskChatbotUtil {
475
420
  }
476
421
  }
477
422
  }
478
-
479
423
  return selected_button;
480
424
  }
481
425
 
482
426
  static stripEmoji(str) {
483
- // console.log("checking:", str);
484
427
  if (str === null) {
485
428
  return str;
486
429
  }
@@ -497,7 +440,6 @@ class TiledeskChatbotUtil {
497
440
  const chatbot_name = chatbot.bot.name.trim();
498
441
  if (message && message.text && message.text.trim() !== "" && message.sender !== "_tdinternal" && !this.isHiddenMessage(message)) {
499
442
  let transcript = await chatbot.getParameter("transcript");
500
- // console.log("transcript got:", transcript);
501
443
  const _name_of = name_of(message, chatbot_name);
502
444
  if (transcript) {
503
445
  transcript = transcript + "\n" + _name_of + message.text;
@@ -505,10 +447,7 @@ class TiledeskChatbotUtil {
505
447
  else {
506
448
  transcript = _name_of + " " + message.text;
507
449
  }
508
- // console.log("transcript update:", transcript);
509
450
  await chatbot.addParameter("transcript", transcript);
510
- // let transcript2 = await chatbot.getParameter("transcript");
511
- // console.log("transcript updated:", transcript2);
512
451
  }
513
452
 
514
453
  function name_of(message, chatbot_name) {
@@ -535,39 +474,32 @@ class TiledeskChatbotUtil {
535
474
  static transcriptJSON(transcript) {
536
475
  const regexp = /(<.*>)/gm;
537
476
  const parts = transcript.split(regexp);
538
- // console.log("parts:", parts);
539
- // console.log("typeof parts:", typeof parts);
540
- // console.log("length parts:", parts.length);
541
- // console.log("Array.isArray(parts):", Array.isArray(parts));
477
+ winston.debug("(TiledeskChatbotUtils) transcriptJSON parts: ", parts)
478
+
542
479
  let messages = [];
543
480
  let current_message;
544
481
  try {
545
482
  for (let i = 0; i < parts.length; i++) {
546
483
  let row = parts[i];
547
- // console.log("row:", row)
548
484
  if (row.startsWith("<bot:")) {
549
- // console.log("start with", row)
550
485
  current_message = {
551
486
  "role": "assistant"
552
487
  }
553
488
  }
554
489
  else if (row.startsWith("<user:")) {
555
- // console.log("start with", row)
556
490
  current_message = {
557
491
  "role": "user"
558
492
  }
559
493
  }
560
494
  else if (current_message) {
561
- // console.log("adding text", row)
562
495
  current_message["content"] = row.trim();
563
496
  messages.push(current_message);
564
497
  }
565
498
  };
566
499
  }
567
500
  catch(error) {
568
- console.error("err:", error);
501
+ winston.error("(TiledeskChatbotUtils) transcriptJSON err: ", error);
569
502
  }
570
- // console.log("messages:", messages);
571
503
  return messages;
572
504
  }
573
505
 
@@ -588,41 +520,42 @@ class TiledeskChatbotUtil {
588
520
 
589
521
  static lastUserMessageFrom(msg) {
590
522
  let message = {};
591
- message["senderFullname"] = msg["senderFullname"]; // ex. "Bot"
592
- message["type"] = msg["type"]; // ex. "text",
593
- message["channel_type"] = msg["channel_type"]; // ex. "group",
594
- message["status"] = msg["status"]; // ex. 0,
595
- message["id"] = msg["_id"]; // ex. "6538cda46cb4d8002cf2317a",
596
- message["sender"] = msg["sender"]; // ex. "system",
597
- message["recipient"] = msg["recipient"]; // ex. "support-group-65203e12f8c0cf002cf4110b-4066a69c8b464646a3ff25f9f41575bb",
598
- message["text"] = msg["text"]; // ex. "\\start",
599
- message["createdBy"] = msg["createdBy"]; // ex. "system",
600
- message["attributes"] = msg["attributes"]; // ex. { "subtype": "info" }
523
+ message["senderFullname"] = msg["senderFullname"]; // ex. "Bot"
524
+ message["type"] = msg["type"]; // ex. "text",
525
+ message["channel_type"] = msg["channel_type"]; // ex. "group",
526
+ message["status"] = msg["status"]; // ex. 0,
527
+ message["id"] = msg["_id"]; // ex. "6538cda46cb4d8002cf2317a",
528
+ message["sender"] = msg["sender"]; // ex. "system",
529
+ message["recipient"] = msg["recipient"]; // ex. "support-group-65203e12f8c0cf002cf4110b-4066a69c8b464646a3ff25f9f41575bb",
530
+ message["text"] = msg["text"]; // ex. "\\start",
531
+ message["createdBy"] = msg["createdBy"]; // ex. "system",
532
+ message["attributes"] = msg["attributes"]; // ex. { "subtype": "info" }
601
533
  message["metadata"] = msg["metadata"];
602
- message["channel"] = msg["channel"]; // ex. { "name": "chat21" }
534
+ message["channel"] = msg["channel"]; // ex. { "name": "chat21" }
603
535
  return message;
604
536
  }
605
537
 
606
538
  static async updateRequestAttributes(chatbot, chatbotToken, message, projectId, requestId) {
607
539
  // update request context
608
540
  try {
609
- if (chatbot.log) {console.log("Updating request variables. Message:", JSON.stringify(message));}
541
+ if (chatbot.log) { winston.debug("Updating request variables. Message:", message); }
610
542
  const messageId = message._id;
611
543
  const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
612
- // await chatbot.addParameter("chatbot", chatbot);
544
+
613
545
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url);
614
- // console.log("Adding proj_", projectId);
615
546
  await chatbot.addParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
616
- // TODO add projectName too
617
547
  await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
548
+
618
549
  if (chatbot.bot) {
619
550
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
620
551
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_ID_KEY, chatbot.bot._id);
621
552
  }
553
+
622
554
  if (chatbotToken) {
623
555
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
624
556
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
625
557
  }
558
+
626
559
  if (process.env.TILEDESK_API) {
627
560
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken); // DEPRECATED
628
561
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN_v2, "JWT " + chatbotToken);
@@ -633,7 +566,6 @@ class TiledeskChatbotUtil {
633
566
  }
634
567
 
635
568
  if (message.text && message.sender !== "_tdinternal") {
636
- // await chatbot.addParameter(TiledeskChatbotConst.USER_INPUT, true); // set userInput
637
569
  await chatbot.deleteParameter(TiledeskChatbotConst.USER_INPUT); // user wrote, delete userInput, replyv2 will not trigger timeout action
638
570
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text); // DEPRECATED
639
571
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_v2_KEY, message.text);
@@ -651,44 +583,18 @@ class TiledeskChatbotUtil {
651
583
 
652
584
  // get image
653
585
  if (message.type && message.type === "image" && message.metadata) {
654
- // "text": "\nimage text",
655
- // "id_project": "65203e12f8c0cf002cf4110b",
656
- // "createdBy": "8ac52a30-133f-4ee1-8b4b-96055bb81757",
657
- // "metadata": {
658
- // "height": 905,
659
- // "name": "tiledesk_Open graph_general.png",
660
- // "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",
661
- // "type": "image/png",
662
- // "uid": "lo68iyq5",
663
- // "width": 1724
664
- // }
665
586
  if (message.metadata.src) {
666
- await chatbot.addParameter("lastUserImageURL", message.metadata.src);
667
- await chatbot.addParameter("lastUserImageName", message.metadata.name);
668
- await chatbot.addParameter("lastUserImageWidth", message.metadata.width);
669
- await chatbot.addParameter("lastUserImageHeight", message.metadata.height);
670
- await chatbot.addParameter("lastUserImageType", message.metadata.type);
587
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_URL, message.metadata.src);
588
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_NAME, message.metadata.name);
589
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_WIDTH, message.metadata.width);
590
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_HEIGHT, message.metadata.height);
591
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_IMAGE_TYPE, message.metadata.type);
671
592
  }
672
593
  }
673
- // else {
674
- // await chatbot.addParameter("lastUserImageURL", null);
675
- // await chatbot.addParameter("lastUserImageName", null);
676
- // await chatbot.addParameter("lastUserImageWidth", null);
677
- // await chatbot.addParameter("lastUserImageHeight", null);
678
- // await chatbot.addParameter("lastUserImageType", null);
679
- // }
594
+
680
595
  // get document
681
596
  if (message.type && message.type === "file" && message.metadata) {
682
- // "type": "file",
683
- // "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",
684
- // "metadata": {
685
- // "name": "LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf",
686
- // "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",
687
- // "type": "application/pdf",
688
- // "uid": "lo68oz8i"
689
- // }
690
- if (message.metadata.src) {
691
-
597
+ if (message.metadata.src) {
692
598
  await chatbot.addParameter("lastUserDocumentURL", message.metadata.src); // legacy. will be deprecated
693
599
  const url_as_attachment = message.metadata.src;
694
600
  await chatbot.addParameter("lastUserDocumentAsAttachmentURL", url_as_attachment);
@@ -701,44 +607,34 @@ class TiledeskChatbotUtil {
701
607
  await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
702
608
  }
703
609
  }
704
- // else {
705
- // await chatbot.addParameter("lastUserDocumentURL", null);
706
- // await chatbot.addParameter("lastUserDocumentName", null);
707
- // await chatbot.addParameter("lastUserDocumentType", null);
708
- // }
610
+
709
611
  if (message && message.request && message.request.lead) {
710
- if (chatbot.log) {console.log("lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)}
612
+ if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Lead found with email: " + message.request.lead.email + " and lead.fullname " + message.request.lead.fullname); }
711
613
  let currentLeadEmail = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY);
712
- if (chatbot.log) {console.log("You lead email from attributes:", currentLeadEmail, "message.request.lead.email:", message.request.lead.email);}
614
+ if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) You lead email from attributes: " + currentLeadEmail); }
713
615
  if (message.request.lead.email && !currentLeadEmail) {
714
616
  // worth saving
715
- if (chatbot.log) {console.log("worth saving email. lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)}
617
+ if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) worth saving email"); }
716
618
  try {
717
619
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY, message.request.lead.email);
718
620
  }
719
621
  catch(error) {
720
- console.error("Error on setting userEmail:", error);
622
+ winston.error("(TiledeskChatbotUtil) Error on setting userEmail:", error);
721
623
  }
722
624
  }
723
625
  let currentLeadName = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY);
724
- if (chatbot.log) {console.log("You lead name from attributes:", currentLeadName, "message.request.lead.fullname:", message.request.lead.fullname)}
725
- // if (message.request.lead.fullname && message.request.lead.fullname.startsWith("guest#") && !currentLeadName) {
726
- // if (message.request.lead.fullname) {
626
+ if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) You lead email from attributes: " + currentLeadEmail); }
727
627
  if (message.request.lead.fullname && !currentLeadName) {
728
628
  // worth saving
729
- if (chatbot.log) {console.log("worth saving fullname. lead found. lead.email:", message.request.lead.email, "lead.fullname:", message.request.lead.fullname)}
629
+ if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) worth saving email"); }
730
630
  try {
731
631
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, message.request.lead.fullname);
732
632
  }
733
633
  catch(error) {
734
- console.error("Error on setting userFullname:", error);
634
+ winston.error("(TiledeskChatbotUtil) Error on setting userFullname: ", error);
735
635
  }
736
- // }
737
- // else {
738
- // // console.log("!lead.fullname");
739
- // }
740
636
  }
741
- // console.log("Getting userPhone:", JSON.stringify(message.request));
637
+
742
638
  if (message.request.lead.phone) {
743
639
  await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_PHONE_KEY, message.request.lead.phone);
744
640
  }
@@ -766,15 +662,12 @@ class TiledeskChatbotUtil {
766
662
  if (message.request && message.request.location && message.request.location.city) {
767
663
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
768
664
  }
769
- // console.log("message.request.language", message.request["language"]);
770
665
  if (message.request) {
771
666
  let user_language = message.request["language"];
772
667
  if (message.request["language"]) {
773
- // console.log("HTTP language:", message.request["language"]);
774
668
  var languages = parser.parse(message.request["language"]);
775
- // console.log("languages:", languages);
776
669
  if (languages && languages.length > 0 && languages[0].code) {
777
- user_language = languages[0].code;
670
+ user_language = languages[0].code;
778
671
  }
779
672
  }
780
673
  await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY, message.request.sourcePage);
@@ -792,7 +685,6 @@ class TiledeskChatbotUtil {
792
685
  }
793
686
  }
794
687
  }
795
- // console.log("message.request.language", message.request["language"])
796
688
  if (message.request && message.request.department) {
797
689
  // It was an error when getting this from widget message's attributes
798
690
  // await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
@@ -810,29 +702,24 @@ class TiledeskChatbotUtil {
810
702
  if (!message.attributes) {
811
703
  message.attributes = {}
812
704
  }
813
- message.attributes.payload = { ...message.attributes.payload, ...message.request.attributes.payload}
814
- if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
815
- // if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
705
+ message.attributes.payload = { ...message.attributes.payload, ...message.request.attributes.payload }
706
+ if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Forced Set message.attributes.payload ", message.attributes.payload); }
816
707
  }
817
708
  if (message.attributes) {
818
- if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
819
- // if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > Ok message.attributes", JSON.stringify(message.attributes));}
709
+ if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Ok message.attributes ", message.attributes); }
710
+
820
711
  await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
821
712
  await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
822
713
  if (message.attributes.payload) {
823
714
  try {
824
715
  for (const [key, value] of Object.entries(message.attributes.payload)) {
825
- // const value = all_parameters[key];
826
716
  const value_type = typeof value;
827
- //if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
828
- //await chatbot.addParameter(key, String(value));
829
- // console.log("Adding from message.attributes:", key, "->", value);
830
717
  await chatbot.addParameter(key, value);
831
718
  }
832
719
  await chatbot.addParameter("payload", message.attributes.payload);
833
720
  }
834
721
  catch(err) {
835
- console.error("Error importing message payload in request variables:", err);
722
+ winston.error("(TiledeskChatbotUtil) Error importing message payload in request variables: ", err);
836
723
  }
837
724
  }
838
725
 
@@ -853,14 +740,12 @@ class TiledeskChatbotUtil {
853
740
 
854
741
 
855
742
  const _bot = chatbot.bot; // aka FaqKB
856
- if (chatbot.log) {
857
- console.log("Adding Globals to context..., chatbot.attributes?", JSON.stringify(_bot));
858
- }
743
+ if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Adding Globals to context: ", _bot); }
859
744
 
860
745
  if (_bot.attributes && _bot.attributes.globals) {
861
- if (chatbot.log) {console.log("Got Globals:", JSON.stringify(_bot.attributes.globals));}
746
+ if (chatbot.log) { winston.error("(TiledeskChatbotUtil) Got Globals: ", _bot.attributes.globals); }
862
747
  _bot.attributes.globals.forEach(async (global_var) => {
863
- if (chatbot.log) {console.log("Adding global:", global_var.key, "value:", global_var.value);}
748
+ if (chatbot.log) { winston.error("(TiledeskChatbotUtil) Adding global: " + global_var.key + " value: " + global_var.value); }
864
749
  await chatbot.addParameter(global_var.key, global_var.value);
865
750
  });
866
751
  }
@@ -874,52 +759,23 @@ class TiledeskChatbotUtil {
874
759
  // );
875
760
 
876
761
  if (chatbot.log) {
877
- // console.log("tdcache:", chatbot.tdcache);
878
- console.log("requestId:", requestId);
879
- console.log("KEY:", TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
880
- // console.log("TiledeskChatbot:", TiledeskChatbot);
762
+ if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) requestId: " + requestId); }
763
+ if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Key: " + TiledeskChatbotConst.REQ_PROJECT_ID_KEY); }
764
+
881
765
  let proj_ = await chatbot.getParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
882
- console.log("request parameter proj_:", proj_);
766
+ if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) request parameter proj_: " + proj_); }
767
+
883
768
  const all_parameters = await chatbot.allParameters();
884
769
  for (const [key, value] of Object.entries(all_parameters)) {
885
- // const value = all_parameters[key];
886
770
  const value_type = typeof value;
887
- if (chatbot.log) {console.log("REQUEST ATTRIBUTE:", key, "VALUE:", value, "TYPE:", value_type)}
771
+ if (chatbot.log) { winston.debug("(TiledeskChatbotUtil) Request Attribute: " + key + " value: " + value + " type: " + value_type); }
772
+
888
773
  }
889
774
  }
890
775
  } catch(error) {
891
- console.error("Error", error)
776
+ winston.error("(TiledeskChatbotUtil) updateRequestAttributes Error: ", error);
892
777
  process.exit(1)
893
778
  }
894
- // message["attributes"]: {
895
- // "departmentId": "63c980054f857c00350535bc",
896
- // "departmentName": "Default Department",
897
- // "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",
898
- // "sourcePage": "https://tiledesk-html-site.tiledesk.repl.co/custom-attributes.html",
899
- // "projectId": "63c980054f857c00350535b8",
900
- // "payload": {
901
- // "user_country": "Italy",
902
- // "user_code": "E001"
903
- // },
904
- // "userFullname": "guest#7216 ",
905
- // "requester_id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
906
- // "ipAddress": "79.8.190.172",
907
- // "sourceTitle": "Custom attributes",
908
- // "widgetVer": "v.5.0.53-rc.4",
909
- // "subtype": "info",
910
- // "decoded_jwt": {
911
- // "_id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
912
- // "firstname": "guest#7216",
913
- // "id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
914
- // "fullName": "guest#7216 ",
915
- // "iat": 1674201892,
916
- // "aud": "https://tiledesk.com",
917
- // "iss": "https://tiledesk.com",
918
- // "sub": "guest",
919
- // "jti": "f053af3d-14ca-411b-9903-78bd74e24218"
920
- // }
921
- // let userFullname = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY);
922
- // console.log("userFullname:", userFullname);
923
779
  }
924
780
 
925
781
  static actionsToDirectives(actions) {
@@ -936,21 +792,17 @@ class TiledeskChatbotUtil {
936
792
  }
937
793
 
938
794
  static addConnectAction(reply) {
939
- // console.log("reply foraddConnectAction:", reply);
940
795
  if (reply && reply.attributes && reply.attributes.nextBlockAction) {
941
796
  if (reply.actions) {
942
797
  reply.actions.push(reply.attributes.nextBlockAction);
943
- // console.log("actions are:", reply.actions)
944
798
  }
945
799
  }
946
800
  }
947
801
 
948
802
  static validateRequestId(requestId, projectId) {
949
- // console.log("checking requestId:", requestId, projectId)
950
803
  let isValid = false;
951
804
  if (requestId.startsWith("support-group-")) {
952
805
  const parts = requestId.split("-");
953
- // console.log("parts support request:", parts);
954
806
  if (parts.length >= 4) {
955
807
  isValid = (parts[0] === "support" && parts[1] === "group" && parts[2] === projectId && parts[3].length > 0);
956
808
  }
@@ -959,7 +811,6 @@ class TiledeskChatbotUtil {
959
811
  }
960
812
  } else if (requestId.startsWith("automation-request-")) {
961
813
  const parts = requestId.split("-");
962
- // console.log("parts automation request:", parts);
963
814
  if (parts.length === 4) {
964
815
  isValid = (parts[0] === "automation" && parts[1] === "request" && parts[2] === projectId && parts[3].length > 0);
965
816
  }
@@ -994,19 +845,18 @@ class TiledeskChatbotUtil {
994
845
  TiledeskChatbotConst.REQ_TRANSCRIPT_KEY,
995
846
  TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_KEY,
996
847
  TiledeskChatbotConst.REQ_DECODED_JWT_KEY,
997
- "lastUserImageURL", // image
998
- "lastUserImageName", // image
999
- "lastUserImageWidth", // image
1000
- "lastUserImageHeight", // image
1001
- "lastUserImageType", // image
1002
- "lastUserDocumentURL", // file
1003
- "lastUserDocumentName", // file
1004
- "lastUserDocumentType", // file
1005
- "ticketId",
848
+ TiledeskChatbotConst.REQ_LAST_USER_IMAGE_URL,
849
+ TiledeskChatbotConst.REQ_LAST_USER_IMAGE_NAME,
850
+ TiledeskChatbotConst.REQ_LAST_USER_IMAGE_WIDTH,
851
+ TiledeskChatbotConst.REQ_LAST_USER_IMAGE_HEIGHT,
852
+ TiledeskChatbotConst.REQ_LAST_USER_IMAGE_TYPE,
853
+ TiledeskChatbotConst.REQ_LAST_USER_DOCUMENT_URL,
854
+ TiledeskChatbotConst.REQ_LAST_USER_DOCUMENT_NAME,
855
+ TiledeskChatbotConst.REQ_LAST_USER_DOCUMENT_TYPE,
856
+ TiledeskChatbotConst.REQ_TICKET_ID_KEY,
1006
857
  TiledeskChatbotConst.REQ_CHAT_CHANNEL,
1007
- "user_lead_id",
1008
- "userLeadId",
1009
- "lastUserText",
858
+ TiledeskChatbotConst.REQ_USER_LEAD_ID_KEY,
859
+ TiledeskChatbotConst.REQ_LAST_USER_TEXT_v2_KEY,
1010
860
  TiledeskChatbotConst.REQ_REQUESTER_IS_AUTHENTICATED_KEY,
1011
861
  TiledeskChatbotConst.USER_INPUT,
1012
862
  TiledeskChatbotConst.REQ_CHATBOT_TOKEN,
@@ -1015,7 +865,6 @@ class TiledeskChatbotUtil {
1015
865
  let userParams = {};
1016
866
  if (flowAttributes) {
1017
867
  for (const [key, value] of Object.entries(flowAttributes)) {
1018
- // console.log(key, value);
1019
868
  // There is a bug that moves the requestId as a key in request attributes, so: && !key.startsWith("support-group-")
1020
869
  if (!key.startsWith("_") && !RESERVED.some(e => e === key) && !key.startsWith("support-group-")) {
1021
870
  userParams[key] = value;
@@ -1059,8 +908,8 @@ class TiledeskChatbotUtil {
1059
908
 
1060
909
  myrequest(options, callback, log) {
1061
910
  if (log) {
1062
- console.log("API URL:", options.url);
1063
- console.log("** Options:", JSON.stringify(options));
911
+ winston.debug("(TiledeskChatbotUtil) myrequest API URL: " + options.url);
912
+ winston.debug("(TiledeskChatbotUtil) myrequest Options URL: ", options);
1064
913
  }
1065
914
  axios(
1066
915
  {
@@ -1072,8 +921,8 @@ class TiledeskChatbotUtil {
1072
921
  })
1073
922
  .then((res) => {
1074
923
  if (log) {
1075
- console.log("Response for url:", options.url);
1076
- console.log("Response headers:\n", JSON.stringify(res.headers));
924
+ winston.debug("(TiledeskChatbotUtil) Response for url: " + options.url);
925
+ winston.debug("(TiledeskChatbotUtil) Response headers:\n", options);
1077
926
  }
1078
927
  if (res && res.status == 200 && res.data) {
1079
928
  if (callback) {
@@ -1087,7 +936,7 @@ class TiledeskChatbotUtil {
1087
936
  }
1088
937
  })
1089
938
  .catch((error) => {
1090
- console.error("(TiledeskChatbotUtil) Axios error: ", JSON.stringify(error));
939
+ winston.error("(TiledeskChatbotUtil) Axios error: ", error.response.data);
1091
940
  if (callback) {
1092
941
  callback(error, null, null);
1093
942
  }