@tiledesk/tiledesk-tybot-connector 0.1.44 → 0.1.46

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,12 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ ### 0.1.46
9
+ - weird npm import fix
10
+
11
+ ### 0.1.45
12
+ - DirLockIntent added varibleName parameter
13
+
8
14
  ### 0.1.44
9
15
  - Variables to webhook
10
16
 
package/index.js CHANGED
@@ -46,7 +46,7 @@ const { DirSendEmail } = require('./tiledeskChatbotPlugs/directives/DirSendEmail
46
46
  const { Directives } = require('./tiledeskChatbotPlugs/directives/Directives.js');
47
47
  //let Faq = require('./models/faq');
48
48
  //let Faq_kb = require('./models/faq_kb');
49
- let connection;
49
+ // let connection;
50
50
  let APIURL = null;
51
51
  let staticBots;
52
52
 
@@ -156,15 +156,17 @@ router.post('/ext/:botid', async (req, res) => {
156
156
  if (message.request && message.request.location && message.request.location.city) {
157
157
  await chatbot.addParameter("_tdCity", message.request.location.city);
158
158
  }
159
-
159
+ if (message.text) {
160
+ await chatbot.addParameter("_tdUserText", message.text);
161
+ }
160
162
  if (requestSourcePage) {
161
- await chatbot.addParameter("requestSourcePage", sourcePage);
163
+ await chatbot.addParameter("_tdRequestSourcePage", sourcePage);
162
164
  }
163
165
  if (requestLanguage) {
164
- await chatbot.addParameter("requestLanguage", language);
166
+ await chatbot.addParameter("_tdRequestLanguage", language);
165
167
  }
166
168
  if (requestUserAgent) {
167
- await chatbot.addParameter("requestUserAgent", userAgent);
169
+ await chatbot.addParameter("_tdRequestUserAgent", userAgent);
168
170
  }
169
171
  let reply = await chatbot.replyToMessage(message);
170
172
  if (!reply) {
@@ -449,7 +451,8 @@ async function startApp(settings, completionCallback) {
449
451
 
450
452
  if (!staticBots) {
451
453
  console.log("(Tilebot) Connecting to mongodb...");
452
- connection = mongoose.connect(settings.MONGODB_URI, { "useNewUrlParser": true, "autoIndex": false }, async (err) => {
454
+ // connection =
455
+ mongoose.connect(settings.MONGODB_URI, { "useNewUrlParser": true, "autoIndex": false }, async (err) => {
453
456
  if (err) {
454
457
  console.error('(Tilebot) Failed to connect to MongoDB on ' + settings.MONGODB_URI + " ", err);
455
458
  //process.exit(1); // add => exitOnFail: true
@@ -7,6 +7,8 @@ const { WebhookChatbotPlug } = require('../tiledeskChatbotPlugs/WebhookChatbotPl
7
7
  const { TiledeskClient } = require('@tiledesk/tiledesk-client');
8
8
  const { IntentForm } = require('./IntentForm.js');
9
9
  const { TiledeskChatbotUtil } = require('./TiledeskChatbotUtil.js');
10
+ const { DirLockIntent } = require('../tiledeskChatbotPlugs/directives/DirLockIntent');
11
+ const { DirUnlockIntent } = require('../tiledeskChatbotPlugs/directives/DirUnlockIntent');
10
12
 
11
13
  class TiledeskChatbot {
12
14
 
@@ -59,32 +61,40 @@ class TiledeskChatbot {
59
61
  console.log("replyToMessage() > lead found:", JSON.stringify(lead));
60
62
  }
61
63
 
62
- // Checking locked intent
63
- const locked_intent = await this.currentLockedIntent(this.requestId);
64
- if (this.log) {console.log("got locked intent", locked_intent)}
65
- if (locked_intent) {
66
- const tdclient = new TiledeskClient({
67
- projectId: this.projectId,
68
- token: this.token,
69
- APIURL: this.APIURL,
70
- APIKEY: this.APIKEY,
71
- log: false
72
- });
73
- // it only gets the locked_intent
74
- const faq = await this.botsDataSource.getByIntentDisplayName(this.botId, locked_intent);
75
- if (this.log) {console.log("locked intent. got faqs", faq)}
76
- let reply;
77
- if (faq) {
78
- reply = await this.execIntent(faq, message, lead);//, bot);
79
- }
80
- else {
81
- reply = {
82
- "text": "An error occurred while getting locked intent:'" + locked_intent
64
+ // Checking locked intent (for non-internal intents)
65
+ // internal intents always "skip" the locked intent
66
+ // if (message.text.startsWith("/") && message.sender != "_tdinternal") {
67
+ const locked_intent = await this.currentLockedIntent(this.requestId);
68
+ if (this.log) {console.log("got locked intent", locked_intent)}
69
+ if (locked_intent) {
70
+ const tdclient = new TiledeskClient({
71
+ projectId: this.projectId,
72
+ token: this.token,
73
+ APIURL: this.APIURL,
74
+ APIKEY: this.APIKEY,
75
+ log: false
76
+ });
77
+ // it only gets the locked_intent
78
+ const faq = await this.botsDataSource.getByIntentDisplayName(this.botId, locked_intent);
79
+ if (this.log) {console.log("locked intent. got faqs", faq)}
80
+ let reply;
81
+ if (faq) {
82
+ reply = await this.execIntent(faq, message, lead);//, bot);
83
83
  }
84
+ else {
85
+ reply = {
86
+ "text": "An error occurred while getting locked intent:'" + locked_intent
87
+ }
88
+ }
89
+ resolve(reply);
90
+ return;
84
91
  }
85
- resolve(reply);
86
- return;
87
- }
92
+ // }
93
+ // else if (message.text.startsWith("/")) {
94
+ // if (this.log) {
95
+ // console.log("Internal intent". message.text, "skips locked intent check");
96
+ // }
97
+ // }
88
98
 
89
99
  let explicit_intent_name = null;
90
100
  // Explicit intent invocation
@@ -225,7 +235,7 @@ class TiledeskChatbot {
225
235
  async execIntent(faq, message, lead) {//, bot) {
226
236
  let answerObj = faq; // faqs[0];
227
237
  const botId = this.botId;
228
- let sender = 'bot_' + botId;
238
+ // let sender = 'bot_' + botId;
229
239
  //var answerObj;
230
240
  //answerObj.score = 100; // exact search has max score
231
241
  if (this.log) {
@@ -253,30 +263,30 @@ class TiledeskChatbot {
253
263
 
254
264
  let intent_name = answerObj.intent_display_name
255
265
  // THE FORM
256
- if (intent_name === "test_form_intent") {
257
- answerObj.form = {
258
- "cancelCommands": ['reset', 'cancel'],
259
- "cancelReply": "Ok canceled!",
260
- "fields": [
261
- {
262
- "name": "userFullname",
263
- "type": "text",
264
- "label": "What is your name?\n* Andrea\n* Marco\n* Mirco\n* Luca Leo"
265
- },{
266
- "name": "companyName",
267
- "type": "text",
268
- "label": "Thank you ${userFullname}! What is your Company name?\n* Tiledesk\n* Frontiere21"
269
- },
270
- {
271
- "name": "userEmail",
272
- "type": "text",
273
- "regex": "/^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)+$/",
274
- "label": "Hi ${userFullname} from ${companyName}\n\nJust one last question\n\nYour email 🙂\n* andrea@libero.it\n* andrea@tiledesk.com",
275
- "errorLabel": "${userFullname} this email address is invalid\n\nCan you insert a correct email address?"
276
- }
277
- ]
278
- };
279
- }
266
+ // if (intent_name === "test_form_intent") {
267
+ // answerObj.form = {
268
+ // "cancelCommands": ['reset', 'cancel'],
269
+ // "cancelReply": "Ok canceled!",
270
+ // "fields": [
271
+ // {
272
+ // "name": "userFullname",
273
+ // "type": "text",
274
+ // "label": "What is your name?\n* Andrea\n* Marco\n* Mirco\n* Luca Leo"
275
+ // },{
276
+ // "name": "companyName",
277
+ // "type": "text",
278
+ // "label": "Thank you ${userFullname}! What is your Company name?\n* Tiledesk\n* Frontiere21"
279
+ // },
280
+ // {
281
+ // "name": "userEmail",
282
+ // "type": "text",
283
+ // "regex": "/^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)+$/",
284
+ // "label": "Hi ${userFullname} from ${companyName}\n\nJust one last question\n\nYour email 🙂\n* andrea@libero.it\n* andrea@tiledesk.com",
285
+ // "errorLabel": "${userFullname} this email address is invalid\n\nCan you insert a correct email address?"
286
+ // }
287
+ // ]
288
+ // };
289
+ // }
280
290
  let intent_form = answerObj.form;
281
291
  if (this.log) {
282
292
  console.log("IntentForm.isValidForm(intent_form)", IntentForm.isValidForm(intent_form));
@@ -398,7 +408,6 @@ class TiledeskChatbot {
398
408
  if (this.log) {console.log("We must clientUpdateUserFullname with:", clientUpdateUserFullname)};
399
409
  static_bot_answer.attributes.updateUserFullname = clientUpdateUserFullname;
400
410
  }
401
- console.log("...static_bot_answer ready:", JSON.stringify(static_bot_answer));
402
411
  // exec webhook
403
412
  const bot_answer = await this.execWebhook(static_bot_answer, message, this.bot, context, this.token);
404
413
  if (this.log) {console.log("bot_answer ready:", JSON.stringify(bot_answer));}
@@ -406,7 +415,8 @@ class TiledeskChatbot {
406
415
  }
407
416
 
408
417
  async lockIntent(requestId, intent_name) {
409
- await this.tdcache.set("tilebot:requests:" + requestId + ":locked", intent_name);
418
+ // await this.tdcache.set("tilebot:requests:" + requestId + ":locked", intent_name);
419
+ await DirLockIntent.lockIntent(this.tdcache, requestId, intent_name);
410
420
  }
411
421
 
412
422
  async currentLockedIntent(requestId) {
@@ -419,7 +429,8 @@ class TiledeskChatbot {
419
429
  }
420
430
 
421
431
  async unlockIntent(requestId) {
422
- await this.tdcache.del("tilebot:requests:" + requestId + ":locked");
432
+ await DirUnlockIntent.unlockIntent(this.tdcache, requestId);
433
+ // await this.tdcache.del("tilebot:requests:" + requestId + ":locked");
423
434
  }
424
435
 
425
436
  async addParameter(parameter_name, parameter_value) {
@@ -459,11 +470,11 @@ class TiledeskChatbot {
459
470
  }
460
471
 
461
472
  async execWebhook(static_bot_answer, userMessage, bot, context) {
462
- console.log("static_bot_answer.attributes.webhook:", static_bot_answer.attributes.webhook)
473
+ if (this.log) {console.log("static_bot_answer.attributes.webhook:", static_bot_answer.attributes.webhook);}
463
474
  if (static_bot_answer.attributes && static_bot_answer.attributes.webhook && static_bot_answer.attributes.webhook === true) {
464
- console.log("adding variables to context...")
465
475
  const variables = await this.allParameters();
466
476
  context.variables = variables;
477
+ if (this.log) {console.log("adding variables to webhook context:", context.variables);}
467
478
  }
468
479
  const messagePipeline = new MessagePipeline(static_bot_answer, context);
469
480
  const webhookurl = bot.webhook_url;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.1.44",
3
+ "version": "0.1.46",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -144,12 +144,12 @@ describe('Conversation1 - Form filling', async () => {
144
144
  });
145
145
 
146
146
  it('/disable_input', (done) => {
147
- // console.log("/disable_input...");
147
+ console.log("/disable_input...");
148
148
  let listener;
149
149
  let endpointServer = express();
150
150
  endpointServer.use(bodyParser.json());
151
151
  endpointServer.post('/:projectId/requests/:requestId/messages', function (req, res) {
152
- // console.log("req.body:", JSON.stringify(req.body));
152
+ console.log("req.body....:", JSON.stringify(req.body));
153
153
  res.send({ success: true });
154
154
  const message = req.body;
155
155
 
@@ -194,14 +194,14 @@ describe('Conversation1 - Form filling', async () => {
194
194
  });
195
195
 
196
196
  it('/good_form', (done) => {
197
- // console.log("/good_form...");
197
+ console.log("/good_form...");
198
198
  const message_id = uuidv4();
199
199
  const reply_text = "Andrea";
200
200
  let listener;
201
201
  let endpointServer = express();
202
202
  endpointServer.use(bodyParser.json());
203
203
  endpointServer.post('/:projectId/requests/:requestId/messages', function (req, res) {
204
- // console.log("req.body:", JSON.stringify(req.body));
204
+ console.log("req.body22222:", JSON.stringify(req.body));
205
205
  res.send({ success: true });
206
206
  const message = req.body;
207
207
 
@@ -223,7 +223,7 @@ describe('Conversation1 - Form filling', async () => {
223
223
  "token": CHATBOT_TOKEN
224
224
  }
225
225
  sendMessageToBot(request, BOT_ID, CHATBOT_TOKEN, () => {
226
- // console.log("Message sent.", request);
226
+ console.log("Message sent.", request);
227
227
  });
228
228
  }
229
229
  else if (message.text === "It's a good form Andrea") {
@@ -233,13 +233,15 @@ describe('Conversation1 - Form filling', async () => {
233
233
  assert.ok(false);
234
234
  }
235
235
  else {
236
- // console.log("params:", params);
236
+ console.log("params2:", params);
237
237
  assert(params);
238
238
  assert(params["_tdLastMessageId"] === message_id);
239
239
  assert(params["_tdProjectId"] === PROJECT_ID);
240
240
  assert(params["your_fullname"] === reply_text);
241
241
  assert(params["_tdTypeOf:your_fullname"]);
242
+ console.log("ok2")
242
243
  listener.close(() => {
244
+ console.log("done2");
243
245
  done();
244
246
  });
245
247
  }
@@ -247,7 +249,7 @@ describe('Conversation1 - Form filling', async () => {
247
249
 
248
250
  }
249
251
  else {
250
- console.error("Unexpected message.");
252
+ console.error("Unexpected message2.");
251
253
  assert.ok(false);
252
254
  }
253
255
 
@@ -279,16 +281,16 @@ describe('Conversation1 - Form filling', async () => {
279
281
  });
280
282
 
281
283
  it('(intent-to-intent) /move_to => /target_intent', (done) => {
282
- // console.log("(intent-to-intent) /move_to => /target_intent");
284
+ console.log("(intent-to-intent) /move_to => /target_intent");
283
285
  let listener;
284
286
  let endpointServer = express();
285
287
  endpointServer.use(bodyParser.json());
286
288
  endpointServer.post('/:projectId/requests/:requestId/messages', function (req, res) {
287
289
  res.send({ success: true });
288
290
  const message = req.body;
289
- // console.log("received message:", JSON.stringify(message));
291
+ console.log("received message33:", JSON.stringify(message));
290
292
  if (message.text === "The target!") {
291
- // console.log("Got it. End.");
293
+ console.log("Got it. End.");
292
294
  listener.close(() => {
293
295
  done();
294
296
  });
@@ -316,13 +318,13 @@ describe('Conversation1 - Form filling', async () => {
316
318
  }
317
319
  // console.log("sending message:", request);
318
320
  sendMessageToBot(request, BOT_ID, CHATBOT_TOKEN, () => {
319
- // console.log("Message sent.");
321
+ console.log("Message sent33.");
320
322
  });
321
323
  });
322
324
  });
323
325
 
324
326
  it('/all_filled (none) => /form_to_unfill => (fill) => /all_filled (all) /form_to_unfill (bypass because filled) => /delete_fullname => all_filled (no fullname) => /form_to_unfill (verify it asks only for fullname) => all_filled (all, again)', (done) => {
325
- // console.log("/all_filled (none) =>...");
327
+ console.log("/all_filled (none) =>...");
326
328
  let request0_uuid = uuidv4();
327
329
  let request1_uuid = uuidv4();
328
330
  let request2_uuid = uuidv4();
@@ -339,9 +341,12 @@ describe('Conversation1 - Form filling', async () => {
339
341
  endpointServer.post('/:projectId/requests/:requestId/messages', function (req, res) {
340
342
  res.send({ success: true });
341
343
  const message = req.body;
342
- // console.log("received message:", JSON.stringify(message));
344
+ console.log("received message__3:", JSON.stringify(message));
345
+ console.log("message.triggeredByMessageId:", message.triggeredByMessageId);
346
+ console.log("message.text:", message.text);
347
+
343
348
  if (message.text === "You filled\nfullname: ${fullname}\nyouremail: ${youremail}" && message.triggeredByMessageId === request0_uuid) {
344
- // console.log("got #0 'You filled...' sending #1");
349
+ console.log("got #0 'You filled...' sending #1");
345
350
  let request = {
346
351
  "payload": {
347
352
  "_id": request1_uuid,
@@ -363,7 +368,7 @@ describe('Conversation1 - Form filling', async () => {
363
368
  });
364
369
  }
365
370
  else if (message.text === "Your name?" && message.triggeredByMessageId === request1_uuid) {
366
- // console.log("got #1 sending #2");
371
+ console.log("got #1 sending #2");
367
372
  let request = {
368
373
  "payload": {
369
374
  "_id": request2_uuid,
@@ -381,14 +386,14 @@ describe('Conversation1 - Form filling', async () => {
381
386
  "token": CHATBOT_TOKEN
382
387
  }
383
388
  sendMessageToBot(request, BOT_ID, CHATBOT_TOKEN, () => {
384
- // console.log("Message sent.", request);
389
+ console.log("Message sent4.", request);
385
390
  });
386
391
  // listener.close( () => {
387
392
  // done();
388
393
  // });
389
394
  }
390
395
  else if (message.text === "Your email?" && message.triggeredByMessageId === request2_uuid) {
391
- // console.log("got #2 sending #3");
396
+ console.log("got #2 sending #3");
392
397
  let request = {
393
398
  "payload": {
394
399
  "_id": request3_uuid,
@@ -406,14 +411,14 @@ describe('Conversation1 - Form filling', async () => {
406
411
  "token": CHATBOT_TOKEN
407
412
  }
408
413
  sendMessageToBot(request, BOT_ID, CHATBOT_TOKEN, () => {
409
- // console.log("Message sent.", request);
414
+ console.log("Message sent5.", request);
410
415
  });
411
416
  // listener.close( () => {
412
417
  // done();
413
418
  // });
414
419
  }
415
420
  else if (message.text === "Thanks Andrea\nYour email test@test.it" && message.triggeredByMessageId === request3_uuid) {
416
- // console.log("got #3 sending #4");
421
+ console.log("got #3 sending #4");
417
422
  let request = {
418
423
  "payload": {
419
424
  "_id": request4_uuid,
@@ -431,14 +436,14 @@ describe('Conversation1 - Form filling', async () => {
431
436
  "token": CHATBOT_TOKEN
432
437
  }
433
438
  sendMessageToBot(request, BOT_ID, CHATBOT_TOKEN, () => {
434
- // console.log("got #3 sending #4. Message sent.", request);
439
+ console.log("got #3 sending #4. Message sent.", request);
435
440
  });
436
441
  // listener.close( () => {
437
442
  // done();
438
443
  // });
439
444
  }
440
445
  else if (message.text === "You filled\nfullname: Andrea\nyouremail: test@test.it" && message.triggeredByMessageId === request4_uuid) {
441
- // console.log("got #4 sending #5");
446
+ console.log("got #4 sending #5");
442
447
  let request = {
443
448
  "payload": {
444
449
  "_id": request5_uuid,
@@ -556,11 +561,11 @@ describe('Conversation1 - Form filling', async () => {
556
561
  "token": CHATBOT_TOKEN
557
562
  }
558
563
  sendMessageToBot(request, BOT_ID, CHATBOT_TOKEN, () => {
559
- // console.log("got #8 sending #9. Message sent.", request);
564
+ console.log("got #8 sending #9. Message sent.", request);
560
565
  });
561
566
  }
562
567
  else if (message.text === "You filled\nfullname: ${fullname}\nyouremail: ${youremail}") {
563
- // console.log("got #9. End.");
568
+ console.log("got #9. End.");
564
569
  listener.close(() => {
565
570
  done();
566
571
  });
@@ -404,17 +404,17 @@ class DirectivesChatbotPlug {
404
404
  });
405
405
  }
406
406
  else if (directive_name === Directives.WAIT) {
407
- new DirWait().execute(directive, () => {
407
+ new DirWait(context).execute(directive, () => {
408
408
  process(nextDirective());
409
409
  });
410
410
  }
411
411
  else if (directive_name === Directives.LOCK_INTENT) {
412
- new DirLockIntent(tdcache).execute(directive, requestId, () => {
412
+ new DirLockIntent(context).execute(directive, () => {
413
413
  process(nextDirective());
414
414
  });
415
415
  }
416
416
  else if (directive_name === Directives.UNLOCK_INTENT) {
417
- new DirUnlockIntent(tdcache).execute(requestId, () => {
417
+ new DirUnlockIntent(context).execute( () => {
418
418
  process(nextDirective());
419
419
  });
420
420
  }
@@ -2,6 +2,7 @@
2
2
  let axios = require('axios');
3
3
  let https = require("https");
4
4
  const { v4: uuidv4 } = require('uuid');
5
+ const ms = require('minimist-string');
5
6
 
6
7
  class DirIntent {
7
8
 
@@ -1,30 +1,88 @@
1
+ const ms = require('minimist-string');
1
2
 
2
3
  class DirLockIntent {
3
4
 
4
- constructor(tdcache) {
5
- if (!tdcache) {
5
+ constructor(context) {
6
+ if (!context) {
7
+ throw new Error('config (TiledeskClient) object is mandatory.');
8
+ }
9
+ this.context = context;
10
+ if (!context.tdcache) {
6
11
  throw new Error('tdcache (TdCache) object is mandatory.');
7
12
  }
8
- this.tdcache = tdcache;
13
+ this.tdcache = this.context.tdcache;
14
+ this.log = context.log;
9
15
  }
10
16
 
11
- async execute(directive, requestId, callback) {
12
- // console.log("Locking intent");
13
- if (directive.parameter) {
14
- let intent_name = directive.parameter.trim();
15
- await this.lockIntent(requestId, intent_name);
16
- callback();
17
+ async execute(directive, callback) {
18
+ if (this.log) {console.log("Locking intent:", JSON.stringify(directive));}
19
+ let action;
20
+ if (directive.action) {
21
+ // console.log("got intent action:", JSON.stringify(directive.action));
22
+ action = directive.action;
23
+ }
24
+ else if (directive.parameter && directive.parameter.trim() !== "") {
25
+ const params = this.parseParams(directive.parameter);
26
+ action = {
27
+ body: {
28
+ intentName: params.intentName // directive.parameter.trim()
29
+ // variableName: params.variableName
30
+ }
31
+ }
17
32
  }
18
33
  else {
34
+ console.error("Incorrect directive:", directive);
19
35
  callback();
36
+ return;
20
37
  }
38
+ // if (directive.parameter) {
39
+ // let intent_name = directive.parameter.trim();
40
+ // await this.lockIntent(requestId, intent_name);
41
+ // callback();
42
+ // }
43
+ // else {
44
+ // callback();
45
+ // }
46
+ this.go(action, () => {
47
+ callback();
48
+ });
21
49
  }
22
50
 
23
- async lockIntent(requestId, intent_name) {
24
- await this.tdcache.set("tilebot:requests:" + requestId + ":locked", intent_name);
25
- // console.log("locked.", intent_name);
51
+ async go(action, callback) {
52
+ let intent_name = action.body.intentName;
53
+ // let variable_name = action.body.variableName;
54
+ await DirLockIntent.lockIntent(this.tdcache, this.context.requestId, intent_name); //, variable_name);
55
+ if (this.log) {console.log("Locked intent:", action.body.intentName);}
56
+ if (callback) {
57
+ callback();
58
+ }
59
+ }
60
+
61
+ static async lockIntent(tdcache, requestId, intent_name) { //}, variable_name) {
62
+ await tdcache.set("tilebot:requests:" + requestId + ":locked", intent_name);
63
+ // if (variable_name) {
64
+ // console.log("locking intent with variable:", variable_name);
65
+ // await this.tdcache.set("tilebot:requests:" + requestId + ":lockedValue", variable_name);
66
+ // }
67
+ // console.log("locked. Intent name:", intent_name, "intent variable:", variable_name);
26
68
  }
27
69
 
70
+ parseParams(directive_parameter) {
71
+ let intentName = null;
72
+ let variableName = null;
73
+ const params = ms(directive_parameter);
74
+ if (params.intentName) {
75
+ intentName = params.intentName
76
+ }
77
+ if (params.variableName) {
78
+ variableName = params.variableName.replace(/[$]/g, ""); // $ sign not permitted. Silently removing if present
79
+ }
80
+ return {
81
+ intentName: intentName,
82
+ variableName: variableName
83
+ }
84
+ }
85
+
28
86
  }
29
87
 
30
88
  module.exports = { DirLockIntent };
@@ -1,4 +1,3 @@
1
- const { commands } = require('npm');
2
1
  const { ExtApi } = require('../../ExtApi.js');
3
2
  const { Directives } = require('./Directives.js');
4
3
 
@@ -29,20 +28,20 @@ class DirMessage {
29
28
  action.body.message.attributes.markbot = false;
30
29
  action.body.message.attributes.fillParams = true; // fillParams can fill commands[].message.text
31
30
  // temp patch for a fix in the future
32
- if (!action.body.message.text || action.body.message.text.trim() === "") {
33
- // because server doesn't allow empty text
34
- // because we anyway need a text/type for conversation summary
35
- // we get info from first commands' message or set a default value
36
- if (action.body.message && action.body.message.attributes && action.body.message.attributes.commands) {
37
- const message_info = DirMessage.firstMessageInfoFromCommands(commands);
38
- action.body.message.text = message_info.text;
39
- action.body.message.type = message_info.type;
40
- }
41
- else {
42
- action.body.message.text = "New message";
43
- action.body.message.type = "text";
44
- }
45
- }
31
+ // if (!action.body.message.text || action.body.message.text.trim() === "") {
32
+ // // because server doesn't allow empty text
33
+ // // because we anyway need a text/type for conversation summary
34
+ // // we get info from first commands' message or set a default value
35
+ // if (action.body.message && action.body.message.attributes && action.body.message.attributes.commands) {
36
+ // const message_info = DirMessage.firstMessageInfoFromCommands(commands);
37
+ // action.body.message.text = message_info.text;
38
+ // action.body.message.type = message_info.type;
39
+ // }
40
+ // else {
41
+ // action.body.message.text = "New message";
42
+ // action.body.message.type = "text";
43
+ // }
44
+ // }
46
45
  // console.log("final message action:", JSON.stringify(action));
47
46
  }
48
47
  }
@@ -1,21 +1,41 @@
1
1
 
2
2
  class DirUnlockIntent {
3
3
 
4
- constructor(tdcache) {
5
- if (!tdcache) {
4
+ constructor(context) {
5
+ if (!context) {
6
+ throw new Error('config (TiledeskClient) object is mandatory.');
7
+ }
8
+ this.context = context;
9
+ if (!context.tdcache) {
6
10
  throw new Error('tdcache (TdCache) object is mandatory.');
7
11
  }
8
- this.tdcache = tdcache;
12
+ this.tdcache = context.tdcache;
13
+ this.log = context.log;
9
14
  }
10
15
 
11
- async execute(requestId, callback) {
12
- console.log("Unocking intent");
13
- await this.unlockIntent(requestId);
14
- callback();
16
+ async execute(directive, requestId, callback) {
17
+ if (this.log) {console.log("Unlocking current intent");}
18
+ this.go(action, () => {
19
+ callback();
20
+ });
15
21
  }
16
22
 
17
- async unlockIntent(requestId) {
18
- await this.tdcache.del("tilebot:requests:" + requestId + ":locked");
23
+ async go(action, callback) {
24
+ await DirUnlockIntent.unlockIntent(this.context.requestId);
25
+ if (callback) {
26
+ callback();
27
+ }
28
+ }
29
+
30
+ // async execute(requestId, callback) {
31
+ // console.log("Unocking intent");
32
+ // await this.unlockIntent(requestId);
33
+ // callback();
34
+ // }
35
+
36
+ static async unlockIntent(tdcache, requestId) {
37
+ await tdcache.del("tilebot:requests:" + requestId + ":locked");
38
+ // await this.tdcache.del("tilebot:requests:" + requestId + ":locked");
19
39
  console.log("unlocked.")
20
40
  }
21
41