@tiledesk/tiledesk-tybot-connector 2.0.21-rc4 → 2.0.21
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/Logger.js +0 -3
- package/TdCache copy.js +242 -0
- package/engine/mock/MockBotsDataSource.js +2 -1
- package/index.js +0 -3
- package/logs/app.log +350 -0
- package/package.json +1 -1
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +11 -29
- package/tiledeskChatbotPlugs/directives/DirAddKbContent.js +26 -25
- package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +114 -32
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +418 -0
- package/tiledeskChatbotPlugs/directives/DirWebResponse.js +1 -0
- package/tiledeskChatbotPlugs/directives/Directives.js +0 -1
- package/utils/TiledeskChatbotUtil.js +27 -3
- package/utils/constants.js +17 -0
- package/services/LLMService.js +0 -105
- package/services/QuotasService.js +0 -69
|
@@ -0,0 +1,418 @@
|
|
|
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.log = context.log;
|
|
17
|
+
|
|
18
|
+
this.intentDir = new DirIntent(context);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
execute(directive, callback) {
|
|
22
|
+
let action;
|
|
23
|
+
if (directive.action) {
|
|
24
|
+
action = directive.action;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
console.error("Incorrect directive:", JSON.stringify(directive));
|
|
28
|
+
callback();
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
this.go(action, (stop) => {
|
|
32
|
+
if (this.log) {console.log("(webrequestv2, stop?", stop); }
|
|
33
|
+
callback(stop);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async go(action, callback) {
|
|
38
|
+
if (this.log) {console.log("webRequest action:", JSON.stringify(action));}
|
|
39
|
+
|
|
40
|
+
// Condition branches
|
|
41
|
+
let trueIntent = action.trueIntent;
|
|
42
|
+
let falseIntent = action.falseIntent;
|
|
43
|
+
const trueIntentAttributes = action.trueIntentAttributes;
|
|
44
|
+
const falseIntentAttributes = action.falseIntentAttributes;
|
|
45
|
+
let stopOnConditionMet = action.stopOnConditionMet;
|
|
46
|
+
if (trueIntent && trueIntent.trim() === "") {
|
|
47
|
+
trueIntent = null;
|
|
48
|
+
}
|
|
49
|
+
if (falseIntent && falseIntent.trim() === "") {
|
|
50
|
+
falseIntent = null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
let requestAttributes = null;
|
|
54
|
+
if (this.tdcache) {
|
|
55
|
+
requestAttributes =
|
|
56
|
+
await TiledeskChatbot.allParametersStatic(
|
|
57
|
+
this.tdcache, this.requestId
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
const filler = new Filler();
|
|
61
|
+
const url = filler.fill(action.url, requestAttributes);
|
|
62
|
+
|
|
63
|
+
let headers = {};
|
|
64
|
+
if (action.headersString) {
|
|
65
|
+
let headersDict = action.headersString
|
|
66
|
+
for (const [key, value] of Object.entries(headersDict)) {
|
|
67
|
+
if (this.log) {console.log("header:", key, "value:", value)}
|
|
68
|
+
let filled_value = filler.fill(value, requestAttributes);
|
|
69
|
+
headers[key] = filled_value;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let json = null;
|
|
74
|
+
try {
|
|
75
|
+
if (action.jsonBody && action.bodyType == "json") {
|
|
76
|
+
if (this.log) {console.log("action.body is:", action.jsonBody);}
|
|
77
|
+
let jsonBody = filler.fill(action.jsonBody, requestAttributes);
|
|
78
|
+
try {
|
|
79
|
+
json = JSON.parse(jsonBody);
|
|
80
|
+
if (this.log) {console.log("json is:", json);}
|
|
81
|
+
}
|
|
82
|
+
catch(err) {
|
|
83
|
+
console.error("Error parsing webRequest jsonBody:", jsonBody);
|
|
84
|
+
if (callback) {
|
|
85
|
+
if (falseIntent) {
|
|
86
|
+
await this.chatbot.addParameter("flowError", "Error parsing jsonBody");
|
|
87
|
+
this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, () => {
|
|
88
|
+
console.log('herrrrr 11111' )
|
|
89
|
+
callback(true); // stop the flow
|
|
90
|
+
return;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
console.log('herrrrr 2222' )
|
|
95
|
+
callback(false); // continue the flow
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
else if (action.formData && action.bodyType == "form-data") {
|
|
102
|
+
let formData = filler.fill(action.formData, requestAttributes);
|
|
103
|
+
if (this.log) {console.log("action.body is form-data:", formData);}
|
|
104
|
+
// // fill
|
|
105
|
+
if (formData && formData.length > 0) {
|
|
106
|
+
for (let i = 0; i < formData.length; i++) {
|
|
107
|
+
let field = formData[i];
|
|
108
|
+
if (field.value) {
|
|
109
|
+
field.value = filler.fill(field.value, requestAttributes);
|
|
110
|
+
if (this.log) {console.log("field filled:", field.value);}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
json = {};
|
|
115
|
+
for (let i = 0; i < formData.length; i++) {
|
|
116
|
+
let field = formData[i];
|
|
117
|
+
if (field.enabled && field.value && field.type === "URL") {
|
|
118
|
+
if (this.log) {console.log("Getting file:", field.value);}
|
|
119
|
+
let response = await axios.get(field.value,
|
|
120
|
+
{
|
|
121
|
+
responseType: 'stream'
|
|
122
|
+
}
|
|
123
|
+
);
|
|
124
|
+
let stream = response.data;
|
|
125
|
+
// if (this.log) {console.log("Stream data:", stream);}
|
|
126
|
+
json[field.name] = stream;
|
|
127
|
+
// process.exit(0);
|
|
128
|
+
}
|
|
129
|
+
else if (field.enabled && field.value && field.type === "Text") {
|
|
130
|
+
json[field.name] = field.value;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (this.log) {console.log("final json:", json);}
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
if (this.log) {console.log("no action upload parts");}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
}
|
|
140
|
+
catch(error) {
|
|
141
|
+
console.error("Error", error);
|
|
142
|
+
if (callback) {
|
|
143
|
+
if (falseIntent) {
|
|
144
|
+
await this.chatbot.addParameter("flowError", "Error: " + error);
|
|
145
|
+
this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, () => {
|
|
146
|
+
callback(true); // stop the flow
|
|
147
|
+
return;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
callback(false); // continue the flow
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
let timeout = this.#webrequest_timeout(action, 20000, 1, 300000);
|
|
160
|
+
|
|
161
|
+
if (this.log) {console.log("webRequest URL", url);}
|
|
162
|
+
|
|
163
|
+
const HTTPREQUEST = {
|
|
164
|
+
url: url,
|
|
165
|
+
headers: headers,
|
|
166
|
+
json: json,
|
|
167
|
+
method: action.method,
|
|
168
|
+
timeout: timeout
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
if (this.log) {console.log("webRequest HTTPREQUEST", HTTPREQUEST);}
|
|
172
|
+
this.#myrequest(
|
|
173
|
+
HTTPREQUEST, async (err, res) => {
|
|
174
|
+
if (this.log && err) {
|
|
175
|
+
console.log("webRequest error:", err);
|
|
176
|
+
}
|
|
177
|
+
if (this.log) {console.log("got res:", res);}
|
|
178
|
+
let resbody = res.data;
|
|
179
|
+
let status = res.status;
|
|
180
|
+
let error = res.error;
|
|
181
|
+
await this.#assignAttributes(action, resbody, status, error)
|
|
182
|
+
if (this.log) {console.log("webRequest resbody:", resbody);}
|
|
183
|
+
if (err) {
|
|
184
|
+
if (this.log) {console.error("webRequest error:", err);}
|
|
185
|
+
if (callback) {
|
|
186
|
+
if (falseIntent) {
|
|
187
|
+
this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, () => {
|
|
188
|
+
callback(true); // stop the flow
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
else {
|
|
192
|
+
callback(false); // continue the flow
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
else if(res.status >= 200 && res.status <= 299) {
|
|
197
|
+
if (trueIntent) {
|
|
198
|
+
await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, () => {
|
|
199
|
+
callback(true); // stop the flow
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
callback(false); // continue the flow
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
if (falseIntent) {
|
|
208
|
+
this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, () => {
|
|
209
|
+
callback(true); // stop the flow
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
callback(false); // continue the flow
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
|
|
221
|
+
let trueIntentDirective = null;
|
|
222
|
+
if (trueIntent) {
|
|
223
|
+
trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
|
|
224
|
+
}
|
|
225
|
+
let falseIntentDirective = null;
|
|
226
|
+
if (falseIntent) {
|
|
227
|
+
falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
|
|
228
|
+
}
|
|
229
|
+
if (result === true) {
|
|
230
|
+
if (trueIntentDirective) {
|
|
231
|
+
this.intentDir.execute(trueIntentDirective, () => {
|
|
232
|
+
callback();
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
if (this.log) {console.log("No trueIntentDirective specified");}
|
|
237
|
+
callback();
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
else {
|
|
241
|
+
if (falseIntentDirective) {
|
|
242
|
+
this.intentDir.execute(falseIntentDirective, () => {
|
|
243
|
+
callback();
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
if (this.log) {console.log("No falseIntentDirective specified");}
|
|
248
|
+
callback();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
async #assignAttributes(action, resbody, status, error) {
|
|
254
|
+
if (this.log) {
|
|
255
|
+
console.log("assignAttributes resbody:", resbody)
|
|
256
|
+
console.log("assignAttributes error:", error)
|
|
257
|
+
console.log("assignAttributes status:", status)
|
|
258
|
+
console.log("assignAttributes action:", action)
|
|
259
|
+
}
|
|
260
|
+
if (this.context.tdcache) {
|
|
261
|
+
if (action.assignResultTo && resbody) {
|
|
262
|
+
if (this.log) {console.log("assign assignResultTo:", resbody);}
|
|
263
|
+
await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignResultTo, resbody);
|
|
264
|
+
}
|
|
265
|
+
if (action.assignErrorTo && error) {
|
|
266
|
+
if (this.log) {console.log("assign assignResultTo:", error);}
|
|
267
|
+
await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignErrorTo, error);
|
|
268
|
+
}
|
|
269
|
+
if (action.assignStatusTo && status) {
|
|
270
|
+
if (this.log) {console.log("assign assignStatusTo:", status);}
|
|
271
|
+
await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignStatusTo, status);
|
|
272
|
+
}
|
|
273
|
+
// Debug log
|
|
274
|
+
if (this.log) {
|
|
275
|
+
const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
|
|
276
|
+
for (const [key, value] of Object.entries(all_parameters)) {
|
|
277
|
+
if (this.log) {console.log("(webRequest) request parameter:", key, "value:", value, "type:", typeof value)}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
#myrequest(options, callback) {
|
|
284
|
+
try {
|
|
285
|
+
if (this.log) {
|
|
286
|
+
console.log("API URL:", options.url);
|
|
287
|
+
//console.log("** Options:", JSON.stringify(options));
|
|
288
|
+
// Stringify "options". FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - START
|
|
289
|
+
let cache = [];
|
|
290
|
+
let str_Options = JSON.stringify(options, function(key, value) { // try to use a separate function
|
|
291
|
+
if (typeof value === 'object' && value != null) {
|
|
292
|
+
if (cache.indexOf(value) !== -1) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
cache.push(value);
|
|
296
|
+
}
|
|
297
|
+
return value;
|
|
298
|
+
});
|
|
299
|
+
console.log("** Options:", str_Options);
|
|
300
|
+
}
|
|
301
|
+
let axios_options = {
|
|
302
|
+
url: options.url,
|
|
303
|
+
method: options.method,
|
|
304
|
+
params: options.params,
|
|
305
|
+
headers: options.headers,
|
|
306
|
+
timeout: options.timeout,
|
|
307
|
+
maxContentLength: 10000000, // max 10mb response size
|
|
308
|
+
maxBodyLength: 10000000 // max 10mb request body size
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
if (options.json !== null) {
|
|
312
|
+
axios_options.data = options.json
|
|
313
|
+
}
|
|
314
|
+
// if (this.log) {
|
|
315
|
+
// console.log("axios_options:", JSON.stringify(axios_options));
|
|
316
|
+
// }
|
|
317
|
+
if (options.url.startsWith("https:")) {
|
|
318
|
+
const httpsAgent = new https.Agent({
|
|
319
|
+
rejectUnauthorized: false,
|
|
320
|
+
});
|
|
321
|
+
axios_options.httpsAgent = httpsAgent;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
axios(axios_options)
|
|
325
|
+
.then((res) => {
|
|
326
|
+
if (this.log) {
|
|
327
|
+
console.log("Success Response:", res);
|
|
328
|
+
console.log("Response for url:", options.url);
|
|
329
|
+
console.log("Response headers:\n", JSON.stringify(res.headers));
|
|
330
|
+
}
|
|
331
|
+
if (callback) {
|
|
332
|
+
callback(null, res);
|
|
333
|
+
}
|
|
334
|
+
})
|
|
335
|
+
.catch( (err) => {
|
|
336
|
+
if (this.log) {
|
|
337
|
+
if (err.response) {
|
|
338
|
+
console.log("Error Response data:", err.response.data);
|
|
339
|
+
}
|
|
340
|
+
// FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - START
|
|
341
|
+
let cache = [];
|
|
342
|
+
let error_log = JSON.stringify(err, function(key, value) { // try to use a separate function
|
|
343
|
+
if (typeof value === 'object' && value != null) {
|
|
344
|
+
if (cache.indexOf(value) !== -1) {
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
cache.push(value);
|
|
348
|
+
}
|
|
349
|
+
return value;
|
|
350
|
+
});
|
|
351
|
+
console.error("An error occurred: ", error_log);
|
|
352
|
+
// FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - END
|
|
353
|
+
// console.error("An error occurred:", JSON.stringify(err));
|
|
354
|
+
}
|
|
355
|
+
if (callback) {
|
|
356
|
+
let status = 1000;
|
|
357
|
+
let cache = [];
|
|
358
|
+
let str_error = JSON.stringify(err, function(key, value) { // try to use a separate function
|
|
359
|
+
if (typeof value === 'object' && value != null) {
|
|
360
|
+
if (cache.indexOf(value) !== -1) {
|
|
361
|
+
return;
|
|
362
|
+
}
|
|
363
|
+
cache.push(value);
|
|
364
|
+
}
|
|
365
|
+
return value;
|
|
366
|
+
});
|
|
367
|
+
let error = JSON.parse(str_error) // "status" disappears without this trick
|
|
368
|
+
let errorMessage = JSON.stringify(error);
|
|
369
|
+
if (error.status) {
|
|
370
|
+
status = error.status;
|
|
371
|
+
}
|
|
372
|
+
if (error.message) {
|
|
373
|
+
errorMessage = error.message;
|
|
374
|
+
}
|
|
375
|
+
let data = null;
|
|
376
|
+
if (err.response) {
|
|
377
|
+
data = err.response.data;
|
|
378
|
+
}
|
|
379
|
+
callback(
|
|
380
|
+
null, {
|
|
381
|
+
status: status,
|
|
382
|
+
data: data,
|
|
383
|
+
error: errorMessage
|
|
384
|
+
}
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
catch(error) {
|
|
390
|
+
console.error("Error:", error);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
#webrequest_timeout(action, default_timeout, min, max) {
|
|
395
|
+
let timeout = default_timeout;
|
|
396
|
+
if (!action.settings) {
|
|
397
|
+
return timeout;
|
|
398
|
+
}
|
|
399
|
+
// console.log("default timeout:", timeout);
|
|
400
|
+
// console.log("action.settings:", action.settings);
|
|
401
|
+
// console.log("action.settings.timeout:", action.settings.timeout);
|
|
402
|
+
// console.log("typeof action.settings.timeout:", typeof action.settings.timeout);
|
|
403
|
+
// console.log("action.settings.timeout > min", action.settings.timeout > min)
|
|
404
|
+
// console.log("action.settings.timeout < max", action.settings.timeout < max)
|
|
405
|
+
|
|
406
|
+
if (action.settings.timeout) {
|
|
407
|
+
if ((typeof action.settings.timeout === "number") && action.settings.timeout > min && action.settings.timeout < max) {
|
|
408
|
+
timeout = Math.round(action.settings.timeout)
|
|
409
|
+
// console.log("new timeout:", timeout);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
// console.log("returning timeout:", timeout);
|
|
413
|
+
return timeout
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
module.exports = { DirWebRequestV2 };
|
|
@@ -90,6 +90,7 @@ class DirWebResponse {
|
|
|
90
90
|
}
|
|
91
91
|
catch (err) {
|
|
92
92
|
winston.error("Error parsing webRequest jsonBody: " + JSON.stringify(jsonBody) + "\nError: " + JSON.stringify(err));
|
|
93
|
+
this.logger.error("[Web Response] Error parsing webRequest jsonBody ", jsonBody)
|
|
93
94
|
reject("Error parsing jsonBody");
|
|
94
95
|
}
|
|
95
96
|
}
|
|
@@ -58,7 +58,6 @@ 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
62
|
static FLOW_LOG = "flow_log";
|
|
64
63
|
static ADD_KB_CONTENT = "add_kb_content";
|
|
@@ -8,6 +8,8 @@ require('dotenv').config();
|
|
|
8
8
|
let axios = require('axios');
|
|
9
9
|
const winston = require('./winston');
|
|
10
10
|
|
|
11
|
+
const { CHANNEL_NAME } = require('./constants.js')
|
|
12
|
+
|
|
11
13
|
class TiledeskChatbotUtil {
|
|
12
14
|
|
|
13
15
|
static parseIntent(explicit_intent_name) {
|
|
@@ -333,6 +335,27 @@ class TiledeskChatbotUtil {
|
|
|
333
335
|
}
|
|
334
336
|
});
|
|
335
337
|
}
|
|
338
|
+
|
|
339
|
+
// "buttons": [
|
|
340
|
+
// {
|
|
341
|
+
// "type": "action",
|
|
342
|
+
// "value": "Button1", // obbligatorio sempre
|
|
343
|
+
// "action": "#bb347206-d639-4926-94c9-e94930623dce", // mandatory
|
|
344
|
+
// "show_echo": true, // lo inserisco sempre
|
|
345
|
+
// "alias": "button1 alias"
|
|
346
|
+
// },
|
|
347
|
+
// {
|
|
348
|
+
// "type": "text",
|
|
349
|
+
// "value": "Button2 text", // obbligatorio sempre
|
|
350
|
+
// "show_echo": true // lo inserisco sempre
|
|
351
|
+
// },
|
|
352
|
+
// {
|
|
353
|
+
// "type": "url",
|
|
354
|
+
// "value": "Button3 link", // obbligatorio sempre
|
|
355
|
+
// "link": "http://", // obbligatorio
|
|
356
|
+
// "show_echo": true // lo inserisco sempre
|
|
357
|
+
// }
|
|
358
|
+
// ]
|
|
336
359
|
}
|
|
337
360
|
catch(error) {
|
|
338
361
|
winston.warn("Invalid json_buttons:", error)
|
|
@@ -583,10 +606,8 @@ class TiledeskChatbotUtil {
|
|
|
583
606
|
}
|
|
584
607
|
let currentLeadName = await chatbot.getParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY);
|
|
585
608
|
winston.debug("(TiledeskChatbotUtil) You lead email from attributes: " + currentLeadEmail);
|
|
586
|
-
console.log("currentLeadName: ", currentLeadName)
|
|
587
609
|
if (message.request.lead.fullname && !currentLeadName) {
|
|
588
610
|
// worth saving
|
|
589
|
-
console.log("inside if")
|
|
590
611
|
winston.debug("(TiledeskChatbotUtil) worth saving email");
|
|
591
612
|
try {
|
|
592
613
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, message.request.lead.fullname);
|
|
@@ -599,7 +620,10 @@ class TiledeskChatbotUtil {
|
|
|
599
620
|
if (message.request.lead.phone) {
|
|
600
621
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_PHONE_KEY, message.request.lead.phone);
|
|
601
622
|
}
|
|
602
|
-
if (message.request.lead.lead_id && message.request.lead.lead_id.startsWith("wab-")
|
|
623
|
+
if (message.request.lead.lead_id && (message.request.lead.lead_id.startsWith("wab-") ||
|
|
624
|
+
message.request.lead.lead_id.startsWith("vxml-") ||
|
|
625
|
+
message.request.lead.lead_id.startsWith(CHANNEL_NAME.VOICE_TWILIO) ||
|
|
626
|
+
message.request.lead.lead_id.startsWith(CHANNEL_NAME.SMS))) {
|
|
603
627
|
const splits = message.request.lead.lead_id.split("-");
|
|
604
628
|
if (splits && splits.length > 1) {
|
|
605
629
|
await chatbot.addParameter(TiledeskChatbotConst.REQ_CURRENT_PHONE_NUMBER_KEY,splits[1]);
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
module.exports = {
|
|
3
|
+
CHANNEL_NAME: {
|
|
4
|
+
SUPPORT_GROUP: 'support-group',
|
|
5
|
+
GROUP: 'group',
|
|
6
|
+
DIRECT: 'direct',
|
|
7
|
+
CHAT21: 'chat21',
|
|
8
|
+
EMAIL: 'email',
|
|
9
|
+
FORM:'form',
|
|
10
|
+
MESSENGER: 'messenger',
|
|
11
|
+
WHATSAPP: 'whatsapp',
|
|
12
|
+
TELEGRAM: 'telegram',
|
|
13
|
+
VOICE: 'voice-vxml',
|
|
14
|
+
VOICE_TWILIO: 'voice-twilio',
|
|
15
|
+
SMS_TWILIO: 'sms-twilio',
|
|
16
|
+
}
|
|
17
|
+
}
|
package/services/LLMService.js
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
const httpUtils = require('../utils/HttpUtils');
|
|
2
|
-
const winston = require('../utils/winston');
|
|
3
|
-
const API_ENDPOINT = process.env.API_ENDPOINT;
|
|
4
|
-
|
|
5
|
-
class LLMService {
|
|
6
|
-
|
|
7
|
-
constructor() { }
|
|
8
|
-
|
|
9
|
-
async getNamespace(id_project, token, name, id) {
|
|
10
|
-
return new Promise((resolve) => {
|
|
11
|
-
const http_request = {
|
|
12
|
-
url: API_ENDPOINT + "/" + id_project + "/kb/namespace/all",
|
|
13
|
-
headers: {
|
|
14
|
-
'Content-Type': 'application/json',
|
|
15
|
-
'Authorization': 'JWT ' + token
|
|
16
|
-
},
|
|
17
|
-
method: "GET"
|
|
18
|
-
}
|
|
19
|
-
winston.debug("Kb HttpRequest", http_request);
|
|
20
|
-
|
|
21
|
-
httpUtils.request(
|
|
22
|
-
http_request, async (err, namespaces) => {
|
|
23
|
-
if (err) {
|
|
24
|
-
winston.error("Error getting namespaces:", err);
|
|
25
|
-
reject(err);
|
|
26
|
-
} else {
|
|
27
|
-
winston.debug("Get namespaces response:", namespaces);
|
|
28
|
-
if (!Array.isArray(namespaces)) {
|
|
29
|
-
reject(new Error('Invalid response format'));
|
|
30
|
-
return;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let namespace;
|
|
34
|
-
if (name) {
|
|
35
|
-
namespace = namespaces.find(n => n.name === name);
|
|
36
|
-
} else {
|
|
37
|
-
namespace = namespaces.find(n => n.id === id);
|
|
38
|
-
}
|
|
39
|
-
resolve(namespace || null);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
)
|
|
43
|
-
})
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
async getKeyFromKbSettings(id_project, token) {
|
|
47
|
-
|
|
48
|
-
return new Promise((resolve) => {
|
|
49
|
-
const http_request = {
|
|
50
|
-
url: API_ENDPOINT + "/" + id_project + "/kbsettings",
|
|
51
|
-
headers: {
|
|
52
|
-
'Content-Type': 'application/json',
|
|
53
|
-
'Authorization': 'JWT ' + token
|
|
54
|
-
},
|
|
55
|
-
method: "GET"
|
|
56
|
-
}
|
|
57
|
-
winston.debug("Kb HttpRequest", http_request);
|
|
58
|
-
|
|
59
|
-
httpUtils.request(
|
|
60
|
-
http_request, async (err, resbody) => {
|
|
61
|
-
if (err) {
|
|
62
|
-
winston.error("Error getting kb settings:", err?.response?.data);
|
|
63
|
-
resolve(null);
|
|
64
|
-
} else {
|
|
65
|
-
if (!resbody || !resbody.gptkey) {
|
|
66
|
-
resolve(null);
|
|
67
|
-
} else {
|
|
68
|
-
resolve(resbody.gptkey);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
)
|
|
73
|
-
})
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async addUnansweredQuestion(id_project, namespace, question, token) {
|
|
77
|
-
|
|
78
|
-
const json = { namespace, question };
|
|
79
|
-
|
|
80
|
-
return new Promise((resolve, reject) => {
|
|
81
|
-
const http_request = {
|
|
82
|
-
url: API_ENDPOINT + "/" + id_project + "/kb/unanswered/",
|
|
83
|
-
headers: {
|
|
84
|
-
'Content-Type': 'application/json',
|
|
85
|
-
'Authorization': 'JWT ' + token
|
|
86
|
-
},
|
|
87
|
-
method: "POST",
|
|
88
|
-
json: json
|
|
89
|
-
}
|
|
90
|
-
winston.debug("Kb HttpRequest", http_request);
|
|
91
|
-
|
|
92
|
-
httpUtils.request(http_request, (err, response) => {
|
|
93
|
-
if (err) {
|
|
94
|
-
winston.error("Error adding unanswered question:", err);
|
|
95
|
-
reject(err);
|
|
96
|
-
} else {
|
|
97
|
-
resolve(response);
|
|
98
|
-
}
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const llmService = new LLMService();
|
|
105
|
-
module.exports = llmService;
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
const httpUtils = require('../utils/HttpUtils');
|
|
2
|
-
const winston = require('../utils/winston');
|
|
3
|
-
const API_ENDPOINT = process.env.API_ENDPOINT;
|
|
4
|
-
|
|
5
|
-
class QuotasService {
|
|
6
|
-
|
|
7
|
-
constructor() { }
|
|
8
|
-
|
|
9
|
-
async checkQuoteAvailability(id_project, token) {
|
|
10
|
-
return new Promise((resolve) => {
|
|
11
|
-
|
|
12
|
-
const http_request = {
|
|
13
|
-
url: API_ENDPOINT + "/" + id_project + "/quotes/tokens",
|
|
14
|
-
headers: {
|
|
15
|
-
'Content-Type': 'application/json',
|
|
16
|
-
'Authorization': 'JWT ' + token
|
|
17
|
-
},
|
|
18
|
-
method: "GET"
|
|
19
|
-
}
|
|
20
|
-
winston.debug("QuotasService HttpRequest", http_request);
|
|
21
|
-
|
|
22
|
-
httpUtils.request(
|
|
23
|
-
http_request, async (err, resbody) => {
|
|
24
|
-
if (err) {
|
|
25
|
-
winston.error("Check quote availability err: ", err);
|
|
26
|
-
resolve(true)
|
|
27
|
-
} else {
|
|
28
|
-
if (resbody.isAvailable === true) {
|
|
29
|
-
resolve(true)
|
|
30
|
-
} else {
|
|
31
|
-
resolve(false)
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
)
|
|
36
|
-
})
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
async updateQuote(id_project, token, tokens_usage) {
|
|
40
|
-
return new Promise((resolve, reject) => {
|
|
41
|
-
|
|
42
|
-
const http_request = {
|
|
43
|
-
url: API_ENDPOINT + "/" + id_project + "/quotes/incr/tokens",
|
|
44
|
-
headers: {
|
|
45
|
-
'Content-Type': 'application/json',
|
|
46
|
-
'Authorization': 'JWT ' + token
|
|
47
|
-
},
|
|
48
|
-
json: tokens_usage,
|
|
49
|
-
method: "POST"
|
|
50
|
-
}
|
|
51
|
-
winston.debug("DirAskGPTV2 update quote HttpRequest ", http_request);
|
|
52
|
-
|
|
53
|
-
httpUtils.request(
|
|
54
|
-
http_request, async (err, resbody) => {
|
|
55
|
-
if (err) {
|
|
56
|
-
winston.error("Increment tokens quote err: ", err);
|
|
57
|
-
reject(false)
|
|
58
|
-
} else {
|
|
59
|
-
resolve(true);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
)
|
|
63
|
-
})
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const quotasService = new QuotasService();
|
|
69
|
-
module.exports = quotasService;
|