@tiledesk/tiledesk-tybot-connector 2.0.34 → 2.0.36-rc12

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.
@@ -43,48 +43,60 @@ class TiledeskChatbot {
43
43
  }
44
44
 
45
45
  async replyToMessage(message, callback) {
46
+ console.log(`(GAB) TiledeskChatbot replyToMessage start --> at : ${new Date().getTime()} for message.sender: ${message.sender} with text: ${message.text}`)
46
47
  return new Promise( async (resolve, reject) => {
47
48
  let lead = null;
48
49
  if (message.request) {
49
50
  this.request = message.request;
50
51
  }
51
52
 
52
- // reset lockedIntent on direct user invocation ( /intent or action => this only?)
53
+
54
+ let start00 = new Date();
53
55
  if (message.sender != "_tdinternal") {
54
56
  try {
55
- winston.verbose("(TiledeskChatbot) Checking locked intent reset on action invocation")
56
- if (message.attributes && message.attributes.action) {
57
- winston.debug("(TiledeskChatbot) Message has action: " + message.attributes.action)
58
- await this.unlockIntent(this.requestId);
59
- await this.unlockAction(this.requestId);
60
- winston.debug("(TiledeskChatbot) Reset locked intent. Intent was explicitly invoked with an action: " + message.attributes.action)
57
+ winston.verbose("(TiledeskChatbot) External user message, running reset operations…");
58
+ const ops = [];
59
+
60
+ // --- Action/intent reset ---
61
+ // reset lockedIntent on direct user invocation ( /intent or action => this only?)
62
+ if (message.attributes?.action) {
63
+ winston.debug("(TiledeskChatbot) Message has action: " + message.attributes.action);
64
+ ops.push(this.unlockIntent(this.requestId), this.unlockAction(this.requestId));
65
+ winston.debug("(TiledeskChatbot) Reset locked intent. Intent was explicitly invoked with an action: " + message.attributes.action);
61
66
  }
62
- } catch(error) {
63
- winston.error("(TiledeskChatbot) Error resetting locked intent: ", error)
64
- }
65
- }
66
67
 
67
- // any external invocation restarts the steps counter
68
- try {
69
- if (message.sender != "_tdinternal") {
68
+ // --- Steps reset ---
69
+ // any external invocation restarts the steps counter
70
70
  winston.verbose("(TiledeskChatbot) Resetting current step by request message: " + message.text);
71
- await TiledeskChatbot.resetStep(this.tdcache, this.requestId);
72
- await TiledeskChatbot.resetStarted(this.tdcache, this.requestId);
71
+ ops.push(
72
+ TiledeskChatbot.resetStep(this.tdcache, this.requestId),
73
+ TiledeskChatbot.resetStarted(this.tdcache, this.requestId)
74
+ );
73
75
 
76
+ await Promise.all(ops);
77
+
78
+ // --- Debug final state ---
74
79
  if (this.tdcache) {
75
- let currentStep =
76
- await TiledeskChatbot.currentStep(this.tdcache, this.requestId);
80
+ const currentStep = await TiledeskChatbot.currentStep(this.tdcache, this.requestId);
77
81
  winston.verbose("(TiledeskChatbot) After reset currentStep:" + currentStep);
78
82
  }
79
-
83
+
84
+ } catch (error) {
85
+ winston.error("(TiledeskChatbot) Error resetting locked intent: ", error)
80
86
  }
81
- } catch(error) {
82
- winston.error("(TiledeskChatbot) Error resetting locked intent: ", error);
83
87
  }
88
+ let end00 = new Date()
89
+ console.log(`(GAB) TiledeskChatbot replyToMessage 00--> after reset lockedIntent at : ${end00.getTime()}, diff: ${end00-start00}[ms]`)
90
+
91
+
84
92
 
85
93
  // Checking locked intent (for non-internal intents)
86
94
  // internal intents always "skip" the locked intent
95
+ let start0 = new Date()
87
96
  const locked_intent = await this.currentLockedIntent(this.requestId);
97
+ let end0 = new Date()
98
+ console.log(`(GAB) TiledeskChatbot replyToMessage 0--> after currentLockedIntent at : ${end0.getTime()}, diff: ${end0-start0}[ms]`)
99
+
88
100
  winston.verbose("(TiledeskChatbot) Got locked intent: -" + locked_intent + "-");
89
101
  if (locked_intent) {
90
102
  // const tdclient = new TiledeskClient({
@@ -95,11 +107,18 @@ class TiledeskChatbot {
95
107
  // });
96
108
  // it only gets the locked_intent
97
109
  // const faq = await this.botsDataSource.getByIntentDisplayName(this.botId, locked_intent);
110
+ let start0 = new Date()
98
111
  const faq = await this.botsDataSource.getByIntentDisplayNameCache(this.botId, locked_intent, this.tdcache);
112
+ let end0 = new Date()
113
+ console.log(`(GAB) TiledeskChatbot replyToMessage 1-> after botsDataSource.getByIntentDisplayNameCache at : ${end0.getTime()}, diff: ${end0-start0}[ms]`)
99
114
  winston.debug("(TiledeskChatbot) Locked intent. Got faqs: ", faq);
100
115
  let reply;
101
116
  if (faq) {
117
+ let start1 = new Date()
102
118
  reply = await this.execIntent(faq, message, lead);//, bot);
119
+ let end1 = new Date()
120
+ console.log(`(GAB) TiledeskChatbot replyToMessage 1--> after execIntent at : ${end1.getTime()}, diff: ${end1-start1}[ms]`)
121
+
103
122
  }
104
123
  else {
105
124
  reply = {
@@ -148,7 +167,10 @@ class TiledeskChatbot {
148
167
  }
149
168
  else {
150
169
  winston.verbose("(TiledeskChatbot) Processing intent:", explicit_intent_name)
170
+ let start2 = new Date()
151
171
  let faq = await this.botsDataSource.getByIntentDisplayNameCache(this.botId, intent.name, this.tdcache);
172
+ let end2 = new Date()
173
+ console.log(`(GAB) TiledeskChatbot replyToMessage 2--> after botsDataSource.getByIntentDisplayNameCache at : ${end2.getTime()}, diff: ${end2-start2}[ms]`)
152
174
  if (faq) {
153
175
  winston.verbose("(TiledeskChatbot) Got a reply (faq) by Intent name:", faq)
154
176
  try {
@@ -158,7 +180,11 @@ class TiledeskChatbot {
158
180
  this.addParameter(key, value);
159
181
  }
160
182
  }
183
+ let start2 = new Date();
161
184
  reply = await this.execIntent(faq, message, lead);
185
+ let end2 = new Date()
186
+ console.log(`(GAB) TiledeskChatbot replyToMessage 2--> after execIntent at : ${end2.getTime()}, diff: ${end2-start2}[ms]`)
187
+
162
188
  resolve(reply);
163
189
  return;
164
190
  }
@@ -189,7 +215,11 @@ class TiledeskChatbot {
189
215
  let reply;
190
216
  const faq = faqs[0];
191
217
  try {
218
+ let start3 = new Date();
192
219
  reply = await this.execIntent(faq, message, lead);//, bot);
220
+ let end3 = new Date()
221
+ console.log(`(GAB) TiledeskChatbot replyToMessage 3--> after execIntent at : ${end3.getTime()}, diff: ${end3-start3}[ms]`)
222
+
193
223
  }
194
224
  catch(error) {
195
225
  winston.error("(TiledeskChatbot) An error occured during exact match execIntent(): ", error);
@@ -217,10 +247,18 @@ class TiledeskChatbot {
217
247
  }
218
248
  winston.debug("(TiledeskChatbot) NLP intents found: ", intents);
219
249
  if (intents && intents.length > 0) {
250
+ let start2 = new Date();
220
251
  let faq = await this.botsDataSource.getByIntentDisplayNameCache(this.botId, intents[0].intent_display_name, this.tdcache);
252
+ let end2 = new Date()
253
+ console.log(`(GAB) TiledeskChatbot replyToMessage 4--> after botsDataSource.getByIntentDisplayNameCache in if at : ${end2.getTime()}, diff: ${end2-start2}[ms]`)
254
+
221
255
  let reply;
222
256
  try {
257
+ let start3 = new Date();
223
258
  reply = await this.execIntent(faq, message, lead);//, bot);
259
+ let end3 = new Date()
260
+ console.log(`(GAB) TiledeskChatbot replyToMessage 4--> after execIntent in if at : ${end3.getTime()}, diff: ${end3-start3}[ms]`)
261
+
224
262
  }
225
263
  catch(error) {
226
264
  winston.error("(TiledeskChatbot) An error occurred during NLP decoding: ", error);
@@ -231,7 +269,11 @@ class TiledeskChatbot {
231
269
  return;
232
270
  }
233
271
  else {
272
+ let start4 = new Date();
234
273
  let fallbackIntent = await this.botsDataSource.getByIntentDisplayNameCache(this.botId, "defaultFallback", this.tdcache);
274
+ let end4 = new Date()
275
+ console.log(`(GAB) TiledeskChatbot replyToMessage 5--> after execIntent in else at : ${end4.getTime()}, diff: ${end4-start4}[ms]`)
276
+
235
277
  if (!fallbackIntent) {
236
278
  resolve(null);
237
279
  return;
@@ -239,7 +281,11 @@ class TiledeskChatbot {
239
281
  else {
240
282
  let reply;
241
283
  try {
284
+ let start5 = new Date()
242
285
  reply = await this.execIntent(fallbackIntent, message, lead);//, bot);
286
+ let end5 = new Date()
287
+ console.log(`(GAB) TiledeskChatbot replyToMessage 5--> after execIntent at : ${end5.getTime()}, diff: ${end5-start5}[ms]`)
288
+
243
289
  }
244
290
  catch(error) {
245
291
  winston.error("(TiledeskChatbot) An error occurred during defaultFallback: ", error);
@@ -40,6 +40,8 @@ class TiledeskChatbotConst {
40
40
  static REQ_LAST_USER_IMAGE_HEIGHT = 'lastUserImageHeight';
41
41
  static REQ_LAST_USER_IMAGE_TYPE = 'lastUserImageType';
42
42
  static REQ_LAST_USER_DOCUMENT_URL = 'lastUserDocumentURL';
43
+ static REQ_LAST_USER_DOCUMENT_AS_ATTACHMENT_URL = 'lastUserDocumentAsAttachmentURL';
44
+ static REQ_LAST_USER_DOCUMENT_AS_INLINE_URL = 'lastUserDocumentAsInlineURL';
43
45
  static REQ_LAST_USER_DOCUMENT_NAME = 'lastUserDocumentName';
44
46
  static REQ_LAST_USER_DOCUMENT_TYPE = 'lastUserDocumentType';
45
47
  static REQ_EMAIL_ATTACHMENTS_LINK = 'link';
package/index.js CHANGED
@@ -43,6 +43,7 @@ let staticBots;
43
43
 
44
44
  router.post('/ext/:botid', async (req, res) => {
45
45
  const botId = req.params.botid;
46
+ console.log(`(GAB) /ext/${botId} 0--> called at : ${new Date().getTime()}`)
46
47
  winston.verbose("(tybotRoute) POST /ext/:botid called: " + botId)
47
48
  if(!botId || botId === "null" || botId === "undefined"){
48
49
  return res.status(400).send({"success": false, error: "Required parameters botid not found. Value is 'null' or 'undefined'"})
@@ -90,6 +91,7 @@ router.post('/ext/:botid', async (req, res) => {
90
91
  {EX: 604800} // 7 days
91
92
  );
92
93
 
94
+ let start0 = new Date()
93
95
  let botsDS;
94
96
  if (!staticBots) {
95
97
  botsDS = new MongodbBotsDataSource({projectId: projectId, botId: botId});
@@ -98,13 +100,20 @@ router.post('/ext/:botid', async (req, res) => {
98
100
  else {
99
101
  botsDS = new MockBotsDataSource(staticBots);
100
102
  }
103
+ let end0 = new Date()
104
+ console.log(`(GAB) /ext/${botId} 1--> after get MongodbBotsDataSource at : ${end0.getTime()}, diff: ${end0-start0}[ms]`)
105
+
101
106
 
102
107
  // get the bot metadata
108
+ let start1 = new Date()
103
109
  let bot = await botsDS.getBotByIdCache(botId, tdcache).catch((err)=> {
104
110
  Promise.reject(err);
105
111
  return;
106
112
  });
113
+ let end1 = new Date()
114
+ console.log(`(GAB) /ext/${botId} 2--> after get botsDS.getBotByIdCache at : ${end1.getTime()}, diff: ${end1-start1}[ms]`)
107
115
 
116
+ let start2 = new Date()
108
117
  let intentsMachine;
109
118
  let backupMachine;
110
119
  if (!staticBots) {
@@ -115,6 +124,8 @@ router.post('/ext/:botid', async (req, res) => {
115
124
  else {
116
125
  intentsMachine = {}
117
126
  }
127
+ let end2 = new Date()
128
+ console.log(`(GAB) /ext/${botId} 3--> after get IntentsMachineFactory.getMachine at : ${end2.getTime()}, diff: ${end2-start2}[ms]`)
118
129
 
119
130
  const chatbot = new TiledeskChatbot({
120
131
  botsDataSource: botsDS,
@@ -133,14 +144,23 @@ router.post('/ext/:botid', async (req, res) => {
133
144
  });
134
145
  winston.verbose("(tybotRoute) Message text: " + message.text)
135
146
 
147
+ let start3 = new Date()
136
148
  await TiledeskChatbotUtil.updateRequestAttributes(chatbot, token, message, projectId, requestId);
149
+ let end3 = new Date()
150
+ console.log(`(GAB) /ext/${botId} 4--> after get TiledeskChatbotUtil.updateRequestAttributes at : ${end3.getTime()}, diff: ${end3-start3}[ms]`)
137
151
  if (requestId.startsWith("support-group-")) {
138
152
  await TiledeskChatbotUtil.updateConversationTranscript(chatbot, message);
139
153
  }
154
+ let end4 = new Date()
155
+ console.log(`(GAB) /ext/${botId} 5--> after get TiledeskChatbotUtil.updateConversationTranscript at : ${end4.getTime()}, diff: ${end4-end3}[ms]`)
156
+
140
157
 
141
158
  let reply = null;
142
159
  try {
160
+ let start5 = new Date()
143
161
  reply = await chatbot.replyToMessage(message);
162
+ let end5 = new Date()
163
+ console.log(`(GAB) /ext/${botId} 6--> after chatbot.replyToMessage at : ${end5.getTime()}, diff: ${end5-start5}[ms]`)
144
164
  }
145
165
  catch(err) {
146
166
  winston.error("(tybotRoute) An error occurred replying to message: ", err);
@@ -151,11 +171,16 @@ router.post('/ext/:botid', async (req, res) => {
151
171
  return;
152
172
  }
153
173
 
174
+
154
175
  if (reply.actions && reply.actions.length > 0) { // structured actions (coming from chatbot designer)
155
176
  try {
177
+ let start6 = new Date()
156
178
  winston.debug("(tybotRoute) Reply actions: ", reply.actions)
157
179
  let directives = TiledeskChatbotUtil.actionsToDirectives(reply.actions);
158
180
  winston.debug("(tybotRoute) the directives:", directives)
181
+ let end6 = new Date()
182
+ console.log(`(GAB) /ext/${botId} 7--> after TiledeskChatbotUtil.actionsToDirectives at : ${end6.getTime()}, diff: ${end6-start6}[ms]`)
183
+
159
184
  let directivesPlug = new DirectivesChatbotPlug(
160
185
  {
161
186
  message: message,
@@ -170,6 +195,7 @@ router.post('/ext/:botid', async (req, res) => {
170
195
  cache: tdcache
171
196
  }
172
197
  );
198
+ console.log(`(GAB) /ext/${botId} 8--> directivesPlug.processDirectives at : ${end6.getTime()}`)
173
199
  directivesPlug.processDirectives( () => {
174
200
  winston.verbose("(tybotRoute) Actions - Directives executed.");
175
201
  });