@tiledesk/tiledesk-tybot-connector 0.1.73 → 0.1.75

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.
Files changed (44) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/TiledeskJSONEval.js +24 -0
  3. package/index.js +33 -11
  4. package/package.json +4 -4
  5. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +1 -1
  6. package/tiledeskChatbotPlugs/directives/DirMessage.js +29 -26
  7. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +33 -1
  8. package/.env +0 -9
  9. package/herokulog.sh +0 -1
  10. package/publish.sh +0 -10
  11. package/test/DEPRECATED-disable_input_text_directive_test.js_ +0 -88
  12. package/test/anomaly-detection-test.js +0 -173
  13. package/test/chatbot_util_test.js +0 -215
  14. package/test/check_steps_test.js +0 -23
  15. package/test/close_directive_test.js +0 -49
  16. package/test/condition_json_to_expression_test.js +0 -615
  17. package/test/conversation-actions-test.js +0 -329
  18. package/test/conversation-actions_bot.js +0 -236
  19. package/test/conversation-form-test.js +0 -1104
  20. package/test/conversation-form_bot.js +0 -834
  21. package/test/conversation-locked-intent-test.js +0 -242
  22. package/test/dir_set_attribute_test.js +0 -544
  23. package/test/directives_test.js +0 -39
  24. package/test/expression_evaluator_test.js +0 -161
  25. package/test/filter_commands_test.js +0 -349
  26. package/test/if_online_agents_directive_test.js +0 -44
  27. package/test/intent_form_pre_filled_test.js +0 -260
  28. package/test/intent_form_test.js +0 -239
  29. package/test/json_condition-actions_bot.js +0 -349
  30. package/test/json_condition-conversation_test.js +0 -334
  31. package/test/json_condition-with-intent-params-conversation_test.js +0 -282
  32. package/test/json_condition-with-intent-params_bot.js +0 -159
  33. package/test/mock_query_test.js +0 -276
  34. package/test/operation_json_to_expression_test.js +0 -783
  35. package/test/send_email_directive_test.js +0 -94
  36. package/test/single_test.sh +0 -11
  37. package/test/support_request.js +0 -249
  38. package/test/testin.js +0 -24
  39. package/test/validate_variable_names.js +0 -29
  40. package/test/var_splitter_test.js +0 -70
  41. package/test/web_request-bot.js +0 -142
  42. package/test/web_request-conversation_test.js +0 -243
  43. package/test/when_open_directive_test.js +0 -167
  44. package/tiledeskChatbotPlugs/publish.sh +0 -10
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.75
9
+ - introduced WebRequest action.assignments
10
+ - created TiledeskJSONEval class supported by handlebars
11
+ - removed debug logs
12
+ - added handlebars lib
13
+
14
+ ### 0.1.74
15
+ - DEBUG VERSION
16
+ - Added if (log || projectId === "64218dfecdb804001380b9ba")
17
+
8
18
  ### 0.1.73
9
19
  - Bug fixing: WebRequest headersString now is a JSON
10
20
 
@@ -0,0 +1,24 @@
1
+ const Handlebars = require("handlebars");
2
+
3
+ class TiledeskJSONEval {
4
+
5
+ static eval(data, expression) {
6
+ Handlebars.registerHelper("last", function(array) {
7
+ return array[array.length-1];
8
+ });
9
+ Handlebars.registerHelper("first", function(array) {
10
+ return array[0];
11
+ });
12
+ let template = null;
13
+ if (expression.startsWith("{")) {
14
+ template = Handlebars.compile(expression);
15
+ }
16
+ else {
17
+ template = Handlebars.compile("{{{" + expression + "}}}");
18
+ }
19
+ const value = template(data);
20
+ return value;
21
+ }
22
+ }
23
+
24
+ module.exports = { TiledeskJSONEval }
package/index.js CHANGED
@@ -248,6 +248,7 @@ router.post('/ext/:botid', async (req, res) => {
248
248
 
249
249
  async function updateRequestVariables(chatbot, message, projectId, requestId) {
250
250
  // update request context
251
+ if (chatbot.log) {console.log("Updating request variables. Message:", JSON.stringify(message));}
251
252
  const messageId = message._id;
252
253
  const chat_url = `https://panel.tiledesk.com/v3/dashboard/#/project/${projectId}/wsrequest/${requestId}/messages`
253
254
  await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_URL, chat_url);
@@ -273,15 +274,28 @@ async function updateRequestVariables(chatbot, message, projectId, requestId) {
273
274
  await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY, message.request["language"]);
274
275
  await chatbot.addParameter(TiledeskChatbotConst.REQ_USER_AGENT_KEY, message.request.userAgent);
275
276
  }
277
+ // console.log("message.request.language", message.request["language"])
276
278
  if (message.request && message.request.department) {
277
- // It was an error getting this from widget message's attributes
279
+ // It was an error when getting this from widget message's attributes
278
280
  // await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.attributes.departmentId);
279
281
  // await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.attributes.departmentName);
280
282
  // get from request.department instead
281
283
  await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY, message.request.department._id);
282
284
  await chatbot.addParameter(TiledeskChatbotConst.REQ_DEPARTMENT_NAME_KEY, message.request.department.name);
283
285
  }
286
+
287
+ // for BUG
288
+ // if (chatbot.log) {console.log("message.request.attributes.payload", JSON.stringify(message.request.attributes.payload))}
289
+ if (message && message.request && message.request.attributes && message.request.attributes.payload) {
290
+ if (!message.attributes) {
291
+ message.attributes = {}
292
+ }
293
+ message.attributes.payload = message.request.attributes.payload
294
+ if (chatbot.log) {console.log("FORCED SET message.attributes.payload:", JSON.stringify(message.attributes.payload))}
295
+ }
296
+
284
297
  if (message.attributes) {
298
+ if (chatbot.log) {console.log("Ok message.attributes", JSON.stringify(message.attributes));}
285
299
  await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_ID_KEY, message.attributes.requester_id);
286
300
  await chatbot.addParameter(TiledeskChatbotConst.REQ_END_USER_IP_ADDRESS_KEY, message.attributes.ipAddress);
287
301
  if (message.attributes.payload) {
@@ -289,7 +303,7 @@ async function updateRequestVariables(chatbot, message, projectId, requestId) {
289
303
  for (const [key, value] of Object.entries(message.attributes.payload)) {
290
304
  // const value = all_parameters[key];
291
305
  const value_type = typeof value;
292
- if (chatbot.log) {console.log("importing payload parameter:", key, "value:", value, "type:", value_type)}
306
+ console.log("importing payload parameter:", key, "value:", value, "type:", value_type)
293
307
  await chatbot.addParameter(key, String(value));
294
308
  }
295
309
  }
@@ -353,8 +367,13 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
353
367
  const projectId = req.params.projectId;
354
368
  const requestId = req.params.requestId;
355
369
  const token = req.headers["authorization"];
370
+ if (log) {console.log("/ext projectId:", projectId);}
371
+ if (log) {console.log("/ext requestId:", requestId);}
372
+ if (log) {console.log("/ext req.headers:", req.headers);}
373
+ if (log) {console.log("/ext token:", token);}
374
+
356
375
  let answer = req.body;
357
- // if (log) {console.log("answer on sendSupportMessageExt:", JSON.stringify(answer));}
376
+ if (log) {console.log("/ext => answer on sendSupportMessageExt:", JSON.stringify(answer));}
358
377
  const tdclient = new TiledeskClient({
359
378
  projectId: projectId,
360
379
  token: token,
@@ -386,15 +405,15 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
386
405
  // console.log("Cache request found.");
387
406
  }
388
407
  catch(err) {
389
- console.error("Request not found:", requestId);
408
+ console.error("/ext => Request not found:", requestId);
390
409
  }
391
410
  // if (log) {console.log("(No tdcache) Got request with APIs");}
392
411
  // }
393
412
  if (!request) {
394
- if (log) {console.log("chatbot-pure directives still work. Tiledesk specific directives don't");}
413
+ if (log) {console.log("/ext => Creating new Request. Chatbot-pure directives still work. Tiledesk specific directives don't");}
395
414
  const request_botId_key = "tilebot:botId_requests:" + requestId;
396
415
  const botId = await tdcache.get(request_botId_key);
397
- if (log) {console.log("current botId [" + request_botId_key + "]:", botId);}
416
+ if (log) {console.log("/ext => current botId [" + request_botId_key + "]:", botId);}
398
417
  request = {
399
418
  request_id: requestId,
400
419
  id_project: projectId,
@@ -407,13 +426,14 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
407
426
  console.log("/ext process.env.TYBOT_ENDPOINT....", process.env.TYBOT_ENDPOINT);
408
427
  }
409
428
  let directivesPlug = new DirectivesChatbotPlug({supportRequest: request, TILEDESK_API_ENDPOINT: APIURL, TILEBOT_ENDPOINT:process.env.TYBOT_ENDPOINT, token: token, log: log, HELP_CENTER_API_ENDPOINT: process.env.HELP_CENTER_API_ENDPOINT, cache: tdcache});
429
+ // let directivesPlug = null;
410
430
  // PIPELINE-EXT
411
431
  // if (log) {console.log("answer to process:", JSON.stringify(answer));}
412
432
  const original_answer_text = answer.text;
413
433
  const bot_answer = await ExtUtil.execPipelineExt(request, answer, directivesPlug, tdcache, log);
414
- // console.log("bot_answer", bot_answer)
434
+ if (log) {console.log("/ext => bot_answer", JSON.stringify(bot_answer))}
415
435
  if (bot_answer) {
416
- if (log) {console.log("adding to bot_answer original_answer_text:", JSON.stringify(original_answer_text));}
436
+ if (log) {console.log("/ext => adding to bot_answer original_answer_text:", JSON.stringify(original_answer_text));}
417
437
  if (!bot_answer.attributes) {
418
438
  bot_answer.attributes = {};
419
439
  }
@@ -423,8 +443,9 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
423
443
  bot_answer.attributes["_raw_message"] = original_answer_text;
424
444
  // if (log) {console.log("bot_answer", JSON.stringify(bot_answer));}
425
445
  tdclient.sendSupportMessage(requestId, bot_answer, (err, response) => {
446
+ if (log) {console.log("/ext => bot_answer sent:", JSON.stringify(bot_answer));}
426
447
  if (err) {
427
- console.error("Error sending message", err);
448
+ console.error("/ext => Error sending message", err);
428
449
  }
429
450
  directivesPlug.processDirectives( () => {
430
451
  if (log) {console.log("After message - Directives executed.");}
@@ -432,6 +453,7 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
432
453
  });
433
454
  }
434
455
  else {
456
+ if (log) {console.log("/ext => !bot_answer");}
435
457
  directivesPlug.processDirectives( () => {
436
458
  if (log) {console.log("Directives executed.");}
437
459
  });
@@ -498,8 +520,8 @@ router.get('/', (req, res) => {
498
520
  res.send('Hello Tilebot!');
499
521
  });
500
522
 
501
- router.get('/test/webrequest/get/plain', async (req, res) => {
502
- res.send("Application var");
523
+ router.get('/test/webrequest/get/plain/:username', async (req, res) => {
524
+ res.send(`Application var ${req.params['username']}`);
503
525
  });
504
526
 
505
527
  router.post('/test/webrequest/post/plain', async (req, res) => {
package/package.json CHANGED
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.1.73",
3
+ "version": "0.1.75",
4
4
  "description": "Tiledesk Tybot connector",
5
-
6
5
  "main": "index.js",
7
6
  "scripts": {
8
7
  "test": "mocha --timeout 60000 --exit",
@@ -21,6 +20,8 @@
21
20
  "cors": "^2.8.5",
22
21
  "dotenv": "^16.0.3",
23
22
  "express": "^4.17.1",
23
+ "handlebars": "^4.7.7",
24
+ "jsonschema": "^1.4.1",
24
25
  "jsonwebtoken": "^8.5.1",
25
26
  "minimist-string": "^1.0.2",
26
27
  "mocha": "^10.2.0",
@@ -28,7 +29,6 @@
28
29
  "nanoid": "^3.1.25",
29
30
  "redis": "^3.1.2",
30
31
  "uuid": "^3.3.3",
31
- "vm2": "^3.9.13",
32
- "jsonschema": "^1.4.1"
32
+ "vm2": "^3.9.13"
33
33
  }
34
34
  }
@@ -458,7 +458,7 @@ class DirectivesChatbotPlug {
458
458
  });
459
459
  }
460
460
  else if (directive_name === Directives.SEND_EMAIL) {
461
- console.log("...DirSendEmail");
461
+ // console.log("...DirSendEmail");
462
462
  new DirSendEmail(context).execute(directive, async () => {
463
463
  let next_dir = await this.nextDirective(this.directives);
464
464
  this.process(next_dir);
@@ -69,6 +69,9 @@ class DirMessage {
69
69
  if (directive.name === Directives.HMESSAGE) {
70
70
  action.attributes.subtype = "info";
71
71
  }
72
+ // if (directive.name === Directives.HMESSAGE) {
73
+ // action.sender = "tiledesk";
74
+ // }
72
75
  }
73
76
  else {
74
77
  console.error("Incorrect directive:", directive);
@@ -113,33 +116,33 @@ class DirMessage {
113
116
  });
114
117
  }
115
118
 
116
- static firstMessageInfoFromCommands(commands) {
117
- let type = "text";
118
- let text = "New message";
119
- for (let i = 0; i < commands.length; i++) {
120
- const command = commands[i];
121
- console.log("cheking command", command)
122
- if (command.type === "message") {
123
- console.log("command.type: message!")
124
- console.log("command.message.type!", command.message.type)
125
- console.log("command.message.text!", command.message.text)
119
+ // static firstMessageInfoFromCommands(commands) {
120
+ // let type = "text";
121
+ // let text = "New message";
122
+ // for (let i = 0; i < commands.length; i++) {
123
+ // const command = commands[i];
124
+ // console.log("cheking command", command)
125
+ // if (command.type === "message") {
126
+ // console.log("command.type: message!")
127
+ // console.log("command.message.type!", command.message.type)
128
+ // console.log("command.message.text!", command.message.text)
126
129
 
127
- if (command.message.type) {
128
- type = command.message.type;
129
- }
130
- if (command.message.text) {
131
- text = command.message.text;
132
- }
133
- break;
134
- }
135
- }
136
- const message_info = {
137
- type: type,
138
- text: text
139
- }
140
- // console.log("message_info:", message_info);
141
- return message_info;
142
- }
130
+ // if (command.message.type) {
131
+ // type = command.message.type;
132
+ // }
133
+ // if (command.message.text) {
134
+ // text = command.message.text;
135
+ // }
136
+ // break;
137
+ // }
138
+ // }
139
+ // const message_info = {
140
+ // type: type,
141
+ // text: text
142
+ // }
143
+ // // console.log("message_info:", message_info);
144
+ // return message_info;
145
+ // }
143
146
 
144
147
  }
145
148
 
@@ -2,6 +2,7 @@ let axios = require('axios');
2
2
  let https = require("https");
3
3
  const { Filler } = require('../Filler');
4
4
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
5
+ const { TiledeskJSONEval } = require('../../TiledeskJSONEval');
5
6
 
6
7
  class DirWebRequest {
7
8
  constructor(context) {
@@ -88,7 +89,7 @@ class DirWebRequest {
88
89
  }
89
90
  }
90
91
  else if (callback) {
91
- if (action.assignTo && this.context.tdcache && resbody) {
92
+ if (action.assignTo && this.context.tdcache && resbody) { // DEPRECATED
92
93
  if (this.log) {console.log("(webRequest) this.requestId:", this.context.requestId);}
93
94
  let attributes =
94
95
  await TiledeskChatbot.allParametersStatic(
@@ -107,6 +108,37 @@ class DirWebRequest {
107
108
  if (this.log) {console.log("(webRequest) request parameter:", key, "value:", value, "type:", value_type)}
108
109
  }
109
110
  }
111
+ } else if (action.assignments && this.context.tdcache && resbody) {
112
+ if (this.log) {console.log("(webRequest) action.assignments for request:", this.context.requestId);}
113
+ let json_body;
114
+ if (typeof resbody === "string") {
115
+ json_body = {
116
+ body: resbody
117
+ }
118
+ }
119
+ else {
120
+ json_body = resbody
121
+ }
122
+ if (this.log) {console.log("(webRequest) action.assignments json_body:", json_body);}
123
+ let attributes =
124
+ await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
125
+ if (this.log) {console.log("(webRequest) action.assignments attributes:", attributes);}
126
+ const assignments = action.assignments;
127
+ if (this.log) {console.log("(webRequest) assignments:", assignments);}
128
+ for (const [attr_name, attr_eval_expression] of Object.entries(assignments)) {
129
+ if (this.log) {console.log("", attr_name, attr_eval_expression);}
130
+ const attributeValue = TiledeskJSONEval.eval(json_body, attr_eval_expression);
131
+ if (this.log) {console.log("(webRequest) Assigning to:", attr_name, "value:", attributeValue);}
132
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, attr_name, attributeValue);
133
+ }
134
+ if (this.log) {
135
+ console.log("(webRequest) All attributes:");
136
+ const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
137
+ for (const [key, value] of Object.entries(all_parameters)) {
138
+ const value_type = typeof value;
139
+ if (this.log) {console.log("(webRequest) request attribute:", key, "value:", value, "type:", value_type)}
140
+ }
141
+ }
110
142
  }
111
143
  callback();
112
144
  }
package/.env DELETED
@@ -1,9 +0,0 @@
1
- API_ENDPOINT=http://localhost:10002
2
- mongoUrl=mongodb://tiledesk-user1:YB62b7gQNGg3G9OW@tiledesk-prod-shard-00-00-vmvst.mongodb.net:27017,tiledesk-prod-shard-00-01-vmvst.mongodb.net:27017,tiledesk-prod-shard-00-02-vmvst.mongodb.net:27017/tiledesk-pre?ssl=true&replicaSet=tiledesk-prod-shard-0&authSource=admin&retryWrites=true
3
- REDIS_HOST=localhost
4
- REDIS_PORT=6379
5
- TYBOT_ENDPOINT=http://localhost:10001
6
- _TYBOT_ENDPOINT=http://localhost:3000
7
- CHATBOT_TOKEN=JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ3ZWJob29rX2VuYWJsZWQiOmZhbHNlLCJ0eXBlIjoiZXh0ZXJuYWwiLCJsYW5ndWFnZSI6ImVuIiwicHVibGljIjpmYWxzZSwiX2lkIjoiNjM4Yzc5MDQxZGI0NDkwMDM1MTEwMjYwIiwibmFtZSI6IlRoZSBGb3JtIHYyIC0gZXh0IiwidXJsIjoiaHR0cHM6Ly90aWxlYm90LWRldi5oZXJva3VhcHAuY29tL2V4dC82MzhjNzhkNzFkYjQ0OTAwMzUxMTAxYzIiLCJpZF9wcm9qZWN0IjoiNjM4Yzc4YTYxZGI0NDkwMDM1MTBmZjkxIiwidHJhc2hlZCI6ZmFsc2UsImNyZWF0ZWRCeSI6IjVlMDlkMTZkNGQzNjExMDAxNzUwNmQ3ZiIsImNyZWF0ZWRBdCI6IjIwMjItMTItMDRUMTA6NDA6MDQuMjA3WiIsInVwZGF0ZWRBdCI6IjIwMjItMTItMDVUMDc6MjE6MDIuOTIxWiIsIl9fdiI6MCwiZGVzY3JpcHRpb24iOiJPbiBIZXJva3UiLCJpYXQiOjE2NzA2NzE4ODksImF1ZCI6Imh0dHBzOi8vdGlsZWRlc2suY29tL2JvdHMvNjM4Yzc5MDQxZGI0NDkwMDM1MTEwMjYwIiwiaXNzIjoiaHR0cHM6Ly90aWxlZGVzay5jb20iLCJzdWIiOiJib3QiLCJqdGkiOiJmNDVlZGIwYS0zNzVhLTQ0NjMtYjFhZi1jM2ZiZDg4YmE3ZGQifQ.FbW3csHl1sQgSyRz5Jg0qaTvvpXWXgWHlJ1JWoVbv3s
8
- _CHATBOT_ENDPOINT=http://localhost:10001
9
- API_LOG=1
package/herokulog.sh DELETED
@@ -1 +0,0 @@
1
- heroku logs --tail --app tilebot-dev
package/publish.sh DELETED
@@ -1,10 +0,0 @@
1
- #npm version patch
2
- version=`node -e 'console.log(require("./package.json").version)'`
3
- echo "version $version"
4
-
5
- if [ "$version" != "" ]; then
6
- git tag -a "$version" -m "`git log -1 --format=%s`"
7
- echo "Created a new tag, $version"
8
- git push --tags
9
- npm publish --access public
10
- fi
@@ -1,88 +0,0 @@
1
- var assert = require('assert');
2
- const { ExtUtil } = require('../ExtUtil');
3
- const { DirectivesChatbotPlug } = require('../tiledeskChatbotPlugs/DirectivesChatbotPlug');
4
- const supportRequest = require('./support_request.js').request;
5
-
6
- describe('Directive DirDisableInputText', function() {
7
-
8
- it('test directive DisableInputText (basic)', async () => {
9
- const message_text = `message1
10
- message2
11
- * button1
12
- \\_tddisableinputtext`;
13
- const answer = {
14
- text: message_text,
15
- attributes: {
16
- splits: true,
17
- directives: true,
18
- markbot: true
19
- }
20
- }
21
- let directivesPlug = new DirectivesChatbotPlug({supportRequest: supportRequest, TILEDESK_API_ENDPOINT: "APIURL", token: "token", log: false, HELP_CENTER_API_ENDPOINT: "HELP_CENTER_API_ENDPOINT"});
22
- const bot_answer = await ExtUtil.execPipelineExt(supportRequest, answer, directivesPlug, null, false);
23
- assert.strictEqual(bot_answer.text, "message1\nmessage2");
24
- assert(bot_answer.attributes.commands == null);
25
- assert(bot_answer.attributes.disableInputMessage == true);
26
- assert(directivesPlug.directives != null);
27
- assert(directivesPlug.directives.length == 1);
28
- assert(directivesPlug.directives[0].name.toLowerCase() == "disableinputtext");
29
- });
30
-
31
- it('test directive DisableInputText (placeholder message option: --label)', async () => {
32
- const message_text = `message1
33
- message2
34
- * button1
35
- \\_tddisableinputtext --label "Press a button to reply"`;
36
- const answer = {
37
- text: message_text,
38
- attributes: {
39
- splits: true,
40
- directives: true,
41
- markbot: true
42
- }
43
- }
44
- let directivesPlug = new DirectivesChatbotPlug({supportRequest: supportRequest, TILEDESK_API_ENDPOINT: "APIURL", token: "token", log: false, HELP_CENTER_API_ENDPOINT: "HELP_CENTER_API_ENDPOINT"});
45
- const bot_answer = await ExtUtil.execPipelineExt(supportRequest, answer, directivesPlug, null, false);
46
- // console.log("bot as obj", bot_answer);
47
- // console.log("bot", JSON.stringify(bot_answer));
48
- assert.strictEqual(bot_answer.text, "message1\nmessage2");
49
- assert(bot_answer.attributes.commands == null);
50
- assert(bot_answer.attributes.disableInputMessage == true);
51
- assert(bot_answer.attributes.inputMessagePlaceholder === "Press a button to reply");
52
- // console.log("Directives:", directivesPlug.directives)
53
- assert(directivesPlug.directives != null);
54
- assert(directivesPlug.directives.length == 1);
55
- assert(directivesPlug.directives[0].name.toLowerCase() == "disableinputtext");
56
- });
57
-
58
- it('test directive DisableInputText (placeholder message, short form option: -l)', async () => {
59
- const message_text = `message1
60
- message2
61
- * button1
62
- \\_tddisableinputtext -l "Press a button to reply"`;
63
- const answer = {
64
- text: message_text,
65
- attributes: {
66
- splits: true,
67
- directives: true,
68
- markbot: true
69
- }
70
- }
71
- let directivesPlug = new DirectivesChatbotPlug({supportRequest: supportRequest, TILEDESK_API_ENDPOINT: "APIURL", token: "token", log: false, HELP_CENTER_API_ENDPOINT: "HELP_CENTER_API_ENDPOINT"});
72
- const bot_answer = await ExtUtil.execPipelineExt(supportRequest, answer, directivesPlug, null, false);
73
- // console.log("bot as obj", bot_answer);
74
- // console.log("bot", JSON.stringify(bot_answer));
75
- assert.strictEqual(bot_answer.text, "message1\nmessage2");
76
- assert(bot_answer.attributes.commands == null);
77
- assert(bot_answer.attributes.disableInputMessage == true);
78
- assert(bot_answer.attributes.inputMessagePlaceholder === "Press a button to reply");
79
- // console.log("Directives:", directivesPlug.directives)
80
- assert(directivesPlug.directives != null);
81
- assert(directivesPlug.directives.length == 1);
82
- assert(directivesPlug.directives[0].name.toLowerCase() == "disableinputtext");
83
- });
84
-
85
- });
86
-
87
-
88
-
@@ -1,173 +0,0 @@
1
- var assert = require('assert');
2
- let axios = require('axios');
3
- const tybot = require("../");
4
- const tybotRoute = tybot.router;
5
- var express = require('express');
6
- var app = express();
7
- app.use("/", tybotRoute);
8
- app.use((err, req, res, next) => {
9
- console.error("General error", err);
10
- });
11
- require('dotenv').config();
12
- const bodyParser = require('body-parser');
13
- const { v4: uuidv4 } = require('uuid');
14
- const bots_data = require('./conversation-actions_bot.js').bots_data;
15
-
16
- const PROJECT_ID = "projectID"; //process.env.TEST_ACTIONS_PROJECT_ID;
17
- const REQUEST_ID = "support-group-" + PROJECT_ID + "-" + uuidv4().replace(/-/g, "");
18
- const BOT_ID = "botID"; //process.env.TEST_ACTIONS_BOT_ID;
19
- const CHATBOT_TOKEN = "XXX"; //process.env.ACTIONS_CHATBOT_TOKEN;
20
-
21
- describe('Conversation for anomaly detection test', async () => {
22
-
23
- let app_listener;
24
-
25
- before(() => {
26
- return new Promise(async (resolve, reject) => {
27
- console.log("Starting tilebot server...");
28
- tybot.startApp(
29
- {
30
- // MONGODB_URI: process.env.mongoUrl,
31
- bots: bots_data,
32
- API_ENDPOINT: process.env.API_ENDPOINT,
33
- REDIS_HOST: process.env.REDIS_HOST,
34
- REDIS_PORT: process.env.REDIS_PORT,
35
- REDIS_PASSWORD: process.env.REDIS_PASSWORD,
36
- log: process.env.API_LOG
37
- }, () => {
38
- console.log("Tilebot route successfully started.");
39
- var port = process.env.PORT || 10001;
40
- app_listener = app.listen(port, () => {
41
- console.log('Tilebot connector listening on port... ', port);
42
- resolve();
43
- });
44
- });
45
- })
46
- });
47
-
48
- after(function (done) {
49
- app_listener.close(() => {
50
- done();
51
- });
52
- });
53
-
54
- it('/anomaly', (done) => {
55
- console.log("/anomaly story...");
56
- let message_id = uuidv4();
57
- let listener;
58
- let endpointServer = express();
59
- endpointServer.use(bodyParser.json());
60
- endpointServer.post('/:projectId/requests/:requestId/messages', function (req, res) {
61
- // console.log("...req.body:", JSON.stringify(req.body));
62
- res.send({ success: true });
63
- const message = req.body;
64
- assert(message.attributes.error !== null);
65
- assert(message.attributes.runtimeError.message === "Request error: anomaly detection. MAX ACTIONS exeeded.");
66
- // console.log("/anomaly test success");
67
- listener.close(() => {
68
- // console.log("/anomaly lister test closed");
69
- done();
70
- });
71
- });
72
-
73
- listener = endpointServer.listen(10002, '0.0.0.0', () => {
74
- console.log('endpointServer started', listener.address());
75
- let request = {
76
- "payload": {
77
- "_id": message_id,
78
- "senderFullname": "guest#367e",
79
- "type": "text",
80
- "sender": "A-SENDER",
81
- "recipient": REQUEST_ID,
82
- "text": "/anomaly",
83
- "id_project": PROJECT_ID,
84
- "metadata": "",
85
- "request": {
86
- "request_id": REQUEST_ID
87
- }
88
- },
89
- "token": CHATBOT_TOKEN
90
- }
91
- sendMessageToBot(request, BOT_ID, () => {
92
- // console.log("Message sent:\n", request);
93
- });
94
- });
95
-
96
- });
97
-
98
- });
99
-
100
- /**
101
- * A stub to send message to the "ext/botId" endpoint, hosted by tilebot on:
102
- * /${TILEBOT_ROUTE}/ext/${botId}
103
- *
104
- * @param {Object} message. The message to send
105
- * @param {string} botId. Tiledesk botId
106
- * @param {string} token. User token
107
- */
108
- function sendMessageToBot(message, botId, callback) {
109
- // const jwt_token = this.fixToken(token);
110
- const url = `${process.env.TYBOT_ENDPOINT}/ext/${botId}`;
111
- // console.log("sendMessageToBot URL", url);
112
- const HTTPREQUEST = {
113
- url: url,
114
- headers: {
115
- 'Content-Type': 'application/json'
116
- },
117
- json: message,
118
- method: 'POST'
119
- };
120
- myrequest(
121
- HTTPREQUEST,
122
- function (err, resbody) {
123
- if (err) {
124
- if (callback) {
125
- callback(err);
126
- }
127
- }
128
- else {
129
- if (callback) {
130
- callback(null, resbody);
131
- }
132
- }
133
- }, false
134
- );
135
- }
136
-
137
- function myrequest(options, callback, log) {
138
- if (log) {
139
- console.log("API URL:", options.url);
140
- console.log("** Options:", JSON.stringify(options));
141
- }
142
- axios(
143
- {
144
- url: options.url,
145
- method: options.method,
146
- data: options.json,
147
- params: options.params,
148
- headers: options.headers
149
- })
150
- .then((res) => {
151
- if (log) {
152
- console.log("Response for url:", options.url);
153
- console.log("Response headers:\n", JSON.stringify(res.headers));
154
- //console.log("******** Response for url:", res);
155
- }
156
- if (res && res.status == 200 && res.data) {
157
- if (callback) {
158
- callback(null, res.data);
159
- }
160
- }
161
- else {
162
- if (callback) {
163
- callback(TiledeskClient.getErr({ message: "Response status not 200" }, options, res), null, null);
164
- }
165
- }
166
- })
167
- .catch((error) => {
168
- console.error("An error occurred:", error);
169
- if (callback) {
170
- callback(error, null, null);
171
- }
172
- });
173
- }