@tiledesk/tiledesk-tybot-connector 0.1.76 → 0.1.78

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,14 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ ### 0.1.78
9
+ - web request assign to fix
10
+ - fix filter on messages: groups removed
11
+
12
+ ### 0.1.77
13
+ - Refactored DirDeflectToHelpcenter. Added projectId option (to allow search from workspaces in different projects)
14
+ - increased MAX_STEPS for MAX ACTIONS EXEEDED error, from 20 to 200
15
+
8
16
  ### 0.1.76
9
17
  - added projectId debug log for production
10
18
 
@@ -12,7 +12,7 @@ const { DirUnlockIntent } = require('../tiledeskChatbotPlugs/directives/DirUnloc
12
12
 
13
13
  class TiledeskChatbot {
14
14
 
15
- static MAX_STEPS = 20;
15
+ static MAX_STEPS = 200;
16
16
 
17
17
  constructor(config) {
18
18
  if (!config.botsDataSource) {
@@ -490,11 +490,11 @@ class TiledeskChatbot {
490
490
  await _tdcache.hset(parameter_key, parameter_name, parameter_value);
491
491
  }
492
492
 
493
- static async checkStep(_tdcache, requestId, max_steps) {
494
- // console.log("CHECKING ON MAX_STEPS:", max_steps);
493
+ static async checkStep(_tdcache, requestId, max_steps, log) {
494
+ if (log) {console.log("CHECKING ON MAX_STEPS:", max_steps);}
495
495
  let go_on = true; // continue
496
496
  const parameter_key = TiledeskChatbot.requestCacheKey(requestId) + ":step";
497
- // console.log("__parameter_key:", parameter_key);
497
+ if (log) {console.log("__parameter_key:", parameter_key);}
498
498
  await _tdcache.incr(parameter_key);
499
499
  // console.log("incr-ed");
500
500
  let _current_step = await _tdcache.get(parameter_key);
@@ -505,8 +505,9 @@ class TiledeskChatbot {
505
505
  // current_step += 1;
506
506
  // await _tdcache.set(parameter_key, current_step); // increment step
507
507
  // console.log("CURRENT-STEP:", current_step);
508
- if (current_step > max_steps) { // max_steps limit just violated
509
- // console.log("CURRENT-STEP > MAX_STEPS!", current_step);
508
+ if (current_step > max_steps) {
509
+ if (log) {console.log("max_steps limit just violated");}
510
+ if (log) {console.log("CURRENT-STEP > MAX_STEPS!", current_step);}
510
511
  // await TiledeskChatbot.resetStep(_tdcache, requestId);
511
512
  // go_on = 1; // stop execution, send error message
512
513
  go_on = false
@@ -161,7 +161,8 @@ class TiledeskChatbotUtil {
161
161
  const jsonCondition = commands[i].message["_tdJSONCondition"];
162
162
  // console.log("jsonCondition:", jsonCondition);
163
163
  if (jsonCondition) {
164
- const expression = TiledeskExpression.JSONGroupsToExpression(jsonCondition.groups);
164
+ // const expression = TiledeskExpression.JSONGroupsToExpression(jsonCondition.groups);
165
+ const expression = TiledeskExpression.JSONGroupToExpression(jsonCondition);
165
166
  // console.log("full json condition expression eval on command.message:", expression);
166
167
  const conditionResult = new TiledeskExpression().evaluateStaticExpression(expression, variables);
167
168
  console.log("conditionResult:", conditionResult);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.1.76",
3
+ "version": "0.1.78",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -130,6 +130,7 @@ class DirectivesChatbotPlug {
130
130
  departmentId: depId,
131
131
  tdcache: tdcache,
132
132
  tdclient: tdclient,
133
+ HELP_CENTER_API_ENDPOINT: this.HELP_CENTER_API_ENDPOINT,
133
134
  log: this.log
134
135
  }
135
136
  if (this.log) {console.log("this.context.departmentId is:", this.context.departmentId);}
@@ -143,14 +144,14 @@ class DirectivesChatbotPlug {
143
144
  }
144
145
 
145
146
  async nextDirective(directives) {
146
- // console.log("....nextDirective() checkStep():");
147
+ if (this.log) {console.log("....nextDirective() checkStep():");}
147
148
  const go_on = await TiledeskChatbot.checkStep(
148
- this.context.tdcache, this.context.requestId, TiledeskChatbot.MAX_STEPS
149
+ this.context.tdcache, this.context.requestId, TiledeskChatbot.MAX_STEPS, this.log
149
150
  );
150
151
  // const current_step = await TiledeskChatbot.currentStep(this.context.tdcache, this.context.requestId);
151
- // console.log("........nextDirective() currentStep:", current_step);
152
+ // if (this.log) {console.log("........nextDirective() currentStep:", current_step);}
152
153
  if (go_on == false) {
153
- // console.log("..nextDirective() Stopped!");
154
+ if (this.log) {console.log("go_on == false! nextDirective() Stopped!");}
154
155
  return this.errorMessage("Request error: anomaly detection. MAX ACTIONS exeeded.");
155
156
  }
156
157
  // else if (go_on == 2) {
@@ -225,7 +226,7 @@ class DirectivesChatbotPlug {
225
226
  else if (directive_name === Directives.MESSAGE) {
226
227
  new DirMessage(context).execute(directive, async (stop) => {
227
228
  if (stop) {
228
- if (context.log) { console.log("Stopping Actions on:", directive);}
229
+ if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
229
230
  this.theend();
230
231
  }
231
232
  else {
@@ -251,7 +252,7 @@ class DirectivesChatbotPlug {
251
252
  else if (directive_name === Directives.IF_OPEN_HOURS) {
252
253
  new DirIfOpenHours(context).execute(directive, async (stop) => {
253
254
  if (stop) {
254
- if (context.log) { console.log("Stopping Actions on:", directive);}
255
+ if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
255
256
  this.theend();
256
257
  }
257
258
  else {
@@ -266,7 +267,7 @@ class DirectivesChatbotPlug {
266
267
  // console.log("...DirIfOnlineAgents")
267
268
  new DirIfOnlineAgents(context).execute(directive, async () => {
268
269
  if (stop) {
269
- if (context.log) { console.log("Stopping Actions on:", directive);}
270
+ if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
270
271
  this.theend();
271
272
  }
272
273
  else {
@@ -289,7 +290,7 @@ class DirectivesChatbotPlug {
289
290
  new DirCondition(context).execute(directive, async (stop) => {
290
291
  if (context.log) { console.log("stop on condition?", stop);}
291
292
  if (stop == true) {
292
- if (context.log) { console.log("Stopping Actions on:", directive);}
293
+ if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
293
294
  this.theend();
294
295
  }
295
296
  else {
@@ -303,7 +304,7 @@ class DirectivesChatbotPlug {
303
304
  new DirJSONCondition(context).execute(directive, async (stop) => {
304
305
  // console.log("stop on condition?", stop);
305
306
  if (stop == true) {
306
- if (context.log) { console.log("Stopping Actions on:", directive);}
307
+ if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
307
308
  this.theend();
308
309
  }
309
310
  else {
@@ -478,6 +479,19 @@ class DirectivesChatbotPlug {
478
479
  this.process(next_dir);
479
480
  });
480
481
  }
482
+ else if (directive_name === Directives.ASK_HELP_CENTER) {
483
+ new DirDeflectToHelpCenter(context).execute(directive, async (stop) => {
484
+ if (context.log) { console.log("DeflectToHelpCenter stop?", stop);}
485
+ if (stop == true) {
486
+ if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
487
+ this.theend();
488
+ }
489
+ else {
490
+ let next_dir = await this.nextDirective(this.directives);
491
+ this.process(next_dir);
492
+ }
493
+ });
494
+ }
481
495
  else {
482
496
  //console.log("Unhandled Post-message Directive:", directive_name);
483
497
  let next_dir = await this.nextDirective(this.directives);
@@ -485,6 +499,7 @@ class DirectivesChatbotPlug {
485
499
  }
486
500
  }
487
501
 
502
+ // DEPRECATED
488
503
  processInlineDirectives(pipeline, theend) {
489
504
  const directives = this.directives;
490
505
  if (!directives || directives.length === 0) {
@@ -1,52 +1,72 @@
1
- //const { HelpCenter } = require('./HelpCenter');
2
1
  const { HelpCenterQuery } = require('@tiledesk/helpcenter-query-client');
2
+ const { TiledeskChatbot } = require('../../models/TiledeskChatbot.js');
3
+ const { TiledeskChatbotConst } = require('../../models/TiledeskChatbotConst');
3
4
  const ms = require('minimist-string');
4
5
 
5
6
  class DirDeflectToHelpCenter {
6
7
 
7
- constructor(config) {
8
- if (!config.projectId) {
9
- throw new Error('projectId is mandatory.');
8
+ constructor(context) {
9
+ if (!context) {
10
+ throw new Error('context object is mandatory.');
10
11
  }
11
- if (config.HELP_CENTER_API_ENDPOINT) {
12
- this.helpcenter_api_endpoint = config.HELP_CENTER_API_ENDPOINT;
13
- }
14
- //console.log("Using helpcenter_api_endpoint:", this.helpcenter_api_endpoint)
15
- this.projectId = config.projectId;
12
+ this.context = context;
13
+ this.log = context.log;
16
14
  }
17
15
 
18
- async execute(directive, pipeline, maxresults, completion) {
19
- let workspace_id = null;
20
- let default_hc_reply = "No matching reply but...\n\nI found something interesting in the Help Center 🧐\n\nTake a look 👇";
21
- let hc_reply = default_hc_reply;
22
- if (directive.parameter) {
23
- // console.log("processing parameters")
16
+ async execute(directive, callback) {
17
+ let action;
18
+ if (directive.action) {
19
+ action = directive.action
20
+ }
21
+ else if (directive.parameter) {
24
22
  const params = this.parseParams(directive.parameter);
25
- // console.log("parameters found", params);
26
23
  workspace_id = params.workspace_id;
27
- // console.log("workspaceid found", workspace_id);
28
24
  if (params.hc_reply) {
29
25
  hc_reply = params.hc_reply;
30
- // console.log("hc_reply found", hc_reply);
31
26
  }
32
27
  }
33
- else {
34
- hc_reply = default_hc_reply;
28
+ this.go(action, (stop) => {
29
+ callback(stop);
30
+ });
31
+ }
32
+
33
+ async go(action, callback) { //directive, pipeline, maxresults, completion) {
34
+ let default_hc_reply = "No matching reply but...\n\nI found something interesting in the Help Center 🧐\n\nTake a look 👇";
35
+ let hc_reply = default_hc_reply;
36
+ if (action.hcReply) {
37
+ hc_reply = action.hcReply;
38
+ }
39
+ let workspace_id = action.workspaceId;
40
+ let project_id = this.context.projectId;
41
+ if (action.projectId) {
42
+ project_id = action.projectId;
43
+ }
44
+ let maxresults = 3;
45
+ if (action.maxresults) {
46
+ maxresults = action.maxresults;
35
47
  }
36
- //console.log("workspace_id param:", workspace_id);
37
- //console.log("hc_reply param:", hc_reply);
38
- let message = pipeline.message;
48
+ let url_target = "blank";
49
+ if (action.urlTarget) {
50
+ url_target = action.urlTarget;
51
+ }
52
+ // let message = pipeline.message;
39
53
  //console.log("help center message", JSON.stringify(message));
40
- const original_text = message.attributes.intent_info.question_payload.text;
41
- //console.log("original_text", original_text);
42
- if (original_text && original_text.trim() != '') {
54
+ const last_user_text = await TiledeskChatbot.getParameterStatic(
55
+ this.context.tdcache,
56
+ this.context.requestId,
57
+ TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY);
58
+ if (this.log) {console.log("last_user_text", last_user_text);}
59
+ if (last_user_text && last_user_text.trim() !== '') {
43
60
  const helpcenter = new HelpCenterQuery({
44
61
  APIKEY: "__",
45
- APIURL: this.helpcenter_api_endpoint,
46
- projectId: this.projectId,
62
+ projectId: project_id,
47
63
  log: false
48
64
  });
65
+ if (this.helpcenter_api_endpoint) {
66
+ helpcenter.APIURL = this.helpcenter_api_endpoint
67
+ }
49
68
  if (!workspace_id) {
69
+ if (this.log) {console.log("No workspaces_id. Listing all workspaces to eventually select the first");}
50
70
  try {
51
71
  // find/select the first workspace
52
72
  const workspaces = await helpcenter.allWorkspaces();
@@ -55,38 +75,87 @@ class DirDeflectToHelpCenter {
55
75
  // console.log("First Workspace selected", workspaces[0]);
56
76
  }
57
77
  else {
58
- // console.log("No Workspace found");
59
- completion();
78
+ if (this.log) {console.log("No Workspaces found");}
79
+ callback(false);
60
80
  }
61
81
  }
62
82
  catch(err) {
63
83
  console.error("deflectToHelpCenter Error (search workspaces):", err);
64
- completion();
84
+ callback(false);
65
85
  }
66
86
  }
67
- // console.log("searching on workspace_id:", workspace_id);
87
+ if (this.log) {console.log("searching on workspace_id:", workspace_id);}
68
88
  try {
69
- const results = await helpcenter.search(workspace_id, original_text, maxresults);
89
+ const results = await helpcenter.search(workspace_id, last_user_text, maxresults);
70
90
  if (results && results.length > 0) {
71
- // console.log("Successfully got results", results);
72
- // console.log("Sending REPL", hc_reply);
73
- pipeline.message.text = hc_reply;
91
+ if (this.log) {console.log("Successfully got results", JSON.stringify(results));}
92
+ if (this.log) {console.log("Sending hcReply", hc_reply);}
93
+ // pipeline.message.text = hc_reply;
94
+ let buttons = [];
74
95
  results.forEach(content => {
75
- if (content.url.charAt(content.url.length -1) != "/") {
76
- content.url = content.url + "/"
96
+ const button = {
97
+ "type": "url",
98
+ "value": content.title,
99
+ "link": content.url,
100
+ "target": url_target
101
+ }
102
+ buttons.push(button);
103
+ // if (content.url.charAt(content.url.length -1) != "/") {
104
+ // content.url = content.url + "/"
105
+ // }
106
+ // pipeline.message.text += "\n* " + content.title + " > " + content.url;
107
+ });
108
+ const message = {
109
+ "text": hc_reply,
110
+ "attributes": {
111
+ "disableInputMessage": false,
112
+ "commands": [
113
+ {
114
+ "type": "wait",
115
+ "time": 0
116
+ },
117
+ {
118
+ "type": "message",
119
+ "message": {
120
+ "type": "text",
121
+ "text": hc_reply,
122
+ "attributes": {
123
+ "attachment": {
124
+ "type": "template",
125
+ "buttons": buttons
126
+ }
127
+ }
128
+ }
129
+ }
130
+ ]
77
131
  }
78
- pipeline.message.text += "\n* " + content.title + " > " + content.url;
132
+ }
133
+
134
+ if (this.log) {console.log("HC reply:", JSON.stringify(message))};
135
+ this.context.tdclient.sendSupportMessage(
136
+ this.context.requestId,
137
+ message,
138
+ (err) => {
139
+ if (err) {
140
+ console.error("Error sending reply:", err.message);
141
+ callback(false);
142
+ }
143
+ if (this.log) {console.log("Reply message sent.");}
144
+ callback(true);
79
145
  });
80
146
  }
81
- completion();
147
+ else {
148
+ if (this.log) {console.log("Nothing found in Help Center. projectId:", project_id, "workspaceId:", workspace_id);}
149
+ callback(false);
150
+ }
82
151
  }
83
152
  catch(err) {
84
153
  console.error("deflectToHelpCenter Error (searching results):", err);
85
- completion();
154
+ callback(false);
86
155
  }
87
156
  }
88
157
  else {
89
- completion();
158
+ callback(false);
90
159
  }
91
160
  }
92
161
 
@@ -1,4 +1,3 @@
1
- const { HelpCenterQuery } = require('@tiledesk/helpcenter-query-client');
2
1
  const { param } = require('express/lib/request');
3
2
  const ms = require('minimist-string');
4
3
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
@@ -1,4 +1,3 @@
1
- const { HelpCenterQuery } = require('@tiledesk/helpcenter-query-client');
2
1
  const { param } = require('express/lib/request');
3
2
  const ms = require('minimist-string');
4
3
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
@@ -18,7 +18,7 @@ class Directives {
18
18
  static IF_NO_AGENTS = "ifnoagents"; // DEPRECATED
19
19
  static IF_AGENTS = "ifagents"; // DEPRECATED
20
20
 
21
- static DEFLECT_TO_HELP_CENTER = "deflecttohelpcenter";
21
+ static ASK_HELP_CENTER = "askhelpcenter";
22
22
  static WAIT = "wait";
23
23
  static LOCK_INTENT = "lockintent";
24
24
  static UNLOCK_INTENT = "unlockintent";
@@ -30,9 +30,9 @@ class Directives {
30
30
  static IF_ONLINE_AGENTS = "ifonlineagents";
31
31
  static IF_NOT_OPEN_HOURS = "ifnotopenhours"; // DEPRECATED
32
32
  static FUNCTION_VALUE = "functionvalue";
33
- static CONDITION = "condition";
33
+ static CONDITION = "condition"; // DEPRECATED
34
34
  static JSON_CONDITION = "jsoncondition";
35
- static ASSIGN = "assign";
35
+ static ASSIGN = "assign"; // DEPRECATED
36
36
  static SET_ATTRIBUTE = "setattribute";
37
37
  // static IF_AVAILABLE_AGENTS = "ifavailableagents"; // TODO
38
38
  // static IF_NO_AVAILABLE_AGENTS = "ifnotavailableagents"; // TODO