@tiledesk/tiledesk-tybot-connector 2.0.8-rc2 → 2.0.8

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 (82) hide show
  1. package/CHANGELOG.md +5 -31
  2. package/ExtApi.js +8 -1
  3. package/ExtUtil.js +6 -5
  4. package/Logger.js +13 -30
  5. package/TdCache copy.js +242 -0
  6. package/engine/IntentForm.js +1 -0
  7. package/engine/IntentsMachineFactory.js +4 -4
  8. package/engine/MongodbBotsDataSource.js +1 -0
  9. package/engine/MongodbIntentsMachine.js +1 -0
  10. package/engine/TiledeskChatbot.js +16 -11
  11. package/engine/TiledeskIntentsMachine.js +2 -1
  12. package/engine/mock/MockBotsDataSource.js +2 -1
  13. package/index.js +25 -22
  14. package/logs/app1.log +1310 -0
  15. package/package.json +2 -3
  16. package/services/AIService.js +1 -1
  17. package/services/IntegrationService.js +1 -1
  18. package/services/TilebotService.js +1 -1
  19. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +18 -24
  20. package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +2 -1
  21. package/tiledeskChatbotPlugs/MarkbotChatbotPlug.js +3 -1
  22. package/tiledeskChatbotPlugs/SplitsChatbotPlug.js +3 -1
  23. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +9 -7
  24. package/tiledeskChatbotPlugs/directives/DEPRECATED_DirIfNotOpenHours.js +10 -3
  25. package/tiledeskChatbotPlugs/directives/DEPRECATED_DirIfOpenHours.js +9 -2
  26. package/tiledeskChatbotPlugs/directives/DirAddTags.js +3 -1
  27. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +1 -0
  28. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +1 -0
  29. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +1 -0
  30. package/tiledeskChatbotPlugs/directives/DirAssign.js +8 -5
  31. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +3 -1
  32. package/tiledeskChatbotPlugs/directives/DirAssistant.js +1 -0
  33. package/tiledeskChatbotPlugs/directives/DirBrevo.js +6 -4
  34. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +1 -0
  35. package/tiledeskChatbotPlugs/directives/DirClose.js +2 -0
  36. package/tiledeskChatbotPlugs/directives/DirCode.js +1 -0
  37. package/tiledeskChatbotPlugs/directives/DirCondition.js +1 -0
  38. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +1 -0
  39. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +3 -1
  40. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +1 -0
  41. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +5 -2
  42. package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +1 -0
  43. package/tiledeskChatbotPlugs/directives/DirDepartment.js +3 -1
  44. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +3 -1
  45. package/tiledeskChatbotPlugs/directives/DirForm.js +3 -1
  46. package/tiledeskChatbotPlugs/directives/DirGptTask.js +1 -0
  47. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +55 -36
  48. package/tiledeskChatbotPlugs/directives/DirHubspot.js +1 -0
  49. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +4 -1
  50. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +4 -1
  51. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +2 -0
  52. package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +12 -8
  53. package/tiledeskChatbotPlugs/directives/DirIntent.js +5 -6
  54. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +1 -0
  55. package/tiledeskChatbotPlugs/directives/DirLockIntent.js +1 -0
  56. package/tiledeskChatbotPlugs/directives/DirMake.js +16 -1
  57. package/tiledeskChatbotPlugs/directives/DirMessage.js +3 -1
  58. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +1 -0
  59. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +4 -1
  60. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +3 -0
  61. package/tiledeskChatbotPlugs/directives/DirQapla.js +1 -0
  62. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +3 -1
  63. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +2 -1
  64. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +3 -1
  65. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +3 -1
  66. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +3 -1
  67. package/tiledeskChatbotPlugs/directives/DirReply.js +18 -7
  68. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +13 -8
  69. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +3 -1
  70. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +1 -0
  71. package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +1 -0
  72. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +4 -1
  73. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +3 -1
  74. package/tiledeskChatbotPlugs/directives/DirUnlockIntent.js +1 -0
  75. package/tiledeskChatbotPlugs/directives/DirWait.js +1 -0
  76. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -0
  77. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +16 -1
  78. package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +417 -0
  79. package/tiledeskChatbotPlugs/directives/DirWebResponse.js +39 -0
  80. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +1 -0
  81. package/tiledeskChatbotPlugs/directives/Directives.js +0 -1
  82. package/utils/TiledeskChatbotUtil.js +57 -19
@@ -17,7 +17,7 @@ class DirReply {
17
17
  this.requestId = context.requestId;
18
18
  this.token = context.token;
19
19
  this.tdcache = context.tdcache;
20
- this.supportRequest = this.context.supportRequest;
20
+ this.log = context.log;
21
21
  this.logger = new Logger({ request_id: this.requestId, dev: this.context.supportRequest.draft });
22
22
 
23
23
  this.API_ENDPOINT = context.API_ENDPOINT;
@@ -25,7 +25,8 @@ class DirReply {
25
25
  projectId: this.context.projectId,
26
26
  token: this.context.token,
27
27
  APIURL: this.API_ENDPOINT,
28
- APIKEY: "___"
28
+ APIKEY: "___",
29
+ log: this.log
29
30
  });
30
31
  }
31
32
 
@@ -43,10 +44,10 @@ class DirReply {
43
44
  callback();
44
45
  return;
45
46
  }
46
- this.logger.info("Executing Action Reply ", directive.action)
47
+ this.logger.info("1 Execute action reply for " + directive.action.text)
47
48
 
48
49
  this.go(action, () => {
49
- this.logger.info("Action Reply terminated")
50
+ this.logger.info("6 End of action reply " + directive.action.text + " -> callback")
50
51
  callback();
51
52
  });
52
53
  }
@@ -67,16 +68,15 @@ class DirReply {
67
68
  const filler = new Filler();
68
69
  // fill text attribute
69
70
  message.text = filler.fill(message.text, requestAttributes);
71
+ this.logger.info("2 Sending reply " + message.text);
70
72
 
71
73
  if (message.metadata) {
72
74
  winston.debug("DirReply filling message 'metadata':", message.metadata);
73
75
  if (message.metadata.src) {
74
76
  message.metadata.src = filler.fill(message.metadata.src, requestAttributes);
75
- this.logger.debug("Filled metadata.src with ", message.metadata.src);
76
77
  }
77
78
  if (message.metadata.name) {
78
79
  message.metadata.name = filler.fill(message.metadata.name, requestAttributes);
79
- this.logger.debug("Filled metadata.name with ", message.metadata.name);
80
80
  }
81
81
  }
82
82
  winston.debug("DirReply filling commands'. Message:", message);
@@ -89,7 +89,7 @@ class DirReply {
89
89
  let command = commands[i];
90
90
  if (command.type === 'message' && command.message && command.message.text) {
91
91
  command.message.text = filler.fill(command.message.text, requestAttributes);
92
- TiledeskChatbotUtil.fillCommandAttachments(command, requestAttributes);
92
+ TiledeskChatbotUtil.fillCommandAttachments(command, requestAttributes, this.log);
93
93
  winston.debug("DirReply command filled: " + command.message.text);
94
94
  }
95
95
  if (command.type === 'settings' && command.settings) {
@@ -143,6 +143,15 @@ class DirReply {
143
143
  }
144
144
 
145
145
  let cleanMessage = message;
146
+ this.logger.info("3 Sending reply (text) " + cleanMessage.text);
147
+ this.logger.info("4 Sending reply with clean message " + JSON.stringify(cleanMessage));
148
+ // cleanMessage = TiledeskChatbotUtil.removeEmptyReplyCommands(message);
149
+ // if (!TiledeskChatbotUtil.isValidReply(cleanMessage)) {
150
+ // console.log("invalid message", cleanMessage);
151
+ // callback(); // cancel reply operation
152
+ // return;
153
+ // }
154
+
146
155
  cleanMessage.senderFullname = this.context.chatbot.bot.name;
147
156
  winston.debug("DirReply reply with clean message: ", cleanMessage);
148
157
 
@@ -153,8 +162,10 @@ class DirReply {
153
162
  (err) => {
154
163
  if (err) {
155
164
  winston.error("DirReply Error sending reply: ", err);
165
+ this.logger.error("Error sending reply: " + err);
156
166
  }
157
167
  winston.verbose("DirReply reply message sent")
168
+ this.logger.info("5 Reply message sent");
158
169
  const delay = TiledeskChatbotUtil.totalMessageWait(cleanMessage);
159
170
  if (delay > 0 && delay <= 30000) { // prevent long delays
160
171
  setTimeout(() => {
@@ -20,6 +20,7 @@ class DirReplyV2 {
20
20
  this.requestId = context.requestId;
21
21
  this.token = context.token;
22
22
  this.tdcache = context.tdcache;
23
+ this.log = context.log;
23
24
  this.intentDir = new DirIntent(context);
24
25
  this.chatbot = context.chatbot;
25
26
  this.reply = context.reply;
@@ -30,7 +31,8 @@ class DirReplyV2 {
30
31
  projectId: this.context.projectId,
31
32
  token: this.context.token,
32
33
  APIURL: this.API_ENDPOINT,
33
- APIKEY: "___"
34
+ APIKEY: "___",
35
+ log: this.log
34
36
  });
35
37
  }
36
38
 
@@ -59,10 +61,11 @@ class DirReplyV2 {
59
61
  const message = action;
60
62
 
61
63
  let current; // debug only
62
- if (message.attributes.commands[1].message.text) {
63
- current = message.attributes.commands[1].message.text
64
+ if (this.log) {
65
+ if (message.attributes.commands[1].message.text) {
66
+ current = message.attributes.commands[1].message.text
67
+ }
64
68
  }
65
-
66
69
  let must_stop = false;
67
70
  // fill
68
71
  let requestAttributes = null;
@@ -71,9 +74,11 @@ class DirReplyV2 {
71
74
  await TiledeskChatbot.allParametersStatic(
72
75
  this.tdcache, this.requestId
73
76
  );
74
- // for (const [key, value] of Object.entries(requestAttributes)) {
75
- // const value_type = typeof value;
76
- // }
77
+ if (this.log) {
78
+ for (const [key, value] of Object.entries(requestAttributes)) {
79
+ const value_type = typeof value;
80
+ }
81
+ }
77
82
 
78
83
  TiledeskChatbotUtil.replaceJSONButtons(message, requestAttributes);
79
84
 
@@ -192,7 +197,7 @@ class DirReplyV2 {
192
197
  let command = commands[i];
193
198
  if (command.type === 'message' && command.message && command.message.text) {
194
199
  command.message.text = filler.fill(command.message.text, requestAttributes);
195
- TiledeskChatbotUtil.fillCommandAttachments(command, requestAttributes);
200
+ TiledeskChatbotUtil.fillCommandAttachments(command, requestAttributes, this.log);
196
201
  }
197
202
  }
198
203
  }
@@ -15,13 +15,15 @@ class DirSendEmail {
15
15
  this.context = context;
16
16
  this.tdcache = context.tdcache;
17
17
  this.requestId = context.requestId;
18
+ this.log = context.log;
18
19
 
19
20
  this.API_ENDPOINT = context.API_ENDPOINT;
20
21
  this.tdClient = new TiledeskClient({
21
22
  projectId: this.context.projectId,
22
23
  token: this.context.token,
23
24
  APIURL: this.API_ENDPOINT,
24
- APIKEY: "___"
25
+ APIKEY: "___",
26
+ log: this.log
25
27
  });
26
28
  }
27
29
 
@@ -19,6 +19,7 @@ class DirSendWhatsapp {
19
19
  this.requestId = this.context.requestId;
20
20
  this.intentDir = new DirIntent(context);
21
21
  this.API_ENDPOINT = this.context.API_ENDPOINT;
22
+ this.log = context.log;
22
23
  }
23
24
 
24
25
  execute(directive, callback) {
@@ -77,6 +77,7 @@ class DirSetAttribute {
77
77
  throw new Error('context object is mandatory.');
78
78
  }
79
79
  this.context = context;
80
+ this.log = context.log;
80
81
  }
81
82
 
82
83
  execute(directive, callback) {
@@ -79,6 +79,7 @@ class DirSetAttributeV2 {
79
79
  }
80
80
  this.context = context;
81
81
  this.tdcache = context.tdcache;
82
+ this.log = context.log;
82
83
  }
83
84
 
84
85
  execute(directive, callback) {
@@ -198,7 +199,9 @@ class DirSetAttributeV2 {
198
199
  httpUtils.request(
199
200
  HTTPREQUEST, async (err, resbody) => {
200
201
  if (err) {
201
- winston.error("(DirSetAttributeV2) persistOnTiledesk() error: ", err);
202
+ if (this.log) {
203
+ winston.error("(DirSetAttributeV2) persistOnTiledesk() error: ", err);
204
+ }
202
205
  } else {
203
206
  winston.debug("(DirSetAttributeV2) Attributes saved: ", resbody);
204
207
  }
@@ -9,6 +9,7 @@ class DirSetConversationTags {
9
9
  throw new Error('context object is mandatory.');
10
10
  }
11
11
  this.context = context;
12
+ this.log = context.log;
12
13
  this.requestId = context.requestId;
13
14
 
14
15
  this.API_ENDPOINT = context.API_ENDPOINT;
@@ -16,7 +17,8 @@ class DirSetConversationTags {
16
17
  projectId: this.context.projectId,
17
18
  token: this.context.token,
18
19
  APIURL: this.API_ENDPOINT,
19
- APIKEY: "___"
20
+ APIKEY: "___",
21
+ log: this.log
20
22
  });
21
23
  }
22
24
 
@@ -11,6 +11,7 @@ class DirUnlockIntent {
11
11
  throw new Error('tdcache (TdCache) object is mandatory.');
12
12
  }
13
13
  this.tdcache = context.tdcache;
14
+ this.log = context.log;
14
15
  }
15
16
 
16
17
  async execute(directive, callback) {
@@ -12,6 +12,7 @@ class DirWait {
12
12
  this.chatbot = context.chatbot;
13
13
  this.tdcache = context.tdcache;
14
14
  this.requestId = context.requestId;
15
+ this.log = context.log;
15
16
  }
16
17
 
17
18
  execute(directive, callback) {
@@ -13,6 +13,7 @@ class DirWebRequest {
13
13
  this.context = context;
14
14
  this.tdcache = context.tdcache;
15
15
  this.requestId = context.requestId;
16
+ this.log = context.log;
16
17
  }
17
18
 
18
19
  execute(directive, callback) {
@@ -16,6 +16,7 @@ class DirWebRequestV2 {
16
16
  this.requestId = context.requestId;
17
17
  this.chatbot = context.chatbot;
18
18
  this.intentDir = new DirIntent(context);
19
+ this.log = context.log;
19
20
  }
20
21
 
21
22
  execute(directive, callback) {
@@ -301,7 +302,21 @@ class DirWebRequestV2 {
301
302
  }
302
303
  })
303
304
  .catch((err) => {
304
- // FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - END
305
+ if (this.log) {
306
+ // FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - START
307
+ let cache = [];
308
+ let error_log = JSON.stringify(err, function (key, value) { // try to use a separate function
309
+ if (typeof value === 'object' && value != null) {
310
+ if (cache.indexOf(value) !== -1) {
311
+ return;
312
+ }
313
+ cache.push(value);
314
+ }
315
+ return value;
316
+ });
317
+ winston.error("(DirWebRequestv2) An error occurred: ", error_log);
318
+ // FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - END
319
+ }
305
320
  if (callback) {
306
321
  let status = 1000;
307
322
  let cache = [];
@@ -0,0 +1,417 @@
1
+ let axios = require('axios');
2
+ let https = require("https");
3
+ const { Filler } = require('../Filler');
4
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
5
+ const { DirIntent } = require('./DirIntent');
6
+
7
+ class DirWebRequestV2 {
8
+ constructor(context) {
9
+ if (!context) {
10
+ throw new Error('context object is mandatory.');
11
+ }
12
+ this.context = context;
13
+ this.tdcache = context.tdcache;
14
+ this.requestId = context.requestId;
15
+ this.chatbot = context.chatbot;
16
+ this.intentDir = new DirIntent(context);
17
+ this.log = context.log;
18
+ }
19
+
20
+ execute(directive, callback) {
21
+ let action;
22
+ if (directive.action) {
23
+ action = directive.action;
24
+ }
25
+ else {
26
+ console.error("Incorrect directive:", JSON.stringify(directive));
27
+ callback();
28
+ return;
29
+ }
30
+ this.go(action, (stop) => {
31
+ if (this.log) {console.log("(webrequestv2, stop?", stop); }
32
+ callback(stop);
33
+ });
34
+ }
35
+
36
+ async go(action, callback) {
37
+ if (this.log) {console.log("webRequest action:", JSON.stringify(action));}
38
+
39
+ // Condition branches
40
+ let trueIntent = action.trueIntent;
41
+ let falseIntent = action.falseIntent;
42
+ const trueIntentAttributes = action.trueIntentAttributes;
43
+ const falseIntentAttributes = action.falseIntentAttributes;
44
+ let stopOnConditionMet = action.stopOnConditionMet;
45
+ if (trueIntent && trueIntent.trim() === "") {
46
+ trueIntent = null;
47
+ }
48
+ if (falseIntent && falseIntent.trim() === "") {
49
+ falseIntent = null;
50
+ }
51
+
52
+ let requestAttributes = null;
53
+ if (this.tdcache) {
54
+ requestAttributes =
55
+ await TiledeskChatbot.allParametersStatic(
56
+ this.tdcache, this.requestId
57
+ );
58
+ }
59
+ const filler = new Filler();
60
+ const url = filler.fill(action.url, requestAttributes);
61
+
62
+ let headers = {};
63
+ if (action.headersString) {
64
+ let headersDict = action.headersString
65
+ for (const [key, value] of Object.entries(headersDict)) {
66
+ if (this.log) {console.log("header:", key, "value:", value)}
67
+ let filled_value = filler.fill(value, requestAttributes);
68
+ headers[key] = filled_value;
69
+ }
70
+ }
71
+
72
+ let json = null;
73
+ try {
74
+ if (action.jsonBody && action.bodyType == "json") {
75
+ if (this.log) {console.log("action.body is:", action.jsonBody);}
76
+ let jsonBody = filler.fill(action.jsonBody, requestAttributes);
77
+ try {
78
+ json = JSON.parse(jsonBody);
79
+ if (this.log) {console.log("json is:", json);}
80
+ }
81
+ catch(err) {
82
+ console.error("Error parsing webRequest jsonBody:", jsonBody);
83
+ if (callback) {
84
+ if (falseIntent) {
85
+ await this.chatbot.addParameter("flowError", "Error parsing jsonBody");
86
+ this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, () => {
87
+ console.log('herrrrr 11111' )
88
+ callback(true); // stop the flow
89
+ return;
90
+ });
91
+ }
92
+ else {
93
+ console.log('herrrrr 2222' )
94
+ callback(false); // continue the flow
95
+ return;
96
+ }
97
+ }
98
+ }
99
+ }
100
+ else if (action.formData && action.bodyType == "form-data") {
101
+ let formData = filler.fill(action.formData, requestAttributes);
102
+ if (this.log) {console.log("action.body is form-data:", formData);}
103
+ // // fill
104
+ if (formData && formData.length > 0) {
105
+ for (let i = 0; i < formData.length; i++) {
106
+ let field = formData[i];
107
+ if (field.value) {
108
+ field.value = filler.fill(field.value, requestAttributes);
109
+ if (this.log) {console.log("field filled:", field.value);}
110
+ }
111
+ }
112
+ }
113
+ json = {};
114
+ for (let i = 0; i < formData.length; i++) {
115
+ let field = formData[i];
116
+ if (field.enabled && field.value && field.type === "URL") {
117
+ if (this.log) {console.log("Getting file:", field.value);}
118
+ let response = await axios.get(field.value,
119
+ {
120
+ responseType: 'stream'
121
+ }
122
+ );
123
+ let stream = response.data;
124
+ // if (this.log) {console.log("Stream data:", stream);}
125
+ json[field.name] = stream;
126
+ // process.exit(0);
127
+ }
128
+ else if (field.enabled && field.value && field.type === "Text") {
129
+ json[field.name] = field.value;
130
+ }
131
+ }
132
+ if (this.log) {console.log("final json:", json);}
133
+ }
134
+ else {
135
+ if (this.log) {console.log("no action upload parts");}
136
+ }
137
+
138
+ }
139
+ catch(error) {
140
+ console.error("Error", error);
141
+ if (callback) {
142
+ if (falseIntent) {
143
+ await this.chatbot.addParameter("flowError", "Error: " + error);
144
+ this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, () => {
145
+ callback(true); // stop the flow
146
+ return;
147
+ });
148
+ }
149
+ else {
150
+ callback(false); // continue the flow
151
+ return;
152
+ }
153
+ }
154
+ }
155
+
156
+
157
+
158
+ let timeout = this.#webrequest_timeout(action, 20000, 1, 300000);
159
+
160
+ if (this.log) {console.log("webRequest URL", url);}
161
+
162
+ const HTTPREQUEST = {
163
+ url: url,
164
+ headers: headers,
165
+ json: json,
166
+ method: action.method,
167
+ timeout: timeout
168
+ };
169
+
170
+ if (this.log) {console.log("webRequest HTTPREQUEST", HTTPREQUEST);}
171
+ this.#myrequest(
172
+ HTTPREQUEST, async (err, res) => {
173
+ if (this.log && err) {
174
+ console.log("webRequest error:", err);
175
+ }
176
+ if (this.log) {console.log("got res:", res);}
177
+ let resbody = res.data;
178
+ let status = res.status;
179
+ let error = res.error;
180
+ await this.#assignAttributes(action, resbody, status, error)
181
+ if (this.log) {console.log("webRequest resbody:", resbody);}
182
+ if (err) {
183
+ if (this.log) {console.error("webRequest error:", err);}
184
+ if (callback) {
185
+ if (falseIntent) {
186
+ this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, () => {
187
+ callback(true); // stop the flow
188
+ });
189
+ }
190
+ else {
191
+ callback(false); // continue the flow
192
+ }
193
+ }
194
+ }
195
+ else if(res.status >= 200 && res.status <= 299) {
196
+ if (trueIntent) {
197
+ await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, () => {
198
+ callback(true); // stop the flow
199
+ });
200
+ }
201
+ else {
202
+ callback(false); // continue the flow
203
+ }
204
+ }
205
+ else {
206
+ if (falseIntent) {
207
+ this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, () => {
208
+ callback(true); // stop the flow
209
+ });
210
+ }
211
+ else {
212
+ callback(false); // continue the flow
213
+ }
214
+ }
215
+ }
216
+ );
217
+ }
218
+
219
+ async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
220
+ let trueIntentDirective = null;
221
+ if (trueIntent) {
222
+ trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
223
+ }
224
+ let falseIntentDirective = null;
225
+ if (falseIntent) {
226
+ falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
227
+ }
228
+ if (result === true) {
229
+ if (trueIntentDirective) {
230
+ this.intentDir.execute(trueIntentDirective, () => {
231
+ callback();
232
+ });
233
+ }
234
+ else {
235
+ if (this.log) {console.log("No trueIntentDirective specified");}
236
+ callback();
237
+ }
238
+ }
239
+ else {
240
+ if (falseIntentDirective) {
241
+ this.intentDir.execute(falseIntentDirective, () => {
242
+ callback();
243
+ });
244
+ }
245
+ else {
246
+ if (this.log) {console.log("No falseIntentDirective specified");}
247
+ callback();
248
+ }
249
+ }
250
+ }
251
+
252
+ async #assignAttributes(action, resbody, status, error) {
253
+ if (this.log) {
254
+ console.log("assignAttributes resbody:", resbody)
255
+ console.log("assignAttributes error:", error)
256
+ console.log("assignAttributes status:", status)
257
+ console.log("assignAttributes action:", action)
258
+ }
259
+ if (this.context.tdcache) {
260
+ if (action.assignResultTo && resbody) {
261
+ if (this.log) {console.log("assign assignResultTo:", resbody);}
262
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignResultTo, resbody);
263
+ }
264
+ if (action.assignErrorTo && error) {
265
+ if (this.log) {console.log("assign assignResultTo:", error);}
266
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignErrorTo, error);
267
+ }
268
+ if (action.assignStatusTo && status) {
269
+ if (this.log) {console.log("assign assignStatusTo:", status);}
270
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignStatusTo, status);
271
+ }
272
+ // Debug log
273
+ if (this.log) {
274
+ const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
275
+ for (const [key, value] of Object.entries(all_parameters)) {
276
+ if (this.log) {console.log("(webRequest) request parameter:", key, "value:", value, "type:", typeof value)}
277
+ }
278
+ }
279
+ }
280
+ }
281
+
282
+ #myrequest(options, callback) {
283
+ try {
284
+ if (this.log) {
285
+ console.log("API URL:", options.url);
286
+ //console.log("** Options:", JSON.stringify(options));
287
+ // Stringify "options". FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - START
288
+ let cache = [];
289
+ let str_Options = JSON.stringify(options, function(key, value) { // try to use a separate function
290
+ if (typeof value === 'object' && value != null) {
291
+ if (cache.indexOf(value) !== -1) {
292
+ return;
293
+ }
294
+ cache.push(value);
295
+ }
296
+ return value;
297
+ });
298
+ console.log("** Options:", str_Options);
299
+ }
300
+ let axios_options = {
301
+ url: options.url,
302
+ method: options.method,
303
+ params: options.params,
304
+ headers: options.headers,
305
+ timeout: options.timeout,
306
+ maxContentLength: 10000000, // max 10mb response size
307
+ maxBodyLength: 10000000 // max 10mb request body size
308
+ }
309
+
310
+ if (options.json !== null) {
311
+ axios_options.data = options.json
312
+ }
313
+ // if (this.log) {
314
+ // console.log("axios_options:", JSON.stringify(axios_options));
315
+ // }
316
+ if (options.url.startsWith("https:")) {
317
+ const httpsAgent = new https.Agent({
318
+ rejectUnauthorized: false,
319
+ });
320
+ axios_options.httpsAgent = httpsAgent;
321
+ }
322
+
323
+ axios(axios_options)
324
+ .then((res) => {
325
+ if (this.log) {
326
+ console.log("Success Response:", res);
327
+ console.log("Response for url:", options.url);
328
+ console.log("Response headers:\n", JSON.stringify(res.headers));
329
+ }
330
+ if (callback) {
331
+ callback(null, res);
332
+ }
333
+ })
334
+ .catch( (err) => {
335
+ if (this.log) {
336
+ if (err.response) {
337
+ console.log("Error Response data:", err.response.data);
338
+ }
339
+ // FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - START
340
+ let cache = [];
341
+ let error_log = JSON.stringify(err, function(key, value) { // try to use a separate function
342
+ if (typeof value === 'object' && value != null) {
343
+ if (cache.indexOf(value) !== -1) {
344
+ return;
345
+ }
346
+ cache.push(value);
347
+ }
348
+ return value;
349
+ });
350
+ console.error("An error occurred: ", error_log);
351
+ // FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - END
352
+ // console.error("An error occurred:", JSON.stringify(err));
353
+ }
354
+ if (callback) {
355
+ let status = 1000;
356
+ let cache = [];
357
+ let str_error = JSON.stringify(err, function(key, value) { // try to use a separate function
358
+ if (typeof value === 'object' && value != null) {
359
+ if (cache.indexOf(value) !== -1) {
360
+ return;
361
+ }
362
+ cache.push(value);
363
+ }
364
+ return value;
365
+ });
366
+ let error = JSON.parse(str_error) // "status" disappears without this trick
367
+ let errorMessage = JSON.stringify(error);
368
+ if (error.status) {
369
+ status = error.status;
370
+ }
371
+ if (error.message) {
372
+ errorMessage = error.message;
373
+ }
374
+ let data = null;
375
+ if (err.response) {
376
+ data = err.response.data;
377
+ }
378
+ callback(
379
+ null, {
380
+ status: status,
381
+ data: data,
382
+ error: errorMessage
383
+ }
384
+ );
385
+ }
386
+ });
387
+ }
388
+ catch(error) {
389
+ console.error("Error:", error);
390
+ }
391
+ }
392
+
393
+ #webrequest_timeout(action, default_timeout, min, max) {
394
+ let timeout = default_timeout;
395
+ if (!action.settings) {
396
+ return timeout;
397
+ }
398
+ // console.log("default timeout:", timeout);
399
+ // console.log("action.settings:", action.settings);
400
+ // console.log("action.settings.timeout:", action.settings.timeout);
401
+ // console.log("typeof action.settings.timeout:", typeof action.settings.timeout);
402
+ // console.log("action.settings.timeout > min", action.settings.timeout > min)
403
+ // console.log("action.settings.timeout < max", action.settings.timeout < max)
404
+
405
+ if (action.settings.timeout) {
406
+ if ((typeof action.settings.timeout === "number") && action.settings.timeout > min && action.settings.timeout < max) {
407
+ timeout = Math.round(action.settings.timeout)
408
+ // console.log("new timeout:", timeout);
409
+ }
410
+ }
411
+ // console.log("returning timeout:", timeout);
412
+ return timeout
413
+ }
414
+
415
+ }
416
+
417
+ module.exports = { DirWebRequestV2 };