@tiledesk/tiledesk-tybot-connector 0.2.17 → 0.2.20

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,16 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ ### v0.2.20
9
+ - updateRequestVariables renamed in updateRequestAttributes
10
+ - Added TildeskChatbot instance method async getParameter(parameter_name)
11
+ - Added "transcript" attribute
12
+
13
+ ### v0.2.19
14
+ - Added native attribute lastUserMessageType
15
+ - Added integration with external intents decode engine
16
+ - Added support for automatic wait time on each reply
17
+
8
18
  ### v0.2.17
9
19
  - Added lastUserMessage JSON native attribute
10
20
  - Added attributes to access image and file properties from incoming messages
package/index.js CHANGED
@@ -27,6 +27,7 @@ const { DirectivesChatbotPlug } = require('./tiledeskChatbotPlugs/DirectivesChat
27
27
  // THE IMPORT
28
28
  let mongoose = require('mongoose');
29
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
 
@@ -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 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) {
@@ -216,36 +218,7 @@ router.post('/ext/:botid', async (req, res) => {
216
218
 
217
219
  });
218
220
 
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) {
221
+ async function updateRequestAttributes(chatbot, message, projectId, requestId) {
249
222
  // update request context
250
223
  if (chatbot.log) {console.log("Updating request variables. Message:", JSON.stringify(message));}
251
224
  const messageId = message._id;
@@ -261,6 +234,7 @@ async function updateRequestVariables(chatbot, message, projectId, requestId) {
261
234
  }
262
235
  if (message.text && message.sender !== "_tdinternal") {
263
236
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text);
237
+ await chatbot.addParameter("lastUserMessageType", message.type);
264
238
  await chatbot.addParameter("lastUserMessage", lastUserMessageFrom(message)); // JSON TYPE *NEW
265
239
  // get image
266
240
  if (message.type && message.type === "image" && message.metadata) {
@@ -275,10 +275,11 @@ class TiledeskChatbot {
275
275
  return;
276
276
  }
277
277
  else { // NLP
278
- if (this.log) {console.log("Chatbot NLP decode intent...");}
278
+ if (this.log) {console.log("Chatbot NLP decoding intent...");}
279
279
  let intents;
280
280
  try {
281
281
  intents = await this.intentsFinder.decode(this.botId, message.text);
282
+ if (this.log) {console.log("Tiledesk AI intents found:", intents);}
282
283
  }
283
284
  catch(error) {
284
285
  console.error("An error occurred on IntentsFinder.decode() (/model/parse error):", error.message);
@@ -289,8 +290,9 @@ class TiledeskChatbot {
289
290
  if (this.log) {console.log("Got intents from backup finder:", intents);}
290
291
  }
291
292
  }
292
- if (this.log) {console.log("NLP decoded found:", intents);}
293
+ if (this.log) {console.log("NLP intents found:", intents);}
293
294
  if (intents && intents.length > 0) {
295
+ console.log("Matching intents found.");
294
296
  // let faq = await this.botsDataSource.getByIntentDisplayName(this.botId, intents[0].intent_display_name);
295
297
  let faq = await this.botsDataSource.getByIntentDisplayNameCache(this.botId, intents[0].intent_display_name, this.tdcache);
296
298
  let reply;
@@ -577,6 +579,14 @@ class TiledeskChatbot {
577
579
  await TiledeskChatbot.addParameterStatic(this.tdcache, this.requestId, parameter_name, parameter_value);
578
580
  }
579
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
+
580
590
  async deleteParameter(parameter_name) {
581
591
  await TiledeskChatbot.deleteParameterStatic(this.tdcache, this.requestId, parameter_name);
582
592
  }
@@ -594,8 +604,8 @@ class TiledeskChatbot {
594
604
  static async allParametersStatic(_tdcache, requestId) {
595
605
  const parameters_key = TiledeskChatbot.requestCacheKey(requestId) + ":parameters";
596
606
  const attributes__as_string_map = await _tdcache.hgetall(parameters_key);
597
- console.log("** getting parameters for requestId:", requestId);
598
- 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));
599
609
  let attributes_native_values = {};
600
610
  if (attributes__as_string_map !== null) {
601
611
  for (const [key, value] of Object.entries(attributes__as_string_map)) {
@@ -233,6 +233,25 @@ class TiledeskChatbotUtil {
233
233
  }
234
234
  }
235
235
 
236
+ static totalMessageWait(message) {
237
+ if (!message) {
238
+ return;
239
+ }
240
+ console.log("compute delay...", message)
241
+ if (message.attributes.commands.length > 0) {
242
+ console.log("going on delay")
243
+ let commands = message.attributes.commands;
244
+ console.log("got commands", commands)
245
+ let totalWaitTime = 0;
246
+ for (let i = commands.length - 1; i >= 0; i--) {
247
+ if (commands[i].type === "wait") { // is a wait
248
+ totalWaitTime += commands[i].time;
249
+ }
250
+ }
251
+ return totalWaitTime;
252
+ }
253
+ }
254
+
236
255
  static fillCommandAttachments(command, variables, log) {
237
256
  if (log) {
238
257
  console.log("filling command button:", JSON.stringify(command))
@@ -254,6 +273,31 @@ class TiledeskChatbotUtil {
254
273
  }
255
274
  }
256
275
 
276
+ static async updateConversationTranscript(chatbot, message) {
277
+ // console.log("transcript updating with:", message)
278
+ if (message && message.text && message.text.trim() !== "" && message.sender !== "_tdinternal" && !this.isHiddenMessage(message)) {
279
+ let transcript = await chatbot.getParameter("transcript");
280
+ // console.log("transcript got:", transcript);
281
+ if (transcript) {
282
+ transcript = transcript + "\n[" + message.senderFullname + "] says: " + message.text;
283
+ }
284
+ else {
285
+ transcript = "[" + message.senderFullname + "] says: " + message.text;
286
+ }
287
+ // console.log("transcript update:", transcript);
288
+ await chatbot.addParameter("transcript", transcript);
289
+ let transcript2 = await chatbot.getParameter("transcript");
290
+ // console.log("transcript updated:", transcript2);
291
+ }
292
+ }
293
+
294
+ static isHiddenMessage(message) {
295
+ if (message && message.attributes && message.attributes.subtype === "info") {
296
+ return true;
297
+ }
298
+ return false;
299
+ }
300
+
257
301
  }
258
302
 
259
303
  module.exports = { TiledeskChatbotUtil };
@@ -37,13 +37,14 @@ class TiledeskIntentsMachine {
37
37
  };
38
38
  this.myrequest(
39
39
  HTTPREQUEST,
40
- function(err, resbody) {
40
+ (err, resbody) => {
41
41
  if (err) {
42
42
  // console.error("An error occurred on /model/parse:", err)
43
43
  reject(err);
44
44
  }
45
45
  else {
46
- resolve(resbody);
46
+ console.log("Tiledesk AI replied:", resbody)
47
+ resolve(this.translateForTiledesk(resbody));
47
48
  }
48
49
  }, false
49
50
  );
@@ -83,7 +84,7 @@ class TiledeskIntentsMachine {
83
84
  // }
84
85
  let intents_array = intents.intent_ranking;
85
86
  let tiledesk_intents = [];
86
- for (i = 0; i < intents_array.length; i++) {
87
+ for (let i = 0; i < intents_array.length; i++) {
87
88
  let td_intent = {
88
89
  "intent_display_name": intents_array[i].name
89
90
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.17",
3
+ "version": "0.2.20",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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,
@@ -106,7 +108,18 @@ class DirReply {
106
108
  console.error("Error sending reply:", err);
107
109
  }
108
110
  if (this.log) {console.log("Reply message sent");}
109
- callback();
111
+ const delay = TiledeskChatbotUtil.totalMessageWait(message);
112
+ console.log("got total delay:", delay)
113
+ if (delay > 0 && delay <= 30000) { // prevent long delays
114
+ setTimeout(() => {
115
+ console.log("callback after delay")
116
+ callback();
117
+ }, delay);
118
+ }
119
+ else {
120
+ console.log("invalid delay.")
121
+ callback();
122
+ }
110
123
  });
111
124
 
112
125
  // this.sendSupportMessage(