@tiledesk/tiledesk-tybot-connector 2.0.10-rc9 → 2.0.11
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.
- package/CHANGELOG.md +2 -27
- package/ExtApi.js +8 -1
- package/ExtUtil.js +6 -5
- package/Logger.js +17 -26
- package/TdCache copy.js +242 -0
- package/engine/IntentForm.js +1 -0
- package/engine/IntentsMachineFactory.js +4 -4
- package/engine/MongodbBotsDataSource.js +1 -0
- package/engine/MongodbIntentsMachine.js +1 -0
- package/engine/TiledeskChatbot.js +16 -11
- package/engine/TiledeskIntentsMachine.js +2 -1
- package/engine/mock/MockBotsDataSource.js +2 -1
- package/index.js +25 -22
- package/logs/app.log +43056 -4268
- package/logs/app1.log +41129 -0
- package/logs/app2.log +14567 -45594
- package/logs/app6.log +29039 -0
- package/logs/app7.log +4726 -0
- package/package.json +2 -3
- package/services/AIService.js +1 -1
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +18 -38
- package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +2 -1
- package/tiledeskChatbotPlugs/MarkbotChatbotPlug.js +3 -1
- package/tiledeskChatbotPlugs/SplitsChatbotPlug.js +3 -1
- package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +9 -7
- package/tiledeskChatbotPlugs/directives/DEPRECATED_DirIfNotOpenHours.js +10 -3
- package/tiledeskChatbotPlugs/directives/DEPRECATED_DirIfOpenHours.js +9 -2
- package/tiledeskChatbotPlugs/directives/DirAddTags.js +4 -16
- package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +0 -16
- package/tiledeskChatbotPlugs/directives/DirAskGPT.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +4 -17
- package/tiledeskChatbotPlugs/directives/DirAssign.js +8 -5
- package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirAssistant.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirBrevo.js +6 -4
- package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirClose.js +2 -0
- package/tiledeskChatbotPlugs/directives/DirCode.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirCondition.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirCustomerio.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +5 -2
- package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirDepartment.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirForm.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirGptTask.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +55 -36
- package/tiledeskChatbotPlugs/directives/DirHubspot.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +4 -1
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +4 -1
- package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +2 -0
- package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +12 -8
- package/tiledeskChatbotPlugs/directives/DirIntent.js +5 -6
- package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirLockIntent.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirMake.js +16 -1
- package/tiledeskChatbotPlugs/directives/DirMessage.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +4 -1
- package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +3 -0
- package/tiledeskChatbotPlugs/directives/DirQapla.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirRandomReply.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +2 -1
- package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirReply.js +18 -7
- package/tiledeskChatbotPlugs/directives/DirReplyV2.js +13 -8
- package/tiledeskChatbotPlugs/directives/DirSendEmail.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +4 -1
- package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +3 -1
- package/tiledeskChatbotPlugs/directives/DirUnlockIntent.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirWait.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -0
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +19 -19
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +417 -0
- package/tiledeskChatbotPlugs/directives/DirWebResponse.js +40 -6
- package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +1 -0
- package/tiledeskChatbotPlugs/directives/Directives.js +0 -3
- package/utils/TiledeskChatbotUtil.js +57 -19
- package/tiledeskChatbotPlugs/directives/DirAddKbContent.js +0 -332
- package/tiledeskChatbotPlugs/directives/DirFlowLog.js +0 -88
|
@@ -4,7 +4,6 @@ const { Filler } = require('../Filler');
|
|
|
4
4
|
const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
|
|
5
5
|
const { DirIntent } = require('./DirIntent');
|
|
6
6
|
const winston = require('../../utils/winston');
|
|
7
|
-
const { Logger } = require('../../Logger');
|
|
8
7
|
|
|
9
8
|
class DirWebRequestV2 {
|
|
10
9
|
|
|
@@ -18,7 +17,6 @@ class DirWebRequestV2 {
|
|
|
18
17
|
this.chatbot = context.chatbot;
|
|
19
18
|
this.intentDir = new DirIntent(context);
|
|
20
19
|
this.log = context.log;
|
|
21
|
-
this.logger = new Logger({ request_id: this.requestId, dev: this.context.supportRequest.draft });
|
|
22
20
|
}
|
|
23
21
|
|
|
24
22
|
execute(directive, callback) {
|
|
@@ -32,9 +30,7 @@ class DirWebRequestV2 {
|
|
|
32
30
|
callback();
|
|
33
31
|
return;
|
|
34
32
|
}
|
|
35
|
-
this.logger.info("Executing WebRequest action ", directive.action)
|
|
36
33
|
this.go(action, (stop) => {
|
|
37
|
-
this.logger.info("WebRequest action terminated")
|
|
38
34
|
callback(stop);
|
|
39
35
|
}).catch((err) => {
|
|
40
36
|
// do not nothing
|
|
@@ -54,10 +50,10 @@ class DirWebRequestV2 {
|
|
|
54
50
|
let trueIntentAttributes = action.trueIntentAttributes;
|
|
55
51
|
let falseIntentAttributes = action.falseIntentAttributes;
|
|
56
52
|
|
|
57
|
-
winston.debug("DirWebRequestV2
|
|
58
|
-
winston.debug("DirWebRequestV2
|
|
59
|
-
winston.debug("DirWebRequestV2
|
|
60
|
-
winston.debug("DirWebRequestV2
|
|
53
|
+
winston.debug("DirWebRequestV2 trueIntent " + trueIntent)
|
|
54
|
+
winston.debug("DirWebRequestV2 falseIntent " + falseIntent)
|
|
55
|
+
winston.debug("DirWebRequestV2 trueIntentAttributes " + trueIntentAttributes)
|
|
56
|
+
winston.debug("DirWebRequestV2 falseIntentAttributes " + falseIntentAttributes)
|
|
61
57
|
|
|
62
58
|
let requestAttributes = null;
|
|
63
59
|
requestAttributes =
|
|
@@ -69,7 +65,6 @@ class DirWebRequestV2 {
|
|
|
69
65
|
const url = filler.fill(action.url, requestAttributes);
|
|
70
66
|
|
|
71
67
|
let headers = await this.getHeadersFromAction(action, filler, requestAttributes).catch( async (err) => {
|
|
72
|
-
this.logger.error("WebRequest: error getting headers");
|
|
73
68
|
await this.chatbot.addParameter("flowError", "Error getting headers");
|
|
74
69
|
if (falseIntent) {
|
|
75
70
|
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
@@ -81,7 +76,6 @@ class DirWebRequestV2 {
|
|
|
81
76
|
});
|
|
82
77
|
|
|
83
78
|
let json = await this.getJsonFromAction(action, filler, requestAttributes).catch( async (err) => {
|
|
84
|
-
this.logger.error("WebRequest: error parsing json body");
|
|
85
79
|
await this.chatbot.addParameter("flowError", "Error parsing json body");
|
|
86
80
|
if (falseIntent) {
|
|
87
81
|
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
@@ -113,10 +107,8 @@ class DirWebRequestV2 {
|
|
|
113
107
|
let error = res.error;
|
|
114
108
|
await this.#assignAttributes(action, resbody, status, error)
|
|
115
109
|
winston.debug("DirWebRequestV2 resbody:", resbody);
|
|
116
|
-
this.logger.info("WebRequest resbody: ", resbody);
|
|
117
110
|
|
|
118
111
|
if (err) {
|
|
119
|
-
this.logger.error("WebRequest error: ", err);
|
|
120
112
|
winston.log("webRequest error: ", err);
|
|
121
113
|
if (callback) {
|
|
122
114
|
if (falseIntent) {
|
|
@@ -138,8 +130,6 @@ class DirWebRequestV2 {
|
|
|
138
130
|
return;
|
|
139
131
|
}
|
|
140
132
|
else {
|
|
141
|
-
this.logger.warn("WebRequest status ", status);
|
|
142
|
-
this.logger.error("WebRequest error ", error);
|
|
143
133
|
if (falseIntent) {
|
|
144
134
|
await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
|
|
145
135
|
callback(true);
|
|
@@ -238,7 +228,6 @@ class DirWebRequestV2 {
|
|
|
238
228
|
}
|
|
239
229
|
if (result === true) {
|
|
240
230
|
if (trueIntentDirective) {
|
|
241
|
-
this.logger.info("WebRequest: executing true condition");
|
|
242
231
|
this.intentDir.execute(trueIntentDirective, () => {
|
|
243
232
|
if (callback) {
|
|
244
233
|
callback();
|
|
@@ -246,7 +235,6 @@ class DirWebRequestV2 {
|
|
|
246
235
|
});
|
|
247
236
|
}
|
|
248
237
|
else {
|
|
249
|
-
this.logger.info("WebRequest: no block connected to true condition");
|
|
250
238
|
winston.debug("DirWebRequestV2 No trueIntentDirective specified");
|
|
251
239
|
if (callback) {
|
|
252
240
|
callback();
|
|
@@ -255,7 +243,6 @@ class DirWebRequestV2 {
|
|
|
255
243
|
}
|
|
256
244
|
else {
|
|
257
245
|
if (falseIntentDirective) {
|
|
258
|
-
this.logger.info("WebRequest: executing false condition");
|
|
259
246
|
this.intentDir.execute(falseIntentDirective, () => {
|
|
260
247
|
if (callback) {
|
|
261
248
|
callback();
|
|
@@ -263,7 +250,6 @@ class DirWebRequestV2 {
|
|
|
263
250
|
});
|
|
264
251
|
}
|
|
265
252
|
else {
|
|
266
|
-
this.logger.info("WebRequest: no block connected to false condition");
|
|
267
253
|
winston.debug("DirWebRequestV2 No falseIntentDirective specified");
|
|
268
254
|
if (callback) {
|
|
269
255
|
callback();
|
|
@@ -316,7 +302,21 @@ class DirWebRequestV2 {
|
|
|
316
302
|
}
|
|
317
303
|
})
|
|
318
304
|
.catch((err) => {
|
|
319
|
-
|
|
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
|
+
}
|
|
320
320
|
if (callback) {
|
|
321
321
|
let status = 1000;
|
|
322
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 };
|
|
@@ -3,7 +3,6 @@ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
|
|
|
3
3
|
const { TiledeskChatbotUtil } = require('../../utils/TiledeskChatbotUtil');
|
|
4
4
|
const winston = require('../../utils/winston');
|
|
5
5
|
let axios = require('axios');
|
|
6
|
-
const { Logger } = require('../../Logger');
|
|
7
6
|
|
|
8
7
|
class DirWebResponse {
|
|
9
8
|
|
|
@@ -16,7 +15,7 @@ class DirWebResponse {
|
|
|
16
15
|
this.requestId = context.requestId;
|
|
17
16
|
this.token = context.token;
|
|
18
17
|
this.tdcache = context.tdcache;
|
|
19
|
-
this.
|
|
18
|
+
this.log = context.log;
|
|
20
19
|
}
|
|
21
20
|
|
|
22
21
|
execute(directive, callback) {
|
|
@@ -30,9 +29,8 @@ class DirWebResponse {
|
|
|
30
29
|
callback();
|
|
31
30
|
return;
|
|
32
31
|
}
|
|
33
|
-
this.logger.info("Executing WebResponse action ", directive.action)
|
|
34
32
|
this.go(action, () => {
|
|
35
|
-
|
|
33
|
+
// return stop true?
|
|
36
34
|
callback();
|
|
37
35
|
});
|
|
38
36
|
}
|
|
@@ -61,8 +59,6 @@ class DirWebResponse {
|
|
|
61
59
|
payload: json
|
|
62
60
|
}
|
|
63
61
|
|
|
64
|
-
this.logger.debug("WebResponse payload: ", webResponse);
|
|
65
|
-
|
|
66
62
|
const topic = `/webhooks/${this.requestId}`;
|
|
67
63
|
|
|
68
64
|
try {
|
|
@@ -100,4 +96,42 @@ class DirWebResponse {
|
|
|
100
96
|
|
|
101
97
|
}
|
|
102
98
|
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* A stub to send message to the "ext/botId" endpoint, hosted by tilebot on:
|
|
103
|
+
* /${TILEBOT_ROUTE}/ext/${botId}
|
|
104
|
+
*
|
|
105
|
+
* @param {Object} webResponse. The webhook response to send back
|
|
106
|
+
* @param {Object} projectId. The projectId
|
|
107
|
+
* @param {string} botId. Tiledesk botId
|
|
108
|
+
* @param {string} token. User token
|
|
109
|
+
*/
|
|
110
|
+
// function sendResponse(webResponse, projectId, botId, callback) {
|
|
111
|
+
// const url = `${WEBHOOK_URL}/${projectId}/${botId}`;
|
|
112
|
+
// const HTTPREQUEST = {
|
|
113
|
+
// url: url,
|
|
114
|
+
// headers: {
|
|
115
|
+
// 'Content-Type': 'application/json'
|
|
116
|
+
// },
|
|
117
|
+
// json: webResponse,
|
|
118
|
+
// method: 'POST'
|
|
119
|
+
// };
|
|
120
|
+
// myrequest(
|
|
121
|
+
// HTTPREQUEST,
|
|
122
|
+
// function (err, resbody) {
|
|
123
|
+
// if (err) {
|
|
124
|
+
// if (callback) {
|
|
125
|
+
// callback(err);
|
|
126
|
+
// }
|
|
127
|
+
// }
|
|
128
|
+
// else {
|
|
129
|
+
// if (callback) {
|
|
130
|
+
// callback(null, resbody);
|
|
131
|
+
// }
|
|
132
|
+
// }
|
|
133
|
+
// }, false
|
|
134
|
+
// );
|
|
135
|
+
// }
|
|
136
|
+
|
|
103
137
|
module.exports = { DirWebResponse };
|
|
@@ -58,10 +58,7 @@ class Directives {
|
|
|
58
58
|
static MOVE_TO_UNASSIGNED = "move_to_unassigned";
|
|
59
59
|
static CONNECT_BLOCK = "connect_block";
|
|
60
60
|
static ADD_TAGS = 'add_tags'
|
|
61
|
-
static WEBHOOK = 'webhook';
|
|
62
61
|
static WEB_RESPONSE = "web_response";
|
|
63
|
-
static FLOW_LOG = "flow_log";
|
|
64
|
-
static ADD_KB_CONTENT = "add_kb_content";
|
|
65
62
|
|
|
66
63
|
// static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
|
|
67
64
|
// static WHEN_OFFLINE_HOURS = "whenofflinehours"; // DEPRECATED // adds a message on top of the original message when offline hours opts: --replace
|