@tiledesk/tiledesk-tybot-connector 0.1.56 → 0.1.58

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,15 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ ### 0.1.57
9
+ - Added Filter on message.commands based on optional _tdJSONCondition field
10
+ - TiledeskExpression: introduced "type" for operand2 (type: "const" and type: "var")
11
+ - added addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url)
12
+ - renamed all system attributes, "td" prefix removed
13
+ - added class IntentsMachineFactory
14
+ - added headersString in WebRequest
15
+ - fixed await bot bug. Full text search and webhook affected
16
+
8
17
  ### 0.1.56
9
18
  - DirWebRequest added
10
19
  - SetAttribute added
@@ -88,11 +88,29 @@ class TiledeskExpression {
88
88
  }
89
89
 
90
90
  // public
91
- evaluateStaticExpression(expression) {
92
- const result = new TiledeskExpression().evaluate(expression);
91
+ evaluateStaticExpression(expression, variables) {
92
+ const result = new TiledeskExpression().evaluateJavascriptExpression(expression, variables);
93
93
  return result;
94
94
  }
95
95
 
96
+ evaluateJavascriptExpression(expression, context) {
97
+ // console.log("evaluating:", expression)
98
+ console.log("context:", context)
99
+ let res;
100
+ try {
101
+ const vm = new VM({
102
+ timeout: 200,
103
+ allowAsync: false,
104
+ sandbox: context
105
+ });
106
+ res = vm.run(`let $data = this;${expression}`);
107
+ }
108
+ catch (err) {
109
+ console.error("TiledeskExpression.evaluate() error:", err.message, "evaluating expression: '" + expression + "'");
110
+ }
111
+ return res;
112
+ }
113
+
96
114
  // private
97
115
  // evaluate(expression, context) {
98
116
  // let fn;
@@ -108,6 +126,7 @@ class TiledeskExpression {
108
126
  // return res;
109
127
  // }
110
128
 
129
+ // DEPRECATED
111
130
  evaluate(expression, context) {
112
131
  // console.log("evaluating:", expression)
113
132
  // console.log("context:", context)
@@ -135,9 +154,16 @@ class TiledeskExpression {
135
154
  // console.log("operator:", operator);
136
155
  const applyPattern = operator.applyPattern;
137
156
  // console.log("applyPattern:", applyPattern);
138
- const operand1_s = TiledeskExpression.stringValueOperand(condition.operand1, variables);
157
+ const operand1_s = TiledeskExpression.variableOperand(condition.operand1);
139
158
  // console.log("operand1_s:", operand1_s);
140
- const operand2_s = TiledeskExpression.stringValueOperand(condition.operand2, variables);
159
+ let operand2_s;
160
+ if (condition.operand2 && condition.operand2.type && condition.operand2.type === "const") {
161
+ operand2_s = TiledeskExpression.stringValueOperand(condition.operand2.value, variables);
162
+ }
163
+ else if (condition.operand2 && condition.operand2.type && condition.operand2.type === "var") {
164
+ operand2_s = TiledeskExpression.variableOperand(condition.operand2.name);
165
+ }
166
+
141
167
  // console.log("operand2_s:", operand2_s);
142
168
  const expression =
143
169
  applyPattern
@@ -182,6 +208,10 @@ class TiledeskExpression {
182
208
  }
183
209
 
184
210
 
211
+ static variableOperand(operand) {
212
+ return "$data." + operand;
213
+ }
214
+
185
215
  static stringValueOperand(operand, variables) {
186
216
  // return operand;
187
217
  if (!operand) {
package/index.js CHANGED
@@ -19,6 +19,7 @@ const { TiledeskIntentsMachine } = require('./models/TiledeskIntentsMachine.js')
19
19
  // const { MockActions } = require('./MockActions');
20
20
  const { MockBotsDataSource } = require('./models/MockBotsDataSource.js');
21
21
  const { TiledeskChatbotConst } = require('./models/TiledeskChatbotConst');
22
+ const { IntentsMachineFactory } = require('./models/IntentsMachineFactory');
22
23
 
23
24
  //router.use(cors());
24
25
  router.use(bodyParser.json({limit: '50mb'}));
@@ -90,6 +91,7 @@ router.post('/ext/:botid', async (req, res) => {
90
91
  let botsDS;
91
92
  if (!staticBots) {
92
93
  botsDS = new MongodbBotsDataSource({projectId: projectId, botId: botId});
94
+ if (log) {console.log("botsDS created with Mongo");}
93
95
  }
94
96
  else {
95
97
  botsDS = new MockBotsDataSource(staticBots);
@@ -101,32 +103,41 @@ router.post('/ext/:botid', async (req, res) => {
101
103
  try {
102
104
  // bot = await botsDS.getBotById(botId);
103
105
  // bot = await botById(botId, projectId, tdcache, botsDS);
104
- bot = botsDS.getBotByIdCache(botId, tdcache);
106
+ bot = await botsDS.getBotByIdCache(botId, tdcache);
105
107
  }
106
108
  catch(error) {
107
109
  console.error("Error getting botId:", botId);
108
110
  console.error("Error getting bot was:", error);
109
111
  return;
110
112
  }
113
+ if (log) {console.log("bot found:", bot);}
111
114
 
112
115
  let intentsMachine;
113
116
  if (!staticBots) {
114
- if (log) {console.log("intentsMachine to MongoDB");}
115
- intentsMachine = new MongodbIntentsMachine({projectId: projectId, language: bot.language, log});
116
- if (bot.intentsEngine === "tiledesk-ai") {
117
- if (log) {console.log("intentsMachine to tiledesk-ai");}
118
- intentsMachine = new TiledeskIntentsMachine(
119
- {
120
- //projectId: projectId,
121
- //language: bot.language,
122
- botId: botId
123
- //TILEBOT_AI_ENDPOINT: process.env.TILEBOT_AI_ENDPOINT
124
- });
125
- }
117
+ intentsMachine = IntentsMachineFactory.getMachine(bot, botId, projectId, log);
126
118
  }
127
119
  else {
128
120
  intentsMachine = {}
129
121
  }
122
+
123
+ // let intentsMachine;
124
+ // if (!staticBots) {
125
+ // if (log) {console.log("intentsMachine to MongoDB");}
126
+ // intentsMachine = new MongodbIntentsMachine({projectId: projectId, language: bot.language, log});
127
+ // if (bot.intentsEngine === "tiledesk-ai") {
128
+ // if (log) {console.log("intentsMachine to tiledesk-ai");}
129
+ // intentsMachine = new TiledeskIntentsMachine(
130
+ // {
131
+ // //projectId: projectId,
132
+ // //language: bot.language,
133
+ // botId: botId
134
+ // //TILEBOT_AI_ENDPOINT: process.env.TILEBOT_AI_ENDPOINT
135
+ // });
136
+ // }
137
+ // }
138
+ // else {
139
+ // intentsMachine = {}
140
+ // }
130
141
  //const intentsMachine = new TiledeskIntentsMachine({API_ENDPOINT: "https://MockIntentsMachine.tiledesk.repl.co", log: true});
131
142
  const chatbot = new TiledeskChatbot({
132
143
  botsDataSource: botsDS,
@@ -141,8 +152,6 @@ router.post('/ext/:botid', async (req, res) => {
141
152
  projectId: projectId,
142
153
  log: log
143
154
  });
144
- // console.log("log:", log)
145
- // process.exit(1)
146
155
  await updateRequestVariables(chatbot, message, projectId, requestId);
147
156
 
148
157
  let reply = await chatbot.replyToMessage(message);
@@ -233,6 +242,8 @@ router.post('/ext/:botid', async (req, res) => {
233
242
  async function updateRequestVariables(chatbot, message, projectId, requestId) {
234
243
  // update request context
235
244
  const messageId = message._id;
245
+ const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
246
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url);
236
247
  await chatbot.addParameter(TiledeskChatbotConst.REQ_PROJECT_ID_KEY, projectId);
237
248
  // TODO add projectName too
238
249
  await chatbot.addParameter(TiledeskChatbotConst.REQ_REQUEST_ID_KEY, requestId);
@@ -529,7 +540,6 @@ async function startApp(settings, completionCallback) {
529
540
  mongoose.connect(settings.MONGODB_URI, { "useNewUrlParser": true, "autoIndex": false }, async (err) => {
530
541
  if (err) {
531
542
  console.error('(Tilebot) Failed to connect to MongoDB on ' + settings.MONGODB_URI + " ", err);
532
- //process.exit(1); // add => exitOnFail: true
533
543
  }
534
544
  else {
535
545
  console.log("(Tilebot) mongodb connection ok.");
@@ -0,0 +1,21 @@
1
+ const { MongodbIntentsMachine } = require('./MongodbIntentsMachine.js');
2
+
3
+ class IntentsMachineFactory {
4
+
5
+ static getMachine(bot, botId, projectId, log) {
6
+ let machine;
7
+ if (bot.intentsEngine === "tiledesk-ai") {
8
+ machine = new TiledeskIntentsMachine(
9
+ {
10
+ botId: botId
11
+ });
12
+ }
13
+ else {
14
+ if (log) {console.log("Setting MongodbIntentsMachine with bot:", JSON.stringify(bot));}
15
+ machine = new MongodbIntentsMachine({projectId: projectId, language: bot.language, log});
16
+ }
17
+ return machine;
18
+ }
19
+ }
20
+
21
+ module.exports = { IntentsMachineFactory }
@@ -26,6 +26,7 @@ class MongodbIntentsMachine {
26
26
  var sort = undefined;
27
27
  var search_obj = { "$search": text };
28
28
 
29
+ if (this.log) {console.log("chatbot lang:", this.language);}
29
30
  if (this.language) {
30
31
  search_obj["$language"] = this.language;
31
32
  }
@@ -449,6 +449,7 @@ class TiledeskChatbot {
449
449
  static_bot_answer.attributes.updateUserFullname = clientUpdateUserFullname;
450
450
  }
451
451
  // exec webhook
452
+ if (this.log) {console.log("exec webhook on bot:", JSON.stringify(this.bot));}
452
453
  const bot_answer = await this.execWebhook(static_bot_answer, message, this.bot, context, this.token);
453
454
  if (this.log) {console.log("bot_answer ready:", JSON.stringify(bot_answer));}
454
455
  return bot_answer;
@@ -1,19 +1,36 @@
1
1
  class TiledeskChatbotConst {
2
2
 
3
- static REQ_DEPARTMENT_ID_KEY = "tdDepartmentId";
4
- static REQ_PROJECT_ID_KEY = "tdProjectId";
5
- static REQ_REQUEST_ID_KEY = "tdRequestId";
6
- static REQ_CHATBOT_NAME_KEY = "tdChatbotName";
7
- static REQ_LAST_USER_TEXT_KEY = "tdLastUserText";
8
- static REQ_LAST_MESSAGE_ID_KEY = "tdLastMessageId";
9
- static REQ_COUNTRY_KEY = "tdCountry";
10
- static REQ_CITY_KEY = "tdCity";
11
- static REQ_USER_SOURCE_PAGE_KEY = "tdUserSourcePage";
12
- static REQ_USER_LANGUAGE_KEY = "tdUserLanguage";
13
- static REQ_USER_AGENT_KEY = "tdUserAgent";
14
- static REQ_DEPARTMENT_NAME_KEY = "tdDepartmentName";
15
- static REQ_END_USER_ID_KEY = "tdEndUserId";
16
- static REQ_END_USER_IP_ADDRESS_KEY = "tdEndUserIpAddress";
3
+ static REQ_DEPARTMENT_ID_KEY = "department_id";
4
+ static REQ_PROJECT_ID_KEY = "project_id";
5
+ static REQ_REQUEST_ID_KEY = "conversation_id";
6
+ static REQ_CHATBOT_NAME_KEY = "chatbot_name";
7
+ static REQ_LAST_USER_TEXT_KEY = "last_user_text";
8
+ static REQ_LAST_MESSAGE_ID_KEY = "last_message_id";
9
+ static REQ_COUNTRY_KEY = "user_country";
10
+ static REQ_CITY_KEY = "user_city";
11
+ static REQ_USER_SOURCE_PAGE_KEY = "user_source_page";
12
+ static REQ_USER_LANGUAGE_KEY = "user_language";
13
+ static REQ_USER_AGENT_KEY = "user_agent";
14
+ static REQ_DEPARTMENT_NAME_KEY = "department_name";
15
+ static REQ_END_USER_ID_KEY = "user_id";
16
+ static REQ_END_USER_IP_ADDRESS_KEY = "user_ip_address";
17
+ static REQ_CHAT_URL = "chat_url";
18
+
19
+ // static REQ_DEPARTMENT_ID_KEY = "tdDepartmentId";
20
+ // static REQ_PROJECT_ID_KEY = "projectId";
21
+ // static REQ_REQUEST_ID_KEY = "tdRequestId";
22
+ // static REQ_CHATBOT_NAME_KEY = "tdChatbotName";
23
+ // static REQ_LAST_USER_TEXT_KEY = "tdLastUserText";
24
+ // static REQ_LAST_MESSAGE_ID_KEY = "tdLastMessageId";
25
+ // static REQ_COUNTRY_KEY = "tdCountry";
26
+ // static REQ_CITY_KEY = "tdCity";
27
+ // static REQ_USER_SOURCE_PAGE_KEY = "tdUserSourcePage";
28
+ // static REQ_USER_LANGUAGE_KEY = "tdUserLanguage";
29
+ // static REQ_USER_AGENT_KEY = "tdUserAgent";
30
+ // static REQ_DEPARTMENT_NAME_KEY = "tdDepartmentName";
31
+ // static REQ_END_USER_ID_KEY = "tdEndUserId";
32
+ // static REQ_END_USER_IP_ADDRESS_KEY = "tdEndUserIpAddress";
33
+ // static REQ_CHAT_URL = "chat_url";
17
34
 
18
35
  }
19
36
 
@@ -1,3 +1,5 @@
1
+ const { TiledeskExpression } = require('../TiledeskExpression');
2
+
1
3
  class TiledeskChatbotUtil {
2
4
 
3
5
  static parseIntent(explicit_intent_name) {
@@ -44,24 +46,36 @@ class TiledeskChatbotUtil {
44
46
  // }
45
47
  // }
46
48
 
47
- static filterOnLanguage(commands, lang) {
48
- if (!lang) {
49
+ static filterOnVariables(commands, variables) {
50
+ if (!variables) {
49
51
  return;
50
52
  }
51
53
  if (commands.length > 0) {
52
54
  for (let i = commands.length - 1; i >= 0; i--) {
53
- // console.log("...commands[" + i + "]");
55
+ console.log("...commands[" + i + "]");
54
56
  if (commands[i].type === "message") { // is a message, not wait
55
57
  // console.log("commands[i]:", commands[i].message.lang);
56
58
  // console.log("commands[i]:", lang, (commands[i].message["lang"] === lang));
57
- if (commands[i].message["lang"] && !(commands[i].message["lang"] === lang)) { // if there is a filter and the filter is false, remove
59
+
60
+ // if (commands[i].message["lang"] && !(commands[i].message["lang"] === lang)) { // if there is a filter and the filter is false, remove
61
+ const jsonCondition = commands[i].message["_tdJSONCondition"];
62
+ console.log("jsonCondition:", jsonCondition);
63
+ if (jsonCondition) {
64
+ const expression = TiledeskExpression.JSONGroupsToExpression(jsonCondition.groups);
65
+ console.log("full json condition expression eval on command.message:", expression);
66
+ const conditionResult = new TiledeskExpression().evaluateStaticExpression(expression, variables);
67
+ console.log("conditionResult:", conditionResult);
68
+ // FALSE
58
69
  // console.log("commands[i]lang:", commands[i]);
59
- commands.splice(i, 1);
60
- if (commands[i-1]) {
61
- // console.log("commands[i-1]?:", commands[i-1]);
62
- if (commands[i-1].type === "wait") {
63
- commands.splice(i-1, 1);
64
- i--;
70
+ if (conditionResult === false) {
71
+ console.log("deleting command:", commands[i]);
72
+ commands.splice(i, 1);
73
+ if (commands[i-1]) {
74
+ // console.log("commands[i-1]?:", commands[i-1]);
75
+ if (commands[i-1].type === "wait") {
76
+ commands.splice(i-1, 1);
77
+ i--;
78
+ }
65
79
  }
66
80
  }
67
81
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.1.56",
3
+ "version": "0.1.58",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -7,29 +7,41 @@ describe('JSON to expression', function() {
7
7
  it('test condition operand lessThan (false)', async () => {
8
8
  const condition = {
9
9
  "type": "condition",
10
- "operand1": "12",
10
+ "operand1": "age",
11
11
  "operator": TiledeskExpression.OPERATORS.lessThan.name,
12
- "operand2": "10"
12
+ "operand2": {
13
+ type: "const",
14
+ value: "10"
15
+ }
16
+ }
17
+ const vars = {
18
+ age: 12
13
19
  }
14
-
15
20
  const expression = TiledeskExpression.JSONConditionToExpression(condition);
16
- assert(expression === "Number(\"" + condition.operand1 + "\") < Number(\"" + condition.operand2 + "\")");
17
- const result = new TiledeskExpression().evaluateStaticExpression(expression);
21
+ console.log("expression:", expression);
22
+ assert(expression === 'Number($data.age) < Number("10")');
23
+ const result = new TiledeskExpression().evaluateStaticExpression(expression, vars);
18
24
  assert(result === false);
19
25
  });
20
26
 
21
27
  it('test condition operand lessThan (true)', async () => {
22
28
  const condition = {
23
29
  "type": "condition",
24
- "operand1": "10",
30
+ "operand1": "age",
25
31
  "operator": TiledeskExpression.OPERATORS.lessThan.name,
26
- "operand2": "12"
32
+ "operand2": {
33
+ type: "const",
34
+ value: "12"
35
+ }
27
36
  }
28
37
 
38
+ const vars = {
39
+ age: 10
40
+ }
29
41
  const expression = TiledeskExpression.JSONConditionToExpression(condition);
30
- // console.log("expression:", expression);
31
- assert(expression === "Number(\"" + condition.operand1 + "\") < Number(\"" + condition.operand2 + "\")");
32
- const result = new TiledeskExpression().evaluateStaticExpression(expression);
42
+ console.log("expression:", expression);
43
+ assert(expression === 'Number($data.age) < Number("12")');
44
+ const result = new TiledeskExpression().evaluateStaticExpression(expression, vars);
33
45
  // console.log("result:", result);
34
46
  assert(result === true);
35
47
  });
@@ -37,14 +49,20 @@ describe('JSON to expression', function() {
37
49
  it('test condition operand greaterThan (true)', async () => {
38
50
  const condition = {
39
51
  "type": "condition",
40
- "operand1": "12",
52
+ "operand1": "age",
41
53
  "operator": TiledeskExpression.OPERATORS.greaterThan.name,
42
- "operand2": "10"
54
+ "operand2": {
55
+ type: "const",
56
+ value: "10"
57
+ }
58
+ }
59
+ const vars = {
60
+ age: 12
43
61
  }
44
-
45
62
  const expression = TiledeskExpression.JSONConditionToExpression(condition);
46
- assert(expression === "Number(\"" + condition.operand1 + "\") > Number(\"" + condition.operand2 + "\")");
47
- const result = new TiledeskExpression().evaluateStaticExpression(expression);
63
+ console.log("expression:", expression);
64
+ assert(expression === 'Number($data.age) > Number("10")');
65
+ const result = new TiledeskExpression().evaluateStaticExpression(expression, vars);
48
66
  // console.log("result:", result);
49
67
  assert(result === true);
50
68
  });
@@ -52,14 +70,20 @@ describe('JSON to expression', function() {
52
70
  it('test condition operand startsWith (true)', async () => {
53
71
  const condition = {
54
72
  "type": "condition",
55
- "operand1": "Andrea",
73
+ "operand1": "name",
56
74
  "operator": TiledeskExpression.OPERATORS.startsWith.name,
57
- "operand2": "And"
75
+ "operand2": {
76
+ type: "const",
77
+ value: "And"
78
+ }
79
+ }
80
+ const vars = {
81
+ name: "Andrea"
58
82
  }
59
-
60
83
  const expression = TiledeskExpression.JSONConditionToExpression(condition);
61
- assert(expression === "String(\"" + condition.operand1 + "\").startsWith(String(\"" + condition.operand2 + "\"))");
62
- const result = new TiledeskExpression().evaluateStaticExpression(expression);
84
+ console.log("expression:", expression);
85
+ assert(expression === 'String($data.name).startsWith(String("And"))');
86
+ const result = new TiledeskExpression().evaluateStaticExpression(expression, vars);
63
87
  // console.log("result:", result);
64
88
  assert(result === true);
65
89
  });
@@ -67,9 +91,12 @@ describe('JSON to expression', function() {
67
91
  it('test condition group in "and" (true)', async () => {
68
92
  const part1 = {
69
93
  "type": "condition",
70
- "operand1": "2",
94
+ "operand1": "height",
71
95
  "operator": TiledeskExpression.OPERATORS.greaterThan.name,
72
- "operand2": "1"
96
+ "operand2": {
97
+ type: "const",
98
+ value: "1"
99
+ }
73
100
  }
74
101
 
75
102
  const part2 = {
@@ -79,20 +106,26 @@ describe('JSON to expression', function() {
79
106
 
80
107
  const part3 = {
81
108
  "type": "condition",
82
- "operand1": "Andrea",
109
+ "operand1": "name",
83
110
  "operator": TiledeskExpression.OPERATORS.startsWith.name,
84
- "operand2": "And"
111
+ "operand2": {
112
+ type: "const",
113
+ value: "And"
114
+ }
85
115
  }
86
116
 
87
117
  const group = {
88
118
  type: "expression",
89
119
  conditions: [ part1, part2, part3 ]
90
120
  }
91
-
121
+ const vars = {
122
+ height: 2,
123
+ name: "Andrea"
124
+ }
92
125
  const expression = TiledeskExpression.JSONGroupToExpression(group);
93
- // console.log("expression:", expression);
94
- assert(expression === "(" + "Number(\"2\") > Number(\"1\") && String(\"Andrea\").startsWith(String(\"And\"))" + ")");
95
- const result = new TiledeskExpression().evaluateStaticExpression(expression);
126
+ console.log("expression:", expression);
127
+ assert(expression === '(Number($data.height) > Number("1") && String($data.name).startsWith(String("And")))');
128
+ const result = new TiledeskExpression().evaluateStaticExpression(expression, vars);
96
129
  // console.log("result:", result);
97
130
  assert(result === true);
98
131
  });
@@ -100,9 +133,12 @@ describe('JSON to expression', function() {
100
133
  it('test condition group in "and" (false)', async () => {
101
134
  const part1 = {
102
135
  "type": "condition",
103
- "operand1": "1",
136
+ "operand1": "height",
104
137
  "operator": TiledeskExpression.OPERATORS.greaterThan.name,
105
- "operand2": "2"
138
+ "operand2": {
139
+ type: "const",
140
+ value: "2"
141
+ }
106
142
  }
107
143
 
108
144
  const part2 = {
@@ -112,19 +148,25 @@ describe('JSON to expression', function() {
112
148
 
113
149
  const part3 = {
114
150
  "type": "condition",
115
- "operand1": "Andrea",
151
+ "operand1": "name",
116
152
  "operator": TiledeskExpression.OPERATORS.startsWith.name,
117
- "operand2": "Ond"
153
+ "operand2": {
154
+ type: "const",
155
+ value: "Ond"
156
+ }
118
157
  }
119
158
 
120
159
  const group = {
121
160
  conditions: [ part1, part2, part3 ]
122
161
  }
123
-
162
+ const vars = {
163
+ height: 1,
164
+ name: "Andrea"
165
+ }
124
166
  const expression = TiledeskExpression.JSONGroupToExpression(group);
125
- // console.log("expression:", expression);
126
- assert(expression === "(" + "Number(\"1\") > Number(\"2\") && String(\"Andrea\").startsWith(String(\"Ond\"))" + ")");
127
- const result = new TiledeskExpression().evaluateStaticExpression(expression);
167
+ console.log("expression:", expression);
168
+ assert(expression === '(Number($data.height) > Number("2") && String($data.name).startsWith(String("Ond")))');
169
+ const result = new TiledeskExpression().evaluateStaticExpression(expression, vars);
128
170
  // console.log("result:", result);
129
171
  assert(result === false);
130
172
  });
@@ -132,9 +174,12 @@ describe('JSON to expression', function() {
132
174
  it('test multiple conditions group (true)', async () => {
133
175
  const part1 = {
134
176
  "type": "condition",
135
- "operand1": "2",
177
+ "operand1": "height",
136
178
  "operator": TiledeskExpression.OPERATORS.greaterThan.name,
137
- "operand2": "1"
179
+ "operand2": {
180
+ type: "const",
181
+ value: "1"
182
+ }
138
183
  }
139
184
 
140
185
  const part2 = {
@@ -146,7 +191,10 @@ describe('JSON to expression', function() {
146
191
  "type": "condition",
147
192
  "operand1": "Andrea",
148
193
  "operator": TiledeskExpression.OPERATORS.startsWith.name,
149
- "operand2": "And"
194
+ "operand2": {
195
+ type: "const",
196
+ value: "And"
197
+ }
150
198
  }
151
199
 
152
200
  const part4 = {
@@ -156,9 +204,12 @@ describe('JSON to expression', function() {
156
204
 
157
205
  const part5 = {
158
206
  "type": "condition",
159
- "operand1": "3",
207
+ "operand1": "size",
160
208
  "operator": TiledeskExpression.OPERATORS.equalAsNumbers.name,
161
- "operand2": "03"
209
+ "operand2": {
210
+ type: "const",
211
+ value: "03"
212
+ }
162
213
  }
163
214
 
164
215
  const part6 = {
@@ -168,19 +219,26 @@ describe('JSON to expression', function() {
168
219
 
169
220
  const part7 = {
170
221
  "type": "condition",
171
- "operand1": "Andrea",
222
+ "operand1": "name",
172
223
  "operator": TiledeskExpression.OPERATORS.equalAsStrings.name,
173
- "operand2": "Andrea"
224
+ "operand2": {
225
+ type: "const",
226
+ value: "Andrea"
227
+ }
174
228
  }
175
229
 
176
230
  const group = {
177
231
  conditions: [ part1, part2, part3, part4, part5, part6, part7 ]
178
232
  }
179
-
233
+ const vars = {
234
+ height: 2,
235
+ size: 3,
236
+ name: "Andrea"
237
+ }
180
238
  const expression = TiledeskExpression.JSONGroupToExpression(group);
181
- // console.log("expression:", expression);
182
- assert(expression === '(Number("2") > Number("1") && String("Andrea").startsWith(String("And")) || Number("3") === Number("03") && String("Andrea") === String("Andrea"))');
183
- const result = new TiledeskExpression().evaluateStaticExpression(expression);
239
+ console.log("expression:", expression);
240
+ assert(expression === '(Number($data.height) > Number("1") && String($data.Andrea).startsWith(String("And")) || Number($data.size) === Number("03") && String($data.name) === String("Andrea"))');
241
+ const result = new TiledeskExpression().evaluateStaticExpression(expression, vars);
184
242
  // console.log("result:", result);
185
243
  assert(result === true);
186
244
  });
@@ -188,9 +246,12 @@ describe('JSON to expression', function() {
188
246
  it('test multiple groups (true)', async () => {
189
247
  const part1 = {
190
248
  "type": "condition",
191
- "operand1": "2",
249
+ "operand1": "height",
192
250
  "operator": TiledeskExpression.OPERATORS.greaterThan.name,
193
- "operand2": "1"
251
+ "operand2": {
252
+ type: "const",
253
+ value: "1"
254
+ }
194
255
  }
195
256
 
196
257
  const part2 = {
@@ -200,16 +261,22 @@ describe('JSON to expression', function() {
200
261
 
201
262
  const part3 = {
202
263
  "type": "condition",
203
- "operand1": "Andrea",
264
+ "operand1": "name",
204
265
  "operator": TiledeskExpression.OPERATORS.startsWith.name,
205
- "operand2": "And"
266
+ "operand2": {
267
+ type: "const",
268
+ value: "And"
269
+ }
206
270
  }
207
271
 
208
272
  const part5 = {
209
273
  "type": "condition",
210
- "operand1": "3",
274
+ "operand1": "size",
211
275
  "operator": TiledeskExpression.OPERATORS.equalAsNumbers.name,
212
- "operand2": "03"
276
+ "operand2": {
277
+ type: "const",
278
+ value: "03"
279
+ }
213
280
  }
214
281
 
215
282
  const part6 = {
@@ -219,9 +286,12 @@ describe('JSON to expression', function() {
219
286
 
220
287
  const part7 = {
221
288
  "type": "condition",
222
- "operand1": "Andrea",
289
+ "operand1": "name",
223
290
  "operator": TiledeskExpression.OPERATORS.equalAsStrings.name,
224
- "operand2": "Andrea"
291
+ "operand2": {
292
+ type: "const",
293
+ value: "Andrea"
294
+ }
225
295
  }
226
296
 
227
297
  const group1 = {
@@ -240,11 +310,15 @@ describe('JSON to expression', function() {
240
310
  }
241
311
 
242
312
  const groups = [group1, group_operator, group2];
243
-
313
+ const vars = {
314
+ height: 2,
315
+ size: 3,
316
+ name: "Andrea"
317
+ }
244
318
  const expression = TiledeskExpression.JSONGroupsToExpression(groups);
245
- // console.log("full expression:", expression);
246
- assert(expression === '(Number("2") > Number("1") && String("Andrea").startsWith(String("And"))) || (Number("3") === Number("03") && String("Andrea") === String("Andrea"))');
247
- const result = new TiledeskExpression().evaluateStaticExpression(expression);
319
+ console.log("expression:", expression);
320
+ assert(expression === '(Number($data.height) > Number("1") && String($data.name).startsWith(String("And"))) || (Number($data.size) === Number("03") && String($data.name) === String("Andrea"))');
321
+ const result = new TiledeskExpression().evaluateStaticExpression(expression, vars);
248
322
  // console.log("result:", result);
249
323
  assert(result === true);
250
324
  });
@@ -252,9 +326,12 @@ describe('JSON to expression', function() {
252
326
  it('test multiple groups with one condition and one group (true)', async () => {
253
327
  const part1 = {
254
328
  "type": "condition",
255
- "operand1": "2",
329
+ "operand1": "height",
256
330
  "operator": TiledeskExpression.OPERATORS.greaterThan.name,
257
- "operand2": "1"
331
+ "operand2": {
332
+ type: "const",
333
+ value: "1"
334
+ }
258
335
  }
259
336
 
260
337
  const group1 = {
@@ -263,11 +340,13 @@ describe('JSON to expression', function() {
263
340
  }
264
341
 
265
342
  const groups = [group1];
266
-
343
+ const vars = {
344
+ height: 2
345
+ }
267
346
  const expression = TiledeskExpression.JSONGroupsToExpression(groups);
268
- // console.log("full expression:", expression);
269
- assert(expression === '(Number("2") > Number("1"))');
270
- const result = new TiledeskExpression().evaluateStaticExpression(expression);
347
+ console.log("expression:", expression);
348
+ assert(expression === '(Number($data.height) > Number("1"))');
349
+ const result = new TiledeskExpression().evaluateStaticExpression(expression, vars);
271
350
  // console.log("result:", result);
272
351
  assert(result === true);
273
352
  });
@@ -275,32 +354,38 @@ describe('JSON to expression', function() {
275
354
  it('test multiple groups with one condition and one group (false)', async () => {
276
355
  const part1 = {
277
356
  "type": "condition",
278
- "operand1": "2",
357
+ "operand1": "height",
279
358
  "operator": TiledeskExpression.OPERATORS.lessThan.name,
280
- "operand2": "1"
359
+ "operand2": {
360
+ type: "const",
361
+ value: "1"
362
+ }
281
363
  }
282
-
283
364
  const group1 = {
284
365
  type: "expression",
285
366
  conditions: [ part1 ]
286
367
  }
287
-
288
368
  const groups = [group1];
289
-
369
+ const vars = {
370
+ height: 2
371
+ }
290
372
  const expression = TiledeskExpression.JSONGroupsToExpression(groups);
291
- // console.log("full expression:", expression);
292
- assert(expression === '(Number("2") < Number("1"))');
293
- const result = new TiledeskExpression().evaluateStaticExpression(expression);
373
+ console.log("expression:", expression);
374
+ assert(expression === '(Number($data.height) < Number("1"))');
375
+ const result = new TiledeskExpression().evaluateStaticExpression(expression, vars);
294
376
  // console.log("result:", result);
295
377
  assert(result === false);
296
378
  });
297
379
 
298
- it('test variables, all set', async () => {
380
+ it('test variables, a > b true', async () => {
299
381
  const part1 = {
300
382
  "type": "condition",
301
- "operand1": "$a",
302
- "operator": TiledeskExpression.OPERATORS.lessThan.name,
303
- "operand2": "$b"
383
+ "operand1": "a",
384
+ "operator": TiledeskExpression.OPERATORS.greaterThan.name,
385
+ "operand2": {
386
+ type: "var",
387
+ name: "b"
388
+ }
304
389
  }
305
390
 
306
391
  const group1 = {
@@ -308,27 +393,30 @@ describe('JSON to expression', function() {
308
393
  conditions: [ part1 ]
309
394
  }
310
395
 
311
- const variables = {
312
- "a": "1",
313
- "b": "2"
396
+ const vars = {
397
+ "a": "2.5",
398
+ "b": "2.4"
314
399
  }
315
400
 
316
401
  const groups = [group1];
317
402
 
318
- const expression = TiledeskExpression.JSONGroupsToExpression(groups, variables);
319
- // console.log("full expression:", expression);
320
- assert(expression === '(Number("1") < Number("2"))');
321
- const result = new TiledeskExpression().evaluateStaticExpression(expression);
403
+ const expression = TiledeskExpression.JSONGroupsToExpression(groups);
404
+ console.log("full expression2:", expression);
405
+ assert(expression === '(Number($data.a) > Number($data.b))');
406
+ const result = new TiledeskExpression().evaluateStaticExpression(expression, vars);
322
407
  // console.log("result:", result);
323
408
  assert(result === true);
324
409
  });
325
410
 
326
- it('test variables, some not set', async () => {
411
+ it('test variables, a < b true', async () => {
327
412
  const part1 = {
328
413
  "type": "condition",
329
- "operand1": "$a",
414
+ "operand1": "a",
330
415
  "operator": TiledeskExpression.OPERATORS.lessThan.name,
331
- "operand2": "$b"
416
+ "operand2": {
417
+ type: "var",
418
+ name: "b"
419
+ }
332
420
  }
333
421
 
334
422
  const group1 = {
@@ -336,49 +424,52 @@ describe('JSON to expression', function() {
336
424
  conditions: [ part1 ]
337
425
  }
338
426
 
339
- const variables = {
340
- // "a": "1",
427
+ const vars = {
428
+ "a": "1",
341
429
  "b": "2"
342
430
  }
343
431
 
344
432
  const groups = [group1];
345
433
 
346
- const expression = TiledeskExpression.JSONGroupsToExpression(groups, variables);
347
- // console.log("full expression:", expression);
348
- assert(expression === '(Number("$a") < Number("2"))');
349
- const result = new TiledeskExpression().evaluateStaticExpression(expression);
350
- console.log("result:", result);
351
- assert(result === false);
434
+ const expression = TiledeskExpression.JSONGroupsToExpression(groups);
435
+ console.log("full expression:", expression);
436
+ assert(expression === '(Number($data.a) < Number($data.b))');
437
+ const result = new TiledeskExpression().evaluateStaticExpression(expression, vars);
438
+ // console.log("result:", result);
439
+ assert(result === true);
352
440
  });
353
441
 
354
- // it('test one', async () => {
355
- // const expression = {
356
- // groups: [
357
- // {
358
- // type: "expression",
359
- // conditions: [
360
- // {
361
- // type: "condition",
362
- // "operand1": "12",
363
- // "operator": TiledeskExpression.OPERATORS.lessThan,
364
- // "operand2": "10"
365
- // },
366
- // {
367
- // type: "operator",
368
- // operator: "OR"
369
- // },
370
- // { type: "condition", ...}
371
- // ]
372
- // },
373
- // {
374
- // type: "operator",
375
- // operator: "AND"
376
- // },
377
- // { type: "expression", ...}
378
- // ]
379
-
380
- // };
381
- // });
442
+
443
+ // Struct
444
+ // const expression = {
445
+ // groups: [
446
+ // {
447
+ // type: "expression",
448
+ // conditions: [
449
+ // {
450
+ // type: "condition",
451
+ // "operand1": "age",
452
+ // "operator": TiledeskExpression.OPERATORS.lessThan,
453
+ // "operand2": {
454
+ // type: "const", // type: "var"
455
+ // value: "10" // name: "b"
456
+ // }
457
+ // },
458
+ // {
459
+ // type: "operator",
460
+ // operator: "OR"
461
+ // },
462
+ // { type: "condition", ...}
463
+ // ]
464
+ // },
465
+ // {
466
+ // type: "operator",
467
+ // operator: "AND"
468
+ // },
469
+ // { type: "expression", ...}
470
+ // ]
471
+ // };
472
+
382
473
 
383
474
  });
384
475
 
@@ -83,8 +83,8 @@ describe('Conversation for actions test', async () => {
83
83
  else {
84
84
  // console.log("params /start:", params);
85
85
  assert(params);
86
- assert(params["tdLastMessageId"] === message_id);
87
- assert(params["tdProjectId"] === PROJECT_ID);
86
+ assert(params["last_message_id"] === message_id);
87
+ assert(params["project_id"] === PROJECT_ID);
88
88
  listener.close(() => {
89
89
  done();
90
90
  });
@@ -33,7 +33,6 @@ const CHATBOT_TOKEN = process.env.CHATBOT_TOKEN;
33
33
  // }
34
34
  // bots_data.bots[BOT_ID] = bot;
35
35
  // console.log("bot:", bot);
36
- // process.exit(1);
37
36
  console.log("Testing conversation setup:");
38
37
  console.log("PROJECT_ID:", PROJECT_ID);
39
38
  console.log("REQUEST_ID:", REQUEST_ID);
@@ -105,8 +104,8 @@ describe('Conversation1 - Form filling', async () => {
105
104
  else {
106
105
  // console.log("params /start:", params);
107
106
  assert(params);
108
- assert(params["tdLastMessageId"] === message_id);
109
- assert(params["tdProjectId"] === PROJECT_ID);
107
+ assert(params["last_message_id"] === message_id);
108
+ assert(params["project_id"] === PROJECT_ID);
110
109
  listener.close(() => {
111
110
  done();
112
111
  });
@@ -231,8 +230,8 @@ describe('Conversation1 - Form filling', async () => {
231
230
  else {
232
231
  // console.log("params2:", params);
233
232
  assert(params);
234
- assert(params["tdLastMessageId"] === message_id);
235
- assert(params["tdProjectId"] === PROJECT_ID);
233
+ assert(params["last_message_id"] === message_id);
234
+ assert(params["project_id"] === PROJECT_ID);
236
235
  assert(params["your_fullname"] === reply_text);
237
236
  assert(params["_tdTypeOf:your_fullname"]);
238
237
  listener.close(() => {
@@ -342,7 +342,7 @@ describe('filter commands()', function() {
342
342
  }
343
343
  ];
344
344
 
345
- TiledeskChatbotUtil.filterOnLanguage(commands, "en");
345
+ // TiledeskChatbotUtil.filterOnLanguage(commands, "en");
346
346
  // console.log("commands after", commands);
347
347
  // assert(commands.length == 6);
348
348
  });
@@ -72,12 +72,16 @@ class DirReply {
72
72
  }
73
73
 
74
74
  // EVALUATE EXPRESSION AND REMOVE BASED ON EVALUATION
75
- const mylang = requestVariables["mylang"];
76
- console.log("mylang:", mylang);
75
+ // const mylang = requestVariables["mylang"];
76
+ console.log("filterOnVariables:", JSON.stringify(requestVariables));
77
+ // if (message.attributes && message.attributes.commands) {
78
+ // TiledeskChatbotUtil.filterOnLanguage(message.attributes.commands, mylang);
79
+ // }
77
80
  if (message.attributes && message.attributes.commands) {
78
- TiledeskChatbotUtil.filterOnLanguage(message.attributes.commands, mylang);
81
+ console.log("filterOnVariables...on commands", message.attributes.commands);
82
+ TiledeskChatbotUtil.filterOnVariables(message.attributes.commands, requestVariables);
79
83
  }
80
-
84
+
81
85
  // temporary send back of reserved attributes
82
86
  if (!message.attributes) {
83
87
  message.attributes = {}
@@ -103,33 +107,6 @@ class DirReply {
103
107
  callback();
104
108
  });
105
109
  }
106
-
107
- // filterOnLanguage(commands, lang) {
108
- // if (!lang) {
109
- // return;
110
- // }
111
- // if (commands.length > 0) {
112
- // for (let i = commands.length - 1; i >= 0; i--) {
113
- // console.log("commands[i]:", commands[i]);
114
- // if (commands[i]) {
115
- // if (commands[i].type === "message") { // is a message, not wait
116
- // if (!commands[i]["lang"] === lang) { // filter is false, remove
117
- // console.log("commands[i]lang:", commands[i]);
118
- // commands.splice(i, 1);
119
- // if (commands[i-1]) {
120
- // console.log("commands[i-1]?:", commands[i-1]);
121
- // if (commands[i-1].type === "wait") {
122
- // commands.splice(i-1, 1);
123
- // }
124
- // }
125
- // }
126
- // }
127
-
128
- // }
129
- // }
130
-
131
- // }
132
- // }
133
110
  }
134
111
 
135
112
  module.exports = { DirReply };
@@ -41,21 +41,36 @@ class DirWebRequest {
41
41
  }
42
42
  const filler = new Filler();
43
43
  const url = filler.fill(action.url, requestVariables);
44
- if (action.headers) {
45
- for (const [key, value] of Object.entries(action.headers)) {
46
- action.headers[key] = filler.fill(value, requestVariables);
44
+
45
+ let headers = null;
46
+ if (action.headersString) {
47
+ let headersString = filler.fill(action.headersString, requestVariables);
48
+ try {
49
+ headers = JSON.parse(headersString);
50
+ }
51
+ catch(err) {
52
+ console.error("Error parsing webRequest headersString as JSON:", headersString);
47
53
  }
54
+ // for (const [key, value] of Object.entries(action.headers)) {
55
+ // action.headers[key] = filler.fill(value, requestVariables);
56
+ // }
48
57
  }
49
- let jsonBody = null;
58
+ let json = null;
50
59
  if (action.jsonBody) {
51
- jsonBody = filler.fill(action.jsonBody, requestVariables);
60
+ let jsonBody = filler.fill(action.jsonBody, requestVariables);
61
+ try {
62
+ json = JSON.parse(jsonBody);
63
+ }
64
+ catch(err) {
65
+ console.error("Error parsing webRequest jsonBody:", jsonBody);
66
+ }
52
67
  }
53
68
 
54
69
  if (this.log) {console.log("webRequest URL", url);}
55
70
  const HTTPREQUEST = {
56
71
  url: url,
57
- headers: action.headers,
58
- json: jsonBody,
72
+ headers: headers,
73
+ json: json,
59
74
  method: action.method
60
75
  };
61
76
  this.myrequest(
@@ -31,7 +31,7 @@ class Directives {
31
31
  static IF_NOT_OPEN_HOURS = "ifnotopenhours"; // DEPRECATED
32
32
  static FUNCTION_VALUE = "functionvalue";
33
33
  static CONDITION = "condition";
34
- static JSON_CONDITION = "jsonCondition";
34
+ static JSON_CONDITION = "jsoncondition";
35
35
  static ASSIGN = "assign";
36
36
  static SET_ATTRIBUTE = "setattribute";
37
37
  // static IF_AVAILABLE_AGENTS = "ifavailableagents"; // TODO