@tiledesk/tiledesk-tybot-connector 0.1.82 → 0.1.85

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,16 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ ### 0.1.85
9
+ - "Match (regex) operator" fix
10
+ - Text in multiple message commands is now merged in main "message.text" property based on filtered commands
11
+
12
+ ### 0.1.84
13
+ - removed debug logs
14
+
15
+ ### 0.1.83
16
+ - added "attributes.intentName" in chatbot replies, showing the last block involved in generating the reply
17
+
8
18
  ### 0.1.82
9
19
  - added /echobot endpoint (only used for performance test)
10
20
  - @tiledesk/tiledesk-client update => 0.10.2
package/ExtApi.js CHANGED
@@ -27,7 +27,7 @@ class ExtApi {
27
27
  }
28
28
 
29
29
  /**
30
- * A stub to send message to the "ext" endpoint, hosted by tilebot on:
30
+ * A stub to send messages to the "ext" endpoint, hosted by tilebot on:
31
31
  * /${TILEBOT_ROUTE}/ext/${projectId}/requests/${requestId}/messages
32
32
  *
33
33
  * @param {Object} message. The message to send
@@ -143,7 +143,7 @@ class TiledeskExpression {
143
143
  },
144
144
  "matches": {
145
145
  name: "matches",
146
- applyPattern: "#1.matches(/#2/)"
146
+ applyPattern: "#1.match(String(#2)) ? true : false"
147
147
  },
148
148
  // Francesco
149
149
  "addAsNumber": {
package/index.js CHANGED
@@ -1,28 +1,18 @@
1
1
  const express = require('express');
2
2
  const router = express.Router();
3
3
  const bodyParser = require('body-parser');
4
- //var cors = require('cors');
5
- //let path = require("path");
6
- //let fs = require('fs');
7
- // const { TiledeskChatbotClient } = require('@tiledesk/tiledesk-chatbot-client');
8
4
  const { TiledeskClient } = require('@tiledesk/tiledesk-client');
9
- //const { TiledeskClientTest } = require('./TiledeskClientTest');
10
- //const jwt = require('jsonwebtoken');
11
- //const { v4: uuidv4 } = require('uuid');
12
5
  const { ExtApi } = require('./ExtApi.js');
13
6
  const { ExtUtil } = require('./ExtUtil.js');
14
7
  const { TdCache } = require('./TdCache.js');
15
- //const { IntentForm } = require('./IntentForm.js');
16
8
  const { TiledeskChatbot } = require('./models/TiledeskChatbot.js');
17
9
  const { MongodbBotsDataSource } = require('./models/MongodbBotsDataSource.js');
18
10
  const { MongodbIntentsMachine } = require('./models/MongodbIntentsMachine.js');
19
11
  const { TiledeskIntentsMachine } = require('./models/TiledeskIntentsMachine.js');
20
- // const { MockActions } = require('./MockActions');
21
12
  const { MockBotsDataSource } = require('./models/MockBotsDataSource.js');
22
13
  const { TiledeskChatbotConst } = require('./models/TiledeskChatbotConst');
23
14
  const { IntentsMachineFactory } = require('./models/IntentsMachineFactory');
24
15
 
25
- //router.use(cors());
26
16
  router.use(bodyParser.json({limit: '50mb'}));
27
17
  router.use(bodyParser.urlencoded({ extended: true , limit: '50mb'}));
28
18
 
@@ -32,20 +22,9 @@ let tdcache = null;
32
22
  // DEV
33
23
  // const { MessagePipeline } = require('./tiledeskChatbotPlugs/MessagePipeline');
34
24
  const { DirectivesChatbotPlug } = require('./tiledeskChatbotPlugs/DirectivesChatbotPlug');
35
- // const { SplitsChatbotPlug } = require('./tiledeskChatbotPlugs/SplitsChatbotPlug');
36
- // const { MarkbotChatbotPlug } = require('./tiledeskChatbotPlugs/MarkbotChatbotPlug');
37
- // const { WebhookChatbotPlug } = require('./tiledeskChatbotPlugs/WebhookChatbotPlug');
38
-
39
- // PROD
40
- // const { MessagePipeline } = require('@tiledesk/tiledesk-chatbot-plugs/MessagePipeline');
41
- // const { DirectivesChatbotPlug } = require('@tiledesk/tiledesk-chatbot-plugs/DirectivesChatbotPlug');
42
- // const { SplitsChatbotPlug } = require('@tiledesk/tiledesk-chatbot-plugs/SplitsChatbotPlug');
43
- // const { MarkbotChatbotPlug } = require('@tiledesk/tiledesk-chatbot-plugs/MarkbotChatbotPlug');
44
- // const { WebhookChatbotPlug } = require('@tiledesk/tiledesk-chatbot-plugs/WebhookChatbotPlug');
45
25
 
46
26
  // THE IMPORT
47
27
  let mongoose = require('mongoose');
48
- // const { DirSendEmail } = require('./tiledeskChatbotPlugs/directives/DirSendEmail.js');
49
28
  const { Directives } = require('./tiledeskChatbotPlugs/directives/Directives.js');
50
29
  let APIURL = null;
51
30
  let staticBots;
@@ -169,13 +148,14 @@ router.post('/ext/:botid', async (req, res) => {
169
148
  }
170
149
  }
171
150
 
172
- // console.log("reply.actions:", reply.actions);
151
+ // console.log("reply is:", reply);
173
152
  if (reply.actions && reply.actions.length > 0) { // structured actions (coming from chatbot designer)
174
153
  if (log) {console.log("the actions:", JSON.stringify(reply.actions));}
175
154
  let directives = actionsToDirectives(reply.actions);
176
155
  if (log) {console.log("the directives:", JSON.stringify(directives));}
177
156
  let directivesPlug = new DirectivesChatbotPlug(
178
157
  {
158
+ reply: reply,
179
159
  directives: directives,
180
160
  supportRequest: message.request,
181
161
  TILEDESK_API_ENDPOINT: APIURL,
@@ -208,10 +188,9 @@ router.post('/ext/:botid', async (req, res) => {
208
188
  ENDPOINT: extEndpoint,
209
189
  log: false
210
190
  });
211
-
212
191
  apiext.sendSupportMessageExt(reply, projectId, requestId, token, () => {
213
192
  if (log) {
214
- // console.log("SupportMessageExt() reply sent:", JSON.stringify(reply));
193
+ //console.log("SupportMessageExt() reply sent:", JSON.stringify(reply));
215
194
  }
216
195
  });
217
196
  }
@@ -269,7 +248,7 @@ async function updateRequestVariables(chatbot, message, projectId, requestId) {
269
248
  if (message.request && message.request.location && message.request.location.city) {
270
249
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CITY_KEY, message.request.location.city);
271
250
  }
272
- // console.log("message.request.language", message.request["language"])
251
+ // console.log("message.request.language", message.request["language"]);
273
252
  if (message.request) {
274
253
  await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY, message.request.sourcePage);
275
254
  await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY, message.request["language"]);
@@ -296,7 +275,7 @@ async function updateRequestVariables(chatbot, message, projectId, requestId) {
296
275
  }
297
276
  message.attributes.payload = message.request.attributes.payload
298
277
  if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
299
- if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
278
+ // if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
300
279
  }
301
280
 
302
281
  if (message.attributes) {
@@ -309,7 +288,7 @@ async function updateRequestVariables(chatbot, message, projectId, requestId) {
309
288
  for (const [key, value] of Object.entries(message.attributes.payload)) {
310
289
  // const value = all_parameters[key];
311
290
  const value_type = typeof value;
312
- if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
291
+ //if (projectId === "641864da99c1fb00131ba495") {console.log("641864da99c1fb00131ba495 > importing payload parameter:", key, "value:", value, "type:", value_type);}
313
292
  await chatbot.addParameter(key, String(value));
314
293
  }
315
294
  }
@@ -103,6 +103,12 @@ class TiledeskChatbot {
103
103
  let reply;
104
104
  if (faq) {
105
105
  reply = await this.execIntent(faq, message, lead);//, bot);
106
+ // if (!reply.attributes) {
107
+ // reply.attributes = {}
108
+ // }
109
+ // // used by the Clients to get some info about the intent that generated this reply
110
+ // reply.attributes.intent_display_name = faq.intent_display_name;
111
+ // reply.attributes.intent_id = faq.intent_id;
106
112
  }
107
113
  else {
108
114
  reply = {
@@ -209,8 +215,15 @@ class TiledeskChatbot {
209
215
  if (faqs && faqs.length > 0 && faqs[0].answer) {
210
216
  if (this.log) {console.log("EXACT MATCH OR ACTION FAQ:", faqs[0]);}
211
217
  let reply;
218
+ const faq = faqs[0];
212
219
  try {
213
- reply = await this.execIntent(faqs[0], message, lead);//, bot);
220
+ reply = await this.execIntent(faq, message, lead);//, bot);
221
+ // if (!reply.attributes) {
222
+ // reply.attributes = {}
223
+ // }
224
+ // // used by the Clients to get some info about the intent that generated this reply
225
+ // reply.attributes.intent_display_name = faq.intent_display_name;
226
+ // reply.attributes.intent_id = faq.intent_id;
214
227
  }
215
228
  catch(error) {
216
229
  console.error("error during exact match execIntent():", error);
@@ -236,6 +249,12 @@ class TiledeskChatbot {
236
249
  let reply;
237
250
  try {
238
251
  reply = await this.execIntent(faq, message, lead);//, bot);
252
+ // if (!reply.attributes) {
253
+ // reply.attributes = {}
254
+ // }
255
+ // // used by the Clients to get some info about the intent that generated this reply
256
+ // reply.attributes.intent_display_name = faq.intent_display_name;
257
+ // reply.attributes.intent_id = faq.intent_id;
239
258
  }
240
259
  catch(error) {
241
260
  console.error("error during NLP decoding:", error);
@@ -258,6 +277,12 @@ class TiledeskChatbot {
258
277
  let reply;
259
278
  try {
260
279
  reply = await this.execIntent(fallbackIntent, message, lead);//, bot);
280
+ // if (!reply.attributes) {
281
+ // reply.attributes = {}
282
+ // }
283
+ // // used by the Clients to get some info about the intent that generated this reply
284
+ // reply.attributes.intent_display_name = fallbackIntent.intent_display_name;
285
+ // reply.attributes.intent_id = fallbackIntent.intent_id;
261
286
  }
262
287
  catch(error) {
263
288
  console.error("error during defaultFallback:", error);
@@ -380,6 +405,9 @@ class TiledeskChatbot {
380
405
  form_reply.message.attributes.fillParams = true;
381
406
  form_reply.message.attributes.splits = true;
382
407
  form_reply.message.attributes.directives = true;
408
+ // // used by the Clients to get some info about the intent that generated this reply
409
+ // form_reply.message.attributes.intent_display_name = faq.intent_display_name;
410
+ // form_reply.message.attributes.intent_id = faq.intent_id;
383
411
  return form_reply.message
384
412
  }
385
413
  }
@@ -438,6 +466,7 @@ class TiledeskChatbot {
438
466
  bot: this.bot
439
467
  }
440
468
  static_bot_answer.attributes.intent_info = intent_info;
469
+ // console.log("static_bot_answer.attributes.intent_info",)
441
470
  // static_bot_answer.attributes.directives = true;
442
471
  // static_bot_answer.attributes.splits = true;
443
472
  // static_bot_answer.attributes.markbot = true;
@@ -146,44 +146,61 @@ class TiledeskChatbotUtil {
146
146
  // }
147
147
  // }
148
148
 
149
- static filterOnVariables(commands, variables) {
149
+ //static filterOnVariables(commands, variables) {
150
+ static filterOnVariables(message, variables) {
150
151
  if (!variables) {
151
152
  return;
152
153
  }
153
- if (commands.length > 0) {
154
- for (let i = commands.length - 1; i >= 0; i--) {
155
- // console.log("...commands[" + i + "]");
156
- if (commands[i].type === "message") { // is a message, not wait
157
- // console.log("commands[i]:", commands[i].message.lang);
158
- // console.log("commands[i]:", lang, (commands[i].message["lang"] === lang));
159
-
160
- // if (commands[i].message["lang"] && !(commands[i].message["lang"] === lang)) { // if there is a filter and the filter is false, remove
161
- const jsonCondition = commands[i].message["_tdJSONCondition"];
162
- // console.log("jsonCondition:", jsonCondition);
163
- if (jsonCondition) {
164
- // const expression = TiledeskExpression.JSONGroupsToExpression(jsonCondition.groups);
165
- const expression = TiledeskExpression.JSONGroupToExpression(jsonCondition);
166
- // console.log("full json condition expression eval on command.message:", expression);
167
- const conditionResult = new TiledeskExpression().evaluateStaticExpression(expression, variables);
168
- console.log("conditionResult:", conditionResult);
169
- // FALSE
170
- // console.log("commands[i]lang:", commands[i]);
171
- if (conditionResult === false) {
172
- // console.log("deleting command:", commands[i]);
173
- commands.splice(i, 1);
174
- if (commands[i-1]) {
175
- // console.log("commands[i-1]?:", commands[i-1]);
176
- if (commands[i-1].type === "wait") {
177
- commands.splice(i-1, 1);
178
- i--;
154
+ if (message.attributes.commands.length > 0) {
155
+ let commands = message.attributes.commands;
156
+ message.text = "";
157
+ for (let i = commands.length - 1; i >= 0; i--) {
158
+ // console.log("...commands[" + i + "]");
159
+ if (commands[i].type === "message") { // is a message, not wait
160
+ // console.log("commands[i]:", commands[i].message.text);
161
+ // console.log("commands[i]:", lang, (commands[i].message["lang"] === lang));
162
+
163
+ // if (commands[i].message["lang"] && !(commands[i].message["lang"] === lang)) { // if there is a filter and the filter is false, remove
164
+ const jsonCondition = commands[i].message["_tdJSONCondition"];
165
+ // console.log("jsonCondition:", jsonCondition);
166
+ if (jsonCondition) {
167
+ // const expression = TiledeskExpression.JSONGroupsToExpression(jsonCondition.groups);
168
+ const expression = TiledeskExpression.JSONGroupToExpression(jsonCondition);
169
+ // console.log("full json condition expression eval on command.message:", expression);
170
+ const conditionResult = new TiledeskExpression().evaluateStaticExpression(expression, variables);
171
+ // console.log("conditionResult:", conditionResult);
172
+ // FALSE
173
+ // console.log("commands[i]lang:", commands[i]);
174
+ if (conditionResult === false) {
175
+ // console.log("deleting command:", commands[i]);
176
+ commands.splice(i, 1);
177
+ if (commands[i-1]) {
178
+ // console.log("commands[i-1]?:", commands[i-1]);
179
+ if (commands[i-1].type === "wait") {
180
+ commands.splice(i-1, 1);
181
+ i--;
182
+ }
183
+ }
184
+ }
185
+ else {
186
+ // console.log("comands[i]:", commands[i], commands[i].message, commands[i].message.text)
187
+ if (commands[i] && commands[i].message && commands[i].message.text) {
188
+ // console.log("curr text:", message.text)
189
+ if (message.text === "") {
190
+ message.text = commands[i].message.text;
191
+ }
192
+ else {
193
+ message.text = commands[i].message.text + "\n\n" + message.text;
194
+ }
195
+ // console.log("new text:", message.text)
196
+ }
197
+ else {
198
+ // console.log("commands@", commands[i])
179
199
  }
180
200
  }
181
201
  }
182
202
  }
183
203
  }
184
-
185
-
186
- }
187
204
  // for (let i = 0; i < commands.length; i++) {
188
205
  // if (commands[i].type === 'message' && commands[i].message && commands[i].message.text) {
189
206
  // if (this.log) {console.log("[" + commands[i].message.lang + "]commands[i].message.text:", commands[i].message.text);}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.1.82",
3
+ "version": "0.1.85",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -24,11 +24,11 @@
24
24
  "jsonschema": "^1.4.1",
25
25
  "jsonwebtoken": "^8.5.1",
26
26
  "minimist-string": "^1.0.2",
27
- "mocha": "^10.2.0",
28
27
  "mongoose": "^6.3.5",
29
28
  "nanoid": "^3.1.25",
30
29
  "redis": "^3.1.2",
31
30
  "uuid": "^3.3.3",
32
- "vm2": "^3.9.13"
31
+ "vm2": "^3.9.13",
32
+ "mocha": "^10.2.0"
33
33
  }
34
34
  }
@@ -26,6 +26,7 @@ const { DirJSONCondition } = require('./directives/DirJSONCondition');
26
26
  const { DirAssign } = require('./directives/DirAssign');
27
27
  const { DirSetAttribute } = require('./directives/DirSetAttribute');
28
28
  const { DirWebRequest } = require('./directives/DirWebRequest');
29
+ const { DirCode } = require('./directives/DirCode');
29
30
 
30
31
  const { TiledeskChatbot } = require('../models/TiledeskChatbot');
31
32
  const { DirIfOnlineAgents } = require('./directives/DirIfOnlineAgents');
@@ -49,6 +50,7 @@ class DirectivesChatbotPlug {
49
50
  this.HELP_CENTER_API_ENDPOINT = config.HELP_CENTER_API_ENDPOINT;
50
51
  this.tdcache = config.cache;
51
52
  this.directives = config.directives;
53
+ this.reply = config.reply;
52
54
  // console.log("We have the support request:", JSON.stringify(this.supportRequest))
53
55
  }
54
56
 
@@ -88,7 +90,6 @@ class DirectivesChatbotPlug {
88
90
  }
89
91
 
90
92
  async processDirectives(theend) {
91
- // console.log("Directives to process:", JSON.stringify(this.directives));
92
93
  this.theend = theend;
93
94
  const directives = this.directives;
94
95
  if (!directives || directives.length === 0) {
@@ -106,7 +107,7 @@ class DirectivesChatbotPlug {
106
107
  // const requestId = supportRequest.request_id
107
108
  let depId;
108
109
  if (supportRequest.department && supportRequest.department._id) {
109
- if (this.log) {console.log("setting depId:", supportRequest.department._id);}
110
+ if (this.log) {console.log("setting depId:", supportRequest.department._id);}
110
111
  depId = supportRequest.department._id;
111
112
  if (this.log) {console.log("depId is:", depId);}
112
113
  }
@@ -124,6 +125,7 @@ class DirectivesChatbotPlug {
124
125
  projectId: projectId,
125
126
  token: token,
126
127
  supportRequest: supportRequest,
128
+ reply: this.reply,
127
129
  requestId: supportRequest.request_id,
128
130
  TILEDESK_APIURL: API_URL,
129
131
  TILEBOT_ENDPOINT: TILEBOT_ENDPOINT,
@@ -189,6 +191,7 @@ class DirectivesChatbotPlug {
189
191
  async process(directive) {
190
192
  // console.log(".process(directive):", JSON.stringify(directive));
191
193
  let context = this.context;
194
+ // console.log(".this.context.reply", JSON.stringify(this.context.reply));
192
195
  if (directive) {
193
196
  if (context.log) {
194
197
  console.log("..process(directive):", JSON.stringify(directive));
@@ -199,7 +202,6 @@ class DirectivesChatbotPlug {
199
202
  if (directive && directive.name) {
200
203
  directive_name = directive.name.toLowerCase();
201
204
  }
202
-
203
205
  if (directive == null || (directive !== null && directive["name"] === undefined)) {
204
206
  if (context.log) { console.log("stop process(). directive is (null?):", directive);}
205
207
  this.theend();
@@ -379,7 +381,7 @@ class DirectivesChatbotPlug {
379
381
  // });
380
382
  // }
381
383
  else if (directive_name === Directives.AGENT) {
382
- console.log("...DirMoveToAgent");
384
+ // console.log("...DirMoveToAgent");
383
385
  new DirMoveToAgent(context).execute(directive, async () => {
384
386
  let next_dir = await this.nextDirective(this.directives);
385
387
  this.process(next_dir);
@@ -472,6 +474,13 @@ class DirectivesChatbotPlug {
472
474
  this.process(next_dir);
473
475
  });
474
476
  }
477
+ else if (directive_name === Directives.CODE) {
478
+ console.log("...DirCode", directive);
479
+ new DirCode(context).execute(directive, async () => {
480
+ let next_dir = await this.nextDirective(this.directives);
481
+ this.process(next_dir);
482
+ });
483
+ }
475
484
  else if (directive_name === Directives.DELETE) {
476
485
  // console.log("got delete dir...")
477
486
  new DirDeleteVariable(context).execute(directive, async () => {
@@ -0,0 +1,65 @@
1
+ const { TiledeskChatbot } = require('../models/TiledeskChatbot');
2
+
3
+ class TiledeskRequestVariables {
4
+
5
+ constructor(requestId, tdcache, vars) {
6
+ this.requestId = requestId;
7
+ this.tdcache = tdcache;
8
+ this.vars = vars;
9
+ this.ops = {
10
+ set: {
11
+ },
12
+ del: {
13
+ }
14
+ }
15
+ }
16
+
17
+ async set(name, value, callback) {
18
+ const parameter_key = TiledeskChatbot.requestCacheKey(this.requestId) + ":parameters";
19
+ await this.tdcache.hset(parameter_key, name, value);
20
+ if (callback) {
21
+ callback();
22
+ }
23
+ }
24
+
25
+ async get(name, callback) {
26
+ const parameter_key = TiledeskChatbot.requestCacheKey(this.requestId) + ":parameters";
27
+ const value = await this.tdcache.hget(parameter_key, name);
28
+ if (callback) {
29
+ callback(value);
30
+ }
31
+ else {
32
+ return value;
33
+ }
34
+ }
35
+
36
+ async delete(name) {
37
+ const parameter_key = TiledeskChatbot.requestCacheKey(this.requestId) + ":parameters";
38
+ return await this.tdcache.hdel(parameter_key, name);
39
+ }
40
+
41
+ async all(callback) {
42
+ const parameter_key = TiledeskChatbot.requestCacheKey(this.requestId) + ":parameters";
43
+ const values = await this.tdcache.hgetall(parameter_key);
44
+ if (callback) {
45
+ callback(values);
46
+ }
47
+ else {
48
+ return values;
49
+ }
50
+ }
51
+
52
+ setVar(key, value) {
53
+ this.ops.set[key] = value;
54
+ }
55
+
56
+ delVar(key) {
57
+ this.ops.del[key] = true;
58
+ }
59
+
60
+ allVars() {
61
+ return this.vars;
62
+ }
63
+ }
64
+
65
+ module.exports = { TiledeskRequestVariables };
@@ -0,0 +1,91 @@
1
+ const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
2
+ const { TiledeskExpression } = require('../../TiledeskExpression');
3
+ const { TiledeskRequestVariables } = require('../TiledeskRequestVariables');
4
+
5
+ class DirCode {
6
+
7
+ constructor(context) {
8
+ if (!context) {
9
+ throw new Error('context object is mandatory.');
10
+ }
11
+ this.context = context;
12
+ this.log = context.log;
13
+ }
14
+
15
+ execute(directive, callback) {
16
+ let action;
17
+ if (directive.action) {
18
+ action = directive.action
19
+ }
20
+ else {
21
+ callback();
22
+ return;
23
+ }
24
+ this.go(action, (stop) => {
25
+ callback(stop);
26
+ });
27
+
28
+ }
29
+
30
+ async go(action, callback) {
31
+ console.log("action.source:", action.source)
32
+ const source_code = action.source;
33
+ if (!source_code || source_code.trim() === "") {
34
+ if (this.log) {console.log("Invalid source_code");}
35
+ callback();
36
+ return;
37
+ }
38
+ let script_context = {
39
+ console: console
40
+ }
41
+ let variables = null;
42
+ if (this.context.tdcache) {
43
+ variables =
44
+ await TiledeskChatbot.allParametersStatic(
45
+ this.context.tdcache, this.context.requestId
46
+ );
47
+ if (this.log) {console.log("Variables:", JSON.stringify(variables))}
48
+ }
49
+ else {
50
+ console.error("(DirCode) No this.context.tdcache");
51
+ callback();
52
+ return;
53
+ }
54
+ console.log("before variables:", variables);
55
+ for (const [key, value] of Object.entries(variables)) {
56
+ script_context[key] = value;
57
+ }
58
+ let variablesManager = new TiledeskRequestVariables(this.context.requestId, this.context.tdcache, variables)
59
+ script_context.tiledeskVars = variablesManager;
60
+ //console.log("script_context:", script_context);
61
+ const tdExpression = new TiledeskExpression();
62
+ //console.log("tdExpression:", tdExpression.evaluateJavascriptExpression);
63
+ try {
64
+ const result = new TiledeskExpression().evaluateJavascriptExpression(source_code, script_context);
65
+ console.log("result:", result);
66
+ // console.log("script_context.tiledeskVars:", script_context.tiledeskVars);
67
+ for (const [key, value] of Object.entries(script_context.tiledeskVars.ops.set)) {
68
+ // await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, key, value);
69
+ await variablesManager.set(key, value);
70
+ }
71
+ // if (this.log) {
72
+ let newvars_set = await variablesManager.all();
73
+ // console.log("newvars_set:", newvars_set);
74
+ // }
75
+ for (const [key, value] of Object.entries(script_context.tiledeskVars.ops.del)) {
76
+ // await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, key, value);
77
+ await variablesManager.delete(key);
78
+ }
79
+ const newvars_del = await variablesManager.all();
80
+ console.log("newvars_del:", newvars_del);
81
+ }
82
+ catch(err) {
83
+ console.error("An error occurred:", err);
84
+ }
85
+ callback();
86
+ return;
87
+ }
88
+
89
+ }
90
+
91
+ module.exports = { DirCode };
@@ -100,7 +100,7 @@ class DirMoveToAgent {
100
100
  console.error("Error moving to agent:", err);
101
101
  }
102
102
  else {
103
- console.log("Successfully moved to agent");
103
+ // console.log("Successfully moved to agent");
104
104
  }
105
105
  callback();
106
106
  });
@@ -54,7 +54,6 @@ class DirOfflineHours {
54
54
  reply(pipeline, directive, offline_reply, callback) {
55
55
  let message = pipeline.message;
56
56
  console.log("message in pipeline:", JSON.stringify(message));
57
- //const original_text = message.attributes.intent_info.question_payload.text
58
57
  this.tdclient.openNow((err, result) => {
59
58
  console.log("whenofflinehours result...", result);
60
59
  if (err) {
@@ -73,12 +73,6 @@ class DirRandomReply {
73
73
  }
74
74
  }
75
75
 
76
- // EVALUATE EXPRESSION AND REMOVE BASED ON EVALUATION
77
- // if (message.attributes && message.attributes.commands) {
78
- // if (this.log) {console.log("filterOnVariables...on commands", JSON.stringify(message.attributes.commands));}
79
- // TiledeskChatbotUtil.filterOnVariables(message.attributes.commands, requestVariables);
80
- // }
81
-
82
76
  // temporary send back of reserved attributes
83
77
  if (!message.attributes) {
84
78
  message.attributes = {}
@@ -17,11 +17,9 @@ class DirReply {
17
17
  }
18
18
 
19
19
  execute(directive, callback) {
20
- // console.log("Reply directive:", JSON.stringify(directive));
21
20
  let action;
22
21
  if (directive.action) {
23
22
  action = directive.action;
24
- // console.log("got action:", JSON.stringify(action));
25
23
  if (!action.attributes) {
26
24
  action.attributes = {}
27
25
  }
@@ -40,21 +38,21 @@ class DirReply {
40
38
  async go(action, callback) {
41
39
  const message = action;
42
40
  // fill
43
- let requestVariables = null;
41
+ let requestAttributes = null;
44
42
  if (this.tdcache) {
45
- requestVariables =
43
+ requestAttributes =
46
44
  await TiledeskChatbot.allParametersStatic(
47
45
  this.tdcache, this.requestId
48
46
  );
49
47
  if (this.log) {
50
- for (const [key, value] of Object.entries(requestVariables)) {
48
+ for (const [key, value] of Object.entries(requestAttributes)) {
51
49
  const value_type = typeof value;
52
50
  if (this.log) {console.log("(DirReply) request parameter:", key, "value:", value, "type:", value_type)}
53
51
  }
54
52
  }
55
53
  const filler = new Filler();
56
54
  // fill text attribute
57
- message.text = filler.fill(message.text, requestVariables);
55
+ message.text = filler.fill(message.text, requestAttributes);
58
56
  if (this.log) {console.log("filling commands'. Message:", JSON.stringify(message));}
59
57
  if (message.attributes && message.attributes.commands) {
60
58
  if (this.log) {console.log("filling commands'. commands found.");}
@@ -65,8 +63,8 @@ class DirReply {
65
63
  for (let i = 0; i < commands.length; i++) {
66
64
  let command = commands[i];
67
65
  if (command.type === 'message' && command.message && command.message.text) {
68
- command.message.text = filler.fill(command.message.text, requestVariables);
69
- TiledeskChatbotUtil.fillCommandAttachments(command, requestVariables, this.log);
66
+ command.message.text = filler.fill(command.message.text, requestAttributes);
67
+ TiledeskChatbotUtil.fillCommandAttachments(command, requestAttributes, this.log);
70
68
  if (this.log) {console.log("command filled:", command.message.text);}
71
69
  }
72
70
  }
@@ -74,26 +72,25 @@ class DirReply {
74
72
  }
75
73
 
76
74
  // EVALUATE EXPRESSION AND REMOVE BASED ON EVALUATION
77
- // const mylang = requestVariables["mylang"];
78
- // console.log("filterOnVariables:", JSON.stringify(requestVariables));
79
- // if (message.attributes && message.attributes.commands) {
80
- // TiledeskChatbotUtil.filterOnLanguage(message.attributes.commands, mylang);
81
- // }
82
75
  if (message.attributes && message.attributes.commands) {
83
76
  if (this.log) {console.log("filterOnVariables...on commands", JSON.stringify(message.attributes.commands));}
84
- TiledeskChatbotUtil.filterOnVariables(message.attributes.commands, requestVariables);
77
+ // TiledeskChatbotUtil.filterOnVariables(message.attributes.commands, requestAttributes);
78
+ TiledeskChatbotUtil.filterOnVariables(message, requestAttributes);
85
79
  }
86
-
87
80
  // temporary send back of reserved attributes
88
81
  if (!message.attributes) {
89
82
  message.attributes = {}
90
83
  }
91
84
  // Reserved names: userEmail, userFullname
92
- if (requestVariables['userEmail']) {
93
- message.attributes.updateUserEmail = requestVariables['userEmail'];
85
+ if (requestAttributes['userEmail']) {
86
+ message.attributes.updateUserEmail = requestAttributes['userEmail'];
94
87
  }
95
- if (requestVariables['userFullname']) {
96
- message.attributes.updateUserFullname = requestVariables['userFullname'];
88
+ if (requestAttributes['userFullname']) {
89
+ message.attributes.updateUserFullname = requestAttributes['userFullname'];
90
+ }
91
+ // intent_info
92
+ if (this.context.reply && this.context.reply.attributes && this.context.reply.attributes.intent_info) {
93
+ message.attributes.intentName = this.context.reply.attributes.intent_info.intent_name;
97
94
  }
98
95
  }
99
96
  // send!
@@ -1,23 +1,13 @@
1
1
  class Directives {
2
2
  static AGENT = 'agent';
3
- static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
4
3
  static CLOSE = 'close';
5
4
  static DEPARTMENT = 'department';
6
- static JSONMESSAGE = 'jsonmessage'; // DEPRECATED?
7
5
  static MESSAGE = 'message';
8
6
  static HMESSAGE = 'hmessage';
9
7
  static INTENT = 'intent';
10
8
  static REMOVE_CURRENT_BOT = "removecurrentbot";
11
9
  static REPLACE_BOT = "replacebot";
12
- static WHEN_NO_AVAILABLE_AGENTS = "whennoavailableagents"; // DEPRECATED
13
- static WHEN_OFFLINE_HOURS = "whenofflinehours"; // DEPRECATED // adds a message on top of the original message when offline hours opts: --replace
14
- //static WHEN_OFFLINE_HOURS_REPLACE_MESSAGE = "whenofflinehoursreplacemessage"; // REMOVE
15
- static DISABLE_INPUT_TEXT = "disableinputtext"; // DEPRECATED
16
- static WHEN_OPEN = "whenopen"; // DEPRECATED
17
- static WHEN_CLOSED = "whenclosed"; // DEPRECATED
18
- static IF_NO_AGENTS = "ifnoagents"; // DEPRECATED
19
- static IF_AGENTS = "ifagents"; // DEPRECATED
20
-
10
+ static ASSIGN = "assign"; // DEPRECATED
21
11
  static ASK_HELP_CENTER = "askhelpcenter";
22
12
  static WAIT = "wait";
23
13
  static LOCK_INTENT = "lockintent";
@@ -28,16 +18,25 @@ class Directives {
28
18
  static DELETE = "delete";
29
19
  static IF_OPEN_HOURS = "ifopenhours";
30
20
  static IF_ONLINE_AGENTS = "ifonlineagents";
31
- static IF_NOT_OPEN_HOURS = "ifnotopenhours"; // DEPRECATED
32
21
  static FUNCTION_VALUE = "functionvalue";
33
- static CONDITION = "condition"; // DEPRECATED
34
22
  static JSON_CONDITION = "jsoncondition";
35
- static ASSIGN = "assign"; // DEPRECATED
36
23
  static SET_ATTRIBUTE = "setattribute";
37
- // static IF_AVAILABLE_AGENTS = "ifavailableagents"; // TODO
38
- // static IF_NO_AVAILABLE_AGENTS = "ifnotavailableagents"; // TODO
39
24
  static REPLY = 'reply';
40
25
  static RANDOM_REPLY = 'randomreply';
26
+ static CODE = 'code';
27
+
28
+ // static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
29
+ // static WHEN_OFFLINE_HOURS = "whenofflinehours"; // DEPRECATED // adds a message on top of the original message when offline hours opts: --replace
30
+ //static WHEN_OFFLINE_HOURS_REPLACE_MESSAGE = "whenofflinehoursreplacemessage"; // REMOVE
31
+ // static DISABLE_INPUT_TEXT = "disableinputtext"; // DEPRECATED
32
+ // static WHEN_OPEN = "whenopen"; // DEPRECATED
33
+ // static WHEN_CLOSED = "whenclosed"; // DEPRECATED
34
+ // static IF_NO_AGENTS = "ifnoagents"; // DEPRECATED
35
+ // static IF_AGENTS = "ifagents"; // DEPRECATED
36
+ // static WHEN_NO_AVAILABLE_AGENTS = "whennoavailableagents"; // DEPRECATED
37
+ // static JSONMESSAGE = 'jsonmessage'; // DEPRECATED?
38
+ // static IF_NOT_OPEN_HOURS = "ifnotopenhours"; // DEPRECATED
39
+ // static CONDITION = "condition"; // DEPRECATED
41
40
 
42
41
  static actionToDirective(action) {
43
42
  // console.log("actionToDirective:", action);