@tiledesk/tiledesk-tybot-connector 0.2.19 → 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 CHANGED
@@ -5,6 +5,17 @@
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
+
14
+ ### v0.2.20
15
+ - updateRequestVariables renamed in updateRequestAttributes
16
+ - Added TildeskChatbot instance method async getParameter(parameter_name)
17
+ - Added "transcript" attribute
18
+
8
19
  ### v0.2.19
9
20
  - Added native attribute lastUserMessageType
10
21
  - Added integration with external intents decode engine
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,8 @@ 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
+ const { TiledeskChatbotUtil } = require('./models/TiledeskChatbotUtil.js'); //require('@tiledesk/tiledesk-chatbot-util');
30
31
  let APIURL = null;
31
32
  let staticBots;
32
33
 
@@ -92,7 +93,7 @@ router.post('/ext/:botid', async (req, res) => {
92
93
  // bot = await botsDS.getBotById(botId);
93
94
  // bot = await botById(botId, projectId, tdcache, botsDS);
94
95
  bot = await botsDS.getBotByIdCache(botId, tdcache);
95
- console.log("getBotByIdCache ---> bot: ", JSON.stringify(bot, null, 2))
96
+ // console.log("getBotByIdCache ---> bot: ", JSON.stringify(bot, null, 2))
96
97
  }
97
98
  catch(error) {
98
99
  console.error("Error getting botId:", botId);
@@ -146,7 +147,8 @@ router.post('/ext/:botid', async (req, res) => {
146
147
  projectId: projectId,
147
148
  log: log
148
149
  });
149
- await updateRequestVariables(chatbot, message, projectId, requestId);
150
+ await TiledeskChatbotUtil.updateRequestAttributes(chatbot, message, projectId, requestId);
151
+ await TiledeskChatbotUtil.updateConversationTranscript(chatbot, message);
150
152
 
151
153
  let reply = await chatbot.replyToMessage(message);
152
154
  if (!reply) {
@@ -160,12 +162,10 @@ router.post('/ext/:botid', async (req, res) => {
160
162
  // process.exit(1)
161
163
  // }
162
164
  if (reply.actions && reply.actions.length > 0) { // structured actions (coming from chatbot designer)
163
- if (log) {console.log("the actions:", JSON.stringify(reply.actions));}
164
- let directives = actionsToDirectives(reply.actions);
165
- if (log) {console.log("the directives:", JSON.stringify(directives));}
166
165
  try {
167
-
168
-
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));}
169
169
  let directivesPlug = new DirectivesChatbotPlug(
170
170
  {
171
171
  message: message,
@@ -186,7 +186,7 @@ router.post('/ext/:botid', async (req, res) => {
186
186
  });
187
187
  }
188
188
  catch (error) {
189
- console.error("an error:", error);
189
+ console.error("Error while processing actions:", error);
190
190
  }
191
191
  }
192
192
  else { // text answer (parse text directives to get actions)
@@ -216,243 +216,6 @@ router.post('/ext/:botid', async (req, res) => {
216
216
 
217
217
  });
218
218
 
219
- // async function botById(botId, projectId, tdcache, botsDS) {
220
- // let bot = null;
221
- // // let botCacheKey = "cacheman:cachegoose-cache:" + projectId + ":faq_kbs:id:" + botId;
222
- // let botCacheKey = "cacheman:cachegoose-cache:faq_kbs:id:" + botId;
223
- // try {
224
- // let _bot_as_string = await tdcache.get(botCacheKey);
225
- // const value_type = typeof _bot_as_string;
226
- // console.log("_bot_as_string found in chache:", _bot_as_string);
227
- // console.log("value_type:", value_type);
228
- // if (_bot_as_string) {
229
- // bot = JSON.parse(_bot_as_string);
230
- // console.log("got bot from cache:", JSON.stringify(bot));
231
- // }
232
- // else {
233
- // console.log("bot not found, getting from datasource...");
234
- // bot = await botsDS.getBotById(botId);
235
- // console.log("bot found in datasource:", JSON.stringify(bot));
236
- // await tdcache.set(botCacheKey, JSON.stringify(bot));
237
- // // DEBUG CODE REMOVE
238
- // let bot_ = await tdcache.get(botCacheKey);
239
- // console.log("_bot_as_string from cache debug:", bot_)
240
- // }
241
- // }
242
- // catch(err) {
243
- // console.error("error getting bot by id:", err);
244
- // }
245
- // return bot;
246
- // }
247
-
248
- async function updateRequestVariables(chatbot, message, projectId, requestId) {
249
- // update request context
250
- if (chatbot.log) {console.log("Updating request variables. Message:", JSON.stringify(message));}
251
- const messageId = message._id;
252
- const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
253
- // await chatbot.addParameter("chatbot", chatbot);
254
- await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url);
255
- console.log("Adding proj_", projectId);
256
- await chatbot.addParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
257
- // TODO add projectName too
258
- await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
259
- if (chatbot.bot) {
260
- await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
261
- }
262
- if (message.text && message.sender !== "_tdinternal") {
263
- await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text);
264
- await chatbot.addParameter("lastUserMessageType", message.type);
265
- await chatbot.addParameter("lastUserMessage", lastUserMessageFrom(message)); // JSON TYPE *NEW
266
- // get image
267
- if (message.type && message.type === "image" && message.metadata) {
268
- // "text": "\nimage text",
269
- // "id_project": "65203e12f8c0cf002cf4110b",
270
- // "createdBy": "8ac52a30-133f-4ee1-8b4b-96055bb81757",
271
- // "metadata": {
272
- // "height": 905,
273
- // "name": "tiledesk_Open graph_general.png",
274
- // "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",
275
- // "type": "image/png",
276
- // "uid": "lo68iyq5",
277
- // "width": 1724
278
- // }
279
- if (message.metadata.src) {
280
- await chatbot.addParameter("lastUserImageURL", message.metadata.src);
281
- await chatbot.addParameter("lastUserImageName", message.metadata.name);
282
- await chatbot.addParameter("lastUserImageWidth", message.metadata.width);
283
- await chatbot.addParameter("lastUserImageHeight", message.metadata.height);
284
- await chatbot.addParameter("lastUserImageType", message.metadata.type);
285
- }
286
- }
287
- else {
288
- await chatbot.addParameter("lastUserImageURL", null);
289
- await chatbot.addParameter("lastUserImageName", null);
290
- await chatbot.addParameter("lastUserImageWidth", null);
291
- await chatbot.addParameter("lastUserImageHeight", null);
292
- await chatbot.addParameter("lastUserImageType", null);
293
- }
294
- // get document
295
- if (message.type && message.type === "file" && message.metadata) {
296
- // "type": "file",
297
- // "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",
298
- // "metadata": {
299
- // "name": "LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf",
300
- // "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",
301
- // "type": "application/pdf",
302
- // "uid": "lo68oz8i"
303
- // }
304
- if (message.metadata.src) {
305
- await chatbot.addParameter("lastUserDocumentURL", message.metadata.src);
306
- await chatbot.addParameter("lastUserDocumentName", message.metadata.name);
307
- await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
308
- }
309
- }
310
- else {
311
- await chatbot.addParameter("lastUserDocumentURL", null);
312
- await chatbot.addParameter("lastUserDocumentName", null);
313
- await chatbot.addParameter("lastUserDocumentType", null);
314
- }
315
- }
316
-
317
-
318
- await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
319
- if (message.request && message.request.location && message.request.location.country) {
320
- await chatbot.addParameter(TiledeskChatbotConst.REQ_COUNTRY_KEY, message.request.location.country);
321
- }
322
- if (message.request && message.request.location && message.request.location.city) {
323
- await chatbot.addParameter(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
324
- }
325
- // console.log("message.request.language", message.request["language"]);
326
- if (message.request) {
327
- let user_language = message.request["language"];
328
- if (message.request["language"]) {
329
- // console.log("HTTP language:", message.request["language"]);
330
- var languages = parser.parse(message.request["language"]);
331
- // console.log("languages:", languages);
332
- if (languages && languages.length > 0 && languages[0].code) {
333
- user_language = languages[0].code;
334
- }
335
- }
336
- await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY, message.request.sourcePage);
337
- await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY, user_language);
338
- await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_AGENT_KEY, message.request.userAgent);
339
- }
340
- // console.log("message.request.language", message.request["language"])
341
- if (message.request && message.request.department) {
342
- // It was an error when getting this from widget message's attributes
343
- // await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
344
- // await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
345
- // get from request.department instead
346
- await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.request.department._id);
347
- await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.request.department.name);
348
- }
349
-
350
- if (projectId === "641864da99c1fb00131ba495") {
351
- console.log("641864da99c1fb00131ba495 > for projectId:", JSON.stringify(message))
352
- }
353
- // for BUG
354
- // if (chatbot.log) {console.log("message.request.attributes.payload", JSON.stringify(message.request.attributes.payload))}
355
- if (message && message.request && message.request.attributes && message.request.attributes.payload) {
356
- if (!message.attributes) {
357
- message.attributes = {}
358
- }
359
- message.attributes.payload = message.request.attributes.payload
360
- if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
361
- // if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
362
- }
363
-
364
- if (message.attributes) {
365
- if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
366
- if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > Ok message.attributes", JSON.stringify(message.attributes));}
367
- await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
368
- await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
369
- if (message.attributes.payload) {
370
- try {
371
- for (const [key, value] of Object.entries(message.attributes.payload)) {
372
- // const value = all_parameters[key];
373
- const value_type = typeof value;
374
- //if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
375
- //await chatbot.addParameter(key, String(value));
376
- await chatbot.addParameter(key, value);
377
- }
378
- }
379
- catch(err) {
380
- console.error("Error importing message payload in request variables:", err);
381
- }
382
- }
383
- }
384
- if (chatbot.log) {
385
- // console.log("tdcache:", chatbot.tdcache);
386
- console.log("requestId:", requestId);
387
- console.log("KEY:", TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
388
- let proj_ = await TiledeskChatbot.getParameterStatic(chatbot.tdcache, requestId, TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
389
- console.log("request parameter proj_:", proj_);
390
- const all_parameters = await TiledeskChatbot.allParametersStatic(chatbot.tdcache, requestId);
391
- for (const [key, value] of Object.entries(all_parameters)) {
392
- // const value = all_parameters[key];
393
- const value_type = typeof value;
394
- if (chatbot.log) {console.log("request parameter:", key, "value:", value, "type:", value_type)}
395
- }
396
- }
397
- // message["attributes"]: {
398
- // "departmentId": "63c980054f857c00350535bc",
399
- // "departmentName": "Default Department",
400
- // "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",
401
- // "sourcePage": "https://tiledesk-html-site.tiledesk.repl.co/custom-attributes.html",
402
- // "projectId": "63c980054f857c00350535b8",
403
- // "payload": {
404
- // "user_country": "Italy",
405
- // "user_code": "E001"
406
- // },
407
- // "userFullname": "guest#7216 ",
408
- // "requester_id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
409
- // "ipAddress": "79.8.190.172",
410
- // "sourceTitle": "Custom attributes",
411
- // "widgetVer": "v.5.0.53-rc.4",
412
- // "subtype": "info",
413
- // "decoded_jwt": {
414
- // "_id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
415
- // "firstname": "guest#7216",
416
- // "id": "7216926a-84c3-4bd5-aa79-8bd763094dc0",
417
- // "fullName": "guest#7216 ",
418
- // "iat": 1674201892,
419
- // "aud": "https://tiledesk.com",
420
- // "iss": "https://tiledesk.com",
421
- // "sub": "guest",
422
- // "jti": "f053af3d-14ca-411b-9903-78bd74e24218"
423
- // }
424
- }
425
-
426
- function lastUserMessageFrom(msg) {
427
- let message = {};
428
- message["senderFullname"] = msg["senderFullname"]; // ex. "Bot"
429
- message["type"] = msg["type"]; // ex. "text",
430
- message["channel_type"] = msg["channel_type"]; // ex. "group",
431
- message["status"] = msg["status"]; // ex. 0,
432
- message["id"] = msg["_id"]; // ex. "6538cda46cb4d8002cf2317a",
433
- message["sender"] = msg["sender"]; // ex. "system",
434
- message["recipient"] = msg["recipient"]; // ex. "support-group-65203e12f8c0cf002cf4110b-4066a69c8b464646a3ff25f9f41575bb",
435
- message["text"] = msg["text"]; // ex. "\\start",
436
- message["createdBy"] = msg["createdBy"]; // ex. "system",
437
- message["attributes"] = msg["attributes"]; // ex. { "subtype": "info" }
438
- message["metadata"] = msg["metadata"];
439
- message["channel"] = msg["channel"]; // ex. { "name": "chat21" }
440
- return message;
441
- }
442
-
443
- function actionsToDirectives(actions) {
444
- let directives = [];
445
- if (actions && actions.length > 0) {
446
- actions.forEach(action => {
447
- let directive = Directives.actionToDirective(action);
448
- if (directive) {
449
- directives.push(directive);
450
- }
451
- });
452
- }
453
- return directives;
454
- }
455
-
456
219
  router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) => {
457
220
  res.json({success:true});
458
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
- // if (this.log) {
32
- // console.log("__bot_as_string found in chache:", _bot_as_string);
33
- // console.log("value_type:", value_type);
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
- // if (this.log) {
39
- // console.log("got bot from cache:", JSON.stringify(bot));
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
- // if (this.log) {
45
- // console.log("bot not found, getting from datasource...");
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
- // if (this.log) {
50
- // console.log("bot found in datasource:", JSON.stringify(bot));
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);
@@ -579,6 +579,14 @@ class TiledeskChatbot {
579
579
  await TiledeskChatbot.addParameterStatic(this.tdcache, this.requestId, parameter_name, parameter_value);
580
580
  }
581
581
 
582
+ async getParameter(parameter_name) {
583
+ // console.log("this.tdcache::", this.tdcache)
584
+ // console.log("this.requestId::", this.requestId)
585
+ // console.log("parameter_name::", parameter_name)
586
+
587
+ return await TiledeskChatbot.getParameterStatic(this.tdcache, this.requestId, parameter_name);
588
+ }
589
+
582
590
  async deleteParameter(parameter_name) {
583
591
  await TiledeskChatbot.deleteParameterStatic(this.tdcache, this.requestId, parameter_name);
584
592
  }
@@ -596,8 +604,8 @@ class TiledeskChatbot {
596
604
  static async allParametersStatic(_tdcache, requestId) {
597
605
  const parameters_key = TiledeskChatbot.requestCacheKey(requestId) + ":parameters";
598
606
  const attributes__as_string_map = await _tdcache.hgetall(parameters_key);
599
- console.log("** getting parameters for requestId:", requestId);
600
- console.log("** for key:", parameters_key, "parameters:", JSON.stringify(attributes__as_string_map));
607
+ // console.log("** getting parameters for requestId:", requestId);
608
+ // console.log("** for key:", parameters_key, "parameters:", JSON.stringify(attributes__as_string_map));
601
609
  let attributes_native_values = {};
602
610
  if (attributes__as_string_map !== null) {
603
611
  for (const [key, value] of Object.entries(attributes__as_string_map)) {
@@ -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
 
@@ -273,6 +277,251 @@ class TiledeskChatbotUtil {
273
277
  }
274
278
  }
275
279
 
280
+ static async updateConversationTranscript(chatbot, message) {
281
+ // console.log("transcript updating with:", message)
282
+ if (message && message.text && message.text.trim() !== "" && message.sender !== "_tdinternal" && !this.isHiddenMessage(message)) {
283
+ let transcript = await chatbot.getParameter("transcript");
284
+ // console.log("transcript got:", transcript);
285
+ if (transcript) {
286
+ transcript = transcript + "\n[" + message.senderFullname + "] says: " + message.text;
287
+ }
288
+ else {
289
+ transcript = "[" + message.senderFullname + "] says: " + message.text;
290
+ }
291
+ // console.log("transcript update:", transcript);
292
+ await chatbot.addParameter("transcript", transcript);
293
+ let transcript2 = await chatbot.getParameter("transcript");
294
+ // console.log("transcript updated:", transcript2);
295
+ }
296
+ }
297
+
298
+ static isHiddenMessage(message) {
299
+ if (message && message.attributes && message.attributes.subtype === "info") {
300
+ return true;
301
+ }
302
+ return false;
303
+ }
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
+
276
525
  }
277
526
 
278
527
  module.exports = { TiledeskChatbotUtil };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.19",
3
+ "version": "0.2.21",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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) {
@@ -97,7 +97,9 @@ class DirReply {
97
97
  }
98
98
  }
99
99
  // send!
100
+ message.senderFullname = this.context.chatbot.bot.name;
100
101
  if (this.log) {console.log("Reply:", JSON.stringify(message))};
102
+ await TiledeskChatbotUtil.updateConversationTranscript(this.context.chatbot, message);
101
103
  this.context.tdclient.sendSupportMessage(
102
104
  this.requestId,
103
105
  message,