@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
@@ -16,6 +16,7 @@ class DirHubspot {
16
16
  this.tdcache = this.context.tdcache;
17
17
  this.requestId = this.context.requestId;
18
18
  this.intentDir = new DirIntent(context);
19
+ this.API_ENDPOINT = this.context.API_ENDPOINT;
19
20
  this.log = context.log;
20
21
  }
21
22
 
@@ -71,14 +72,10 @@ class DirHubspot {
71
72
  return;
72
73
  }
73
74
 
74
- const server_base_url = process.env.API_ENDPOINT || process.env.API_URL;
75
75
  const hubspot_base_url = process.env.HUBSPOT_ENDPOINT || "https://api.hubapi.com/crm/v3/";
76
- if (this.log) {
77
- console.log("DirHubspot server_base_url ", server_base_url);
78
- console.log("DirHubspot hubspot_base_url ", hubspot_base_url);
79
- }
76
+ if (this.log) { console.log("DirHubspot hubspot_base_url ", hubspot_base_url); }
80
77
 
81
- let key = await this.getKeyFromIntegrations(server_base_url);
78
+ let key = await this.getKeyFromIntegrations();
82
79
  if (!key) {
83
80
  if (this.log) { console.log("DirHubspot - Key not found in Integrations."); }
84
81
  if (falseIntent) {
@@ -289,11 +286,11 @@ class DirHubspot {
289
286
  }
290
287
  }
291
288
 
292
- async getKeyFromIntegrations(server_base_url) {
289
+ async getKeyFromIntegrations() {
293
290
  return new Promise((resolve) => {
294
291
 
295
292
  const INTEGRATIONS_HTTPREQUEST = {
296
- url: server_base_url + "/" + this.context.projectId + "/integration/name/hubspot",
293
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/hubspot",
297
294
  headers: {
298
295
  'Content-Type': 'application/json',
299
296
  'Authorization': 'JWT ' + this.context.token
@@ -1,4 +1,5 @@
1
1
  // const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
+ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
3
  const { DirIntent } = require('./DirIntent');
3
4
  const ms = require('minimist-string');
4
5
 
@@ -8,33 +9,19 @@ class DirIfOnlineAgents {
8
9
  if (!context) {
9
10
  throw new Error('context object is mandatory.');
10
11
  }
11
- this.tdclient = context.tdclient;
12
- // this.tdclient = new TiledeskClient({
13
- // projectId: context.projectId,
14
- // token: context.token,
15
- // APIURL: context.TILEDESK_APIURL,
16
- // APIKEY: "___",
17
- // log: context.log
18
- // });
19
- // let context = {
20
- // projectId: projectId,
21
- // token: token,
22
- // requestId: supportRequest,
23
- // APIURL: API_URL,
24
- // TILEBOT_ENDPOINT:TILEBOT_ENDPOINT,
25
- // departmentId: depId,
26
- // log: false
27
- // }
12
+
13
+ this.context = context;
28
14
  this.intentDir = new DirIntent(context);
29
- // {
30
- // API_ENDPOINT: context.TILEDESK_APIURL,
31
- // TILEBOT_ENDPOINT: context.TILEBOT_ENDPOINT,
32
- // supportRequest: context.supportRequest,
33
- // token: context.token,
34
- // log: context.log
35
- // }
36
- // );
15
+ this.API_ENDPOINT = context.API_ENDPOINT;
37
16
  this.log = context.log;
17
+
18
+ this.tdClient = new TiledeskClient({
19
+ projectId: this.context.projectId,
20
+ token: this.context.token,
21
+ APIURL: this.API_ENDPOINT,
22
+ APIKEY: "___",
23
+ log: this.log
24
+ });
38
25
  }
39
26
 
40
27
  execute(directive, callback) {
@@ -83,7 +70,7 @@ class DirIfOnlineAgents {
83
70
  const trueIntentAttributes = action.trueIntentAttributes;
84
71
  const falseIntentAttributes = action.falseIntentAttributes;
85
72
  let stopOnConditionMet = action.stopOnConditionMet;
86
- this.tdclient.openNow((err, result) => {
73
+ this.tdClient.openNow((err, result) => {
87
74
  if (this.log) {console.log("openNow():", result);}
88
75
  if (err) {
89
76
  console.error("IfOnlineAgents:tdclient.openNow Error:", err);
@@ -92,7 +79,7 @@ class DirIfOnlineAgents {
92
79
  }
93
80
  else {
94
81
  if (result && result.isopen) {
95
- this.tdclient.getProjectAvailableAgents((err, agents) => {
82
+ this.tdClient.getProjectAvailableAgents((err, agents) => {
96
83
  if (this.log) {console.log("Agents", agents);}
97
84
  if (err) {
98
85
  console.error("IfOnlineAgents:Error getting available agents:", err);
@@ -0,0 +1,278 @@
1
+ // const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
+ const { DirIntent } = require('./DirIntent');
3
+ const axios = require("axios").default;
4
+ let https = require("https");
5
+ const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
6
+ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
7
+
8
+ class DirIfOnlineAgentsV2 {
9
+
10
+ constructor(context) {
11
+ if (!context) {
12
+ throw new Error('context object is mandatory.');
13
+ }
14
+ this.context = context;
15
+ this.chatbot = context.chatbot;
16
+ this.intentDir = new DirIntent(context);
17
+ this.API_ENDPOINT = context.API_ENDPOINT;
18
+ this.log = context.log;
19
+
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
+ });
27
+
28
+ }
29
+
30
+ execute(directive, callback) {
31
+ let action;
32
+ if (directive.action) {
33
+ action = directive.action
34
+ }
35
+ else {
36
+ callback();
37
+ return;
38
+ }
39
+ this.go(action, (stop) => {
40
+ callback(stop);
41
+ });
42
+ }
43
+
44
+ async go(action, callback) {
45
+ // console.log("(DirIfOnlineAgents) action:", action);
46
+ if (!action.trueIntent && !action.falseIntent) {
47
+ if (this.log) {
48
+ console.log("Error DirIfOnlineAgents: missing both action.trueIntent & action.falseIntent");
49
+ }
50
+ callback();
51
+ return;
52
+ }
53
+ const trueIntent = action.trueIntent;
54
+ const falseIntent = action.falseIntent;
55
+ if (this.log) {
56
+ console.log("(DirIfOnlineAgents) IfOnlineAgents:trueIntent:", trueIntent);
57
+ console.log("(DirIfOnlineAgents) IfOnlineAgents:falseIntent:", falseIntent);
58
+ }
59
+ const trueIntentAttributes = action.trueIntentAttributes;
60
+ const falseIntentAttributes = action.falseIntentAttributes;
61
+ let stopOnConditionMet = true; //action.stopOnConditionMet;
62
+
63
+ try {
64
+ const ignoreProjectWideOperatingHours = action.ignoreOperatingHours;
65
+ let isOpen = false;
66
+ if (ignoreProjectWideOperatingHours === true) {
67
+ // always go on to only check agents availability
68
+ isOpen = true;
69
+ }
70
+ else {
71
+ const result = await this.openNow();
72
+ if (result && result.isopen) {
73
+ isOpen = true;
74
+ }
75
+ else {
76
+ isOpen = false;
77
+ }
78
+ }
79
+
80
+ // if (result && result.isopen) {
81
+ if (isOpen === true) { // always true if ignoreProjectWideOperatingHours = true
82
+ const selectedOption = action.selectedOption;
83
+
84
+ let agents;
85
+ if (selectedOption === "currentDep") {
86
+ if (this.log) {console.log("(DirIfOnlineAgents) currentDep"); }
87
+ let departmentId = await this.chatbot.getParameter("department_id");
88
+ if (this.log) {console.log("this.context.departmentId:", departmentId);}
89
+
90
+ if (departmentId) {
91
+ if (this.log) {console.log("(DirIfOnlineAgents) agents = await this.getProjectAvailableAgents(", departmentId, ", true);"); }
92
+ agents = await this.getProjectAvailableAgents(departmentId, true);
93
+ if (this.log) {console.log("(DirIfOnlineAgents) agents:", agents); }
94
+ } else {
95
+ console.error("(DirIfOnlineAgents) no departmentId found in attributes");
96
+ await this.chatbot.addParameter("flowError", "(If online Agents) No departmentId found in attributes.");
97
+ if (this.log) {console.log("(DirIfOnlineAgents) flowError added in attributes", await this.chatbot.getParameter("flowError") ); }
98
+ if (falseIntent) { // no agents available
99
+ let intentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
100
+ this.intentDir.execute(intentDirective, () => {
101
+ callback(stopOnConditionMet);
102
+ });
103
+ return;
104
+ }
105
+ else {
106
+ callback(false);
107
+ return;
108
+ }
109
+ }
110
+ }
111
+ else if (selectedOption === "selectedDep") {
112
+ if (this.log) {console.log("(DirIfOnlineAgents) selectedOption === selectedDep", action.selectedDepartmentId); }
113
+ if (this.log) {console.log("(DirIfOnlineAgents) agents = await this.getProjectAvailableAgents(", action.selectedDepartmentId, ", true);"); }
114
+
115
+ agents = await this.getProjectAvailableAgents(action.selectedDepartmentId, true);
116
+ if (this.log) {console.log("(DirIfOnlineAgents) agents:", agents); }
117
+ }
118
+ else { // if (checkAll) => go project-wide
119
+ if (this.log) {console.log("(DirIfOnlineAgents) selectedOption === all | getProjectAvailableAgents(null, true)"); }
120
+ agents = await this.getProjectAvailableAgents(null, true);
121
+ if (this.log) {console.log("(DirIfOnlineAgents) agents:", agents); }
122
+ }
123
+
124
+ if (agents && agents.length > 0) {
125
+ if (trueIntent) {
126
+ let intentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
127
+ if (this.log) {console.log("agents (openHours) => trueIntent");}
128
+ this.intentDir.execute(intentDirective, () => {
129
+ callback(stopOnConditionMet);
130
+ });
131
+ }
132
+ else {
133
+ if (this.log) { console.log("(DirIfOnlineAgents) No IfOnlineAgents trueIntent defined. callback()"); } // prod
134
+ this.chatbot.addParameter("flowError", "(If online Agents) No IfOnlineAgents success path defined.");
135
+ callback();
136
+ return;
137
+ }
138
+ }
139
+ else if (falseIntent) { // no agents available
140
+ let intentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
141
+ if (this.log) {console.log("(DirIfOnlineAgents) !agents (openHours) => falseIntent", intentDirective);}
142
+ this.intentDir.execute(intentDirective, () => {
143
+ callback(stopOnConditionMet);
144
+ });
145
+ }
146
+ else {
147
+ if (this.log) {console.log("(DirIfOnlineAgents) Error: No falseIntent defined", intentDirective);}
148
+ this.chatbot.addParameter("flowError", "(If online Agents) No path for 'no available agents' defined.");
149
+ callback();
150
+ }
151
+ } else {
152
+ if (falseIntent) {
153
+ let intentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
154
+ if (this.log) { console.log("!agents (!openHours) => falseIntent"); }
155
+ this.intentDir.execute(intentDirective, () => {
156
+ callback();
157
+ });
158
+ }
159
+ else {
160
+ callback();
161
+ }
162
+ }
163
+ }
164
+ catch(err) {
165
+ console.error("(DirIfOnlineAgents) An error occurred:" + err);
166
+ this.chatbot.addParameter("flowError", "(If online Agents) An error occurred: " + err);
167
+ callback();
168
+ }
169
+ }
170
+
171
+ async openNow() {
172
+ return new Promise( (resolve, reject) => {
173
+ this.tdClient.openNow(async (err, result) => {
174
+ if (this.log) {console.log("(DirIfOnlineAgents) openNow():", result);}
175
+ if (err) {
176
+ reject(err);
177
+ }
178
+ else {
179
+ resolve(result);
180
+ }
181
+ });
182
+ });
183
+ }
184
+
185
+ async getProjectAvailableAgents(departmentId, raw, callback) {
186
+ return new Promise( (resolve, reject) => {
187
+ let URL = `${this.API_ENDPOINT}/projects/${this.context.projectId}/users/availables?raw=${raw}`
188
+ if (departmentId) {
189
+ URL = URL + `&department=${departmentId}`
190
+ }
191
+ const HTTPREQUEST = {
192
+ url: URL,
193
+ headers: {
194
+ 'Content-Type' : 'application/json',
195
+ 'Authorization': this.fixToken(this.context.token)
196
+ },
197
+ // json: true,
198
+ method: 'GET',
199
+ };
200
+ this.#myrequest(
201
+ HTTPREQUEST,
202
+ function(err, resbody) {
203
+ if (err) {
204
+ if (callback) {
205
+ callback(err);
206
+ }
207
+ reject(err);
208
+ }
209
+ else {
210
+ if (callback) {
211
+ callback(null, resbody);
212
+ }
213
+ resolve(resbody);
214
+ }
215
+ }, this.log);
216
+ });
217
+
218
+ }
219
+
220
+ #myrequest(options, callback) {
221
+ if (this.log) {
222
+ console.log("API URL:", options.url);
223
+ console.log("** Options:", JSON.stringify(options));
224
+ }
225
+ let axios_options = {
226
+ url: options.url,
227
+ method: options.method,
228
+ params: options.params,
229
+ headers: options.headers
230
+ }
231
+ if (options.json !== null) {
232
+ axios_options.data = options.json
233
+ }
234
+ if (this.log) {
235
+ console.log("axios_options:", JSON.stringify(axios_options));
236
+ }
237
+ if (options.url.startsWith("https:")) {
238
+ const httpsAgent = new https.Agent({
239
+ rejectUnauthorized: false,
240
+ });
241
+ axios_options.httpsAgent = httpsAgent;
242
+ }
243
+ axios(axios_options)
244
+ .then((res) => {
245
+ if (this.log) {
246
+ console.log("Response for url:", options.url);
247
+ console.log("Response headers:\n", JSON.stringify(res.headers));
248
+ }
249
+ if (res && res.status == 200 && res.data) {
250
+ if (callback) {
251
+ callback(null, res.data);
252
+ }
253
+ }
254
+ else {
255
+ if (callback) {
256
+ callback(new Error("Response status is not 200"), null);
257
+ }
258
+ }
259
+ })
260
+ .catch((error) => {
261
+ console.error("(DirIfOnlineAgents) Axios error: ", JSON.stringify(error));
262
+ if (callback) {
263
+ callback(error, null);
264
+ }
265
+ });
266
+ }
267
+
268
+ fixToken(token) {
269
+ if (token.startsWith('JWT ')) {
270
+ return token
271
+ }
272
+ else {
273
+ return 'JWT ' + token
274
+ }
275
+ }
276
+ }
277
+
278
+ module.exports = { DirIfOnlineAgentsV2 };
@@ -1,5 +1,7 @@
1
1
  // const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
+ let axios = require('axios');
2
3
  const { DirIntent } = require('./DirIntent');
4
+ let https = require("https");
3
5
  const ms = require('minimist-string');
4
6
 
5
7
  class DirIfOpenHours {
@@ -8,32 +10,10 @@ class DirIfOpenHours {
8
10
  if (!context) {
9
11
  throw new Error('context object is mandatory.');
10
12
  }
11
- this.tdclient = context.tdclient;
12
- // this.tdclient = new TiledeskClient({
13
- // projectId: context.projectId,
14
- // token: context.token,
15
- // APIURL: context.TILEDESK_APIURL,
16
- // APIKEY: "___",
17
- // log: context.log
18
- // });
19
- // let context = {
20
- // projectId: projectId,
21
- // token: token,
22
- // requestId: supportRequest,
23
- // APIURL: API_URL,
24
- // TILEBOT_ENDPOINT:TILEBOT_ENDPOINT,
25
- // departmentId: depId,
26
- // log: false
27
- // }
13
+
14
+ this.context = context;
15
+ this.API_ENDPOINT = context.API_ENDPOINT;
28
16
  this.intentDir = new DirIntent(context);
29
- // {
30
- // API_ENDPOINT: context.TILEDESK_APIURL,
31
- // TILEBOT_ENDPOINT: context.TILEBOT_ENDPOINT,
32
- // supportRequest: context.supportRequest,
33
- // token: context.token,
34
- // log: context.log
35
- // }
36
- // );
37
17
  this.log = context.log;
38
18
  }
39
19
 
@@ -67,11 +47,13 @@ class DirIfOpenHours {
67
47
  }
68
48
 
69
49
  go(action, callback) {
50
+
70
51
  const trueIntent = action.trueIntent;
71
52
  const falseIntent = action.falseIntent;
72
53
  const trueIntentAttributes = action.trueIntentAttributes;
73
54
  const falseIntentAttributes = action.falseIntentAttributes;
74
55
  const stopOnConditionMet = action.stopOnConditionMet;
56
+
75
57
  if (trueIntent && trueIntent.trim() === "") {
76
58
  trueIntent = null;
77
59
  }
@@ -84,36 +66,150 @@ class DirIfOpenHours {
84
66
  callback();
85
67
  return;
86
68
  }
87
- this.tdclient.openNow((err, result) => {
88
- if (this.log) {console.log("openNow():", result);}
89
- if (err) {
90
- console.error("DirIfOpenHours Error:", err);
91
- callback();
69
+
70
+ let slot_id = null;
71
+ if (action.slotId) {
72
+ slot_id = action.slotId;
73
+ }
74
+
75
+ let isopen_url = this.API_ENDPOINT + "/projects/" + this.context.projectId + "/isopen";
76
+ if (slot_id) {
77
+ isopen_url = isopen_url.concat("?timeSlot=" + slot_id);
78
+ }
79
+
80
+ const HTTPREQUEST = {
81
+ url: isopen_url,
82
+ headers: {
83
+ 'Content-Type': 'application/json',
84
+ 'Authorization': 'JWT ' + this.context.token
85
+ },
86
+ method: 'GET'
87
+ }
88
+ if (this.log) { console.log("DirIfOpenHours HTTPREQUEST", HTTPREQUEST); }
89
+
90
+ this.#myrequest(
91
+ HTTPREQUEST, async (err, resbody) => {
92
+ if (this.log && err) {
93
+ console.log("DirIfOpenHours error: ", err);
94
+ }
95
+ if (this.log) { console.log("DirIfOpenHours resbody:", resbody); }
96
+
97
+ if (err) {
98
+ if (callback) {
99
+ if (falseIntent) {
100
+ let intentDirective = DirIntent.intentDirectiveFor(falseIntent);
101
+ if (this.log) {console.log("!agents (openHours) => falseIntent", falseIntent);}
102
+ this.intentDir.execute(intentDirective, () => {
103
+ callback(stopOnConditionMet);
104
+ });
105
+ }
106
+ }
107
+ } else {
108
+ if (resbody.isopen && resbody.isopen === true) {
109
+ if (trueIntent) {
110
+ let intentDirective = DirIntent.intentDirectiveFor(trueIntent);
111
+ if (this.log) {console.log("agents (openHours) => trueIntent");}
112
+ this.intentDir.execute(intentDirective, () => {
113
+ callback(stopOnConditionMet);
114
+ });
115
+ }
116
+ callback();
117
+ return;
118
+ } else {
119
+ if (falseIntent) {
120
+ let intentDirective = DirIntent.intentDirectiveFor(falseIntent);
121
+ if (this.log) {console.log("!agents (openHours) => falseIntent", falseIntent);}
122
+ this.intentDir.execute(intentDirective, () => {
123
+ callback(stopOnConditionMet);
124
+ });
125
+ }
126
+ callback();
127
+ return;
128
+ }
129
+ }
92
130
  }
93
- else if (result && result.isopen) {
94
- if (trueIntent) {
95
- let intentDirective = DirIntent.intentDirectiveFor(trueIntent);
96
- if (this.log) {console.log("agents (openHours) => trueIntent");}
97
- this.intentDir.execute(intentDirective, () => {
98
- callback(stopOnConditionMet);
99
- });
131
+ )
132
+
133
+ // this.tdClient.openNow(action.slot_id, (err, result) => {
134
+ // console.log("openNow():", result);
135
+ // if (this.log) {console.log("openNow():", result);}
136
+ // if (err) {
137
+ // console.error("*** DirIfOpenHours Error:", err);
138
+ // callback();
139
+ // }
140
+ // else if (result && result.isopen) {
141
+ // console.log("yes is open")
142
+ // if (trueIntent) {
143
+ // let intentDirective = DirIntent.intentDirectiveFor(trueIntent);
144
+ // if (this.log) {console.log("agents (openHours) => trueIntent");}
145
+ // this.intentDir.execute(intentDirective, () => {
146
+ // callback(stopOnConditionMet);
147
+ // });
148
+ // }
149
+ // else {
150
+ // callback();
151
+ // return;
152
+ // }
153
+ // }
154
+ // else if (falseIntent) {
155
+ // let intentDirective = DirIntent.intentDirectiveFor(falseIntent);
156
+ // if (this.log) {console.log("!agents (openHours) => falseIntent", falseIntent);}
157
+ // this.intentDir.execute(intentDirective, () => {
158
+ // callback(stopOnConditionMet);
159
+ // });
160
+ // }
161
+ // else {
162
+ // callback();
163
+ // }
164
+ // });
165
+ }
166
+
167
+ #myrequest(options, callback) {
168
+ if (this.log) {
169
+ console.log("API URL:", options.url);
170
+ console.log("** Options:", JSON.stringify(options));
171
+ }
172
+ let axios_options = {
173
+ url: options.url,
174
+ method: options.method,
175
+ params: options.params,
176
+ headers: options.headers
177
+ }
178
+ if (options.json !== null) {
179
+ axios_options.data = options.json
180
+ }
181
+ if (this.log) {
182
+ console.log("axios_options:", JSON.stringify(axios_options));
183
+ }
184
+ if (options.url.startsWith("https:")) {
185
+ const httpsAgent = new https.Agent({
186
+ rejectUnauthorized: false,
187
+ });
188
+ axios_options.httpsAgent = httpsAgent;
189
+ }
190
+ axios(axios_options)
191
+ .then((res) => {
192
+ if (this.log) {
193
+ console.log("Response for url:", options.url);
194
+ console.log("Response headers:\n", JSON.stringify(res.headers));
195
+ }
196
+ if (res && res.status == 200 && res.data) {
197
+ if (callback) {
198
+ callback(null, res.data);
199
+ }
100
200
  }
101
201
  else {
102
- callback();
103
- return;
202
+ if (callback) {
203
+ callback(new Error("Response status is not 200"), null);
204
+ }
104
205
  }
105
- }
106
- else if (falseIntent) {
107
- let intentDirective = DirIntent.intentDirectiveFor(falseIntent);
108
- if (this.log) {console.log("!agents (openHours) => falseIntent", falseIntent);}
109
- this.intentDir.execute(intentDirective, () => {
110
- callback(stopOnConditionMet);
111
- });
112
- }
113
- else {
114
- callback();
115
- }
116
- });
206
+ })
207
+ .catch((error) => {
208
+ console.error("(DirIfOpenHours) Axios error: ", JSON.stringify(error));
209
+ if (callback) {
210
+ callback(error, null);
211
+ }
212
+ });
117
213
  }
118
214
 
119
215
  parseParams(directive_parameter) {