@tiledesk/tiledesk-tybot-connector 0.2.96 → 0.2.98

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
@@ -17,6 +17,17 @@ available on:
17
17
  - Added flowError on JSONCondition when result = null
18
18
  - Added fix on Filler -->
19
19
 
20
+ # v0.2.98
21
+ - Support for MAX_EXECUTION_TIME (total execution time of a flow without user interaction)
22
+ - MAX_STEPS = 1000
23
+ - MAX_EXECUTION_TIME = 12 hrs
24
+
25
+ # v0.2.97
26
+ - Added support for "chatbotToken” attribute
27
+ - Added new Action: clear_transcript
28
+ - Exclude transcript generation when request type differs from "support-group-*"
29
+ - "step" key reset on DirWait => this.chatbot.addParameter( step_key, 0 )
30
+
20
31
  # v0.2.96
21
32
  - Added timestamp (number) and now (Date Object) attributes
22
33
 
package/index.js CHANGED
@@ -171,8 +171,10 @@ router.post('/ext/:botid', async (req, res) => {
171
171
  // if (log) {console.log("forced conversion of \\\\start /start");}
172
172
  // message.text = "/start";
173
173
  // }
174
- await TiledeskChatbotUtil.updateRequestAttributes(chatbot, message, projectId, requestId);
175
- await TiledeskChatbotUtil.updateConversationTranscript(chatbot, message);
174
+ await TiledeskChatbotUtil.updateRequestAttributes(chatbot, token, message, projectId, requestId);
175
+ if (requestId.startsWith("support-group-")) {
176
+ await TiledeskChatbotUtil.updateConversationTranscript(chatbot, message);
177
+ }
176
178
 
177
179
  let reply = null;
178
180
  try {
@@ -13,7 +13,8 @@ const { TiledeskChatbotConst } = require('./TiledeskChatbotConst.js');
13
13
 
14
14
  class TiledeskChatbot {
15
15
 
16
- static MAX_STEPS = 200;
16
+ static MAX_STEPS = 1000;
17
+ static MAX_EXECUTION_TIME = 1000 * 60 * 60 * 12 // 12 hours = 43.200 secs;
17
18
 
18
19
  constructor(config) {
19
20
  if (!config.botsDataSource) {
@@ -104,6 +105,7 @@ class TiledeskChatbot {
104
105
  console.log("Resetting current step by request message:", message.text);
105
106
  }
106
107
  await TiledeskChatbot.resetStep(this.tdcache, this.requestId);
108
+ await TiledeskChatbot.resetStarted(this.tdcache, this.requestId);
107
109
  if (this.log) {
108
110
  if (this.tdcache) {
109
111
  let currentStep =
@@ -671,44 +673,50 @@ class TiledeskChatbot {
671
673
  TiledeskChatbot.requestCacheKey(requestId) + ":parameters", paramName);
672
674
  }
673
675
 
674
- static async checkStep(_tdcache, requestId, max_steps, log) {
676
+ static async checkStep(_tdcache, requestId, max_steps, max_execution_time, log) {
675
677
  if (log) {console.log("CHECKING ON MAX_STEPS:", max_steps);}
676
- let go_on = true; // continue
678
+ // let go_on = true; // continue
677
679
  const parameter_key = TiledeskChatbot.requestCacheKey(requestId) + ":step";
678
680
  if (log) {console.log("__parameter_key:", parameter_key);}
679
681
  await _tdcache.incr(parameter_key);
680
682
  // console.log("incr-ed");
681
683
  let _current_step = await _tdcache.get(parameter_key);
682
- // if (!_current_step) { // this shouldn't be happening
683
- // _current_step = 0;
684
- // }
685
684
  let current_step = Number(_current_step);
686
- // current_step += 1;
687
- // await _tdcache.set(parameter_key, current_step); // increment step
688
- // console.log("CURRENT-STEP:", current_step);
689
685
  if (current_step > max_steps) {
690
686
  if (log) {console.log("max_steps limit just violated");}
691
687
  if (log) {console.log("CURRENT-STEP > MAX_STEPS!", current_step);}
692
- // await TiledeskChatbot.resetStep(_tdcache, requestId);
693
- // go_on = 1; // stop execution, send error message
694
- go_on = false
695
- }
696
- // else if (current_step > max_steps + 1) { // max_steps limit already violated
697
- // console.log("CURRENT-STEP > MAX_STEPS!", current_step);
698
- // // await TiledeskChatbot.resetStep(_tdcache, requestId);
699
- // go_on = 2; // stop execution, don't send error message (already sent with go_on = 1)
700
- // }
701
- else {
702
- // go_on = 0;
703
- go_on = true;
688
+ // go_on = false
689
+ return {
690
+ error: "Anomaly detection. MAX ACTIONS (" + max_steps + ") exeeded."
691
+ };
704
692
  }
705
693
  // else {
706
- // console.log("CURRENT-STEP UNDER MAX_STEPS THRESHOLD:)", current_step);
707
- // current_step += 1;
708
- // await _tdcache.set(parameter_key, current_step); // increment step
709
- // console.log("current_step from cache:", await _tdcache.get(parameter_key));
694
+ // go_on = true;
710
695
  // }
711
- return go_on;
696
+
697
+ // check execution_time
698
+ // const TOTAL_ALLOWED_EXECUTION_TIME = 1000 * 60 // * 60 * 12 // 12 hours
699
+ let start_time_key = TiledeskChatbot.requestCacheKey(requestId) + ":started";
700
+ let start_time = await _tdcache.get(start_time_key);
701
+ // console.log("cached start_time is:", start_time, typeof start_time);
702
+ const now = Date.now();
703
+ if (start_time === null || Number(start_time) === 0) {
704
+ // console.log("start_time is null");
705
+ await _tdcache.set(start_time_key, now);
706
+ return {};
707
+ }
708
+ else {
709
+ // console.log("start_time:", start_time);
710
+ const execution_time = now - Number(start_time);
711
+ // console.log("execution_time:", execution_time);
712
+ if (execution_time > max_execution_time) {
713
+ if (log) {console.log("execution_time > TOTAL_ALLOWED_EXECUTION_TIME. Stopping flow");}
714
+ return {
715
+ error: "Anomaly detection. MAX EXECUTION TIME (" + max_execution_time + " ms) exeeded."
716
+ };
717
+ }
718
+ }
719
+ return {};
712
720
  }
713
721
 
714
722
  static async resetStep(_tdcache, requestId) {
@@ -719,6 +727,14 @@ class TiledeskChatbot {
719
727
  }
720
728
  }
721
729
 
730
+ static async resetStarted(_tdcache, requestId) {
731
+ const parameter_key = TiledeskChatbot.requestCacheKey(requestId) + ":started";
732
+ // console.log("resetStarted() parameter_key:", parameter_key);
733
+ if (_tdcache) {
734
+ await _tdcache.set(parameter_key, 0);
735
+ }
736
+ }
737
+
722
738
  static async currentStep(_tdcache, requestId) {
723
739
  const parameter_key = TiledeskChatbot.requestCacheKey(requestId) + ":step";
724
740
  // console.log("currentStep() parameter_key:", parameter_key);
@@ -30,6 +30,7 @@ class TiledeskChatbotConst {
30
30
  static REQ_USER_LEAD_ID_KEY = "userLeadId";
31
31
  static REQ_USER_COMPANY_KEY = "userCompany";
32
32
  static REQ_TICKET_ID_KEY = "ticketId";
33
+ static REQ_CHATBOT_TOKEN = "chatbotToken";
33
34
 
34
35
 
35
36
  // static REQ_DEPARTMENT_ID_KEY = "tdDepartmentId";
@@ -442,6 +442,13 @@ class TiledeskChatbotUtil {
442
442
  }
443
443
  }
444
444
 
445
+ static async clearConversationTranscript(chatbot, callback) {
446
+ await chatbot.addParameter("transcript", "");
447
+ if (callback) {
448
+ callback();
449
+ }
450
+ }
451
+
445
452
  static transcriptJSON(transcript) {
446
453
  const regexp = /(<.*>)/gm;
447
454
  const parts = transcript.split(regexp);
@@ -506,7 +513,7 @@ class TiledeskChatbotUtil {
506
513
  return message;
507
514
  }
508
515
 
509
- static async updateRequestAttributes(chatbot, message, projectId, requestId) {
516
+ static async updateRequestAttributes(chatbot, chatbotToken, message, projectId, requestId) {
510
517
  // update request context
511
518
  try {
512
519
  if (chatbot.log) {console.log("Updating request variables. Message:", JSON.stringify(message));}
@@ -519,7 +526,10 @@ class TiledeskChatbotUtil {
519
526
  // TODO add projectName too
520
527
  await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
521
528
  if (chatbot.bot) {
522
- await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
529
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_NAME_KEY, chatbot.bot.name);
530
+ }
531
+ if (chatbotToken) {
532
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHATBOT_TOKEN, chatbotToken);
523
533
  }
524
534
 
525
535
  if (message.text && message.sender !== "_tdinternal") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.96",
3
+ "version": "0.2.98",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -50,6 +50,7 @@ const { DirAssistant } = require('./directives/DirAssistant');
50
50
  const { DirReplyV2 } = require('./directives/DirReplyV2');
51
51
  const { DirIfOnlineAgentsV2 } = require('./directives/DirIfOnlineAgentsV2');
52
52
  const { DirContactUpdate } = require('./directives/DirContactUpdate');
53
+ const { DirClearTranscript } = require('./directives/DirClearTranscript');
53
54
 
54
55
  class DirectivesChatbotPlug {
55
56
 
@@ -170,13 +171,13 @@ class DirectivesChatbotPlug {
170
171
  async nextDirective(directives) {
171
172
  if (this.log) {console.log("....nextDirective() checkStep():");}
172
173
  const go_on = await TiledeskChatbot.checkStep(
173
- this.context.tdcache, this.context.requestId, TiledeskChatbot.MAX_STEPS, this.log
174
+ this.context.tdcache, this.context.requestId, TiledeskChatbot.MAX_STEPS, TiledeskChatbot.MAX_EXECUTION_TIME, this.log
174
175
  );
175
176
  // const current_step = await TiledeskChatbot.currentStep(this.context.tdcache, this.context.requestId);
176
177
  // if (this.log) {console.log("........nextDirective() currentStep:", current_step);}
177
- if (go_on == false) {
178
+ if (go_on.error) {
178
179
  if (this.log) {console.log("go_on == false! nextDirective() Stopped!");}
179
- return this.errorMessage("Request error: anomaly detection. MAX ACTIONS exeeded.");
180
+ return this.errorMessage(go_on.error); //"Request error: anomaly detection. MAX ACTIONS exeeded.");
180
181
  }
181
182
  // else if (go_on == 2) {
182
183
  // return null;
@@ -684,12 +685,19 @@ class DirectivesChatbotPlug {
684
685
  });
685
686
  }
686
687
  else if (directive_name === Directives.CONTACT_UPDATE) {
687
- console.log("...CONTACT_UPDATE");
688
+ // console.log("...CONTACT_UPDATE");
688
689
  new DirContactUpdate(context).execute(directive, async () => {
689
690
  let next_dir = await this.nextDirective(this.directives);
690
691
  this.process(next_dir);
691
692
  });
692
693
  }
694
+ else if (directive_name === Directives.CLEAR_TRANSCRIPT) {
695
+ console.log("...CLEAR_TRANSCRIPT");
696
+ new DirClearTranscript(context).execute(directive, async () => {
697
+ let next_dir = await this.nextDirective(this.directives);
698
+ this.process(next_dir);
699
+ });
700
+ }
693
701
  else {
694
702
  //console.log("Unhandled Post-message Directive:", directive_name);
695
703
  let next_dir = await this.nextDirective(this.directives);
@@ -0,0 +1,23 @@
1
+
2
+ const { TiledeskChatbotUtil } = require('../../models/TiledeskChatbotUtil');
3
+
4
+ class DirClearTranscript {
5
+
6
+ constructor(context) {
7
+ if (!context) {
8
+ throw new Error('context object is mandatory.');
9
+ }
10
+ this.context = context;
11
+ this.tdclient = context.tdclient;
12
+ this.requestId = context.requestId;
13
+ }
14
+
15
+ execute(directive, callback) {
16
+ TiledeskChatbotUtil.clearConversationTranscript(this.context.chatbot, () => {
17
+ callback();
18
+ });
19
+ }
20
+
21
+ }
22
+
23
+ module.exports = { DirClearTranscript };
@@ -1,7 +1,18 @@
1
1
 
2
+ const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
+
2
4
  class DirWait {
3
5
 
4
- constructor() {
6
+ constructor(context) {
7
+ if (!context) {
8
+ throw new Error('context object is mandatory.');
9
+ }
10
+ this.context = context;
11
+ this.tdclient = context.tdclient;
12
+ this.chatbot = context.chatbot;
13
+ this.tdcache = context.tdcache;
14
+ this.requestId = context.requestId;
15
+ this.log = context.log;
5
16
  }
6
17
 
7
18
  execute(directive, callback) {
@@ -38,10 +49,17 @@ class DirWait {
38
49
  })
39
50
  }
40
51
 
41
- go(action, callback) {
42
- // console.log(">>>>__", callback)
52
+ async go(action, callback) {
53
+ // reset step?
54
+ // const step_key = TiledeskChatbot.requestCacheKey(this.requestId) + ":step";
55
+ // console.log("step_key:", step_key);
56
+ if (action && action.millis >= 1000) {//2000 * 60) { // at list 2 minutes waiting time to reset the steps counter
57
+ // await this.tdcache.set(step_key, 0);
58
+ console.log("resetting steps counter");
59
+ await TiledeskChatbot.resetStep(this.tdcache, this.requestId);
60
+ // console.log("step_key after:", await this.tdcache.get( step_key ));
61
+ }
43
62
  setTimeout(() => {
44
- // console.log("QUINO....__")
45
63
  callback();
46
64
  }, action.millis);
47
65
  }
@@ -50,6 +50,7 @@ class Directives {
50
50
  static REPLY_V2 = 'replyv2';
51
51
  static IF_ONLINE_AGENTS_V2 = "ifonlineagentsv2";
52
52
  static CONTACT_UPDATE = "leadupdate";
53
+ static CLEAR_TRANSCRIPT = "clear_transcript";
53
54
 
54
55
  // static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
55
56
  // static WHEN_OFFLINE_HOURS = "whenofflinehours"; // DEPRECATED // adds a message on top of the original message when offline hours opts: --replace