@tiledesk/tiledesk-tybot-connector 0.2.15 → 0.2.17

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,15 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ ### v0.2.17
9
+ - Added lastUserMessage JSON native attribute
10
+ - Added attributes to access image and file properties from incoming messages
11
+ - Added support for JSON attributes in conditions
12
+ - Added backupIntentsFinder based on Fulltext in TiledeskChatbot
13
+
14
+ ### v0.2.16
15
+ - update whatsapp api url for pre environment
16
+
8
17
  ### v0.2.15
9
18
  - replyto in send email fix
10
19
 
@@ -230,9 +230,9 @@ class TiledeskExpression {
230
230
  }
231
231
 
232
232
  evaluateJavascriptExpression(expression, context) {
233
- // console.log("evaluating:", expression)
233
+ // console.log("(evaluateJavascriptExpression) evaluating:", expression)
234
234
  // console.log("context:", context)
235
- let res;
235
+ let res = null;
236
236
  try {
237
237
  const vm = new VM({
238
238
  timeout: 200,
@@ -240,9 +240,10 @@ class TiledeskExpression {
240
240
  sandbox: context
241
241
  });
242
242
  res = vm.run(`let $data = this;${expression}`);
243
+ // console.log("res=", res)
243
244
  }
244
245
  catch (err) {
245
- console.error("TiledeskExpression.evaluate() error:", err.message, "evaluating expression: '" + expression + "'");
246
+ console.error("(evaluateJavascriptExpression) TiledeskExpression.evaluate() error:", err.message, "evaluating expression: '" + expression + "'");
246
247
  }
247
248
  return res;
248
249
  }
@@ -324,11 +325,13 @@ class TiledeskExpression {
324
325
  // console.log("applyPattern:", applyPattern);
325
326
  let operand1_s;
326
327
  let is_valid_operand1 = TiledeskExpression.validateVariableName(condition.operand1);
328
+ // console.log("is_valid_operand1:", condition.operand1, is_valid_operand1);
327
329
  if (is_valid_operand1) {
328
330
  operand1_s = TiledeskExpression.variableOperand(condition.operand1);
331
+ // console.log("operand1_s:", operand1_s);
329
332
  }
330
333
  else {
331
- // console.error("Condition evaluation stopped because of invalid operand", condition.operand1);
334
+ console.error("Condition evaluation stopped because of invalid operand", condition.operand1);
332
335
  return null;
333
336
  }
334
337
 
@@ -343,24 +346,26 @@ class TiledeskExpression {
343
346
  operand2_s = TiledeskExpression.variableOperand(condition.operand2.name);
344
347
  }
345
348
  else {
346
- // console.error("Condition evaluation stopped because of invalid operand2", condition.operand2);
349
+ console.error("Condition evaluation stopped because of invalid operand2", condition.operand2);
347
350
  return null;
348
351
  }
349
352
  }
350
353
  else {
351
- // console.error("Condition evaluation stopped because of: No operand2", JSON.stringify(condition));
354
+ console.error("Condition evaluation stopped because of: No operand2", JSON.stringify(condition));
352
355
  return null;
353
356
  }
354
357
 
355
- // console.log("operand2_s:", operand2_s);
358
+ // console.log("operand1_s, operand2_s:",operand1_s, operand2_s);
356
359
  const expression =
357
360
  applyPattern
358
361
  .replace("#1", operand1_s)
359
362
  .replace("#2", operand2_s);
363
+ // console.log("expression is:", expression);
360
364
  return expression;
361
365
  }
362
366
 
363
367
  static JSONGroupToExpression(group, variables) {
368
+ // console.log("attributes:", variables);
364
369
  let conditions = group.conditions;
365
370
  let group_expression = "";
366
371
  // console.log("conditions:", conditions)
@@ -368,6 +373,7 @@ class TiledeskExpression {
368
373
  let part = conditions[i];
369
374
  if (part.type === "condition") {
370
375
  let expression = TiledeskExpression.JSONConditionToExpression(part, variables);
376
+ // console.log("returned expression:", expression);
371
377
  if (expression === null) {
372
378
  // console.error("Invalid JSON expression", JSON.stringify(part));
373
379
  return null;
@@ -412,7 +418,8 @@ class TiledeskExpression {
412
418
  static validateVariableName(variableName) {
413
419
  // console.log("variableName", variableName)
414
420
  // console.log("type of variableName:", typeof variableName);
415
- let matches = variableName.match(/^[a-zA-Z_]*[a-zA-Z_]+[a-zA-Z0-9_]*$/gm);
421
+ // let matches = variableName.match(/^[a-zA-Z_]*[a-zA-Z_]+[a-zA-Z0-9_]*$/gm);
422
+ let matches = variableName.match(/^[a-zA-Z_]+.*$/gm);
416
423
  // console.log("matches:", matches)
417
424
  if (matches !== null) {
418
425
  return true;
package/index.js CHANGED
@@ -102,8 +102,11 @@ router.post('/ext/:botid', async (req, res) => {
102
102
  if (log) {console.log("bot found:", JSON.stringify(bot));}
103
103
 
104
104
  let intentsMachine;
105
+ let backupMachine;
105
106
  if (!staticBots) {
106
107
  intentsMachine = IntentsMachineFactory.getMachine(bot, botId, projectId, log);
108
+ backupMachine = IntentsMachineFactory.getBackupMachine(bot, botId, projectId, log);
109
+ console.log("Created backupMachine:", backupMachine);
107
110
  }
108
111
  else {
109
112
  intentsMachine = {}
@@ -132,6 +135,7 @@ router.post('/ext/:botid', async (req, res) => {
132
135
  const chatbot = new TiledeskChatbot({
133
136
  botsDataSource: botsDS,
134
137
  intentsFinder: intentsMachine,
138
+ backupIntentsFinder: backupMachine,
135
139
  botId: botId,
136
140
  bot: bot,
137
141
  token: token,
@@ -248,6 +252,7 @@ async function updateRequestVariables(chatbot, message, projectId, requestId) {
248
252
  const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
249
253
  // await chatbot.addParameter("chatbot", chatbot);
250
254
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url);
255
+ console.log("Adding proj_", projectId);
251
256
  await chatbot.addParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
252
257
  // TODO add projectName too
253
258
  await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
@@ -256,7 +261,59 @@ async function updateRequestVariables(chatbot, message, projectId, requestId) {
256
261
  }
257
262
  if (message.text && message.sender !== "_tdinternal") {
258
263
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text);
264
+ await chatbot.addParameter("lastUserMessage", lastUserMessageFrom(message)); // JSON TYPE *NEW
265
+ // get image
266
+ if (message.type && message.type === "image" && message.metadata) {
267
+ // "text": "\nimage text",
268
+ // "id_project": "65203e12f8c0cf002cf4110b",
269
+ // "createdBy": "8ac52a30-133f-4ee1-8b4b-96055bb81757",
270
+ // "metadata": {
271
+ // "height": 905,
272
+ // "name": "tiledesk_Open graph_general.png",
273
+ // "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",
274
+ // "type": "image/png",
275
+ // "uid": "lo68iyq5",
276
+ // "width": 1724
277
+ // }
278
+ if (message.metadata.src) {
279
+ await chatbot.addParameter("lastUserImageURL", message.metadata.src);
280
+ await chatbot.addParameter("lastUserImageName", message.metadata.name);
281
+ await chatbot.addParameter("lastUserImageWidth", message.metadata.width);
282
+ await chatbot.addParameter("lastUserImageHeight", message.metadata.height);
283
+ await chatbot.addParameter("lastUserImageType", message.metadata.type);
284
+ }
285
+ }
286
+ else {
287
+ await chatbot.addParameter("lastUserImageURL", null);
288
+ await chatbot.addParameter("lastUserImageName", null);
289
+ await chatbot.addParameter("lastUserImageWidth", null);
290
+ await chatbot.addParameter("lastUserImageHeight", null);
291
+ await chatbot.addParameter("lastUserImageType", null);
292
+ }
293
+ // get document
294
+ if (message.type && message.type === "file" && message.metadata) {
295
+ // "type": "file",
296
+ // "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",
297
+ // "metadata": {
298
+ // "name": "LIBRETTO-WEB-ISTRUZIONI-GENITORI.pdf",
299
+ // "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",
300
+ // "type": "application/pdf",
301
+ // "uid": "lo68oz8i"
302
+ // }
303
+ if (message.metadata.src) {
304
+ await chatbot.addParameter("lastUserDocumentURL", message.metadata.src);
305
+ await chatbot.addParameter("lastUserDocumentName", message.metadata.name);
306
+ await chatbot.addParameter("lastUserDocumentType", message.metadata.type);
307
+ }
308
+ }
309
+ else {
310
+ await chatbot.addParameter("lastUserDocumentURL", null);
311
+ await chatbot.addParameter("lastUserDocumentName", null);
312
+ await chatbot.addParameter("lastUserDocumentType", null);
313
+ }
259
314
  }
315
+
316
+
260
317
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_MESSAGE_ID_KEY, messageId);
261
318
  if (message.request && message.request.location && message.request.location.country) {
262
319
  await chatbot.addParameter(TiledeskChatbotConst.REQ_COUNTRY_KEY, message.request.location.country);
@@ -324,6 +381,11 @@ async function updateRequestVariables(chatbot, message, projectId, requestId) {
324
381
  }
325
382
  }
326
383
  if (chatbot.log) {
384
+ // console.log("tdcache:", chatbot.tdcache);
385
+ console.log("requestId:", requestId);
386
+ console.log("KEY:", TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
387
+ let proj_ = await TiledeskChatbot.getParameterStatic(chatbot.tdcache, requestId, TiledeskChatbotConst.REQ_PROJECT_ID_KEY);
388
+ console.log("request parameter proj_:", proj_);
327
389
  const all_parameters = await TiledeskChatbot.allParametersStatic(chatbot.tdcache, requestId);
328
390
  for (const [key, value] of Object.entries(all_parameters)) {
329
391
  // const value = all_parameters[key];
@@ -360,6 +422,23 @@ async function updateRequestVariables(chatbot, message, projectId, requestId) {
360
422
  // }
361
423
  }
362
424
 
425
+ function lastUserMessageFrom(msg) {
426
+ let message = {};
427
+ message["senderFullname"] = msg["senderFullname"]; // ex. "Bot"
428
+ message["type"] = msg["type"]; // ex. "text",
429
+ message["channel_type"] = msg["channel_type"]; // ex. "group",
430
+ message["status"] = msg["status"]; // ex. 0,
431
+ message["id"] = msg["_id"]; // ex. "6538cda46cb4d8002cf2317a",
432
+ message["sender"] = msg["sender"]; // ex. "system",
433
+ message["recipient"] = msg["recipient"]; // ex. "support-group-65203e12f8c0cf002cf4110b-4066a69c8b464646a3ff25f9f41575bb",
434
+ message["text"] = msg["text"]; // ex. "\\start",
435
+ message["createdBy"] = msg["createdBy"]; // ex. "system",
436
+ message["attributes"] = msg["attributes"]; // ex. { "subtype": "info" }
437
+ message["metadata"] = msg["metadata"];
438
+ message["channel"] = msg["channel"]; // ex. { "name": "chat21" }
439
+ return message;
440
+ }
441
+
363
442
  function actionsToDirectives(actions) {
364
443
  let directives = [];
365
444
  if (actions && actions.length > 0) {
@@ -216,8 +216,15 @@ class IntentForm {
216
216
  _regex = regex.substring(1, regex.length-1);
217
217
  }
218
218
  if (this.log) {console.log("Validating using regex:", _regex);}
219
- const rg = new RegExp(_regex, "g");
220
- return rg.test(text);
219
+ try {
220
+ const rg = new RegExp(_regex, "g");
221
+ return rg.test(text);
222
+ }
223
+ catch(error) {
224
+ console.error("Error, invalid regex:", _regex);
225
+ return true;
226
+ }
227
+
221
228
  }
222
229
 
223
230
  static isValidForm(form) {
@@ -13,12 +13,18 @@ class IntentsMachineFactory {
13
13
  });
14
14
  }
15
15
  else {
16
- console.log("bot.intentsEngine is null");
17
16
  if (log) {console.log("Setting MongodbIntentsMachine with bot:", JSON.stringify(bot));}
18
17
  machine = new MongodbIntentsMachine({projectId: projectId, language: bot.language, log});
19
18
  }
20
19
  return machine;
21
20
  }
21
+
22
+ static getBackupMachine(bot, botId, projectId, log) {
23
+ let machine;
24
+ if (log) {console.log("Setting MongodbIntentsMachine as Backup Intents Machine on bot:", JSON.stringify(bot));}
25
+ machine = new MongodbIntentsMachine({projectId: projectId, language: bot.language, log});
26
+ return machine;
27
+ }
22
28
  }
23
29
 
24
30
  module.exports = { IntentsMachineFactory }
@@ -29,6 +29,7 @@ class TiledeskChatbot {
29
29
  }
30
30
  this.botsDataSource = config.botsDataSource;
31
31
  this.intentsFinder = config.intentsFinder;
32
+ this.backupIntentsFinder = config.backupIntentsFinder;
32
33
  this.botId = config.botId;
33
34
  this.bot = config.bot;
34
35
  this.token = config.token;
@@ -79,7 +80,7 @@ class TiledeskChatbot {
79
80
  if (this.log) {console.log("RESETTING LOCKED INTENT. Intent was explicitly invoked with an action:", message.attributes.action);}
80
81
  await this.unlockIntent(this.requestId);
81
82
  await this.unlockAction(this.requestId);
82
- if (this.log) {console.log("RESET LOCKED INTENT. Intent was explicitly invoked with an action:", message.attributes.action);}
83
+ if (this.log) {console.log("RESET LOCKED INTENT. Intent was explicitly invoked with an action:", message.attributes.action);}
83
84
  }
84
85
  }
85
86
  catch(error) {
@@ -151,7 +152,7 @@ class TiledeskChatbot {
151
152
 
152
153
  let explicit_intent_name = null;
153
154
  // Explicit intent invocation
154
- if (message.text.startsWith("/")) {
155
+ if (message.text && message.text.startsWith("/")) {
155
156
  if (this.log) {console.log("Intent was explicitly invoked:", message.text);}
156
157
  let intent_name = message.text.substring(message.text.indexOf("/") + 1);
157
158
  if (this.log) {console.log("Invoked Intent:", intent_name);}
@@ -280,7 +281,13 @@ class TiledeskChatbot {
280
281
  intents = await this.intentsFinder.decode(this.botId, message.text);
281
282
  }
282
283
  catch(error) {
283
- console.error("An error occurred:", error);
284
+ console.error("An error occurred on IntentsFinder.decode() (/model/parse error):", error.message);
285
+ // recover on fulltext
286
+ if (this.backupIntentsFinder) {
287
+ if (this.log) {console.log("using backup Finder:", this.backupIntentsFinder);}
288
+ intents = await this.backupIntentsFinder.decode(this.botId, message.text);
289
+ if (this.log) {console.log("Got intents from backup finder:", intents);}
290
+ }
284
291
  }
285
292
  if (this.log) {console.log("NLP decoded found:", intents);}
286
293
  if (intents && intents.length > 0) {
@@ -39,7 +39,7 @@ class TiledeskIntentsMachine {
39
39
  HTTPREQUEST,
40
40
  function(err, resbody) {
41
41
  if (err) {
42
- console.error("error:", err)
42
+ // console.error("An error occurred on /model/parse:", err)
43
43
  reject(err);
44
44
  }
45
45
  else {
@@ -123,7 +123,7 @@ class TiledeskIntentsMachine {
123
123
  }
124
124
  })
125
125
  .catch( (error) => {
126
- console.error("An error occurred:", error);
126
+ // console.error("An error occurred:", error);
127
127
  if (callback) {
128
128
  callback(error, null, null);
129
129
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.15",
3
+ "version": "0.2.17",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -252,9 +252,17 @@ class DirectivesChatbotPlug {
252
252
  }
253
253
  else if (directive_name === Directives.INTENT) {
254
254
  // console.log(".....DirIntent")
255
- new DirIntent(context).execute(directive, async () => {
256
- let next_dir = await this.nextDirective(this.directives);
257
- this.process(next_dir);
255
+ new DirIntent(context).execute(directive, async (stop) => {
256
+ if (stop) {
257
+ if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
258
+ this.theend();
259
+ }
260
+ else {
261
+ let next_dir = await this.nextDirective(this.directives);
262
+ this.process(next_dir);
263
+ }
264
+ // let next_dir = await this.nextDirective(this.directives);
265
+ // this.process(next_dir);
258
266
  });
259
267
  }
260
268
  else if (directive_name === Directives.MESSAGE) {
@@ -57,8 +57,8 @@ class DirIntent {
57
57
  callback();
58
58
  return;
59
59
  }
60
- this.go(action, () => {
61
- callback();
60
+ this.go(action, (stop) => {
61
+ callback(stop);
62
62
  });
63
63
  }
64
64
 
@@ -80,6 +80,7 @@ class DirIntent {
80
80
  else {
81
81
  console.error("Invalid intent");
82
82
  callback();
83
+ return;
83
84
  }
84
85
 
85
86
  // if (intentName) {
@@ -111,7 +112,7 @@ class DirIntent {
111
112
  }
112
113
  this.sendMessageToBot(TILEBOT_ENDPOINT, intent_command_request, botId, () => {
113
114
  // console.log("sendMessageToBot() req_body sent:", intent_command_request);
114
- callback();
115
+ callback(true);
115
116
  });
116
117
 
117
118
  // }
@@ -1,7 +1,6 @@
1
1
  const axios = require("axios").default;
2
2
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
3
 
4
- // const whatsapp_api_url = "https://tiledesk-whatsapp-app-pre.giovannitroisi3.repl.co/ext"
5
4
  let whatsapp_api_url;
6
5
 
7
6
  class DirWhatsappByAttribute {
@@ -36,7 +35,7 @@ class DirWhatsappByAttribute {
36
35
  }
37
36
 
38
37
  if (process.env.API_URL) {
39
- whatsapp_api_url = "https://tiledesk-whatsapp-app-pre.giovannitroisi3.repl.co/api";
38
+ whatsapp_api_url = "https://tiledesk-whatsapp-connector.giovannitroisi3.repl.co/api";
40
39
  // whatsapp_api_url = process.env.API_URL + "/modules/whatsapp";
41
40
  console.log("(Tilebot) DirWhatsappByAttribute whatsapp_api_url: ", whatsapp_api_url);
42
41
  } else {