@tiledesk/tiledesk-tybot-connector 0.2.601-rc1 → 0.3.1

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 (73) hide show
  1. package/CHANGELOG.md +387 -1
  2. package/ExtApi.js +6 -7
  3. package/Logger.js +74 -0
  4. package/TdCache.js +81 -176
  5. package/TdCache_v3.js +261 -0
  6. package/TiledeskExpression.js +7 -3
  7. package/TiledeskServices/AIService.js +43 -0
  8. package/TiledeskServices/utils.js +99 -0
  9. package/index.js +305 -50
  10. package/logs/app.log +279 -0
  11. package/models/IntentsMachineFactory.js +5 -2
  12. package/models/MockBotsDataSource.js +19 -11
  13. package/models/TiledeskChatbot.js +97 -79
  14. package/models/TiledeskChatbotConst.js +12 -17
  15. package/models/TiledeskChatbotUtil.js +359 -109
  16. package/models/TiledeskIntentsMachine.js +1 -1
  17. package/models/faqKbService.js +1 -1
  18. package/package.json +8 -6
  19. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +172 -106
  20. package/tiledeskChatbotPlugs/Filler.js +13 -2
  21. package/tiledeskChatbotPlugs/TildeskContextForCodeOrchestrator.js +8 -0
  22. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +13 -7
  23. package/tiledeskChatbotPlugs/directives/DirAddTags.js +374 -0
  24. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +476 -0
  25. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +16 -19
  26. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +221 -34
  27. package/tiledeskChatbotPlugs/directives/DirAssign.js +0 -11
  28. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +11 -21
  29. package/tiledeskChatbotPlugs/directives/DirAssistant.js +728 -0
  30. package/tiledeskChatbotPlugs/directives/DirBrevo.js +353 -0
  31. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +3 -30
  32. package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +22 -0
  33. package/tiledeskChatbotPlugs/directives/DirClose.js +16 -3
  34. package/tiledeskChatbotPlugs/directives/DirCode.js +1 -1
  35. package/tiledeskChatbotPlugs/directives/DirCondition.js +0 -26
  36. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +182 -0
  37. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +121 -0
  38. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +5 -8
  39. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +11 -1
  40. package/tiledeskChatbotPlugs/directives/DirDepartment.js +15 -6
  41. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +17 -6
  42. package/tiledeskChatbotPlugs/directives/DirForm.js +12 -2
  43. package/tiledeskChatbotPlugs/directives/DirGptTask.js +83 -38
  44. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +4 -7
  45. package/tiledeskChatbotPlugs/directives/DirHubspot.js +5 -8
  46. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +14 -27
  47. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +278 -0
  48. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +147 -51
  49. package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +125 -0
  50. package/tiledeskChatbotPlugs/directives/DirIntent.js +8 -37
  51. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +5 -26
  52. package/tiledeskChatbotPlugs/directives/DirMessage.js +19 -17
  53. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +135 -0
  54. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +20 -87
  55. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +59 -0
  56. package/tiledeskChatbotPlugs/directives/DirQapla.js +6 -9
  57. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +17 -7
  58. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +17 -7
  59. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +11 -2
  60. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +135 -21
  61. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +163 -0
  62. package/tiledeskChatbotPlugs/directives/DirReply.js +53 -9
  63. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +347 -0
  64. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +13 -23
  65. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +247 -0
  66. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +202 -15
  67. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +13 -4
  68. package/tiledeskChatbotPlugs/directives/DirWait.js +21 -4
  69. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -2
  70. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +166 -103
  71. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +2 -60
  72. package/tiledeskChatbotPlugs/directives/Directives.js +16 -1
  73. /package/tiledeskChatbotPlugs/directives/{DirOfflineHours.js → DEPRECATED_DirOfflineHours.js} +0 -0
@@ -15,6 +15,7 @@ class DirQapla {
15
15
  this.tdcache = this.context.tdcache;
16
16
  this.requestId = this.context.requestId;
17
17
  this.intentDir = new DirIntent(context);
18
+ this.API_ENDPOINT = this.context.API_ENDPOINT;
18
19
  this.log = context.log;
19
20
  }
20
21
 
@@ -82,18 +83,14 @@ class DirQapla {
82
83
  return;
83
84
  }
84
85
 
85
- const server_base_url = process.env.API_ENDPOINT || process.env.API_URL;
86
86
  const qapla_base_url = process.env.QAPLA_ENDPOINT || "https://api.qapla.it/1.2"
87
- if (this.log) {
88
- console.log("DirQapla server_base_url: ", qapla_base_url);
89
- console.log("DirQapla qapla_base_url: ", qapla_base_url);
90
- }
87
+ if (this.log) { console.log("DirQapla qapla_base_url: ", qapla_base_url); }
91
88
 
92
89
  let key = action.apiKey;
93
90
 
94
91
  if (!key) {
95
92
  if (this.log) { console.log("DirQapla - Key not found into action. Searching in integrations..."); }
96
- key = await this.getKeyFromIntegrations(server_base_url);
93
+ key = await this.getKeyFromIntegrations();
97
94
  }
98
95
 
99
96
  if (!key) {
@@ -281,18 +278,18 @@ class DirQapla {
281
278
  }
282
279
  })
283
280
  .catch((error) => {
284
- // console.error("An error occurred:", JSON.stringify(error.data));
281
+ console.error("(DirQapla) Axios error: ", JSON.stringify(error));
285
282
  if (callback) {
286
283
  callback(error, null);
287
284
  }
288
285
  });
289
286
  }
290
287
 
291
- async getKeyFromIntegrations(server_base_url) {
288
+ async getKeyFromIntegrations() {
292
289
  return new Promise((resolve) => {
293
290
 
294
291
  const INTEGRATIONS_HTTPREQUEST = {
295
- url: server_base_url + "/" + this.context.projectId + "/integration/name/qapla",
292
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/qapla",
296
293
  headers: {
297
294
  'Content-Type': 'application/json',
298
295
  'Authorization': 'JWT ' + this.context.token
@@ -1,6 +1,7 @@
1
1
  const { Filler } = require('../Filler');
2
2
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
3
  const { TiledeskChatbotUtil } = require('../../models/TiledeskChatbotUtil');
4
+ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
4
5
 
5
6
  class DirRandomReply {
6
7
 
@@ -14,6 +15,15 @@ class DirRandomReply {
14
15
  this.token = context.token;
15
16
  this.tdcache = context.tdcache;
16
17
  this.log = context.log;
18
+
19
+ this.API_ENDPOINT = context.API_ENDPOINT;
20
+ this.tdClient = new TiledeskClient({
21
+ projectId: this.context.projectId,
22
+ token: this.context.token,
23
+ APIURL: this.API_ENDPOINT,
24
+ APIKEY: "___",
25
+ log: this.log
26
+ });
17
27
  }
18
28
 
19
29
  execute(directive, callback) {
@@ -78,16 +88,16 @@ class DirRandomReply {
78
88
  message.attributes = {}
79
89
  }
80
90
  // Reserved names: userEmail, userFullname
81
- if (requestVariables['userEmail']) {
82
- message.attributes.updateUserEmail = requestVariables['userEmail'];
83
- }
84
- if (requestVariables['userFullname']) {
85
- message.attributes.updateUserFullname = requestVariables['userFullname'];
86
- }
91
+ // if (requestVariables['userEmail']) {
92
+ // message.attributes.updateUserEmail = requestVariables['userEmail'];
93
+ // }
94
+ // if (requestVariables['userFullname']) {
95
+ // message.attributes.updateUserFullname = requestVariables['userFullname'];
96
+ // }
87
97
  }
88
98
  // send!
89
99
  if (this.log) {console.log("Reply:", JSON.stringify(message))};
90
- this.context.tdclient.sendSupportMessage(
100
+ this.tdClient.sendSupportMessage(
91
101
  this.requestId,
92
102
  message,
93
103
  (err) => {
@@ -1,16 +1,26 @@
1
+ const { TiledeskClient } = require("@tiledesk/tiledesk-client");
1
2
 
2
3
  class DirRemoveCurrentBot {
3
4
 
4
- constructor(config) {
5
- if (!config.tdclient) {
6
- throw new Error('tdclient (TiledeskClient) object is mandatory.');
5
+ constructor(context) {
6
+ if (!context) {
7
+ throw new Error('context object is mandatory.');
7
8
  }
8
- this.tdclient = config.tdclient;
9
- this.requestId = config.requestId;
9
+
10
+ this.context = context;
11
+ this.requestId = context.requestId;
12
+ this.API_ENDPOINT = context.API_ENDPOINT;
13
+
14
+ this.tdClient = new TiledeskClient({
15
+ projectId: this.context.projectId,
16
+ token: this.context.token,
17
+ APIURL: this.API_ENDPOINT,
18
+ APIKEY: "___",
19
+ log: this.log
20
+ });
10
21
  }
11
22
 
12
23
  execute(directive, callback) {
13
- console.log("Remove current bot");
14
24
  let action;
15
25
  if (directive.action) {
16
26
  action = directive.action;
@@ -27,7 +37,7 @@ class DirRemoveCurrentBot {
27
37
  }
28
38
 
29
39
  go(action, callback) {
30
- tdclient.removeCurrentBot(this.requestId, (err) => {
40
+ this.tdClient.removeCurrentBot(this.requestId, (err) => {
31
41
  callback();
32
42
  });
33
43
  }
@@ -1,3 +1,4 @@
1
+ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
1
2
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
2
3
  const { Filler } = require('../Filler');
3
4
 
@@ -8,9 +9,17 @@ class DirReplaceBot {
8
9
  throw new Error('context object is mandatory.');
9
10
  }
10
11
  this.context = context;
11
- this.tdclient = context.tdclient;
12
12
  this.requestId = context.requestId;
13
13
  this.log = context.log;
14
+
15
+ this.API_ENDPOINT = context.API_ENDPOINT;
16
+ this.tdClient = new TiledeskClient({
17
+ projectId: this.context.projectId,
18
+ token: this.context.token,
19
+ APIURL: this.API_ENDPOINT,
20
+ APIKEY: "___",
21
+ log: this.log
22
+ });
14
23
  }
15
24
 
16
25
  execute(directive, callback) {
@@ -42,7 +51,7 @@ class DirReplaceBot {
42
51
  );
43
52
  const filler = new Filler();
44
53
  botName = filler.fill(botName, variables);
45
- this.tdclient.replaceBotByName(this.requestId, botName, () => {
54
+ this.tdClient.replaceBotByName(this.requestId, botName, () => {
46
55
  callback();
47
56
  });
48
57
  }
@@ -1,6 +1,10 @@
1
+ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
1
2
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
2
3
  const { Filler } = require('../Filler');
3
4
 
5
+ const axios = require("axios").default;
6
+ let https = require("https");
7
+
4
8
  class DirReplaceBotV2 {
5
9
 
6
10
  constructor(context) {
@@ -8,9 +12,17 @@ class DirReplaceBotV2 {
8
12
  throw new Error('context object is mandatory.');
9
13
  }
10
14
  this.context = context;
11
- this.tdclient = context.tdclient;
12
15
  this.requestId = context.requestId;
13
16
  this.log = context.log;
17
+
18
+ this.API_ENDPOINT = context.API_ENDPOINT;
19
+ this.tdClient = new TiledeskClient({
20
+ projectId: this.context.projectId,
21
+ token: this.context.token,
22
+ APIURL: this.API_ENDPOINT,
23
+ APIKEY: "___",
24
+ log: this.log
25
+ });
14
26
  }
15
27
 
16
28
  execute(directive, callback) {
@@ -43,30 +55,132 @@ class DirReplaceBotV2 {
43
55
  );
44
56
  const filler = new Filler();
45
57
  botName = filler.fill(botName, variables);
46
- this.tdclient.replaceBotByName(this.requestId, botName, () => {
47
- if (blockName) {
48
- if (this.log) {console.log("Sending hidden /start message to bot in dept");}
49
- const message = {
50
- type: "text",
51
- text: "/" + blockName,
52
- attributes : {
53
- subtype: "info"
58
+
59
+ let data = {};
60
+ if (action.nameAsSlug && action.nameAsSlug === true) {
61
+ data.slug = botName;
62
+ } else {
63
+ data.name = botName;
64
+ }
65
+
66
+ const HTTPREQUEST = {
67
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/requests/" + this.requestId + "/replace",
68
+ headers: {
69
+ 'Content-Type': 'application/json',
70
+ 'Authorization': 'JWT ' + this.context.token
71
+ },
72
+ json: data,
73
+ method: 'PUT'
74
+ }
75
+
76
+ this.#myrequest(
77
+ HTTPREQUEST, async (err, resbody) => {
78
+ if (err) {
79
+ console.log("DirReplaceBot error: ", err);
80
+ if (callback) {
81
+ callback();
82
+ return;
54
83
  }
55
84
  }
56
- this.tdclient.sendSupportMessage(
57
- this.requestId,
58
- message, (err) => {
59
- if (err) {
60
- console.error("Error sending hidden message:", err.message);
85
+
86
+ if (this.log) { console.log("DirReplaceBot replace resbody: ", resbody) };
87
+ if (blockName) {
88
+ if (this.log) { console.log("Sending hidden /start message to bot in dept"); }
89
+ const message = {
90
+ type: "text",
91
+ text: "/" + blockName,
92
+ attributes: {
93
+ subtype: "info"
61
94
  }
62
- if (this.log) {console.log("Hidden message sent.");}
63
- callback();
64
- });
65
- }
66
- else {
67
- callback();
95
+ }
96
+ this.tdClient.sendSupportMessage(
97
+ this.requestId,
98
+ message, (err) => {
99
+ if (err) {
100
+ console.error("Error sending hidden message:", err.message);
101
+ }
102
+ if (this.log) { console.log("Hidden message sent."); }
103
+ callback();
104
+ });
105
+ }
106
+ else {
107
+ callback();
108
+ }
68
109
  }
69
- });
110
+ )
111
+
112
+ // this.tdClient.replaceBotByName(this.requestId, botName, () => {
113
+ // if (blockName) {
114
+ // if (this.log) {console.log("Sending hidden /start message to bot in dept");}
115
+ // const message = {
116
+ // type: "text",
117
+ // text: "/" + blockName,
118
+ // attributes : {
119
+ // subtype: "info"
120
+ // }
121
+ // }
122
+ // this.tdClient.sendSupportMessage(
123
+ // this.requestId,
124
+ // message, (err) => {
125
+ // if (err) {
126
+ // console.error("Error sending hidden message:", err.message);
127
+ // }
128
+ // if (this.log) {console.log("Hidden message sent.");}
129
+ // callback();
130
+ // });
131
+ // }
132
+ // else {
133
+ // callback();
134
+ // }
135
+ // });
136
+ }
137
+
138
+ #myrequest(options, callback) {
139
+ if (this.log) {
140
+ console.log("API URL:", options.url);
141
+ console.log("** Options:", JSON.stringify(options));
142
+ }
143
+ let axios_options = {
144
+ url: options.url,
145
+ method: options.method,
146
+ params: options.params,
147
+ headers: options.headers
148
+ }
149
+ if (options.json !== null) {
150
+ axios_options.data = options.json
151
+ }
152
+ if (this.log) {
153
+ console.log("axios_options:", JSON.stringify(axios_options));
154
+ }
155
+ if (options.url.startsWith("https:")) {
156
+ const httpsAgent = new https.Agent({
157
+ rejectUnauthorized: false,
158
+ });
159
+ axios_options.httpsAgent = httpsAgent;
160
+ }
161
+ axios(axios_options)
162
+ .then((res) => {
163
+ if (this.log) {
164
+ console.log("Response for url:", options.url);
165
+ console.log("Response headers:\n", JSON.stringify(res.headers));
166
+ }
167
+ if (res && res.status == 200 && res.data) {
168
+ if (callback) {
169
+ callback(null, res.data);
170
+ }
171
+ }
172
+ else {
173
+ if (callback) {
174
+ callback(new Error("Response status is not 200"), null);
175
+ }
176
+ }
177
+ })
178
+ .catch((error) => {
179
+ console.error("(DirAskGPT) Axios error: ", JSON.stringify(error));
180
+ if (callback) {
181
+ callback(error, null);
182
+ }
183
+ });
70
184
  }
71
185
  }
72
186
 
@@ -0,0 +1,163 @@
1
+ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
+ const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
+ const { Filler } = require('../Filler');
4
+
5
+ const axios = require("axios").default;
6
+ let https = require("https");
7
+
8
+ class DirReplaceBotV3 {
9
+
10
+ constructor(context) {
11
+ if (!context) {
12
+ throw new Error('context object is mandatory.');
13
+ }
14
+ this.context = context;
15
+ this.requestId = context.requestId;
16
+ this.log = context.log;
17
+
18
+ this.API_ENDPOINT = context.API_ENDPOINT;
19
+ this.tdClient = new TiledeskClient({
20
+ projectId: this.context.projectId,
21
+ token: this.context.token,
22
+ APIURL: this.API_ENDPOINT,
23
+ APIKEY: "___",
24
+ log: this.log
25
+ });
26
+ }
27
+
28
+ execute(directive, callback) {
29
+ if (this.log) {console.log("Replacing bot");}
30
+ let action;
31
+ if (directive.action) {
32
+ action = directive.action;
33
+ }
34
+ else {
35
+ console.error("DirReplaceBot Incorrect directive: ", JSON.stringify(directive));
36
+ callback();
37
+ return;
38
+ }
39
+ this.go(action, () => {
40
+ callback();
41
+ })
42
+ }
43
+
44
+ async go(action, callback) {
45
+ let botId = action.botId;
46
+ let botSlug = action.botSlug;
47
+ let useSlug = action.useSlug;
48
+ let blockName = action.blockName;
49
+ let variables = null;
50
+ variables =
51
+ await TiledeskChatbot.allParametersStatic(
52
+ this.context.tdcache, this.context.requestId
53
+ );
54
+ const filler = new Filler();
55
+ //botId = filler.fill(botId, variables);
56
+ botSlug = filler.fill(botSlug, variables);
57
+ blockName = filler.fill(blockName, variables);
58
+
59
+ let data = {};
60
+ if (useSlug && useSlug === true) {
61
+ data.slug = botSlug;
62
+ } else {
63
+ data.id = botId;
64
+ }
65
+
66
+ const HTTPREQUEST = {
67
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/requests/" + this.requestId + "/replace",
68
+ headers: {
69
+ 'Content-Type': 'application/json',
70
+ 'Authorization': 'JWT ' + this.context.token
71
+ },
72
+ json: data,
73
+ method: 'PUT'
74
+ }
75
+
76
+ this.#myrequest(
77
+ HTTPREQUEST, async (err, resbody) => {
78
+ if (err) {
79
+ console.log("DirReplaceBot error: ", err);
80
+ if (callback) {
81
+ callback();
82
+ return;
83
+ }
84
+ }
85
+
86
+ if (this.log) { console.log("DirReplaceBot replace resbody: ", resbody) };
87
+ if (blockName) {
88
+ if (this.log) { console.log("Sending hidden /start message to bot in dept"); }
89
+ const message = {
90
+ type: "text",
91
+ text: "/" + blockName,
92
+ attributes: {
93
+ subtype: "info"
94
+ }
95
+ }
96
+ this.tdClient.sendSupportMessage(
97
+ this.requestId,
98
+ message, (err) => {
99
+ if (err) {
100
+ console.error("Error sending hidden message:", err.message);
101
+ }
102
+ if (this.log) { console.log("Hidden message sent."); }
103
+ callback();
104
+ });
105
+ }
106
+ else {
107
+ callback();
108
+ }
109
+ }
110
+ )
111
+ }
112
+
113
+ #myrequest(options, callback) {
114
+ if (this.log) {
115
+ console.log("API URL:", options.url);
116
+ console.log("** Options:", JSON.stringify(options));
117
+ }
118
+ let axios_options = {
119
+ url: options.url,
120
+ method: options.method,
121
+ params: options.params,
122
+ headers: options.headers
123
+ }
124
+ if (options.json !== null) {
125
+ axios_options.data = options.json
126
+ }
127
+ if (this.log) {
128
+ console.log("axios_options:", JSON.stringify(axios_options));
129
+ }
130
+ if (options.url.startsWith("https:")) {
131
+ const httpsAgent = new https.Agent({
132
+ rejectUnauthorized: false,
133
+ });
134
+ axios_options.httpsAgent = httpsAgent;
135
+ }
136
+ axios(axios_options)
137
+ .then((res) => {
138
+ if (this.log) {
139
+ console.log("Response for url:", options.url);
140
+ console.log("Response headers:\n", JSON.stringify(res.headers));
141
+ }
142
+ if (res && res.status == 200 && res.data) {
143
+ if (callback) {
144
+ callback(null, res.data);
145
+ }
146
+ }
147
+ else {
148
+ if (callback) {
149
+ callback(new Error("Response status is not 200"), null);
150
+ }
151
+ }
152
+ })
153
+ .catch((error) => {
154
+ console.error("(DirAskGPT) Axios error: ", JSON.stringify(error));
155
+ if (callback) {
156
+ callback(error, null);
157
+ }
158
+ });
159
+ }
160
+
161
+ }
162
+
163
+ module.exports = { DirReplaceBotV3 };
@@ -2,6 +2,8 @@ const { Filler } = require('../Filler');
2
2
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
3
  const { TiledeskChatbotUtil } = require('../../models/TiledeskChatbotUtil');
4
4
  let axios = require('axios');
5
+ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
6
+ const { Logger } = require('../../Logger');
5
7
 
6
8
  class DirReply {
7
9
 
@@ -15,6 +17,16 @@ class DirReply {
15
17
  this.token = context.token;
16
18
  this.tdcache = context.tdcache;
17
19
  this.log = context.log;
20
+ this.logger = new Logger({ request_id: this.requestId, dev: this.context.supportRequest.draft });
21
+
22
+ this.API_ENDPOINT = context.API_ENDPOINT;
23
+ this.tdClient = new TiledeskClient({
24
+ projectId: this.context.projectId,
25
+ token: this.context.token,
26
+ APIURL: this.API_ENDPOINT,
27
+ APIKEY: "___",
28
+ log: this.log
29
+ });
18
30
  }
19
31
 
20
32
  execute(directive, callback) {
@@ -31,7 +43,10 @@ class DirReply {
31
43
  callback();
32
44
  return;
33
45
  }
46
+ this.logger.info("1 Execute action reply for " + directive.action.text)
47
+
34
48
  this.go(action, () => {
49
+ this.logger.info("6 End of action reply " + directive.action.text + " -> callback")
35
50
  callback();
36
51
  });
37
52
  }
@@ -54,6 +69,8 @@ class DirReply {
54
69
  const filler = new Filler();
55
70
  // fill text attribute
56
71
  message.text = filler.fill(message.text, requestAttributes);
72
+ this.logger.info("2 Sending reply " + message.text);
73
+
57
74
  if (message.metadata) {
58
75
  if (this.log) {console.log("filling message 'metadata':", JSON.stringify(message.metadata));}
59
76
  if (message.metadata.src) {
@@ -77,6 +94,12 @@ class DirReply {
77
94
  TiledeskChatbotUtil.fillCommandAttachments(command, requestAttributes, this.log);
78
95
  if (this.log) {console.log("command filled:", command.message.text);}
79
96
  }
97
+ if (command.type === 'settings' && command.settings) {
98
+ Object.keys(command.settings).forEach(k => {
99
+ command.settings[k] = filler.fill(command.settings[k], requestAttributes)
100
+ if (this.log) {console.log("settings command filled:", command.settings[k]);}
101
+ })
102
+ }
80
103
  }
81
104
  }
82
105
  }
@@ -94,37 +117,58 @@ class DirReply {
94
117
  message.attributes = {}
95
118
  }
96
119
  // Reserved names: userEmail, userFullname
97
- if (requestAttributes['userEmail']) {
98
- message.attributes.updateUserEmail = requestAttributes['userEmail'];
99
- }
100
- if (requestAttributes['userFullname']) {
101
- message.attributes.updateUserFullname = requestAttributes['userFullname'];
102
- }
120
+ // if (requestAttributes['userEmail']) {
121
+ // message.attributes.updateUserEmail = requestAttributes['userEmail'];
122
+ // }
123
+ // if (requestAttributes['userFullname']) {
124
+ // message.attributes.updateUserFullname = requestAttributes['userFullname'];
125
+ // }
103
126
  // intent_info
104
127
  if (this.context.reply && this.context.reply.attributes && this.context.reply.attributes.intent_info) {
105
128
  message.attributes.intentName = this.context.reply.attributes.intent_info.intent_name;
106
129
  }
130
+ // userFlowAttributes
131
+ let userFlowAttributes = TiledeskChatbotUtil.userFlowAttributes(requestAttributes);
132
+ if (this.log) { console.log("userFlowAttributes:", userFlowAttributes); }
133
+ if (userFlowAttributes) {
134
+ message.attributes["flowAttributes"] = {};
135
+ for (const [key, value] of Object.entries(userFlowAttributes)) {
136
+ try {
137
+ if(typeof value === 'string' && value.length <= 1000){
138
+ message.attributes["flowAttributes"][key] = value;
139
+ }
140
+ }
141
+ catch(err) {
142
+ console.error("An error occurred while JSON.parse(). Parsed value:" + value + " in allParametersStatic(). Error:", err);
143
+ }
144
+ }
145
+ }
107
146
  }
108
147
  // send!
109
148
  let cleanMessage = message;
149
+ this.logger.info("3 Sending reply (text) " + cleanMessage.text);
150
+ this.logger.info("4 Sending reply with clean message " + JSON.stringify(cleanMessage));
110
151
  // cleanMessage = TiledeskChatbotUtil.removeEmptyReplyCommands(message);
111
152
  // if (!TiledeskChatbotUtil.isValidReply(cleanMessage)) {
112
153
  // console.log("invalid message", cleanMessage);
113
154
  // callback(); // cancel reply operation
114
155
  // return;
115
156
  // }
116
- // console.log("valid message!", cleanMessage);
157
+
117
158
  cleanMessage.senderFullname = this.context.chatbot.bot.name;
118
159
  if (this.log) {console.log("Reply:", JSON.stringify(cleanMessage))};
119
160
  await TiledeskChatbotUtil.updateConversationTranscript(this.context.chatbot, cleanMessage);
120
- this.context.tdclient.sendSupportMessage(
161
+ // console.log("sending message!", cleanMessage);
162
+ this.tdClient.sendSupportMessage(
121
163
  this.requestId,
122
164
  cleanMessage,
123
165
  (err) => {
124
166
  if (err) {
125
167
  console.error("Error sending reply:", err);
168
+ this.logger.error("Error sending reply: " + err);
126
169
  }
127
- if (this.log) {console.log("Reply message sent");}
170
+ if (this.log) {console.log("Reply message sent:", JSON.stringify(cleanMessage));}
171
+ this.logger.info("5 Reply message sent");
128
172
  const delay = TiledeskChatbotUtil.totalMessageWait(cleanMessage);
129
173
  // console.log("got total delay:", delay)
130
174
  if (delay > 0 && delay <= 30000) { // prevent long delays