@tiledesk/tiledesk-server 2.4.72 → 2.4.74

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
  🚀 IN PRODUCTION 🚀
6
6
  (https://www.npmjs.com/package/@tiledesk/tiledesk-server/v/2.3.77)
7
7
 
8
+ # 2.4.74
9
+ - update tiledesk-messenger-connector to 0.1.12
10
+
11
+ # 2.4.73
12
+ - Fix KB Settings bugs
13
+
8
14
  # 2.4.72
9
15
  - update tiledesk-messenger-connector to 0.1.11
10
16
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-server",
3
3
  "description": "The Tiledesk server module",
4
- "version": "2.4.72",
4
+ "version": "2.4.74",
5
5
  "scripts": {
6
6
  "start": "node ./bin/www",
7
7
  "pretest": "mongodb-runner start",
@@ -42,10 +42,10 @@
42
42
  "@tiledesk/tiledesk-dialogflow-connector": "^1.8.4",
43
43
  "@tiledesk/tiledesk-json-rules-engine": "^4.0.3",
44
44
  "@tiledesk/tiledesk-kaleyra-proxy": "^0.1.7",
45
- "@tiledesk/tiledesk-messenger-connector": "0.1.11",
46
45
  "@tiledesk/tiledesk-rasa-connector": "^1.0.10",
47
- "@tiledesk/tiledesk-tybot-connector": "^0.2.16",
46
+ "@tiledesk/tiledesk-tybot-connector": "^0.2.17",
48
47
  "@tiledesk/tiledesk-whatsapp-connector": "^0.1.52",
48
+ "@tiledesk/tiledesk-messenger-connector": "^0.1.12",
49
49
  "@tiledesk/tiledesk-telegram-connector": "^0.1.8",
50
50
  "amqplib": "^0.5.5",
51
51
  "app-root-path": "^3.0.0",
package/routes/faq.js CHANGED
@@ -122,7 +122,7 @@ router.post('/uploadcsv', upload.single('uploadFile'), function (req, res, next)
122
122
  })
123
123
  .on("end", function () {
124
124
  winston.debug("PARSE DONE");
125
- faqBotEvent.emit('faq_train.create', id_faq_kb)
125
+ //faqBotEvent.emit('faq_train.create', id_faq_kb)
126
126
  res.json({ success: true, msg: 'CSV Parsed' });
127
127
  })
128
128
  .on("error", function (err) {
@@ -191,7 +191,7 @@ router.post('/', function (req, res) {
191
191
  winston.debug('1. ID FAQKB GET IN THE OBJECT OF NEW FAQ CREATED ', savedFaq.id_faq_kb);
192
192
 
193
193
  faqBotEvent.emit('faq.create', savedFaq);
194
- faqBotEvent.emit('faq_train.create', req.body.id_faq_kb)
194
+ //faqBotEvent.emit('faq_train.create', req.body.id_faq_kb)
195
195
 
196
196
  res.json(savedFaq);
197
197
 
@@ -317,7 +317,7 @@ router.put('/:faqid', function (req, res) {
317
317
  }
318
318
 
319
319
  faqBotEvent.emit('faq.update', updatedFaq);
320
- faqBotEvent.emit('faq_train.update', updatedFaq.id_faq_kb);
320
+ //faqBotEvent.emit('faq_train.update', updatedFaq.id_faq_kb);
321
321
 
322
322
  res.status(200).send(updatedFaq);
323
323
  })
@@ -334,7 +334,7 @@ router.put('/:faqid', function (req, res) {
334
334
  }
335
335
 
336
336
  faqBotEvent.emit('faq.update', updatedFaq);
337
- faqBotEvent.emit('faq_train.update', updatedFaq.id_faq_kb);
337
+ //faqBotEvent.emit('faq_train.update', updatedFaq.id_faq_kb);
338
338
 
339
339
  res.status(200).send(updatedFaq);
340
340
  // updateRemoteFaq(updatedFaq)
@@ -373,7 +373,7 @@ router.delete('/:faqid', function (req, res) {
373
373
  winston.debug('Deleted FAQ ', faq);
374
374
 
375
375
  faqBotEvent.emit('faq.delete', faq);
376
- faqBotEvent.emit('faq_train.delete', faq.id_faq_kb);
376
+ //faqBotEvent.emit('faq_train.delete', faq.id_faq_kb);
377
377
 
378
378
  res.status(200).send(faq);
379
379
 
@@ -387,7 +387,7 @@ router.delete('/:faqid', function (req, res) {
387
387
  winston.debug('Deleted FAQ ', faq);
388
388
 
389
389
  faqBotEvent.emit('faq.delete', faq);
390
- faqBotEvent.emit('faq_train.delete', faq.id_faq_kb);
390
+ //faqBotEvent.emit('faq_train.delete', faq.id_faq_kb);
391
391
 
392
392
  res.status(200).send(faq);
393
393
 
package/routes/faq_kb.js CHANGED
@@ -15,7 +15,7 @@ var configGlobal = require('../config/global');
15
15
  const faq = require('../models/faq');
16
16
  var jwt = require('jsonwebtoken');
17
17
  const uuidv4 = require('uuid/v4');
18
- var ObjectId = require('mongoose').Types.ObjectId;
18
+ const trainingService = require('../services/trainingService');
19
19
 
20
20
  let chatbot_templates_api_url = process.env.CHATBOT_TEMPLATES_API_URL
21
21
 
@@ -130,6 +130,50 @@ router.post('/train', function (req, res) {
130
130
 
131
131
  });
132
132
 
133
+ router.post('/aitrain/', async (req, res) => {
134
+
135
+ let id_faq_kb = req.body.id_faq_kb;
136
+ let webhook_enabled = req.body.webhook_enabled;
137
+
138
+ Faq_kb.findById(id_faq_kb, async (err, chatbot) => {
139
+ if (err) {
140
+ return res.status(400).send({ success: false, error: err })
141
+ }
142
+ if (!chatbot) {
143
+ return res.status(404).send({ sucess: false, error: "Chatbot not found" });
144
+ }
145
+ if (chatbot.intentsEngine === 'tiledesk-ai') {
146
+
147
+ // Option 1: emit event
148
+ //faqBotEvent.emit('faq_train.train', id_faq_kb, webhook_enabled);
149
+
150
+ // Option 2: call service directly
151
+ trainingService.train(null, id_faq_kb, webhook_enabled).then((training_result) => {
152
+ winston.info("training result: ", training_result);
153
+ let response = {
154
+ succes: true,
155
+ message: "Training started"
156
+ }
157
+ if (webhook_enabled === false) {
158
+ response.queue_message = training_result;
159
+ }
160
+ return res.status(200).send(response);
161
+
162
+ }).catch((err) => {
163
+ winston.error("training error: ", err);
164
+ return res.status(200).send({ success: false, message: "Trained not started", error: err });
165
+ })
166
+
167
+
168
+
169
+ } else {
170
+ return res.status(200).send({ success: true, message: "Trained not started", reason: "Training available for intentsEngine equal to tiledesk-ai only" })
171
+ }
172
+ })
173
+
174
+
175
+ })
176
+
133
177
 
134
178
  router.post('/askbot', function (req, res) {
135
179
 
@@ -696,7 +740,7 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, r
696
740
  }
697
741
 
698
742
  })
699
- faqBotEvent.emit('faq_train.importedall', id_faq_kb);
743
+ //faqBotEvent.emit('faq_train.importedall', id_faq_kb);
700
744
  return res.status(200).send({ success: true, msg: "Intents imported successfully" })
701
745
 
702
746
  } else {
@@ -788,7 +832,7 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, r
788
832
 
789
833
  })
790
834
  }
791
- faqBotEvent.emit('faq_train.importedall', savedEditedFaq_kb._id);
835
+ //faqBotEvent.emit('faq_train.importedall', savedEditedFaq_kb._id);
792
836
  return res.status(200).send(savedEditedFaq_kb);
793
837
  })
794
838
 
@@ -914,7 +958,7 @@ router.post('/importjson/:id_faq_kb', upload.single('uploadFile'), async (req, r
914
958
  })
915
959
 
916
960
  }
917
- faqBotEvent.emit('faq_train.importedall', id_faq_kb);
961
+ //faqBotEvent.emit('faq_train.importedall', id_faq_kb);
918
962
  return res.send(updatedFaq_kb);
919
963
 
920
964
  })
@@ -989,6 +1033,7 @@ router.get('/exportjson/:id_faq_kb', (req, res) => {
989
1033
  router.post('/:faq_kbid/training', function (req, res) {
990
1034
 
991
1035
  winston.debug(req.body);
1036
+ winston.info(req.params.faq_kbid + "/training called" );
992
1037
 
993
1038
  var update = {};
994
1039
  update.trained = true;
@@ -132,12 +132,43 @@ router.post('/startscrape', async (req, res) => {
132
132
 
133
133
  router.post('/checkstatus', async (req, res) => {
134
134
 
135
- let data = req.body;
136
- winston.debug("/checkstatus data: ", data);
135
+ // let data = req.body;
136
+ winston.debug("/checkstatus req.body: ", req.body);
137
+
138
+ let full_url = req.body.full_url;
139
+ let data = {
140
+ url_list: [ req.body.full_url ]
141
+ }
137
142
 
138
143
  openaiService.checkStatus(data).then((resp) => {
139
144
  winston.debug("checkStatus resp: ", resp.data);
140
- res.status(200).send(resp.data);
145
+ winston.debug("checkStatus resp: ", resp.data);
146
+ winston.debug("checkStatus resp: ", resp.data[full_url]);
147
+
148
+ let response = resp.data[full_url];
149
+
150
+ let return_data = {
151
+ status_message: response.status_message
152
+ }
153
+
154
+ if (response.status_code === 3) {
155
+ return_data.status_code = 2;
156
+ }
157
+
158
+ if (response.status_code === 1 || response.status_code === 2 ) {
159
+ return_data.status_code = 1;
160
+ }
161
+
162
+ if (response.status_code === 0) {
163
+ return_data.status_code = 0;
164
+ }
165
+
166
+ if (!response.status_code) {
167
+ return_data.status_code = 0;
168
+ }
169
+
170
+
171
+ res.status(200).send(return_data);
141
172
  }).catch((err) => {
142
173
  winston.error("checkstatus err: ", err);
143
174
  let status = err.response.status;
@@ -62,7 +62,7 @@ class OpenaiService {
62
62
  return new Promise((resolve, reject) => {
63
63
 
64
64
  axios({
65
- url: kb_endpoint + "/scrape/",
65
+ url: kb_endpoint + "/scrape",
66
66
  headers: {
67
67
  'Content-Type': 'application/json'
68
68
  },
@@ -83,7 +83,7 @@ class OpenaiService {
83
83
  return new Promise((resolve, reject) => {
84
84
 
85
85
  axios({
86
- url: kb_endpoint + "/qa/",
86
+ url: kb_endpoint + "/qa",
87
87
  headers: {
88
88
  'Content-Type': 'application/json'
89
89
  },
@@ -6,78 +6,97 @@ const axios = require("axios").default;
6
6
  var configGlobal = require('../config/global');
7
7
 
8
8
 
9
- let chatbot_training_api_url = "http://34.65.210.38/model/train"
9
+ let training_api_url = process.env.CHATBOT_TRAINING_API_URL || "http://34.65.210.38/model/enqueuetrain";
10
+ let token = process.env.TRAINING_BOT_JWT_TOKEN;
10
11
 
11
12
  class TrainingService {
12
13
 
13
14
 
14
- train(eventName, id_faq_kb) {
15
-
16
- Faq_kb.findById(id_faq_kb, (err, faq_kb) => {
17
-
18
- if (err) {
19
- winston.error("train error: ", err)
20
- return null;
21
- }
22
-
23
- if (!faq_kb) {
24
- winston.error("faq_kb is undefined");
25
- return null;
26
- }
27
-
28
- if (faq_kb.intentsEngine !== 'tiledesk-ai') {
29
- winston.debug("intentsEngine: off")
30
- return null;
31
- }
32
-
33
- winston.debug("intentsEngine: on")
34
- Faq.find({ id_faq_kb: id_faq_kb }, (err, faqs) => {
15
+ async train(eventName, id_faq_kb, webhook_enabled) {
35
16
 
17
+ return new Promise((resolve, reject) => {
18
+
19
+ Faq_kb.findById(id_faq_kb, (err, faq_kb) => {
20
+
36
21
  if (err) {
37
- winston.error("[Training] find all error: ", err);
38
- } else {
39
-
40
- let json = {
41
- "configuration": {
42
- "language": faq_kb.language,
43
- "pipeline":["lstm"]
44
- },
45
- "model": faq_kb._id,
46
- "nlu": [],
47
-
48
- // curl -v -X PUT -H 'Content-Type:application/json' -u admin@tiledesk.com:adminadmin -d '{"trained":false}' http://localhost:3000/63ed15febb8a5eb3b247fdfd/bots/64551b3422cdfb93ddb1b784
49
- "webhook_url": process.env.API_URL || configGlobal.apiUrl + "/" + faq_kb.id_project + "/bots/" + faq_kb._id+"/training",
50
- "token" : process.env.TRAINING_BOT_JWT_TOKEN
51
- }
52
-
53
- faqs.forEach((f) => {
54
- if (f.enabled == true) {
55
- let intent = {
56
- "intent": f.intent_display_name,
57
- "examples": []
58
- }
59
- let questions = f.question.split("\n");
60
- intent.examples = questions;
61
- json.nlu.push(intent);
62
- }
63
- })
64
-
65
- winston.debug("training json: \n" + JSON.stringify(json, null, 2));
66
-
67
- axios({
68
- url: chatbot_training_api_url,
69
- headers: {
70
- 'Content-Type': 'application/json'
71
- },
72
- data: json,
73
- method: 'POST'
74
- }).then((resbody) => {
75
- winston.info("[Training] resbody: ", resbody.data);
76
- return true;
77
- }).catch((err) => {
78
- winston.error("[Training] error: ", err.response.data);
79
- })
22
+ winston.error("train error: ", err)
23
+ // return null;
24
+ reject(null);
25
+ }
26
+
27
+ if (!faq_kb) {
28
+ winston.error("faq_kb is undefined");
29
+ // return null;
30
+ reject(null);
80
31
  }
32
+
33
+ if (faq_kb.intentsEngine !== 'tiledesk-ai') {
34
+ winston.debug("intentsEngine: off")
35
+ // return null;
36
+ reject(null);
37
+ }
38
+
39
+ winston.debug("intentsEngine: on")
40
+ Faq.find({ id_faq_kb: id_faq_kb }, async (err, faqs) => {
41
+
42
+ if (err) {
43
+ winston.error("[Training] find all error: ", err);
44
+ } else {
45
+
46
+ let json = {
47
+ "configuration": {
48
+ "language": faq_kb.language,
49
+ "pipeline":["lstm"]
50
+ },
51
+ "model": faq_kb._id,
52
+ "nlu": [],
53
+ //"webhook_url": process.env.API_URL || configGlobal.apiUrl + "/" + faq_kb.id_project + "/bots/" + faq_kb._id+"/training",
54
+ // curl -v -X PUT -H 'Content-Type:application/json' -u admin@tiledesk.com:adminadmin -d '{"trained":false}' http://localhost:3000/63ed15febb8a5eb3b247fdfd/bots/64551b3422cdfb93ddb1b784
55
+ }
56
+
57
+ if (webhook_enabled === true) {
58
+ json.webhook_url = process.env.API_URL || configGlobal.apiUrl + "/" + faq_kb.id_project + "/bots/" + faq_kb._id+"/training"
59
+ }
60
+
61
+ faqs.forEach((f) => {
62
+ if (f.enabled == true) {
63
+ let intent = {
64
+ "intent": f.intent_display_name,
65
+ "examples": []
66
+ }
67
+ if (f.question) {
68
+ let questions = f.question.split("\n");
69
+ intent.examples = questions;
70
+ json.nlu.push(intent);
71
+ } else {
72
+ winston.debug("faq question null!")
73
+ }
74
+ }
75
+ })
76
+
77
+ winston.debug("training json: \n", json);
78
+
79
+ await axios({
80
+ url: training_api_url,
81
+ headers: {
82
+ 'Content-Type': 'application/json',
83
+ 'Authorization': token
84
+ },
85
+ data: json,
86
+ method: 'POST'
87
+ }).then((resbody) => {
88
+ winston.debug("[Training] resbody: ", resbody.data);
89
+ // return true;
90
+ resolve(resbody.data);
91
+ }).catch((err) => {
92
+ winston.error("[Training] error: ", err);
93
+ // return false;
94
+ reject(false);
95
+ // winston.error("[Training] error: ", err);
96
+ })
97
+ }
98
+ })
99
+
81
100
  })
82
101
 
83
102
  })
@@ -89,6 +108,12 @@ class TrainingService {
89
108
  start() {
90
109
  winston.info('TrainingService start');
91
110
 
111
+ faqBotEvent.on('faq_train.train', (id_faq_kb, webhook_enabled) => {
112
+ setImmediate(() => {
113
+ trainingService.train('faq_train.train', id_faq_kb, webhook_enabled);
114
+ })
115
+ })
116
+
92
117
  faqBotEvent.on('faq_train.importedall', (id_faq_kb) => {
93
118
  setImmediate(() => {
94
119
  trainingService.train('faq.importedall', id_faq_kb);
package/test/faqRoute.js CHANGED
@@ -28,7 +28,7 @@ describe('FaqKBRoute', () => {
28
28
 
29
29
  describe('/create', () => {
30
30
 
31
- it('create qwerty', (done) => {
31
+ it('create', (done) => {
32
32
 
33
33
  // this.timeout();
34
34
 
@@ -469,9 +469,7 @@ describe('FaqKBRoute', () => {
469
469
  .auth(email, pwd)
470
470
  .send({ "name": "testbot", type: "internal", template: "example" })
471
471
  .end((err, res) => {
472
- if (log) {
473
- console.log("res.body", res.body);
474
- }
472
+ if (log) { console.log("res.body", res.body); }
475
473
  res.should.have.status(200);
476
474
  res.body.should.be.a('object');
477
475
  expect(res.body.name).to.equal("testbot");
@@ -483,9 +481,7 @@ describe('FaqKBRoute', () => {
483
481
  .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb + "&page=0&limit=25&text=looking")
484
482
  .auth(email, pwd)
485
483
  .end((err, res) => {
486
- if (log) {
487
- console.log("found these faqs: \n", res.body);
488
- }
484
+ if (log) { console.log("found these faqs: \n", res.body); }
489
485
  res.should.have.status(200);
490
486
  res.body.should.be.an('array');
491
487
 
@@ -509,11 +505,9 @@ describe('FaqKBRoute', () => {
509
505
  chai.request(server)
510
506
  .post('/' + savedProject._id + '/faq_kb')
511
507
  .auth(email, pwd)
512
- .send({ "name": "testbot", type: "internal", template: "example", intentsEngine: 'tiledesk-ai' })
508
+ .send({ "name": "testbot", type: "internal", template: "blank", intentsEngine: 'tiledesk-ai' })
513
509
  .end((err, res) => {
514
- if (log) {
515
- console.log("res.body", res.body);
516
- }
510
+ if (log) { console.log("res.body", res.body); }
517
511
  res.should.have.status(200);
518
512
  res.body.should.be.a('object');
519
513
  expect(res.body.name).to.equal("testbot");
@@ -523,7 +517,7 @@ describe('FaqKBRoute', () => {
523
517
  chai.request(server)
524
518
  .post('/' + savedProject._id + '/faq')
525
519
  .auth(email, pwd)
526
- .send({ id_faq_kb: id_faq_kb, question: "question1", answer: "answer1" })
520
+ .send({ id_faq_kb: id_faq_kb, question: "question1\nciao\nbuongiorno", answer: "answer1" })
527
521
  .end((err, res) => {
528
522
  if (log) { console.log("intentEngin on resbody (create faq): \n", res.body); }
529
523
  res.should.have.status(200);
@@ -73,6 +73,53 @@ describe('FaqKBRoute', () => {
73
73
 
74
74
  }).timeout(20000);
75
75
 
76
+ // it('train with tiledesk-ai', (done) => {
77
+ // var email = "test-signup-" + Date.now() + "@email.com";
78
+ // var pwd = "pwd";
79
+
80
+ // userService.signup(email, pwd, "Test Firstname", "Test Lastname").then((savedUser) => {
81
+ // projectService.create("test-faqkb-create", savedUser._id).then((savedProject) => {
82
+
83
+ // chai.request(server)
84
+ // .post('/' + savedProject._id + '/faq_kb')
85
+ // .auth(email, pwd)
86
+ // .send({ "name": "testbot", type: "internal", template: "example", intentsEngine: "tiledesk-ai" })
87
+ // .end((err, res) => {
88
+ // if (log) {
89
+ // console.log("res.body", res.body);
90
+ // }
91
+ // res.should.have.status(200);
92
+ // res.body.should.be.a('object');
93
+ // expect(res.body.name).to.equal("testbot");
94
+ // var id_faq_kb = res.body._id;
95
+
96
+ // chai.request(server)
97
+ // .get('/' + savedProject._id + '/faq?id_faq_kb=' + id_faq_kb)
98
+ // .auth(email, pwd)
99
+ // .end((err, res) => {
100
+ // if (log) { console.log("faq_list: ", res.body); }
101
+ // res.should.have.status(200);
102
+ // res.body.should.be.an('array').that.is.not.empty;
103
+
104
+ // chai.request(server)
105
+ // .post('/' + savedProject._id + '/faq_kb/aitrain')
106
+ // .auth(email, pwd)
107
+ // .send({ id_faq_kb: id_faq_kb, webhook_enabled: false })
108
+ // .end((err, res) => {
109
+ // if (log) { console.log("train res.body: ", res.body); }
110
+
111
+ // done();
112
+ // })
113
+
114
+ // })
115
+
116
+
117
+
118
+ // });
119
+ // })
120
+ // })
121
+ // })
122
+
76
123
 
77
124
  it('create with template example', (done) => {
78
125
 
@@ -453,7 +453,7 @@ describe('KbSettingsRoute', () => {
453
453
 
454
454
  // }).timeout(20000)
455
455
 
456
- // it('check status error - no db created for', (done) => {
456
+ // it('checkstatuserror - no db created for', (done) => {
457
457
 
458
458
  // var email = "test-signup-" + Date.now() + "@email.com";
459
459
  // var pwd = "pwd";
@@ -474,7 +474,7 @@ describe('KbSettingsRoute', () => {
474
474
  // chai.request(server)
475
475
  // .post('/' + savedProject._id + "/kbsettings/" + res.body._id)
476
476
  // .auth(email, pwd)
477
- // .send({ name: "dbnevercreated.com/kb/", url: "https://exampleurl.com/kb/" })
477
+ // .send({ name: "gethelp.tiledesk.com/", url: "https://gethelp.tiledesk.com/" })
478
478
  // .end((err, res) => {
479
479
  // if (log) { console.log("add kb to kb settings res.body: ", res.body); }
480
480
  // res.should.have.status(200);
@@ -484,12 +484,12 @@ describe('KbSettingsRoute', () => {
484
484
  // chai.request(server)
485
485
  // .post('/' + savedProject._id + "/kbsettings/checkstatus")
486
486
  // .auth(email, pwd)
487
- // .send({ full_url: "dbnevercreated.com/kb/" })
487
+ // .send({ full_url: "https://gethelp.tiledesk.com/" })
488
488
  // .end((err, res) => {
489
489
  // res.should.have.status(200);
490
490
  // res.body.should.be.a('object');
491
- // expect(res.body.status_message).to.equal("Database is not created yet for dbnevercreated.com/kb/, please wait a few minutes and try again");
492
- // expect(res.body.status_code).to.equal(0);
491
+ // // expect(res.body.status_message).to.equal("Database is not created yet for dbnevercreated.com/kb/, please wait a few minutes and try again");
492
+ // // expect(res.body.status_code).to.equal(0);
493
493
 
494
494
  // done();
495
495
  // })