@tiledesk/tiledesk-server 2.1.40 → 2.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. package/.circleci/config.yml +54 -0
  2. package/.env.sample +1 -1
  3. package/.github/workflows/docker-community-push-latest.yml +22 -0
  4. package/.github/workflows/{docker-image-push.yml → docker-image-en-tag-push.yml} +1 -1
  5. package/.github/workflows/docker-image-tag-community-tag-push.yml +21 -0
  6. package/.github/workflows/{docker-push-latest.yml → docker-push-en-push-latest.yml} +1 -1
  7. package/CHANGELOG.md +195 -1
  8. package/Dockerfile +1 -1
  9. package/Dockerfile-en +1 -1
  10. package/README.md +5 -7
  11. package/app.js +12 -1
  12. package/channels/channelManager.js +1 -1
  13. package/channels/chat21/chat21Contact.js +34 -8
  14. package/channels/chat21/chat21Handler.js +48 -5
  15. package/channels/chat21/chat21WebHook.js +34 -9
  16. package/channels/chat21/nativeauth.js +2 -2
  17. package/channels/chat21/package-lock.json +3013 -0
  18. package/config/email.js +2 -0
  19. package/config/global.js +3 -0
  20. package/config/labels/widget.json +170 -16
  21. package/event/messageEvent.js +18 -1
  22. package/middleware/passport.js +10 -4
  23. package/migrations/1619185894304-request-remove-duplicated-request-by-request_id--autosync.js +67 -0
  24. package/models/actionsConstants.js +7 -0
  25. package/models/department.js +3 -0
  26. package/models/faq.js +8 -2
  27. package/models/faq_kb.js +6 -0
  28. package/models/message.js +10 -4
  29. package/models/messageConstants.js +9 -3
  30. package/models/request.js +33 -3
  31. package/package.json +31 -28
  32. package/pubmodules/emailNotification/requestNotification.js +483 -56
  33. package/pubmodules/messageActions/messageActionsInterceptor.js +20 -7
  34. package/pubmodules/messageTransformer/index.js +5 -1
  35. package/pubmodules/messageTransformer/messageTransformerInterceptor.js +4 -2
  36. package/pubmodules/messageTransformer/microLanguageAttributesTransformerInterceptor.js +67 -0
  37. package/pubmodules/messageTransformer/microLanguageTransformerInterceptor.js +67 -0
  38. package/pubmodules/pubModulesManager.js +66 -13
  39. package/pubmodules/rules/conciergeBot.js +81 -49
  40. package/routes/auth.js +46 -11
  41. package/routes/campaigns.js +117 -25
  42. package/routes/department.js +2 -2
  43. package/routes/faq.js +19 -0
  44. package/routes/faq_kb.js +13 -4
  45. package/routes/faqpub.js +1 -1
  46. package/routes/files.js +17 -2
  47. package/routes/images.js +1 -1
  48. package/routes/jwt.js +0 -1
  49. package/routes/logs.js +26 -0
  50. package/routes/message.js +7 -2
  51. package/routes/messagesRoot.js +73 -16
  52. package/routes/project_user.js +36 -1
  53. package/routes/request.js +88 -12
  54. package/routes/requestUtilRoot.js +30 -0
  55. package/routes/urls.js +12 -0
  56. package/routes/users.js +5 -1
  57. package/services/BotSubscriptionNotifier.js +1 -0
  58. package/services/departmentService.js +29 -5
  59. package/services/emailService.js +1170 -239
  60. package/services/faqBotHandler.js +176 -61
  61. package/services/faqBotSupport.js +182 -117
  62. package/services/faqService.js +18 -14
  63. package/services/messageService.js +57 -9
  64. package/services/modulesManager.js +86 -23
  65. package/services/requestService.js +58 -17
  66. package/template/email/assignedEmailMessage.html +205 -0
  67. package/template/email/assignedRequest.html +44 -14
  68. package/template/email/beenInvitedExistingUser.html +2 -2
  69. package/template/email/beenInvitedNewUser.html +1 -1
  70. package/template/email/newMessage.html +31 -12
  71. package/template/email/passwordChanged.html +2 -3
  72. package/template/email/pooledEmailMessage.html +208 -0
  73. package/template/email/pooledRequest.html +41 -14
  74. package/template/email/resetPassword.html +2 -3
  75. package/template/email/sendTranscript.html +1 -1
  76. package/template/email/test.html +1 -1
  77. package/template/email/ticket.html +191 -0
  78. package/template/email/ticket.txt +11 -0
  79. package/template/email/verify.html +1 -1
  80. package/test/authentication.js +76 -4
  81. package/test/authenticationJwt.js +76 -2
  82. package/test/campaignsRoute.js +226 -0
  83. package/test/faqService.js +3 -3
  84. package/test/faqkbRoute.js +3 -2
  85. package/test/messageRootRoute.js +193 -0
  86. package/test/messageRoute.js +75 -0
  87. package/test/messageService.js +39 -2
  88. package/test/requestRoute.js +27 -9
  89. package/test/requestService.js +472 -11
  90. package/test-int/bot.js +673 -8
  91. package/websocket/webSocketServer.js +7 -4
package/test-int/bot.js CHANGED
@@ -15,6 +15,7 @@ var faqService = require('../services/faqService');
15
15
  var Department = require('../models/department');
16
16
  var Faq = require('../models/faq');
17
17
  var faqBotSupport = require('../services/faqBotSupport');
18
+ var Project_user = require("../models/project_user");
18
19
 
19
20
  var expect = chai.expect;
20
21
  var assert = chai.assert;
@@ -68,7 +69,7 @@ describe('bot', () => {
68
69
  .post('/'+ savedProject._id + '/subscriptions')
69
70
  .auth(email, pwd)
70
71
  .set('content-type', 'application/json')
71
- .send({"event":"message.create", "target":"http://localhost:3006/"})
72
+ .send({"event":"message.create", "target":"http://localhost:3005/"})
72
73
  .end((err, res) => {
73
74
  console.log("res.body", JSON.stringify(res.body));
74
75
  // console.dir("res.body 1", res.body);
@@ -102,13 +103,19 @@ describe('bot', () => {
102
103
  expect(req.body.payload.recipient).to.equal("request_id-subscription-message-sending");
103
104
  // expect(req.body.payload.attributes._answer._id.toString()).to.equal(savedFaq._id.toString());
104
105
  expect(req.body.payload.attributes._answerid.toString()).to.equal(savedFaq._id.toString());
106
+
107
+ expect(req.body.payload.attributes.intent_info.is_fallback).to.equal(false);
108
+
109
+ expect(req.body.payload.attributes.intent_info.question_payload.text).to.equal("question");
110
+
111
+
105
112
  done();
106
113
 
107
114
 
108
115
 
109
116
 
110
117
  });
111
- var listener = serverClient.listen(3006, '0.0.0.0', function(){ console.log('Node js Express started', listener.address());});
118
+ var listener = serverClient.listen(3005, '0.0.0.0', function(){ console.log('Node js Express started', listener.address());});
112
119
 
113
120
 
114
121
  leadService.createIfNotExists("leadfullname-subscription-message-sending", "andrea.leo@-subscription-message-sending.it", savedProject._id).then(function(createdLead) {
@@ -133,6 +140,237 @@ describe('bot', () => {
133
140
 
134
141
 
135
142
 
143
+ // mocha test-int/bot.js --grep 'createSimpleAgent'
144
+ it('createSimpleAgent', (done) => {
145
+
146
+ var email = "test-bot-" + Date.now() + "@email.com";
147
+ var pwd = "pwd";
148
+
149
+
150
+
151
+ userService.signup( email ,pwd, "Test Firstname", "Test lastname").then(function(savedUser) {
152
+ projectService.create("test-bot", savedUser._id).then(function(savedProject) {
153
+ // create(name, url, projectid, user_id, type)
154
+ faqService.create("testbot", null, savedProject._id, savedUser._id, "internal").then(function(savedBot) {
155
+
156
+ var newFaq = new Faq({
157
+ id_faq_kb: savedBot._id,
158
+ question: 'switch agent',
159
+ answer: '\\agent',
160
+ id_project: savedProject._id,
161
+ createdBy: savedUser._id,
162
+ updatedBy: savedUser._id
163
+ });
164
+
165
+ newFaq.save(function (err, savedFaq) {
166
+
167
+
168
+ Department.findOneAndUpdate({id_project: savedProject._id, default:true}, {id_bot:savedBot._id}, function (err, updatedDepartment) {
169
+ console.log('000');
170
+ chai.request(server)
171
+ .post('/'+ savedProject._id + '/subscriptions')
172
+ .auth(email, pwd)
173
+ .set('content-type', 'application/json')
174
+ .send({"event":"request.update", "target":"http://localhost:3006/"})
175
+ .end((err, res) => {
176
+ console.log("res.body", JSON.stringify(res.body));
177
+ // console.dir("res.body 1", res.body);
178
+ console.log("res.headers", res.headers);
179
+ res.should.have.status(200);
180
+ res.body.should.be.a('object');
181
+ expect(res.body.event).to.equal("request.update");
182
+ var secret = res.body.secret;
183
+ expect(secret).to.not.equal(null);
184
+ expect(res.headers["x-hook-secret"]).to.equal(secret);
185
+ console.log('001');
186
+
187
+
188
+ let messageReceived = 0;
189
+ var serverClient = express();
190
+ serverClient.use(bodyParser.json());
191
+ serverClient.post('/', function (req, res) {
192
+ console.log('serverClient req', JSON.stringify(req.body));
193
+ console.log("serverClient.headers", JSON.stringify(req.headers));
194
+ messageReceived = messageReceived+1;
195
+ expect(req.body.hook.event).to.equal("request.update");
196
+ console.log('11');
197
+ expect(req.body.payload.request_id).to.equal("request_id-subscription-message-sending-createSimpleAgent");
198
+ console.log('12');
199
+ expect(req.body.payload.hasBot).equal(false);
200
+ console.log('savedUser._id',savedUser._id);
201
+ expect(req.body.payload.participantsAgents[0]).equal(savedUser._id.toString());
202
+ console.log('13');
203
+
204
+ expect(req.headers["x-hook-secret"]).to.equal(secret);
205
+ res.send('POST request to the homepage');
206
+ expect(req.body.payload.first_text).to.equal("first_text");
207
+
208
+ done();
209
+
210
+
211
+
212
+
213
+ });
214
+ var listener = serverClient.listen(3006, '0.0.0.0', function(){ console.log('Node js Express started', listener.address());});
215
+
216
+
217
+ leadService.createIfNotExists("leadfullname-subscription-message-sending-createSimpleAgent", "andrea.leo@-subscription-message-sending.it", savedProject._id).then(function(createdLead) {
218
+ requestService.createWithId("request_id-subscription-message-sending-createSimpleAgent", createdLead._id, savedProject._id, "first_text").then(function(savedRequest) {
219
+ messageService.create(savedUser._id, "test sender", savedRequest.request_id, "switch agent",
220
+ savedProject._id, savedUser._id).then(function(savedMessage){
221
+ expect(savedMessage.text).to.equal("switch agent");
222
+ // expect(savedMessage.sender).to.equal("question");
223
+ });
224
+ });
225
+ });
226
+ });
227
+ });
228
+ });
229
+ });
230
+
231
+ });
232
+ });
233
+ }).timeout(20000);
234
+
235
+
236
+
237
+
238
+
239
+ // mocha test-int/bot.js --grep 'createSimpleAgentTwoAgent'
240
+ it('createSimpleAgentTwoAgent', (done) => {
241
+
242
+ var email = "test-bot-" + Date.now() + "@email.com";
243
+ var pwd = "pwd";
244
+
245
+
246
+
247
+ userService.signup( email ,pwd, "Test Firstname", "Test lastname").then(function(savedUser) {
248
+
249
+ projectService.create("test-bot", savedUser._id).then(function(savedProject) {
250
+
251
+ userService.signup( "test-bot-" + Date.now() + "@email.com" ,pwd, "Test Firstname", "Test lastname").then(function(savedUser2) {
252
+
253
+ var newProject_user = new Project_user({
254
+ // _id: new mongoose.Types.ObjectId(),
255
+ id_project: savedProject._id.toString(),
256
+ id_user: savedUser2._id.toString(),
257
+ role: "agent",
258
+ user_available: true,
259
+ createdBy: savedUser._id,
260
+ updatedBy: savedUser._id
261
+ });
262
+
263
+ return newProject_user.save(function (err, savedProject_user) {
264
+
265
+ if (err) {
266
+ console.log("err",err)
267
+ }
268
+ leadService.createIfNotExists("leadfullname-subscription-message-sending-createSimpleAgent", "andrea.leo@-subscription-message-sending.it", savedProject._id).then(function(createdLead) {
269
+ requestService.createWithId("request_id-subscription-message-sending-createSimpleAgent-2", createdLead._id, savedProject._id, "first_text").then(function(savedRequest2) {
270
+ console.log("savedRequest2", savedRequest2);
271
+
272
+ expect(savedRequest2.request_id).to.equal("request_id-subscription-message-sending-createSimpleAgent-2");
273
+ // expect(savedRequest2.participantsAgents[0]).equal(savedUser2._id.toString());
274
+ var selectedAgent = savedRequest2.participantsAgents[0];
275
+ console.log("selectedAgent", selectedAgent);
276
+
277
+ expect(savedRequest2.hasBot).equal(false);
278
+
279
+ messageService.create(savedUser._id, "test sender", savedRequest2.request_id, "switch agent", savedProject._id, savedUser._id).then(function(savedMessage2){
280
+ expect(savedMessage2.text).to.equal("switch agent");
281
+ // expect(savedMessage.sender).to.equal("question");
282
+
283
+ // create(name, url, projectid, user_id, type)
284
+ faqService.create("testbot", null, savedProject._id, savedUser._id, "internal").then(function(savedBot) {
285
+
286
+ var newFaq = new Faq({
287
+ id_faq_kb: savedBot._id,
288
+ question: 'switch agent',
289
+ answer: '\\agent',
290
+ id_project: savedProject._id,
291
+ createdBy: savedUser._id,
292
+ updatedBy: savedUser._id
293
+ });
294
+
295
+ newFaq.save(function (err, savedFaq) {
296
+
297
+
298
+ Department.findOneAndUpdate({id_project: savedProject._id, default:true}, {id_bot:savedBot._id}, function (err, updatedDepartment) {
299
+ console.log('000');
300
+ chai.request(server)
301
+ .post('/'+ savedProject._id + '/subscriptions')
302
+ .auth(email, pwd)
303
+ .set('content-type', 'application/json')
304
+ .send({"event":"request.update", "target":"http://localhost:3021/"})
305
+ .end((err, res) => {
306
+ console.log("res.body", JSON.stringify(res.body));
307
+ // console.dir("res.body 1", res.body);
308
+ console.log("res.headers", res.headers);
309
+ res.should.have.status(200);
310
+ res.body.should.be.a('object');
311
+ expect(res.body.event).to.equal("request.update");
312
+ var secret = res.body.secret;
313
+ expect(secret).to.not.equal(null);
314
+ expect(res.headers["x-hook-secret"]).to.equal(secret);
315
+ console.log('001');
316
+
317
+
318
+ let messageReceived = 0;
319
+ var serverClient = express();
320
+ serverClient.use(bodyParser.json());
321
+ serverClient.post('/', function (req, res) {
322
+ console.log('serverClient req', JSON.stringify(req.body));
323
+ console.log("serverClient.headers", JSON.stringify(req.headers));
324
+ messageReceived = messageReceived+1;
325
+ expect(req.body.hook.event).to.equal("request.update");
326
+ console.log('11');
327
+ expect(req.body.payload.request_id).to.equal("request_id-subscription-message-sending-createSimpleAgent");
328
+ console.log('12');
329
+ expect(req.body.payload.hasBot).equal(false);
330
+ console.log('savedUser._id',savedUser._id);
331
+ console.log('savedUser2._id',savedUser2._id);
332
+ // expect(req.body.payload.participantsAgents[0]).equal(savedUser._id.toString());
333
+ expect(req.body.payload.participantsAgents[0]).not.equal(selectedAgent);
334
+
335
+ console.log('13');
336
+
337
+ expect(req.headers["x-hook-secret"]).to.equal(secret);
338
+ res.send('POST request to the homepage');
339
+ expect(req.body.payload.first_text).to.equal("first_text");
340
+
341
+ done();
342
+
343
+
344
+
345
+
346
+ });
347
+ var listener = serverClient.listen(3021, '0.0.0.0', function(){ console.log('Node js Express started', listener.address());});
348
+
349
+
350
+
351
+ requestService.createWithId("request_id-subscription-message-sending-createSimpleAgent", createdLead._id, savedProject._id, "first_text").then(function(savedRequest) {
352
+ messageService.create(savedUser._id, "test sender", savedRequest.request_id, "switch agent",
353
+ savedProject._id, savedUser._id).then(function(savedMessage){
354
+ expect(savedMessage.text).to.equal("switch agent");
355
+
356
+ // expect(savedMessage.sender).to.equal("question");
357
+ });
358
+ });
359
+ });
360
+ });
361
+ });
362
+ });
363
+ });
364
+ });
365
+ });
366
+ });
367
+ });
368
+ });
369
+ });
370
+ }).timeout(20000);
371
+
372
+
373
+
136
374
 
137
375
  // mocha test-int/bot.js --grep 'createSimpleFulltext'
138
376
  it('createSimpleFulltext', (done) => {
@@ -403,6 +641,8 @@ describe('bot', () => {
403
641
  expect(req.body.payload.metadata.src).to.equal("https://www.tiledesk.com/wp-content/uploads/2018/03/tiledesk-logo.png");
404
642
  expect(req.body.payload.metadata.width).to.equal(200);
405
643
  expect(req.body.payload.metadata.height).to.equal(200);
644
+ expect(req.body.payload.attributes.intent_info.is_fallback).to.equal(false);
645
+ expect(req.body.payload.attributes.intent_info.question_payload.text).to.equal("question");
406
646
  expect(req.body.payload.attributes._raw_message).to.equal('answer \n\\image:https://www.tiledesk.com/wp-content/uploads/2018/03/tiledesk-logo.png');
407
647
 
408
648
  done();;
@@ -502,6 +742,8 @@ describe('bot', () => {
502
742
  expect(req.body.payload.text).to.equal("answer");
503
743
  expect(req.body.payload.attributes.attachment.buttons[0].value).to.equal("Button 1");
504
744
  expect(req.body.payload.attributes.attachment.buttons[0].type).to.equal("text");
745
+ expect(req.body.payload.attributes.intent_info.is_fallback).to.equal(false);
746
+ expect(req.body.payload.attributes.intent_info.question_payload.text).to.equal("question");
505
747
  expect(req.body.payload.attributes._raw_message).to.equal('answer\n* Button 1\n* Button 2');
506
748
  done();;
507
749
 
@@ -614,6 +856,8 @@ describe('bot', () => {
614
856
  res.send('POST request to the homepage');
615
857
  expect(req.body.payload.text).to.equal("answer2");
616
858
  expect(req.body.payload.attributes._raw_message).to.equal('answer2');
859
+ expect(req.body.payload.attributes.intent_info.is_fallback).to.equal(false);
860
+ expect(req.body.payload.attributes.intent_info.question_payload.text).to.equal("start action");
617
861
  // expect(req.body.payload.attributes.attachment.buttons[0].value).to.equal("Button 1");
618
862
  // expect(req.body.payload.attributes.attachment.buttons[0].type).to.equal("text");
619
863
 
@@ -730,6 +974,8 @@ describe('bot', () => {
730
974
  res.send('POST request to the homepage');
731
975
  expect(req.body.payload.text).to.equal("answer2");
732
976
  expect(req.body.payload.attributes._raw_message).to.equal('answer2');
977
+ expect(req.body.payload.attributes.intent_info.is_fallback).to.equal(false);
978
+ expect(req.body.payload.attributes.intent_info.question_payload.text).to.equal("start action");
733
979
  // expect(req.body.payload.attributes.attachment.buttons[0].value).to.equal("Button 1");
734
980
  // expect(req.body.payload.attributes.attachment.buttons[0].type).to.equal("text");
735
981
 
@@ -977,7 +1223,7 @@ describe('bot', () => {
977
1223
  userService.signup( email ,pwd, "Test Firstname", "Test lastname").then(function(savedUser) {
978
1224
  projectService.create("test-bot", savedUser._id).then(function(savedProject) {
979
1225
  // create(name, url, projectid, user_id, type)
980
- faqService.create("testbot", undefined, savedProject._id, savedUser._id, "internal", undefined, "http://localhost:3019/").then(function(savedBot) {
1226
+ faqService.create("testbot", undefined, savedProject._id, savedUser._id, "internal", undefined, "http://localhost:3019/", true).then(function(savedBot) {
981
1227
 
982
1228
  var newFaq = new Faq({
983
1229
  id_faq_kb: savedBot._id,
@@ -1025,14 +1271,17 @@ describe('bot', () => {
1025
1271
  messageReceived = messageReceived+1;
1026
1272
  expect(req.body.hook.event).to.equal("message.create");
1027
1273
  expect(req.body.payload.type).to.equal("text");
1274
+ console.log('1');
1028
1275
  expect(req.body.payload.request.request_id).to.equal("request_id-subscription-message-createFaqWithButton");
1029
1276
  expect(req.body.payload.request.department).to.not.equal(null);
1030
1277
  expect(req.body.payload.request.department.bot).to.not.equal(null);
1031
1278
  expect(req.body.payload.request.department.bot.name).to.equal("testbot");
1032
-
1279
+ console.log('2');
1033
1280
  expect(req.headers["x-hook-secret"]).to.equal(secret);
1034
1281
  res.send('POST request to the homepage');
1282
+ console.log('3',req.body.payload.text);
1035
1283
  expect(req.body.payload.text).to.equal("ok from webhook");
1284
+ console.log('4');
1036
1285
  expect(req.body.payload.attributes.attachment.buttons[0].value).to.equal("button1");
1037
1286
  expect(req.body.payload.attributes.attachment.buttons[0].type).to.equal("text");
1038
1287
 
@@ -1074,6 +1323,379 @@ describe('bot', () => {
1074
1323
 
1075
1324
 
1076
1325
 
1326
+ // mocha test-int/bot.js --grep 'createFaqWithDefaultIntentWebhook'
1327
+
1328
+ it('createFaqWithDefaultIntentWebhook', (done) => {
1329
+
1330
+ var email = "test-bot-" + Date.now() + "@email.com";
1331
+ var pwd = "pwd";
1332
+
1333
+
1334
+
1335
+ userService.signup( email ,pwd, "Test Firstname", "Test lastname").then(function(savedUser) {
1336
+ projectService.create("test-bot", savedUser._id).then(function(savedProject) {
1337
+ // create(name, url, projectid, user_id, type)
1338
+ faqService.create("testbot", undefined, savedProject._id, savedUser._id, "internal", undefined, "http://localhost:3029/", true).then(function(savedBot) {
1339
+
1340
+ Faq.findOneAndUpdate({id_project:savedProject._id,id_faq_kb:savedBot._id, question: "defaultFallback" }, {webhook_enabled: true},{new: true, upsert:false}, function (err, savedFaq) {
1341
+ console.log("savedFaq",savedFaq);
1342
+
1343
+ Department.findOneAndUpdate({id_project: savedProject._id, default:true}, {id_bot:savedBot._id}, function (err, updatedDepartment) {
1344
+
1345
+ chai.request(server)
1346
+ .post('/'+ savedProject._id + '/subscriptions')
1347
+ .auth(email, pwd)
1348
+ .set('content-type', 'application/json')
1349
+ .send({"event":"message.create", "target":"http://localhost:3022/"})
1350
+ .end((err, res) => {
1351
+ console.log("res.body", JSON.stringify(res.body));
1352
+ // console.dir("res.body 1", res.body);
1353
+ console.log("res.headers", res.headers);
1354
+ res.should.have.status(200);
1355
+ res.body.should.be.a('object');
1356
+ expect(res.body.event).to.equal("message.create");
1357
+ var secret = res.body.secret;
1358
+ expect(secret).to.not.equal(null);
1359
+ expect(res.headers["x-hook-secret"]).to.equal(secret);
1360
+
1361
+
1362
+ let messageReceived = 0;
1363
+ var serverClient = express();
1364
+ serverClient.use(bodyParser.json());
1365
+ serverClient.post('/', function (req, res) {
1366
+ console.log('serverClient req', JSON.stringify(req.body));
1367
+ console.log("serverClient.headers", JSON.stringify(req.headers));
1368
+
1369
+ if (req.body.payload.text.indexOf("I can not provide an adequate answer")>-1) {
1370
+ return res.send('POST request to the homepage');
1371
+ }
1372
+ console.log('sono qui');
1373
+ messageReceived = messageReceived+1;
1374
+ expect(req.body.hook.event).to.equal("message.create");
1375
+ expect(req.body.payload.type).to.equal("text");
1376
+ expect(req.body.payload.request.request_id).to.equal("request_id-subscription-message-createFaqWithDefaultIntentWebhook");
1377
+ expect(req.body.payload.request.department).to.not.equal(null);
1378
+ expect(req.body.payload.request.department.bot).to.not.equal(null);
1379
+ expect(req.body.payload.request.department.bot.name).to.equal("testbot");
1380
+
1381
+ expect(req.headers["x-hook-secret"]).to.equal(secret);
1382
+ res.send('POST request to the homepage');
1383
+ expect(req.body.payload.text).to.equal("ok from webhook");
1384
+ expect(req.body.payload.attributes.attachment.buttons[0].value).to.equal("button1");
1385
+ expect(req.body.payload.attributes.attachment.buttons[0].type).to.equal("text");
1386
+
1387
+ expect(req.body.payload.attributes.intent_info.is_fallback).to.equal(true);
1388
+ expect(req.body.payload.attributes.intent_info.question_payload.text).to.equal("notfoundword");
1389
+ expect(req.body.payload.attributes._raw_message).to.equal('ok from webhook\n* button1');
1390
+
1391
+
1392
+ done();
1393
+
1394
+
1395
+ });
1396
+ var listener = serverClient.listen(3022, '0.0.0.0', function(){ console.log('Node js Express started', listener.address());});
1397
+
1398
+ var serverClient2 = express();
1399
+ serverClient2.use(bodyParser.json());
1400
+ serverClient2.post('/', function (req, res) {
1401
+ console.log('serverClient req2', JSON.stringify(req.body));
1402
+ console.log("serverClient.headers2", JSON.stringify(req.headers));
1403
+ res.send({text:"ok from webhook\n* button1"});
1404
+ });
1405
+ var listener2 = serverClient2.listen(3029, '0.0.0.0', function(){ console.log('Node js Express started', listener2.address());});
1406
+
1407
+
1408
+ leadService.createIfNotExists("leadfullname-subscription-message-sending", "andrea.leo@-subscription-message-sending.it", savedProject._id).then(function(createdLead) {
1409
+ requestService.createWithId("request_id-subscription-message-createFaqWithDefaultIntentWebhook", createdLead._id, savedProject._id, "first_text").then(function(savedRequest) {
1410
+ messageService.create(savedUser._id, "test sender", savedRequest.request_id, "notfoundword",
1411
+ savedProject._id, savedUser._id).then(function(savedMessage){
1412
+ expect(savedMessage.text).to.equal("notfoundword");
1413
+ });
1414
+ });
1415
+ });
1416
+ });
1417
+ });
1418
+ });
1419
+ });
1420
+
1421
+ });
1422
+ });
1423
+ }).timeout(20000);
1424
+
1425
+
1426
+
1427
+
1428
+
1429
+
1430
+
1431
+ // mocha test-int/bot.js --grep 'createFaqWithDefaultIntentWebhookReturnAttributes'
1432
+
1433
+ it('createFaqWithDefaultIntentWebhookReturnAttributes', (done) => {
1434
+
1435
+ var email = "test-bot-" + Date.now() + "@email.com";
1436
+ var pwd = "pwd";
1437
+
1438
+
1439
+
1440
+ userService.signup( email ,pwd, "Test Firstname", "Test lastname").then(function(savedUser) {
1441
+ projectService.create("test-bot", savedUser._id).then(function(savedProject) {
1442
+ // create(name, url, projectid, user_id, type)
1443
+ faqService.create("testbot", undefined, savedProject._id, savedUser._id, "internal", undefined, "http://localhost:3028/", true).then(function(savedBot) {
1444
+
1445
+ Faq.findOneAndUpdate({id_project:savedProject._id,id_faq_kb:savedBot._id, question: "defaultFallback" }, {webhook_enabled: true},{new: true, upsert:false}, function (err, savedFaq) {
1446
+ console.log("savedFaq",savedFaq);
1447
+
1448
+ Department.findOneAndUpdate({id_project: savedProject._id, default:true}, {id_bot:savedBot._id}, function (err, updatedDepartment) {
1449
+
1450
+ chai.request(server)
1451
+ .post('/'+ savedProject._id + '/subscriptions')
1452
+ .auth(email, pwd)
1453
+ .set('content-type', 'application/json')
1454
+ .send({"event":"message.create", "target":"http://localhost:3023/"})
1455
+ .end((err, res) => {
1456
+ console.log("res.body", JSON.stringify(res.body));
1457
+ // console.dir("res.body 1", res.body);
1458
+ console.log("res.headers", res.headers);
1459
+ res.should.have.status(200);
1460
+ res.body.should.be.a('object');
1461
+ expect(res.body.event).to.equal("message.create");
1462
+ var secret = res.body.secret;
1463
+ expect(secret).to.not.equal(null);
1464
+ expect(res.headers["x-hook-secret"]).to.equal(secret);
1465
+
1466
+
1467
+ var serverClient = express();
1468
+ serverClient.use(bodyParser.json());
1469
+ serverClient.post('/', function (req, res) {
1470
+ console.log('serverClient req', JSON.stringify(req.body));
1471
+ console.log("serverClient.headers", JSON.stringify(req.headers));
1472
+ console.log('sono qui',req.body.payload.text);
1473
+ // if (req.body.payload.text.indexOf("I can not provide an adequate answer")>-1) {
1474
+ // return res.send('POST request to the homepage');
1475
+ // }
1476
+
1477
+ expect(req.body.hook.event).to.equal("message.create");
1478
+ console.log('req.body.payload',req.body.payload);
1479
+ expect(req.body.payload.type).to.equal("text");
1480
+ console.log('01');
1481
+ expect(req.body.payload.request.request_id).to.equal("request_id-subscription-message-createFaqWithDefaultIntentWebhookReturnAttributes");
1482
+ console.log('02');
1483
+ expect(req.body.payload.request.department).to.not.equal(null);
1484
+ console.log('03');
1485
+ expect(req.body.payload.request.department.bot).to.not.equal(null);
1486
+ console.log('04');
1487
+ expect(req.body.payload.request.department.bot.name).to.equal("testbot");
1488
+ console.log('05');
1489
+ expect(req.headers["x-hook-secret"]).to.equal(secret);
1490
+
1491
+ console.log('06');
1492
+ res.send('POST request to the homepage');
1493
+ console.log('07', req.body.payload.text);
1494
+ expect(req.body.payload.text).to.equal("ok from webhook with no microlanguage but attributes");
1495
+ console.log('before attributes',req.body.payload.attributes);
1496
+
1497
+
1498
+ // expect(req.body.payload.channel_type).to.equal("group");
1499
+ expect(req.body.payload.type).to.equal("text");
1500
+ console.log('11');
1501
+ // expect(req.body.payload.language).to.equal("IT");
1502
+ // console.log('22');
1503
+ // expect(req.body.payload.channel.name).to.equal("custom-channel");
1504
+
1505
+ expect(req.body.payload.attributes.attachment.buttons[0].value).to.equal("button1");
1506
+ console.log('33');
1507
+ expect(req.body.payload.attributes.attachment.buttons[0].type).to.equal("text");
1508
+ console.log('44');
1509
+ expect(req.body.payload.attributes.intent_info.is_fallback).to.equal(true);
1510
+ console.log('55');
1511
+ expect(req.body.payload.attributes.intent_info.question_payload.text).to.equal("notfoundword");
1512
+ console.log('66');
1513
+ expect(req.body.payload.attributes._raw_message).to.equal('ok from webhook with no microlanguage but attributes');
1514
+ console.log('77');
1515
+
1516
+ done();
1517
+
1518
+
1519
+ });
1520
+ var listener = serverClient.listen(3023, '0.0.0.0', function(){ console.log('Node js Express started', listener.address());});
1521
+
1522
+ var serverClient2 = express();
1523
+ serverClient2.use(bodyParser.json());
1524
+ serverClient2.post('/', function (req, res) {
1525
+ console.log('serverClient req2', JSON.stringify(req.body));
1526
+ console.log("serverClient.headers2", JSON.stringify(req.headers));
1527
+ res.send({text:"ok from webhook with no microlanguage but attributes", attributes: {attachment: {buttons: [{value: "button1", type:"text"}]}}});
1528
+ });
1529
+ var listener2 = serverClient2.listen(3028, '0.0.0.0', function(){ console.log('Node js Express started', listener2.address());});
1530
+
1531
+
1532
+ leadService.createIfNotExists("leadfullname-subscription-message-sending", "andrea.leo@-subscription-message-sending.it", savedProject._id).then(function(createdLead) {
1533
+ requestService.createWithId("request_id-subscription-message-createFaqWithDefaultIntentWebhookReturnAttributes", createdLead._id, savedProject._id, "first_text").then(function(savedRequest) {
1534
+ messageService.create(savedUser._id, "test sender", savedRequest.request_id, "notfoundword",
1535
+ savedProject._id, savedUser._id
1536
+
1537
+ ,undefined, undefined, undefined, undefined, "IT", undefined,
1538
+ //{name:"custom-channel"}
1539
+ )
1540
+ .then(function(savedMessage){
1541
+ console.log("message saved ok")
1542
+ // create(sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language, channel_type, channel) {
1543
+ // messageService.save({sender:savedUser._id, senderFullname:"test sender",recipient: savedRequest.request_id, text:"notfoundword",
1544
+ // id_project:savedProject._id, createdBy: savedUser._id, status: 0, channel:{name:"custom-channel"}}).then(function(savedMessage){
1545
+ expect(savedMessage.text).to.equal("notfoundword");
1546
+ expect(savedMessage.language).to.equal("IT");
1547
+ });
1548
+ });
1549
+ });
1550
+ });
1551
+ });
1552
+ });
1553
+ });
1554
+
1555
+ });
1556
+ });
1557
+ }).timeout(20000);
1558
+
1559
+
1560
+
1561
+
1562
+
1563
+ // mocha test-int/bot.js --grep 'createFaqWithDefaultIntentWebhookReturnTypeAndMetadata'
1564
+
1565
+ it('createFaqWithDefaultIntentWebhookReturnTypeAndMetadata', (done) => {
1566
+
1567
+ var email = "test-bot-" + Date.now() + "@email.com";
1568
+ var pwd = "pwd";
1569
+
1570
+
1571
+
1572
+ userService.signup( email ,pwd, "Test Firstname", "Test lastname").then(function(savedUser) {
1573
+ projectService.create("test-bot", savedUser._id).then(function(savedProject) {
1574
+ // create(name, url, projectid, user_id, type)
1575
+ faqService.create("testbot", undefined, savedProject._id, savedUser._id, "internal", undefined, "http://localhost:3030/", true).then(function(savedBot) {
1576
+
1577
+ Faq.findOneAndUpdate({id_project:savedProject._id,id_faq_kb:savedBot._id, question: "defaultFallback" }, {webhook_enabled: true},{new: true, upsert:false}, function (err, savedFaq) {
1578
+ console.log("savedFaq",savedFaq);
1579
+
1580
+ Department.findOneAndUpdate({id_project: savedProject._id, default:true}, {id_bot:savedBot._id}, function (err, updatedDepartment) {
1581
+
1582
+ chai.request(server)
1583
+ .post('/'+ savedProject._id + '/subscriptions')
1584
+ .auth(email, pwd)
1585
+ .set('content-type', 'application/json')
1586
+ .send({"event":"message.create", "target":"http://localhost:3031/"})
1587
+ .end((err, res) => {
1588
+ console.log("res.body", JSON.stringify(res.body));
1589
+ // console.dir("res.body 1", res.body);
1590
+ console.log("res.headers", res.headers);
1591
+ res.should.have.status(200);
1592
+ res.body.should.be.a('object');
1593
+ expect(res.body.event).to.equal("message.create");
1594
+ var secret = res.body.secret;
1595
+ expect(secret).to.not.equal(null);
1596
+ expect(res.headers["x-hook-secret"]).to.equal(secret);
1597
+
1598
+
1599
+ var serverClient = express();
1600
+ serverClient.use(bodyParser.json());
1601
+ serverClient.post('/', function (req, res) {
1602
+ console.log('serverClient req', JSON.stringify(req.body));
1603
+ console.log("serverClient.headers", JSON.stringify(req.headers));
1604
+ console.log('sono qui',req.body.payload.text);
1605
+ // if (req.body.payload.text.indexOf("I can not provide an adequate answer")>-1) {
1606
+ // return res.send('POST request to the homepage');
1607
+ // }
1608
+
1609
+ expect(req.body.hook.event).to.equal("message.create");
1610
+ console.log('req.body.payload',req.body.payload);
1611
+ expect(req.body.payload.type).to.equal("image");
1612
+ console.log('01');
1613
+ expect(req.body.payload.request.request_id).to.equal("request_id-subscription-message-createFaqWithDefaultIntentWebhookReturnAttributes");
1614
+ console.log('02');
1615
+ expect(req.body.payload.request.department).to.not.equal(null);
1616
+ console.log('03');
1617
+ expect(req.body.payload.request.department.bot).to.not.equal(null);
1618
+ console.log('04');
1619
+ expect(req.body.payload.request.department.bot.name).to.equal("testbot");
1620
+ console.log('05');
1621
+ expect(req.headers["x-hook-secret"]).to.equal(secret);
1622
+
1623
+ console.log('06');
1624
+ res.send('POST request to the homepage');
1625
+ console.log('07', req.body.payload.text);
1626
+ expect(req.body.payload.text).to.equal("ok from webhook with no microlanguage but type and metadata");
1627
+ console.log('before attributes',req.body.payload.attributes);
1628
+
1629
+
1630
+ // expect(req.body.payload.channel_type).to.equal("group");
1631
+ expect(req.body.payload.type).to.equal("image");
1632
+ console.log('11');
1633
+ // expect(req.body.payload.language).to.equal("IT");
1634
+ // console.log('22');
1635
+ // expect(req.body.payload.channel.name).to.equal("custom-channel");
1636
+
1637
+ expect(req.body.payload.metadata.src).to.equal("http://image.jpg");
1638
+ console.log('44');
1639
+ expect(req.body.payload.attributes.intent_info.is_fallback).to.equal(true);
1640
+ console.log('55');
1641
+ expect(req.body.payload.attributes.intent_info.question_payload.text).to.equal("notfoundword");
1642
+ console.log('66');
1643
+ expect(req.body.payload.attributes._raw_message).to.equal('ok from webhook with no microlanguage but type and metadata');
1644
+ console.log('77');
1645
+
1646
+ done();
1647
+
1648
+
1649
+ });
1650
+ var listener = serverClient.listen(3031, '0.0.0.0', function(){ console.log('Node js Express started', listener.address());});
1651
+
1652
+ var serverClient2 = express();
1653
+ serverClient2.use(bodyParser.json());
1654
+ serverClient2.post('/', function (req, res) {
1655
+ console.log('serverClient req2', JSON.stringify(req.body));
1656
+ console.log("serverClient.headers2", JSON.stringify(req.headers));
1657
+ res.send({text:"ok from webhook with no microlanguage but type and metadata", type: "image", metadata: {src: "http://image.jpg" }});
1658
+ });
1659
+ var listener2 = serverClient2.listen(3030, '0.0.0.0', function(){ console.log('Node js Express started', listener2.address());});
1660
+
1661
+
1662
+ leadService.createIfNotExists("leadfullname-subscription-message-sending", "andrea.leo@-subscription-message-sending.it", savedProject._id).then(function(createdLead) {
1663
+ requestService.createWithId("request_id-subscription-message-createFaqWithDefaultIntentWebhookReturnAttributes", createdLead._id, savedProject._id, "first_text").then(function(savedRequest) {
1664
+ messageService.create(savedUser._id, "test sender", savedRequest.request_id, "notfoundword",
1665
+ savedProject._id, savedUser._id
1666
+
1667
+ ,undefined, undefined, undefined, undefined, "IT", undefined,
1668
+ //{name:"custom-channel"}
1669
+ )
1670
+ .then(function(savedMessage){
1671
+ console.log("message saved ok")
1672
+ // create(sender, senderFullname, recipient, text, id_project, createdBy, status, attributes, type, metadata, language, channel_type, channel) {
1673
+ // messageService.save({sender:savedUser._id, senderFullname:"test sender",recipient: savedRequest.request_id, text:"notfoundword",
1674
+ // id_project:savedProject._id, createdBy: savedUser._id, status: 0, channel:{name:"custom-channel"}}).then(function(savedMessage){
1675
+ expect(savedMessage.text).to.equal("notfoundword");
1676
+ expect(savedMessage.language).to.equal("IT");
1677
+ });
1678
+ });
1679
+ });
1680
+ });
1681
+ });
1682
+ });
1683
+ });
1684
+
1685
+ });
1686
+ });
1687
+ }).timeout(20000);
1688
+
1689
+
1690
+
1691
+
1692
+
1693
+
1694
+
1695
+
1696
+
1697
+
1698
+
1077
1699
  // mocha test-int/bot.js --grep 'createFaqWithWebhookMicrolanguage'
1078
1700
 
1079
1701
  it('createFaqWithWebhookMicrolanguage', (done) => {
@@ -1086,7 +1708,7 @@ describe('bot', () => {
1086
1708
  userService.signup( email ,pwd, "Test Firstname", "Test lastname").then(function(savedUser) {
1087
1709
  projectService.create("test-bot", savedUser._id).then(function(savedProject) {
1088
1710
  // create(name, url, projectid, user_id, type)
1089
- faqService.create("testbot", null, savedProject._id, savedUser._id, "internal").then(function(savedBot) {
1711
+ faqService.create("testbot", null, savedProject._id, savedUser._id, "internal", undefined, undefined, true).then(function(savedBot) {
1090
1712
 
1091
1713
  var newFaq = new Faq({
1092
1714
  id_faq_kb: savedBot._id,
@@ -1106,7 +1728,7 @@ describe('bot', () => {
1106
1728
  .post('/'+ savedProject._id + '/subscriptions')
1107
1729
  .auth(email, pwd)
1108
1730
  .set('content-type', 'application/json')
1109
- .send({"event":"message.create", "target":"http://localhost:3015/"})
1731
+ .send({"event":"message.create", "target":"http://localhost:3025/"})
1110
1732
  .end((err, res) => {
1111
1733
  console.log("res.body", JSON.stringify(res.body));
1112
1734
  // console.dir("res.body 1", res.body);
@@ -1133,14 +1755,17 @@ describe('bot', () => {
1133
1755
  messageReceived = messageReceived+1;
1134
1756
  expect(req.body.hook.event).to.equal("message.create");
1135
1757
  expect(req.body.payload.type).to.equal("text");
1758
+ console.log("1")
1136
1759
  expect(req.body.payload.request.request_id).to.equal("request_id-subscription-message-createFaqWithButton");
1137
1760
  expect(req.body.payload.request.department).to.not.equal(null);
1138
1761
  expect(req.body.payload.request.department.bot).to.not.equal(null);
1139
1762
  expect(req.body.payload.request.department.bot.name).to.equal("testbot");
1140
-
1763
+ console.log("2")
1141
1764
  expect(req.headers["x-hook-secret"]).to.equal(secret);
1142
1765
  res.send('POST request to the homepage');
1766
+ console.log("3",req.body.payload.text)
1143
1767
  expect(req.body.payload.text).to.equal("ok from webhook");
1768
+ console.log("4")
1144
1769
  expect(req.body.payload.attributes.attachment.buttons[0].value).to.equal("button1");
1145
1770
  expect(req.body.payload.attributes.attachment.buttons[0].type).to.equal("text");
1146
1771
 
@@ -1149,7 +1774,7 @@ describe('bot', () => {
1149
1774
 
1150
1775
 
1151
1776
  });
1152
- var listener = serverClient.listen(3015, '0.0.0.0', function(){ console.log('Node js Express started', listener.address());});
1777
+ var listener = serverClient.listen(3025, '0.0.0.0', function(){ console.log('Node js Express started', listener.address());});
1153
1778
 
1154
1779
  var serverClient2 = express();
1155
1780
  serverClient2.use(bodyParser.json());
@@ -1288,3 +1913,43 @@ describe('bot', () => {
1288
1913
 
1289
1914
  });
1290
1915
  });
1916
+
1917
+
1918
+
1919
+
1920
+ // 5 failing
1921
+
1922
+ // 1) bot
1923
+ // /messages
1924
+ // createFaqWithWebhook:
1925
+ // Error: Timeout of 20000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/andrealeo/dev/chat21/tiledesk-server-dev-org/test-int/bot.js)
1926
+ // at listOnTimeout (internal/timers.js:557:17)
1927
+ // at processTimers (internal/timers.js:500:7)
1928
+
1929
+ // 2) bot
1930
+ // /messages
1931
+ // createFaqWithDefaultIntentWebhook:
1932
+ // Error: Timeout of 20000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/andrealeo/dev/chat21/tiledesk-server-dev-org/test-int/bot.js)
1933
+ // at listOnTimeout (internal/timers.js:557:17)
1934
+ // at processTimers (internal/timers.js:500:7)
1935
+
1936
+ // 3) bot
1937
+ // /messages
1938
+ // createFaqWithDefaultIntentWebhookReturnAttributes:
1939
+ // Error: Timeout of 20000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/andrealeo/dev/chat21/tiledesk-server-dev-org/test-int/bot.js)
1940
+ // at listOnTimeout (internal/timers.js:557:17)
1941
+ // at processTimers (internal/timers.js:500:7)
1942
+
1943
+ // 4) bot
1944
+ // /messages
1945
+ // createFaqWithDefaultIntentWebhookReturnTypeAndMetadata:
1946
+ // Error: Timeout of 20000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/andrealeo/dev/chat21/tiledesk-server-dev-org/test-int/bot.js)
1947
+ // at listOnTimeout (internal/timers.js:557:17)
1948
+ // at processTimers (internal/timers.js:500:7)
1949
+
1950
+ // 5) bot
1951
+ // /messages
1952
+ // createFaqWithWebhookMicrolanguage:
1953
+ // Error: Timeout of 20000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (/Users/andrealeo/dev/chat21/tiledesk-server-dev-org/test-int/bot.js)
1954
+ // at listOnTimeout (internal/timers.js:557:17)
1955
+ // at processTimers (internal/timers.js:500:7)