@tiledesk/tiledesk-tybot-connector 0.2.601-rc1 → 0.3.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.
Files changed (71) hide show
  1. package/CHANGELOG.md +378 -1
  2. package/ExtApi.js +6 -6
  3. package/TdCache copy.js +242 -0
  4. package/TdCache.js +81 -176
  5. package/TdCache_v3.js +261 -0
  6. package/TiledeskExpression.js +7 -3
  7. package/index.js +290 -43
  8. package/logs/app.log +279 -0
  9. package/models/IntentsMachineFactory.js +5 -2
  10. package/models/MockBotsDataSource.js +19 -11
  11. package/models/TiledeskChatbot.js +97 -79
  12. package/models/TiledeskChatbotConst.js +12 -17
  13. package/models/TiledeskChatbotUtil.js +353 -109
  14. package/models/TiledeskIntentsMachine.js +1 -1
  15. package/models/faqKbService.js +1 -1
  16. package/package.json +7 -6
  17. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +172 -106
  18. package/tiledeskChatbotPlugs/Filler.js +13 -2
  19. package/tiledeskChatbotPlugs/TildeskContextForCodeOrchestrator.js +8 -0
  20. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +13 -7
  21. package/tiledeskChatbotPlugs/directives/DirAddTags.js +374 -0
  22. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +476 -0
  23. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +16 -19
  24. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +221 -34
  25. package/tiledeskChatbotPlugs/directives/DirAssign.js +0 -11
  26. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +11 -21
  27. package/tiledeskChatbotPlugs/directives/DirAssistant.js +728 -0
  28. package/tiledeskChatbotPlugs/directives/DirBrevo.js +353 -0
  29. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +3 -30
  30. package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +22 -0
  31. package/tiledeskChatbotPlugs/directives/DirClose.js +16 -3
  32. package/tiledeskChatbotPlugs/directives/DirCode.js +1 -1
  33. package/tiledeskChatbotPlugs/directives/DirCondition.js +0 -26
  34. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +183 -0
  35. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +121 -0
  36. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +5 -8
  37. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +11 -1
  38. package/tiledeskChatbotPlugs/directives/DirDepartment.js +15 -6
  39. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +17 -6
  40. package/tiledeskChatbotPlugs/directives/DirForm.js +12 -2
  41. package/tiledeskChatbotPlugs/directives/DirGptTask.js +83 -38
  42. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +4 -7
  43. package/tiledeskChatbotPlugs/directives/DirHubspot.js +5 -8
  44. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +14 -27
  45. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +278 -0
  46. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +147 -51
  47. package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +125 -0
  48. package/tiledeskChatbotPlugs/directives/DirIntent.js +8 -36
  49. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +5 -26
  50. package/tiledeskChatbotPlugs/directives/DirMessage.js +19 -17
  51. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +136 -0
  52. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +20 -87
  53. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +59 -0
  54. package/tiledeskChatbotPlugs/directives/DirQapla.js +6 -9
  55. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +17 -7
  56. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +17 -7
  57. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +11 -2
  58. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +135 -21
  59. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +163 -0
  60. package/tiledeskChatbotPlugs/directives/DirReply.js +42 -9
  61. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +347 -0
  62. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +13 -23
  63. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +247 -0
  64. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +202 -15
  65. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +13 -4
  66. package/tiledeskChatbotPlugs/directives/DirWait.js +21 -4
  67. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -2
  68. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +166 -103
  69. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +2 -60
  70. package/tiledeskChatbotPlugs/directives/Directives.js +16 -1
  71. /package/tiledeskChatbotPlugs/directives/{DirOfflineHours.js → DEPRECATED_DirOfflineHours.js} +0 -0
@@ -97,10 +97,35 @@ class DirSetAttributeV2 {
97
97
 
98
98
  async go(action, callback) {
99
99
  if (this.log) {console.log("(DirSetAttribute) action before filling:", JSON.stringify(action));}
100
+ // if (!action) {
101
+ // if (this.log) {console.log("(SetAttributeV2) Error 'action' is missing");}
102
+ // callback();
103
+ // return;
104
+ // }
105
+ if (action && !action.operation) {
106
+ if (this.log) {console.log("(SetAttributeV2) Error operation is mandatory");}
107
+ callback();
108
+ }
100
109
  if (action && action.operation && action.operation.operands) {
101
- if (this.log) {console.log("filling in setattribute...");}
110
+ if (this.log) {console.log("(SetAttributeV2) filling in setattribute...");}
102
111
  await this.fillValues(action.operation.operands);
103
112
  }
113
+ if (this.log) { console.log("action.operation.operands.length", action.operation.operands.length); }
114
+ if (this.log) { console.log("action.operation.operands[0].type", action.operation.operands[0].type); }
115
+
116
+ // FUN FACT: THIS TOOK A LOT OF EFFORT BUT IT WAS NEVER USED. YOU CAN SIMPLY CREATE A JSON ATTRIBUTE APPLYING
117
+ // JSONparse FUNCTION TO AN ATTRIBUTE.
118
+ // DEPRECATED because type = json is not available in the UI!
119
+ if (action.operation.operands && action.operation.operands.length === 1 && action.operation.operands[0].type === "json") {
120
+ if (this.log) {console.log("(SetAttributeV2) setting json value...");}
121
+ if (this.log) { console.log("(SetAttributeV2) setting json value... destination:", action.destination); }
122
+ const json_value = JSON.parse(action.operation.operands[0].value);
123
+ if (this.log) { console.log("(SetAttributeV2) json_value:", json_value); }
124
+ await this.saveAttribute(action.destination, json_value);
125
+ // await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.destination, json_value);
126
+ callback();
127
+ return; // on json types no operations are permitted beyond assignment
128
+ }
104
129
  if (this.log) {console.log("filled in setattribute:", action.operation);}
105
130
  // let res = validate(action, schema);
106
131
  // if (res.errors) {
@@ -112,12 +137,12 @@ class DirSetAttributeV2 {
112
137
  // callback();
113
138
  // return;
114
139
  // }
115
- if (action.operation.operators === undefined && action.operation.operands.length !== 1) {
140
+ if (action.operation?.operators === undefined && action.operation?.operands?.length !== 1) {
116
141
  if (this.log) {console.error("(DirSetAttribute) Invalid action: operators === undefined && operands.length !== 1")};
117
142
  callback();
118
143
  return;
119
144
  }
120
- if (action.operation.operators !== undefined && action.operation.operators.length !== action.operation.operands.length - 1) {
145
+ if (action.operation?.operators !== undefined && action.operation?.operators?.length !== action.operation?.operands?.length - 1) {
121
146
  if (this.log) {console.error("(DirSetAttribute) Invalid action: operators.length !== operands.length - 1")};
122
147
  callback();
123
148
  return;
@@ -127,21 +152,89 @@ class DirSetAttributeV2 {
127
152
  // await this.fillValues(action.operation.operands);
128
153
  // }
129
154
  // console.log("dirsetattribute, action.operation.operands:", action.operation.operands);
130
- const expression = TiledeskExpression.JSONOperationToExpression(action.operation.operators, action.operation.operands);
131
- const attributes = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
132
- // console.log("dirsetattribute, attributes:", attributes);
133
- attributes.TiledeskMath = TiledeskMath;
134
- attributes.TiledeskString = TiledeskString;
135
- const result = new TiledeskExpression().evaluateJavascriptExpression(expression, attributes);
136
- // console.log("filling in setattribute, result:", result);
137
- // THE GOAL OF ATTRIBUTE-FILLING THE "DESTINATION" FIELD IS TO SUPPORT DYNAMIC ATTRIBUTES
138
- // (ATTRS WHOSE NAME IS UNKNOWN AD DESIGN-TIME)
139
- // STILL UNSUPPORTED IN UI
140
- let destination = await this.fillDestination(action.destination);
141
- await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, destination, result);
155
+ try {
156
+ const expression = TiledeskExpression.JSONOperationToExpression(action.operation?.operators, action.operation?.operands);
157
+ const attributes = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
158
+ // console.log("dirsetattribute, attributes:", attributes);
159
+ if (attributes) {
160
+ attributes.TiledeskMath = TiledeskMath;
161
+ attributes.TiledeskString = TiledeskString;
162
+ const result = new TiledeskExpression().evaluateJavascriptExpression(expression, attributes);
163
+ // console.log("filling in setattribute, result:", result);
164
+ // THE GOAL OF ATTRIBUTE-FILLING THE "DESTINATION" FIELD IS TO SUPPORT DYNAMIC ATTRIBUTES
165
+ // (ATTRS WHOSE NAME IS UNKNOWN AD DESIGN-TIME)
166
+ // STILL UNSUPPORTED IN UI
167
+ let destination = await this.fillDestination(action.destination);
168
+ await this.saveAttribute(destination, result);
169
+ }
170
+ }
171
+ catch(err) {
172
+ console.error("SetAttributeV2 error:", err);
173
+ }
174
+
175
+ // await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, destination, result);
142
176
  callback();
143
177
  }
144
178
 
179
+ async saveAttribute(key, value, persist) {
180
+ if (this.log) {
181
+ console.log("SetAttributeV2 saving attribute:", key, value, persist);
182
+ }
183
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, key, value);
184
+ // if (persist) {
185
+ // console.log("SetAttributeV2 persisting...");
186
+ // await this.persistOnTiledesk(destination, result);
187
+ // }
188
+
189
+ // await make persistent autenticato con "Chatbot Token" (solo il chatbot può usare questo servizio)
190
+ // sarebbe il top che accodasse
191
+ /**
192
+ * ACL ?
193
+ * Solo agents appartenenti a quella conversazione possono accedere agli attibuti. E gli Admin
194
+ * collection flow_attributes
195
+ * {
196
+ * "projectId",
197
+ * "flowId",
198
+ * "requestId",
199
+ * attributes: [
200
+ * {"key": destination, "value": {}, 2, "Andrea"} => In pratica value è un "any"
201
+ * ]
202
+ * }
203
+ */
204
+ }
205
+
206
+ async persistOnTiledesk(key, value) {
207
+ if (!process.env.PERSIST_API_ENDPOINT) {
208
+ return;
209
+ }
210
+ const HTTPREQUEST = {
211
+ url: process.env.PERSIST_API_ENDPOINT,
212
+ headers: {
213
+ 'Content-Type': 'application/json',
214
+ 'Authorization': this.fixToken(this.context.token)
215
+ },
216
+ json: json,
217
+ method: 'POST'
218
+ }
219
+ if (this.log) { console.log("SetAttribute. HTTPREQUEST: ", HTTPREQUEST); }
220
+ this.#myrequest(
221
+ HTTPREQUEST, async (err, resbody) => {
222
+ if (err) {
223
+ if (this.log) {
224
+ console.error("SetAttribute. persistOnTiledesk() error:", err);
225
+ }
226
+ // callback();
227
+ // return;
228
+ } else {
229
+ if (this.log) { console.log("SetAttribute. Attributes saved.", JSON.stringify(resbody)); }
230
+ // callback();
231
+ // return;
232
+ }
233
+ }
234
+ );
235
+ return;
236
+ }
237
+
145
238
  async fillDestination(destination) {
146
239
  if (this.tdcache) {
147
240
  // console.log("tdcache in setattribute...", this.tdcache);
@@ -201,6 +294,100 @@ class DirSetAttributeV2 {
201
294
  console.error("Error while filling operands:", error);
202
295
  }
203
296
  }
297
+
298
+ convertOperandValues(operands) {
299
+ console.log("Converting operands:", operands);
300
+ // operation: {
301
+ // operators: ["addAsNumber", "subtractAsNumber", "divideAsNumber", "multiplyAsNumber"],
302
+ // operands: [
303
+ // {
304
+ // value: "previous",
305
+ // isVariable: true,
306
+ // type: "string"
307
+ // }
308
+ // ]
309
+ try {
310
+ operands.forEach(operand => {
311
+ if (operand.type) {
312
+ console.log("Converting operands - operand.type:", operand.type.toLowerCase());
313
+ if (operand.type.toLowerCase() === "number") {
314
+ console.log("Converting operands - number");
315
+ operand.value = Number(operand.value);
316
+ console.log("new value:", operand.value);
317
+ console.log("new value type:", typeof operand.value);
318
+ }
319
+ else if (operand.type.toLowerCase() === "json") {
320
+ console.log("Converting operands - json, value =", operand.value);
321
+ operand.value = JSON.parse(operand.value);
322
+ console.log("new value:", operand.value);
323
+ console.log("new value type:", typeof operand.value);
324
+ }
325
+ else {
326
+ console.log("Converting operands - ??");
327
+ }
328
+ }
329
+ });
330
+ }
331
+ catch(error) {
332
+ console.error("Error while converting operands:", error);
333
+ }
334
+ }
335
+
336
+ #myrequest(options, callback) {
337
+ if (this.log) {
338
+ console.log("API URL:", options.url);
339
+ console.log("** Options:", JSON.stringify(options));
340
+ }
341
+ let axios_options = {
342
+ url: options.url,
343
+ method: options.method,
344
+ params: options.params,
345
+ headers: options.headers
346
+ }
347
+ if (options.json !== null) {
348
+ axios_options.data = options.json
349
+ }
350
+ if (this.log) {
351
+ console.log("axios_options:", JSON.stringify(axios_options));
352
+ }
353
+ if (options.url.startsWith("https:")) {
354
+ const httpsAgent = new https.Agent({
355
+ rejectUnauthorized: false,
356
+ });
357
+ axios_options.httpsAgent = httpsAgent;
358
+ }
359
+ axios(axios_options)
360
+ .then((res) => {
361
+ if (this.log) {
362
+ console.log("Response for url:", options.url);
363
+ console.log("Response headers:\n", JSON.stringify(res.headers));
364
+ }
365
+ if (res && res.status == 200 && res.data) {
366
+ if (callback) {
367
+ callback(null, res.data);
368
+ }
369
+ }
370
+ else {
371
+ if (callback) {
372
+ callback(new Error("Response status is not 200"), null);
373
+ }
374
+ }
375
+ })
376
+ .catch((error) => {
377
+ if (callback) {
378
+ callback(error, null);
379
+ }
380
+ });
381
+ }
382
+
383
+ fixToken(token) {
384
+ if (token.startsWith('JWT ')) {
385
+ return token
386
+ }
387
+ else {
388
+ return 'JWT ' + token
389
+ }
390
+ }
204
391
  }
205
392
 
206
393
  module.exports = { DirSetAttributeV2 };
@@ -1,3 +1,4 @@
1
+ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
1
2
  const { Filler } = require('../Filler');
2
3
 
3
4
  class DirSetConversationTags {
@@ -8,8 +9,16 @@ class DirSetConversationTags {
8
9
  }
9
10
  this.context = context;
10
11
  this.log = context.log;
11
- this.tdclient = context.tdclient;
12
12
  this.requestId = context.requestId;
13
+
14
+ this.API_ENDPOINT = context.API_ENDPOINT;
15
+ this.tdClient = new TiledeskClient({
16
+ projectId: this.context.projectId,
17
+ token: this.context.token,
18
+ APIURL: this.API_ENDPOINT,
19
+ APIKEY: "___",
20
+ log: this.log
21
+ });
13
22
  }
14
23
 
15
24
  execute(directive, callback) {
@@ -72,7 +81,7 @@ class DirSetConversationTags {
72
81
  subtype: "info"
73
82
  }
74
83
  }
75
- this.tdclient.sendSupportMessage(
84
+ this.tdClient.sendSupportMessage(
76
85
  this.requestId,
77
86
  message, (err) => {
78
87
  if (err) {
@@ -91,7 +100,7 @@ class DirSetConversationTags {
91
100
  }
92
101
 
93
102
  moveToDepartment(requestId, depName, callback) {
94
- this.tdclient.getAllDepartments((err, deps) => {
103
+ this.tdClient.getAllDepartments((err, deps) => {
95
104
  if (this.log) {console.log("deps:", JSON.stringify(deps));}
96
105
  if (err) {
97
106
  console.error("getAllDepartments() error:", err);
@@ -108,7 +117,7 @@ class DirSetConversationTags {
108
117
  }
109
118
  }
110
119
  if (dep) {
111
- this.tdclient.updateRequestDepartment(requestId, dep._id, null, (err, res) => {
120
+ this.tdClient.updateRequestDepartment(requestId, dep._id, null, (err, res) => {
112
121
  if (err) {
113
122
  console.error("DirDepartment error:", err);
114
123
  callback();
@@ -1,7 +1,17 @@
1
1
 
2
+ const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
+
2
4
  class DirWait {
3
5
 
4
- constructor() {
6
+ constructor(context) {
7
+ if (!context) {
8
+ throw new Error('context object is mandatory.');
9
+ }
10
+ this.context = context;
11
+ this.chatbot = context.chatbot;
12
+ this.tdcache = context.tdcache;
13
+ this.requestId = context.requestId;
14
+ this.log = context.log;
5
15
  }
6
16
 
7
17
  execute(directive, callback) {
@@ -38,10 +48,17 @@ class DirWait {
38
48
  })
39
49
  }
40
50
 
41
- go(action, callback) {
42
- // console.log(">>>>__", callback)
51
+ async go(action, callback) {
52
+ // reset step?
53
+ // const step_key = TiledeskChatbot.requestCacheKey(this.requestId) + ":step";
54
+ // console.log("step_key:", step_key);
55
+ if (action && action.millis >= 1000) {//2000 * 60) { // at list 2 minutes waiting time to reset the steps counter
56
+ // await this.tdcache.set(step_key, 0);
57
+ // console.log("resetting steps counter");
58
+ await TiledeskChatbot.resetStep(this.tdcache, this.requestId);
59
+ // console.log("step_key after:", await this.tdcache.get( step_key ));
60
+ }
43
61
  setTimeout(() => {
44
- // console.log("QUINO....__")
45
62
  callback();
46
63
  }, action.millis);
47
64
  }
@@ -10,7 +10,6 @@ class DirWebRequest {
10
10
  throw new Error('context object is mandatory.');
11
11
  }
12
12
  this.context = context;
13
- this.tdclient = context.tdclient;
14
13
  this.tdcache = context.tdcache;
15
14
  this.requestId = context.requestId;
16
15
  this.log = context.log;
@@ -190,7 +189,7 @@ class DirWebRequest {
190
189
  }
191
190
  })
192
191
  .catch( (error) => {
193
- console.error("An error occurred:", JSON.stringify(error));
192
+ console.error("(DirWebRequest) Axios error: ", JSON.stringify(error));
194
193
  if (callback) {
195
194
  callback(error, null);
196
195
  }