@tiledesk/tiledesk-tybot-connector 0.2.20 → 0.2.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 +6 -0
- package/index.js +11 -221
- package/models/MongodbBotsDataSource.js +18 -18
- package/models/TiledeskChatbotUtil.js +224 -0
- package/package.json +1 -1
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +5 -5
- /package/tiledeskChatbotPlugs/directives/{DirIfAvailableAgents.js → DEPRECATED_DirIfAvailableAgents.js} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,12 @@
|
|
|
5
5
|
available on:
|
|
6
6
|
▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
|
|
7
7
|
|
|
8
|
+
### v0.2.21
|
|
9
|
+
- log clean
|
|
10
|
+
- added log for production in IfOnlineAgent
|
|
11
|
+
- Deprecated IfAvailableAgents
|
|
12
|
+
- index.js refactored. Moved static utility methods in TiledeskChatbotUtil.js
|
|
13
|
+
|
|
8
14
|
### v0.2.20
|
|
9
15
|
- updateRequestVariables renamed in updateRequestAttributes
|
|
10
16
|
- Added TildeskChatbot instance method async getParameter(parameter_name)
|
package/index.js
CHANGED
|
@@ -7,12 +7,12 @@ const { ExtUtil } = require('./ExtUtil.js');
|
|
|
7
7
|
const { TdCache } = require('./TdCache.js');
|
|
8
8
|
const { TiledeskChatbot } = require('./models/TiledeskChatbot.js');
|
|
9
9
|
const { MongodbBotsDataSource } = require('./models/MongodbBotsDataSource.js');
|
|
10
|
-
const { MongodbIntentsMachine } = require('./models/MongodbIntentsMachine.js');
|
|
11
|
-
const { TiledeskIntentsMachine } = require('./models/TiledeskIntentsMachine.js');
|
|
10
|
+
// const { MongodbIntentsMachine } = require('./models/MongodbIntentsMachine.js');
|
|
11
|
+
// const { TiledeskIntentsMachine } = require('./models/TiledeskIntentsMachine.js');
|
|
12
12
|
const { MockBotsDataSource } = require('./models/MockBotsDataSource.js');
|
|
13
|
-
const { TiledeskChatbotConst } = require('./models/TiledeskChatbotConst');
|
|
13
|
+
// const { TiledeskChatbotConst } = require('./models/TiledeskChatbotConst');
|
|
14
14
|
const { IntentsMachineFactory } = require('./models/IntentsMachineFactory');
|
|
15
|
-
let parser = require('accept-language-parser');
|
|
15
|
+
// let parser = require('accept-language-parser');
|
|
16
16
|
|
|
17
17
|
router.use(bodyParser.json({limit: '50mb'}));
|
|
18
18
|
router.use(bodyParser.urlencoded({ extended: true , limit: '50mb'}));
|
|
@@ -26,7 +26,7 @@ const { DirectivesChatbotPlug } = require('./tiledeskChatbotPlugs/DirectivesChat
|
|
|
26
26
|
|
|
27
27
|
// THE IMPORT
|
|
28
28
|
let mongoose = require('mongoose');
|
|
29
|
-
const { Directives } = require('./tiledeskChatbotPlugs/directives/Directives.js');
|
|
29
|
+
// const { Directives } = require('./tiledeskChatbotPlugs/directives/Directives.js');
|
|
30
30
|
const { TiledeskChatbotUtil } = require('./models/TiledeskChatbotUtil.js'); //require('@tiledesk/tiledesk-chatbot-util');
|
|
31
31
|
let APIURL = null;
|
|
32
32
|
let staticBots;
|
|
@@ -93,7 +93,7 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
93
93
|
// bot = await botsDS.getBotById(botId);
|
|
94
94
|
// bot = await botById(botId, projectId, tdcache, botsDS);
|
|
95
95
|
bot = await botsDS.getBotByIdCache(botId, tdcache);
|
|
96
|
-
console.log("getBotByIdCache ---> bot: ", JSON.stringify(bot, null, 2))
|
|
96
|
+
// console.log("getBotByIdCache ---> bot: ", JSON.stringify(bot, null, 2))
|
|
97
97
|
}
|
|
98
98
|
catch(error) {
|
|
99
99
|
console.error("Error getting botId:", botId);
|
|
@@ -147,7 +147,7 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
147
147
|
projectId: projectId,
|
|
148
148
|
log: log
|
|
149
149
|
});
|
|
150
|
-
await updateRequestAttributes(chatbot, message, projectId, requestId);
|
|
150
|
+
await TiledeskChatbotUtil.updateRequestAttributes(chatbot, message, projectId, requestId);
|
|
151
151
|
await TiledeskChatbotUtil.updateConversationTranscript(chatbot, message);
|
|
152
152
|
|
|
153
153
|
let reply = await chatbot.replyToMessage(message);
|
|
@@ -162,12 +162,10 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
162
162
|
// process.exit(1)
|
|
163
163
|
// }
|
|
164
164
|
if (reply.actions && reply.actions.length > 0) { // structured actions (coming from chatbot designer)
|
|
165
|
-
if (log) {console.log("the actions:", JSON.stringify(reply.actions));}
|
|
166
|
-
let directives = actionsToDirectives(reply.actions);
|
|
167
|
-
if (log) {console.log("the directives:", JSON.stringify(directives));}
|
|
168
165
|
try {
|
|
169
|
-
|
|
170
|
-
|
|
166
|
+
if (log) {console.log("the actions:", JSON.stringify(reply.actions));}
|
|
167
|
+
let directives = TiledeskChatbotUtil.actionsToDirectives(reply.actions);
|
|
168
|
+
if (log) {console.log("the directives:", JSON.stringify(directives));}
|
|
171
169
|
let directivesPlug = new DirectivesChatbotPlug(
|
|
172
170
|
{
|
|
173
171
|
message: message,
|
|
@@ -188,7 +186,7 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
188
186
|
});
|
|
189
187
|
}
|
|
190
188
|
catch (error) {
|
|
191
|
-
console.error("
|
|
189
|
+
console.error("Error while processing actions:", error);
|
|
192
190
|
}
|
|
193
191
|
}
|
|
194
192
|
else { // text answer (parse text directives to get actions)
|
|
@@ -218,214 +216,6 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
218
216
|
|
|
219
217
|
});
|
|
220
218
|
|
|
221
|
-
async function updateRequestAttributes(chatbot, message, projectId, requestId) {
|
|
222
|
-
// update request context
|
|
223
|
-
if (chatbot.log) {console.log("Updating request variables. Message:", JSON.stringify(message));}
|
|
224
|
-
const messageId = message._id;
|
|
225
|
-
const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
|
|
226
|
-
// await chatbot.addParameter("chatbot", chatbot);
|
|
227
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url);
|
|
228
|
-
console.log("Adding proj_", projectId);
|
|
229
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
|
|
230
|
-
// TODO add projectName too
|
|
231
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
|
|
232
|
-
if (chatbot.bot) {
|
|
233
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
|
|
234
|
-
}
|
|
235
|
-
if (message.text && message.sender !== "_tdinternal") {
|
|
236
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text);
|
|
237
|
-
await chatbot.addParameter("lastUserMessageType", message.type);
|
|
238
|
-
await chatbot.addParameter("lastUserMessage", lastUserMessageFrom(message)); // JSON TYPE *NEW
|
|
239
|
-
// get image
|
|
240
|
-
if (message.type && message.type === "image" && message.metadata) {
|
|
241
|
-
// "text": "\nimage text",
|
|
242
|
-
// "id_project": "65203e12f8c0cf002cf4110b",
|
|
243
|
-
// "createdBy": "8ac52a30-133f-4ee1-8b4b-96055bb81757",
|
|
244
|
-
// "metadata": {
|
|
245
|
-
// "height": 905,
|
|
246
|
-
// "name": "tiledesk_Open graph_general.png",
|
|
247
|
-
// "src": "https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2Fda5bbc8d-5174-49a8-a041-3d9355242da5%2Ftiledesk_Open%20graph_general.png?alt=media&token=be82fecb-3cd1-45b9-a135-c2c57a932862",
|
|
248
|
-
// "type": "image/png",
|
|
249
|
-
// "uid": "lo68iyq5",
|
|
250
|
-
// "width": 1724
|
|
251
|
-
// }
|
|
252
|
-
if (message.metadata.src) {
|
|
253
|
-
await chatbot.addParameter("lastUserImageURL", message.metadata.src);
|
|
254
|
-
await chatbot.addParameter("lastUserImageName", message.metadata.name);
|
|
255
|
-
await chatbot.addParameter("lastUserImageWidth", message.metadata.width);
|
|
256
|
-
await chatbot.addParameter("lastUserImageHeight", message.metadata.height);
|
|
257
|
-
await chatbot.addParameter("lastUserImageType", message.metadata.type);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
else {
|
|
261
|
-
await chatbot.addParameter("lastUserImageURL", null);
|
|
262
|
-
await chatbot.addParameter("lastUserImageName", null);
|
|
263
|
-
await chatbot.addParameter("lastUserImageWidth", null);
|
|
264
|
-
await chatbot.addParameter("lastUserImageHeight", null);
|
|
265
|
-
await chatbot.addParameter("lastUserImageType", null);
|
|
266
|
-
}
|
|
267
|
-
// get document
|
|
268
|
-
if (message.type && message.type === "file" && message.metadata) {
|
|
269
|
-
// "type": "file",
|
|
270
|
-
// "text": "[LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf](https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2F502265ee-4f4a-47a4-9375-172bb0e6bf39%2FLIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf?alt=media&token=a09d065a-9b56-4507-8960-344cc294e4d1)\nistruzioni",
|
|
271
|
-
// "metadata": {
|
|
272
|
-
// "name": "LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf",
|
|
273
|
-
// "src": "https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2F502265ee-4f4a-47a4-9375-172bb0e6bf39%2FLIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf?alt=media&token=a09d065a-9b56-4507-8960-344cc294e4d1",
|
|
274
|
-
// "type": "application/pdf",
|
|
275
|
-
// "uid": "lo68oz8i"
|
|
276
|
-
// }
|
|
277
|
-
if (message.metadata.src) {
|
|
278
|
-
await chatbot.addParameter("lastUserDocumentURL", message.metadata.src);
|
|
279
|
-
await chatbot.addParameter("lastUserDocumentName", message.metadata.name);
|
|
280
|
-
await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
else {
|
|
284
|
-
await chatbot.addParameter("lastUserDocumentURL", null);
|
|
285
|
-
await chatbot.addParameter("lastUserDocumentName", null);
|
|
286
|
-
await chatbot.addParameter("lastUserDocumentType", null);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
|
|
292
|
-
if (message.request && message.request.location && message.request.location.country) {
|
|
293
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_COUNTRY_KEY, message.request.location.country);
|
|
294
|
-
}
|
|
295
|
-
if (message.request && message.request.location && message.request.location.city) {
|
|
296
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
|
|
297
|
-
}
|
|
298
|
-
// console.log("message.request.language", message.request["language"]);
|
|
299
|
-
if (message.request) {
|
|
300
|
-
let user_language = message.request["language"];
|
|
301
|
-
if (message.request["language"]) {
|
|
302
|
-
// console.log("HTTP language:", message.request["language"]);
|
|
303
|
-
var languages = parser.parse(message.request["language"]);
|
|
304
|
-
// console.log("languages:", languages);
|
|
305
|
-
if (languages && languages.length > 0 && languages[0].code) {
|
|
306
|
-
user_language = languages[0].code;
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY, message.request.sourcePage);
|
|
310
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY, user_language);
|
|
311
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_AGENT_KEY, message.request.userAgent);
|
|
312
|
-
}
|
|
313
|
-
// console.log("message.request.language", message.request["language"])
|
|
314
|
-
if (message.request && message.request.department) {
|
|
315
|
-
// It was an error when getting this from widget message's attributes
|
|
316
|
-
// await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
|
|
317
|
-
// await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
|
|
318
|
-
// get from request.department instead
|
|
319
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.request.department._id);
|
|
320
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.request.department.name);
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
if (projectId === "641864da99c1fb00131ba495") {
|
|
324
|
-
console.log("641864da99c1fb00131ba495 > for projectId:", JSON.stringify(message))
|
|
325
|
-
}
|
|
326
|
-
// for BUG
|
|
327
|
-
// if (chatbot.log) {console.log("message.request.attributes.payload", JSON.stringify(message.request.attributes.payload))}
|
|
328
|
-
if (message && message.request && message.request.attributes && message.request.attributes.payload) {
|
|
329
|
-
if (!message.attributes) {
|
|
330
|
-
message.attributes = {}
|
|
331
|
-
}
|
|
332
|
-
message.attributes.payload = message.request.attributes.payload
|
|
333
|
-
if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
|
|
334
|
-
// if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
if (message.attributes) {
|
|
338
|
-
if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
|
|
339
|
-
if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > Ok message.attributes", JSON.stringify(message.attributes));}
|
|
340
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
|
|
341
|
-
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
|
|
342
|
-
if (message.attributes.payload) {
|
|
343
|
-
try {
|
|
344
|
-
for (const [key, value] of Object.entries(message.attributes.payload)) {
|
|
345
|
-
// const value = all_parameters[key];
|
|
346
|
-
const value_type = typeof value;
|
|
347
|
-
//if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
|
|
348
|
-
//await chatbot.addParameter(key, String(value));
|
|
349
|
-
await chatbot.addParameter(key, value);
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
catch(err) {
|
|
353
|
-
console.error("Error importing message payload in request variables:", err);
|
|
354
|
-
}
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
if (chatbot.log) {
|
|
358
|
-
// console.log("tdcache:", chatbot.tdcache);
|
|
359
|
-
console.log("requestId:", requestId);
|
|
360
|
-
console.log("KEY:", TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
|
|
361
|
-
let proj_ = await TiledeskChatbot.getParameterStatic(chatbot.tdcache, requestId, TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
|
|
362
|
-
console.log("request parameter proj_:", proj_);
|
|
363
|
-
const all_parameters = await TiledeskChatbot.allParametersStatic(chatbot.tdcache, requestId);
|
|
364
|
-
for (const [key, value] of Object.entries(all_parameters)) {
|
|
365
|
-
// const value = all_parameters[key];
|
|
366
|
-
const value_type = typeof value;
|
|
367
|
-
if (chatbot.log) {console.log("request parameter:", key, "value:", value, "type:", value_type)}
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
// message["attributes"]: {
|
|
371
|
-
// "departmentId": "63c980054f857c00350535bc",
|
|
372
|
-
// "departmentName": "Default Department",
|
|
373
|
-
// "client": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
|
|
374
|
-
// "sourcePage": "https://tiledesk-html-site.tiledesk.repl.co/custom-attributes.html",
|
|
375
|
-
// "projectId": "63c980054f857c00350535b8",
|
|
376
|
-
// "payload": {
|
|
377
|
-
// "user_country": "Italy",
|
|
378
|
-
// "user_code": "E001"
|
|
379
|
-
// },
|
|
380
|
-
// "userFullname": "guest#7216 ",
|
|
381
|
-
// "requester_id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
382
|
-
// "ipAddress": "79.8.190.172",
|
|
383
|
-
// "sourceTitle": "Custom attributes",
|
|
384
|
-
// "widgetVer": "v.5.0.53-rc.4",
|
|
385
|
-
// "subtype": "info",
|
|
386
|
-
// "decoded_jwt": {
|
|
387
|
-
// "_id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
388
|
-
// "firstname": "guest#7216",
|
|
389
|
-
// "id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
390
|
-
// "fullName": "guest#7216 ",
|
|
391
|
-
// "iat": 1674201892,
|
|
392
|
-
// "aud": "https://tiledesk.com",
|
|
393
|
-
// "iss": "https://tiledesk.com",
|
|
394
|
-
// "sub": "guest",
|
|
395
|
-
// "jti": "f053af3d-14ca-411b-9903-78bd74e24218"
|
|
396
|
-
// }
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
function lastUserMessageFrom(msg) {
|
|
400
|
-
let message = {};
|
|
401
|
-
message["senderFullname"] = msg["senderFullname"]; // ex. "Bot"
|
|
402
|
-
message["type"] = msg["type"]; // ex. "text",
|
|
403
|
-
message["channel_type"] = msg["channel_type"]; // ex. "group",
|
|
404
|
-
message["status"] = msg["status"]; // ex. 0,
|
|
405
|
-
message["id"] = msg["_id"]; // ex. "6538cda46cb4d8002cf2317a",
|
|
406
|
-
message["sender"] = msg["sender"]; // ex. "system",
|
|
407
|
-
message["recipient"] = msg["recipient"]; // ex. "support-group-65203e12f8c0cf002cf4110b-4066a69c8b464646a3ff25f9f41575bb",
|
|
408
|
-
message["text"] = msg["text"]; // ex. "\\start",
|
|
409
|
-
message["createdBy"] = msg["createdBy"]; // ex. "system",
|
|
410
|
-
message["attributes"] = msg["attributes"]; // ex. { "subtype": "info" }
|
|
411
|
-
message["metadata"] = msg["metadata"];
|
|
412
|
-
message["channel"] = msg["channel"]; // ex. { "name": "chat21" }
|
|
413
|
-
return message;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
function actionsToDirectives(actions) {
|
|
417
|
-
let directives = [];
|
|
418
|
-
if (actions && actions.length > 0) {
|
|
419
|
-
actions.forEach(action => {
|
|
420
|
-
let directive = Directives.actionToDirective(action);
|
|
421
|
-
if (directive) {
|
|
422
|
-
directives.push(directive);
|
|
423
|
-
}
|
|
424
|
-
});
|
|
425
|
-
}
|
|
426
|
-
return directives;
|
|
427
|
-
}
|
|
428
|
-
|
|
429
219
|
router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) => {
|
|
430
220
|
res.json({success:true});
|
|
431
221
|
const projectId = req.params.projectId;
|
|
@@ -22,33 +22,33 @@ class MongodbBotsDataSource {
|
|
|
22
22
|
async getBotByIdCache(botId, tdcache) {
|
|
23
23
|
let bot = null;
|
|
24
24
|
if (tdcache) {
|
|
25
|
-
console.log("getBotByIdCache cache ok");
|
|
25
|
+
// console.log("getBotByIdCache cache ok");
|
|
26
26
|
let botCacheKey = "cacheman:cachegoose-cache:faq_kbs:id:" + botId;
|
|
27
27
|
try {
|
|
28
28
|
let _bot_as_string = await tdcache.get(botCacheKey);
|
|
29
29
|
const value_type = typeof _bot_as_string;
|
|
30
|
-
console.log("__bot_as_string found in chache:", _bot_as_string);
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
30
|
+
// console.log("__bot_as_string found in chache:", _bot_as_string);
|
|
31
|
+
if (this.log) {
|
|
32
|
+
console.log("__bot_as_string found in chache:", _bot_as_string);
|
|
33
|
+
console.log("value_type:", value_type);
|
|
34
|
+
}
|
|
35
35
|
if (_bot_as_string) {
|
|
36
36
|
bot = JSON.parse(_bot_as_string);
|
|
37
|
-
console.log("got bot from cache:", JSON.stringify(bot));
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
37
|
+
// console.log("got bot from cache:", JSON.stringify(bot));
|
|
38
|
+
if (this.log) {
|
|
39
|
+
console.log("got bot from cache:", JSON.stringify(bot));
|
|
40
|
+
}
|
|
41
41
|
}
|
|
42
42
|
else {
|
|
43
|
-
console.log("bot not found, getting from datasource...");
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
43
|
+
// console.log("bot not found, getting from datasource...");
|
|
44
|
+
if (this.log) {
|
|
45
|
+
console.log("bot not found, getting from datasource...");
|
|
46
|
+
}
|
|
47
47
|
bot = await this.getBotById(botId);
|
|
48
|
-
console.log("bot found in datasource:", JSON.stringify(bot));
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
48
|
+
// console.log("bot found in datasource:", JSON.stringify(bot));
|
|
49
|
+
if (this.log) {
|
|
50
|
+
console.log("bot found in datasource:", JSON.stringify(bot));
|
|
51
|
+
}
|
|
52
52
|
await tdcache.set(botCacheKey, JSON.stringify(bot));
|
|
53
53
|
// DEBUG CODE REMOVE
|
|
54
54
|
// let bot_ = await tdcache.get(botCacheKey);
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
const { TiledeskExpression } = require('../TiledeskExpression');
|
|
2
2
|
const { Filler } = require('../tiledeskChatbotPlugs/Filler');
|
|
3
|
+
const { TiledeskChatbotConst } = require('./TiledeskChatbotConst');
|
|
4
|
+
const { TiledeskChatbot } = require('./TiledeskChatbot.js');
|
|
5
|
+
let parser = require('accept-language-parser');
|
|
6
|
+
const { Directives } = require('../tiledeskChatbotPlugs/directives/Directives.js');
|
|
3
7
|
|
|
4
8
|
class TiledeskChatbotUtil {
|
|
5
9
|
|
|
@@ -298,6 +302,226 @@ class TiledeskChatbotUtil {
|
|
|
298
302
|
return false;
|
|
299
303
|
}
|
|
300
304
|
|
|
305
|
+
static lastUserMessageFrom(msg) {
|
|
306
|
+
let message = {};
|
|
307
|
+
message["senderFullname"] = msg["senderFullname"]; // ex. "Bot"
|
|
308
|
+
message["type"] = msg["type"]; // ex. "text",
|
|
309
|
+
message["channel_type"] = msg["channel_type"]; // ex. "group",
|
|
310
|
+
message["status"] = msg["status"]; // ex. 0,
|
|
311
|
+
message["id"] = msg["_id"]; // ex. "6538cda46cb4d8002cf2317a",
|
|
312
|
+
message["sender"] = msg["sender"]; // ex. "system",
|
|
313
|
+
message["recipient"] = msg["recipient"]; // ex. "support-group-65203e12f8c0cf002cf4110b-4066a69c8b464646a3ff25f9f41575bb",
|
|
314
|
+
message["text"] = msg["text"]; // ex. "\\start",
|
|
315
|
+
message["createdBy"] = msg["createdBy"]; // ex. "system",
|
|
316
|
+
message["attributes"] = msg["attributes"]; // ex. { "subtype": "info" }
|
|
317
|
+
message["metadata"] = msg["metadata"];
|
|
318
|
+
message["channel"] = msg["channel"]; // ex. { "name": "chat21" }
|
|
319
|
+
return message;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
static async updateRequestAttributes(chatbot, message, projectId, requestId) {
|
|
323
|
+
// update request context
|
|
324
|
+
try {
|
|
325
|
+
if (chatbot.log) {console.log("Updating request variables. Message:", JSON.stringify(message));}
|
|
326
|
+
const messageId = message._id;
|
|
327
|
+
const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
|
|
328
|
+
// await chatbot.addParameter("chatbot", chatbot);
|
|
329
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url);
|
|
330
|
+
console.log("Adding proj_", projectId);
|
|
331
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
|
|
332
|
+
// TODO add projectName too
|
|
333
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
|
|
334
|
+
if (chatbot.bot) {
|
|
335
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
if (message.text && message.sender !== "_tdinternal") {
|
|
339
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text); // DEPRECATED
|
|
340
|
+
await chatbot.addParameter("lastUserText", message.text);
|
|
341
|
+
|
|
342
|
+
await chatbot.addParameter("lastUserMessageType", message.type);
|
|
343
|
+
await chatbot.addParameter("lastUserMessage", TiledeskChatbotUtil.lastUserMessageFrom(message)); // JSON TYPE *NEW
|
|
344
|
+
// get image
|
|
345
|
+
if (message.type && message.type === "image" && message.metadata) {
|
|
346
|
+
// "text": "\nimage text",
|
|
347
|
+
// "id_project": "65203e12f8c0cf002cf4110b",
|
|
348
|
+
// "createdBy": "8ac52a30-133f-4ee1-8b4b-96055bb81757",
|
|
349
|
+
// "metadata": {
|
|
350
|
+
// "height": 905,
|
|
351
|
+
// "name": "tiledesk_Open graph_general.png",
|
|
352
|
+
// "src": "https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2Fda5bbc8d-5174-49a8-a041-3d9355242da5%2Ftiledesk_Open%20graph_general.png?alt=media&token=be82fecb-3cd1-45b9-a135-c2c57a932862",
|
|
353
|
+
// "type": "image/png",
|
|
354
|
+
// "uid": "lo68iyq5",
|
|
355
|
+
// "width": 1724
|
|
356
|
+
// }
|
|
357
|
+
if (message.metadata.src) {
|
|
358
|
+
await chatbot.addParameter("lastUserImageURL", message.metadata.src);
|
|
359
|
+
await chatbot.addParameter("lastUserImageName", message.metadata.name);
|
|
360
|
+
await chatbot.addParameter("lastUserImageWidth", message.metadata.width);
|
|
361
|
+
await chatbot.addParameter("lastUserImageHeight", message.metadata.height);
|
|
362
|
+
await chatbot.addParameter("lastUserImageType", message.metadata.type);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
else {
|
|
366
|
+
await chatbot.addParameter("lastUserImageURL", null);
|
|
367
|
+
await chatbot.addParameter("lastUserImageName", null);
|
|
368
|
+
await chatbot.addParameter("lastUserImageWidth", null);
|
|
369
|
+
await chatbot.addParameter("lastUserImageHeight", null);
|
|
370
|
+
await chatbot.addParameter("lastUserImageType", null);
|
|
371
|
+
}
|
|
372
|
+
// get document
|
|
373
|
+
if (message.type && message.type === "file" && message.metadata) {
|
|
374
|
+
// "type": "file",
|
|
375
|
+
// "text": "[LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf](https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2F502265ee-4f4a-47a4-9375-172bb0e6bf39%2FLIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf?alt=media&token=a09d065a-9b56-4507-8960-344cc294e4d1)\nistruzioni",
|
|
376
|
+
// "metadata": {
|
|
377
|
+
// "name": "LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf",
|
|
378
|
+
// "src": "https://firebasestorage.googleapis.com/v0/b/chat21-pre-01.appspot.com/o/public%2Fimages%2F8ac52a30-133f-4ee1-8b4b-96055bb81757%2F502265ee-4f4a-47a4-9375-172bb0e6bf39%2FLIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf?alt=media&token=a09d065a-9b56-4507-8960-344cc294e4d1",
|
|
379
|
+
// "type": "application/pdf",
|
|
380
|
+
// "uid": "lo68oz8i"
|
|
381
|
+
// }
|
|
382
|
+
if (message.metadata.src) {
|
|
383
|
+
await chatbot.addParameter("lastUserDocumentURL", message.metadata.src);
|
|
384
|
+
await chatbot.addParameter("lastUserDocumentName", message.metadata.name);
|
|
385
|
+
await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
await chatbot.addParameter("lastUserDocumentURL", null);
|
|
390
|
+
await chatbot.addParameter("lastUserDocumentName", null);
|
|
391
|
+
await chatbot.addParameter("lastUserDocumentType", null);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
|
|
397
|
+
if (message.request && message.request.location && message.request.location.country) {
|
|
398
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_COUNTRY_KEY, message.request.location.country);
|
|
399
|
+
}
|
|
400
|
+
if (message.request && message.request.location && message.request.location.city) {
|
|
401
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
|
|
402
|
+
}
|
|
403
|
+
// console.log("message.request.language", message.request["language"]);
|
|
404
|
+
if (message.request) {
|
|
405
|
+
let user_language = message.request["language"];
|
|
406
|
+
if (message.request["language"]) {
|
|
407
|
+
// console.log("HTTP language:", message.request["language"]);
|
|
408
|
+
var languages = parser.parse(message.request["language"]);
|
|
409
|
+
// console.log("languages:", languages);
|
|
410
|
+
if (languages && languages.length > 0 && languages[0].code) {
|
|
411
|
+
user_language = languages[0].code;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY, message.request.sourcePage);
|
|
415
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY, user_language);
|
|
416
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_AGENT_KEY, message.request.userAgent);
|
|
417
|
+
}
|
|
418
|
+
// console.log("message.request.language", message.request["language"])
|
|
419
|
+
if (message.request && message.request.department) {
|
|
420
|
+
// It was an error when getting this from widget message's attributes
|
|
421
|
+
// await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
|
|
422
|
+
// await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
|
|
423
|
+
// get from request.department instead
|
|
424
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.request.department._id);
|
|
425
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.request.department.name);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (projectId === "641864da99c1fb00131ba495") {
|
|
429
|
+
console.log("641864da99c1fb00131ba495 > for projectId:", JSON.stringify(message))
|
|
430
|
+
}
|
|
431
|
+
// for BUG
|
|
432
|
+
// if (chatbot.log) {console.log("message.request.attributes.payload", JSON.stringify(message.request.attributes.payload))}
|
|
433
|
+
if (message && message.request && message.request.attributes && message.request.attributes.payload) {
|
|
434
|
+
if (!message.attributes) {
|
|
435
|
+
message.attributes = {}
|
|
436
|
+
}
|
|
437
|
+
message.attributes.payload = message.request.attributes.payload
|
|
438
|
+
if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
|
|
439
|
+
// if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
if (message.attributes) {
|
|
443
|
+
if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
|
|
444
|
+
if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > Ok message.attributes", JSON.stringify(message.attributes));}
|
|
445
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
|
|
446
|
+
await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
|
|
447
|
+
if (message.attributes.payload) {
|
|
448
|
+
try {
|
|
449
|
+
for (const [key, value] of Object.entries(message.attributes.payload)) {
|
|
450
|
+
// const value = all_parameters[key];
|
|
451
|
+
const value_type = typeof value;
|
|
452
|
+
//if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
|
|
453
|
+
//await chatbot.addParameter(key, String(value));
|
|
454
|
+
await chatbot.addParameter(key, value);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
catch(err) {
|
|
458
|
+
console.error("Error importing message payload in request variables:", err);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
|
|
465
|
+
if (chatbot.log) {
|
|
466
|
+
// console.log("tdcache:", chatbot.tdcache);
|
|
467
|
+
console.log("requestId:", requestId);
|
|
468
|
+
console.log("KEY:", TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
|
|
469
|
+
console.log("TiledeskChatbot:", TiledeskChatbot);
|
|
470
|
+
let proj_ = await chatbot.getParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
|
|
471
|
+
console.log("request parameter proj_:", proj_);
|
|
472
|
+
const all_parameters = await chatbot.allParameters();
|
|
473
|
+
for (const [key, value] of Object.entries(all_parameters)) {
|
|
474
|
+
// const value = all_parameters[key];
|
|
475
|
+
const value_type = typeof value;
|
|
476
|
+
if (chatbot.log) {console.log("request parameter:", key, "value:", value, "type:", value_type)}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
} catch(error) {
|
|
480
|
+
console.error("Error", error)
|
|
481
|
+
process.exit(1)
|
|
482
|
+
}
|
|
483
|
+
// message["attributes"]: {
|
|
484
|
+
// "departmentId": "63c980054f857c00350535bc",
|
|
485
|
+
// "departmentName": "Default Department",
|
|
486
|
+
// "client": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
|
|
487
|
+
// "sourcePage": "https://tiledesk-html-site.tiledesk.repl.co/custom-attributes.html",
|
|
488
|
+
// "projectId": "63c980054f857c00350535b8",
|
|
489
|
+
// "payload": {
|
|
490
|
+
// "user_country": "Italy",
|
|
491
|
+
// "user_code": "E001"
|
|
492
|
+
// },
|
|
493
|
+
// "userFullname": "guest#7216 ",
|
|
494
|
+
// "requester_id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
495
|
+
// "ipAddress": "79.8.190.172",
|
|
496
|
+
// "sourceTitle": "Custom attributes",
|
|
497
|
+
// "widgetVer": "v.5.0.53-rc.4",
|
|
498
|
+
// "subtype": "info",
|
|
499
|
+
// "decoded_jwt": {
|
|
500
|
+
// "_id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
501
|
+
// "firstname": "guest#7216",
|
|
502
|
+
// "id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
|
|
503
|
+
// "fullName": "guest#7216 ",
|
|
504
|
+
// "iat": 1674201892,
|
|
505
|
+
// "aud": "https://tiledesk.com",
|
|
506
|
+
// "iss": "https://tiledesk.com",
|
|
507
|
+
// "sub": "guest",
|
|
508
|
+
// "jti": "f053af3d-14ca-411b-9903-78bd74e24218"
|
|
509
|
+
// }
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
static actionsToDirectives(actions) {
|
|
513
|
+
let directives = [];
|
|
514
|
+
if (actions && actions.length > 0) {
|
|
515
|
+
actions.forEach(action => {
|
|
516
|
+
let directive = Directives.actionToDirective(action);
|
|
517
|
+
if (directive) {
|
|
518
|
+
directives.push(directive);
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
}
|
|
522
|
+
return directives;
|
|
523
|
+
}
|
|
524
|
+
|
|
301
525
|
}
|
|
302
526
|
|
|
303
527
|
module.exports = { TiledeskChatbotUtil };
|
package/package.json
CHANGED
|
@@ -212,21 +212,21 @@ class DirectivesChatbotPlug {
|
|
|
212
212
|
directive_name = directive.name.toLowerCase();
|
|
213
213
|
}
|
|
214
214
|
if (directive && directive.action) {
|
|
215
|
-
console.log("Checking locks", JSON.stringify(directive));
|
|
215
|
+
// console.log("Checking locks", JSON.stringify(directive));
|
|
216
216
|
// try {
|
|
217
217
|
const action_id = directive.action["_tdActionId"];
|
|
218
|
-
console.log("Checking locked directive:", action_id, "for request:", this.supportRequest.request_id);
|
|
218
|
+
// console.log("Checking locked directive:", action_id, "for request:", this.supportRequest.request_id);
|
|
219
219
|
const locked_action_id = await this.chatbot.currentLockedAction(this.supportRequest.request_id);
|
|
220
|
-
console.log("locked_action_id:", locked_action_id);
|
|
220
|
+
// console.log("locked_action_id:", locked_action_id);
|
|
221
221
|
if ( locked_action_id && (locked_action_id !== action_id) ) {
|
|
222
|
-
console.log("Found locked action:", locked_action_id, "Skipping this action:", action_id);
|
|
222
|
+
// console.log("Found locked action:", locked_action_id, "Skipping this action:", action_id);
|
|
223
223
|
let next_dir = await this.nextDirective(this.directives);
|
|
224
224
|
this.process(next_dir);
|
|
225
225
|
return;
|
|
226
226
|
}
|
|
227
227
|
else {
|
|
228
228
|
// go on
|
|
229
|
-
console.log("Going on to next directive...");
|
|
229
|
+
// console.log("Going on to next directive...");
|
|
230
230
|
}
|
|
231
231
|
// }
|
|
232
232
|
// catch(error) {
|
|
File without changes
|