@tiledesk/tiledesk-tybot-connector 0.2.97 → 0.2.99

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,14 @@ available on:
17
17
  - Added flowError on JSONCondition when result = null
18
18
  - Added fix on Filler -->
19
19
 
20
+ # v0.2.99
21
+ - Added "chatbotToken" to RESERVED attributes
22
+
23
+ # v0.2.98
24
+ - Support for MAX_EXECUTION_TIME (total execution time of a flow without user interaction)
25
+ - MAX_STEPS = 1000
26
+ - MAX_EXECUTION_TIME = 12 hrs
27
+
20
28
  # v0.2.97
21
29
  - Added support for "chatbotToken” attribute
22
30
  - Added new Action: clear_transcript
@@ -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);
@@ -901,7 +901,8 @@ class TiledeskChatbotUtil {
901
901
  "userLeadId",
902
902
  "lastUserText",
903
903
  TiledeskChatbotConst.REQ_REQUESTER_IS_AUTHENTICATED_KEY,
904
- TiledeskChatbotConst.USER_INPUT
904
+ TiledeskChatbotConst.USER_INPUT,
905
+ TiledeskChatbotConst.REQ_CHATBOT_TOKEN
905
906
  ]
906
907
  let userParams = {};
907
908
  if (flowAttributes) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.97",
3
+ "version": "0.2.99",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -171,13 +171,13 @@ class DirectivesChatbotPlug {
171
171
  async nextDirective(directives) {
172
172
  if (this.log) {console.log("....nextDirective() checkStep():");}
173
173
  const go_on = await TiledeskChatbot.checkStep(
174
- 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
175
175
  );
176
176
  // const current_step = await TiledeskChatbot.currentStep(this.context.tdcache, this.context.requestId);
177
177
  // if (this.log) {console.log("........nextDirective() currentStep:", current_step);}
178
- if (go_on == false) {
178
+ if (go_on.error) {
179
179
  if (this.log) {console.log("go_on == false! nextDirective() Stopped!");}
180
- return this.errorMessage("Request error: anomaly detection. MAX ACTIONS exeeded.");
180
+ return this.errorMessage(go_on.error); //"Request error: anomaly detection. MAX ACTIONS exeeded.");
181
181
  }
182
182
  // else if (go_on == 2) {
183
183
  // return null;
@@ -9,6 +9,7 @@ class DirWait {
9
9
  }
10
10
  this.context = context;
11
11
  this.tdclient = context.tdclient;
12
+ this.chatbot = context.chatbot;
12
13
  this.tdcache = context.tdcache;
13
14
  this.requestId = context.requestId;
14
15
  this.log = context.log;
@@ -49,9 +50,15 @@ class DirWait {
49
50
  }
50
51
 
51
52
  async go(action, callback) {
52
- // reset step
53
- const step_key = TiledeskChatbot.requestCacheKey(this.requestId) + ":step";
54
- await this.chatbot.addParameter( step_key, 0 );
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
+ }
55
62
  setTimeout(() => {
56
63
  callback();
57
64
  }, action.millis);