@tiledesk/tiledesk-tybot-connector 0.1.21 → 0.1.23

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 (36) hide show
  1. package/CHANGELOG.md +10 -1
  2. package/ExtApi.js +37 -0
  3. package/index backup.js +656 -0
  4. package/index.js +58 -461
  5. package/models/IntentForm.js +12 -13
  6. package/models/MockBotsDataSource.js +74 -0
  7. package/models/MockIntentsMachine.js +39 -0
  8. package/models/MongodbBotsDataSource.js +73 -0
  9. package/models/MongodbIntentsMachine.js +55 -0
  10. package/models/{TiledeskChatbot_Intents_Adapter.js → TiledeskChatbot backup.js } +0 -1
  11. package/models/TiledeskChatbot.js +206 -180
  12. package/models/TiledeskIntentsMachine.js +90 -0
  13. package/models/faqKbService.js +27 -0
  14. package/models/faqService.js +22 -0
  15. package/models/faq_kb.js +11 -28
  16. package/package.json +2 -2
  17. package/test/close_directive_test.js +49 -0
  18. package/test/directives_test.js +24 -26
  19. package/test/disable_input_text_directive_test.js +88 -0
  20. package/test/mock_query_test.js +276 -0
  21. package/test/single_test.sh +4 -0
  22. package/test/when_open_directive_test.js +167 -0
  23. package/tiledeskChatbotPlugs/CHANGELOG.md +2 -0
  24. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +66 -46
  25. package/tiledeskChatbotPlugs/directives/DirClose.js +25 -0
  26. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +6 -6
  27. package/tiledeskChatbotPlugs/directives/DirDepartment.js +52 -0
  28. package/tiledeskChatbotPlugs/directives/DirDisableInputText.js +45 -0
  29. package/tiledeskChatbotPlugs/directives/DirIntent.js +50 -0
  30. package/tiledeskChatbotPlugs/directives/DirMessage.js +5 -4
  31. package/tiledeskChatbotPlugs/directives/DirWhenOpen.js +95 -0
  32. package/tiledeskChatbotPlugs/directives/Directives.js +3 -0
  33. package/tiledeskChatbotPlugs/package.json +1 -1
  34. package/models/MongoDBIntentsDataSource.js +0 -22
  35. package/models/StaticIntentsDataSource.js +0 -110
  36. package/test/chatbot_query_test.js_ +0 -41
package/index.js CHANGED
@@ -13,6 +13,9 @@ const { ExtUtil } = require('./ExtUtil.js');
13
13
  const { TdCache } = require('./TdCache.js');
14
14
  //const { IntentForm } = require('./IntentForm.js');
15
15
  const { TiledeskChatbot } = require('./models/TiledeskChatbot.js');
16
+ const { MongodbBotsDataSource } = require('./models/MongodbBotsDataSource.js');
17
+ const { MongodbIntentsMachine } = require('./models/MongodbIntentsMachine.js');
18
+ const { TiledeskIntentsMachine } = require('./models/TiledeskIntentsMachine.js');
16
19
 
17
20
  //router.use(cors());
18
21
  router.use(bodyParser.json({limit: '50mb'}));
@@ -35,8 +38,6 @@ const { SplitsChatbotPlug } = require('@tiledesk/tiledesk-chatbot-plugs/SplitsCh
35
38
  const { MarkbotChatbotPlug } = require('@tiledesk/tiledesk-chatbot-plugs/MarkbotChatbotPlug');
36
39
  const { WebhookChatbotPlug } = require('@tiledesk/tiledesk-chatbot-plugs/WebhookChatbotPlug');*/
37
40
 
38
-
39
-
40
41
  // THE IMPORT
41
42
  let mongoose = require('mongoose');
42
43
  //let Faq = require('./models/faq');
@@ -45,14 +46,14 @@ let connection;
45
46
  let APIURL = null;
46
47
 
47
48
  router.post('/ext/:botid', async (req, res) => {
48
- if (log) {console.log("REQUEST BODY:", JSON.stringify(req.body));}
49
+ console.log("REQUEST BODY:", JSON.stringify(req.body));
49
50
  res.status(200).send({"success":true});
50
51
 
51
52
  const botId = req.params.botid;
52
53
  if (log) {console.log("query botId:", botId);}
53
54
  const message = req.body.payload;
54
55
  const messageId = message._id;
55
- const faq_kb = req.body.hook;
56
+ //const faq_kb = req.body.hook; now it is "bot"
56
57
  const token = req.body.token;
57
58
  const requestId = message.request.request_id;
58
59
  const projectId = message.id_project;
@@ -71,16 +72,42 @@ router.post('/ext/:botid', async (req, res) => {
71
72
  if (log) {console.log("message context saved for messageid:", message_context_key)}
72
73
  // provide a http method for set/get message context, authenticated with tiledesk token and APIKEY.
73
74
 
75
+ const botsDS = new MongodbBotsDataSource({projectId: projectId, botId: botId});
76
+
77
+ // get the bot metadata
78
+ let bot = null;
79
+ try {
80
+ bot = await botsDS.getBotById(botId);
81
+ }
82
+ catch(error) {
83
+ console.error("Error getting botId:", botId);
84
+ console.error("Error getting bot was:", error);
85
+ return;
86
+ }
87
+
88
+ let intentsMachine = new MongodbIntentsMachine({projectId: projectId, language: bot.language});
89
+ if (bot.intentsEngine === "tiledesk-ai") {
90
+ intentsMachine = new TiledeskIntentsMachine(
91
+ {
92
+ projectId: projectId,
93
+ language: bot.language,
94
+ TILEBOT_AI_ENDPOINT: process.env.TILEBOT_AI_ENDPOINT
95
+ });
96
+ }
97
+
98
+ //const intentsMachine = new TiledeskIntentsMachine({API_ENDPOINT: "https://MockIntentsMachine.tiledesk.repl.co", log: true});
74
99
  const chatbot = new TiledeskChatbot({
100
+ botsDataSource: botsDS,
101
+ intentsFinder: intentsMachine,
75
102
  botId: botId,
103
+ bot: bot,
76
104
  token: token,
77
- faq_kb: faq_kb,
78
105
  APIURL: APIURL,
79
106
  APIKEY: "___",
80
107
  tdcache: tdcache,
81
108
  requestId: requestId,
82
109
  projectId: projectId,
83
- log: log
110
+ log: true
84
111
  });
85
112
 
86
113
  const parameters_key = "tilebot:requests:" + requestId + ":parameters";
@@ -88,8 +115,12 @@ router.post('/ext/:botid', async (req, res) => {
88
115
  //all_params = await chatbot.allParameters(requestId);
89
116
  //console.log("Allparams", all_params);
90
117
  let reply = await chatbot.replyToMessage(message);
118
+ if (!reply) {
119
+ reply = {
120
+ "text": "No messages found. Is 'defaultFallback' intent missing?"
121
+ }
122
+ }
91
123
  reply.triggeredByMessageId = messageId;
92
- //console.log("reply ok", reply);
93
124
  let extEndpoint = `${APIURL}/modules/tilebot/`;
94
125
  if (process.env.TYBOT_ENDPOINT) {
95
126
  extEndpoint = `${process.env.TYBOT_ENDPOINT}`;
@@ -100,7 +131,9 @@ router.post('/ext/:botid', async (req, res) => {
100
131
  });
101
132
 
102
133
  apiext.sendSupportMessageExt(reply, projectId, requestId, token, () => {
103
- if (log) {console.log("FORM Message sent.", );}
134
+ if (log) {
135
+ console.log("SupportMessageExt() reply sent:", reply);
136
+ }
104
137
  });
105
138
 
106
139
  });
@@ -123,11 +156,11 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
123
156
  const request_key = "tilebot:" + requestId;
124
157
  if (tdcache) {
125
158
  request = await tdcache.getJSON(request_key)
126
- if (log) {console.log("HIT! Request from cache:", request);}
159
+ if (log) {console.log("HIT! Request from cache:", request.request_id);}
127
160
  if (!request) {
128
161
  if (log) {console.log("!Request from cache", requestId);}
129
162
  request = await tdclient.getRequestById(requestId);
130
- if (log) {console.log("Got request with APIs (after no cache hit):", request);}
163
+ if (log) {console.log("Got request with APIs (after no cache hit)");}
131
164
  }
132
165
  }
133
166
  else {
@@ -135,432 +168,17 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
135
168
  request = await tdclient.getRequestById(requestId);
136
169
  if (log) {console.log("(No tdcache) Got request with APIs");}
137
170
  }
138
- let directivesPlug = new DirectivesChatbotPlug({supportRequest: request, TILEDESK_API_ENDPOINT: APIURL, token: token, log: log, HELP_CENTER_API_ENDPOINT: process.env.HELP_CENTER_API_ENDPOINT, cache: tdcache});
171
+ 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});
139
172
  // PIPELINE-EXT
140
173
  const bot_answer = await ExtUtil.execPipelineExt(request, answer, directivesPlug, tdcache, log);
141
174
  //const bot_answer = answer;
142
175
  tdclient.sendSupportMessage(requestId, bot_answer, () => {
143
- directivesPlug.processDirectives(() => {
176
+ directivesPlug.processDirectives(bot_answer, () => {
144
177
  if (log) {console.log("After message execute directives end.");}
145
178
  });
146
179
  });
147
180
  });
148
181
 
149
- /*
150
- async function execFaq(req, res, faqs, botId, message, token, bot) {
151
- answerObj = faqs[0];
152
- //console.log("answerObj:", answerObj)
153
- let sender = 'bot_' + botId;
154
- var answerObj;
155
- answerObj.score = 100; //exact search not set score
156
-
157
- const requestId = message.request.request_id;
158
- const projectId = message.id_project;
159
- //console.log("requestId:", requestId)
160
- //console.log("token:", token)
161
- //console.log("projectId:", projectId)
162
-
163
- if (tdcache) {
164
- const requestKey = "tilebot:" + requestId
165
- //console.log("Setting request key:", requestKey)
166
- // best effort, do not "await", go on, trust redis speed.
167
- tdcache.setJSON(requestKey, message.request);
168
- //await tdcache.setJSON(requestKey, message.request);
169
- }
170
- // /ext/:projectId/requests/:requestId/messages ENDPOINT COINCIDES
171
- // with API_ENDPOINT (APIRURL) ONLY WHEN THE TYBOT ROUTE IS HOSTED
172
- // ON THE MAIN SERVER. OTHERWISE WE USE TYBOT_ROUTE TO SPECIFY
173
- // THE ALTERNATIVE ROUTE.
174
- let extEndpoint = `${APIURL}/modules/tilebot/`;
175
- if (process.env.TYBOT_ENDPOINT) {
176
- extEndpoint = `${process.env.TYBOT_ENDPOINT}`;
177
- }
178
- const apiext = new ExtApi({
179
- ENDPOINT: extEndpoint,
180
- log: log
181
- });
182
-
183
-
184
- // THE FORM
185
- let intent_form = answerObj.form;
186
- let intent_name = answerObj.intent_display_name
187
- if (intent_name === "test_form_intent") {
188
- intent_form = {
189
- "name": "form_name",
190
- "id": "form_id",
191
- "cancelCommands": ['annulla', 'cancella', 'reset', 'cancel'],
192
- "cancelReply": "Form Annullata",
193
- "fields": [
194
- {
195
- "name": "userFullname",
196
- "type": "text",
197
- "label": "What is your name?"
198
- },{
199
- "name": "companyName",
200
- "type": "text",
201
- "label": "Thank you ${userFullname}! What is your Company name?"
202
- },
203
- {
204
- "name": "userEmail",
205
- "type": "text",
206
- "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])?)+$/",
207
- "label": "Hi ${userFullname} from ${companyName}\n\nJust one last question\n\nYour email 🙂",
208
- "errorLabel": "${userFullname} this email address is invalid\n\nCan you insert a correct email address?"
209
- }
210
- ]
211
- };
212
- }
213
-
214
- if (intent_form) {
215
- await lockIntent(requestId, intent_name);
216
- const user_reply = message.text;
217
- const intent_answer = req.body.payload.text;
218
- let form_reply = await execIntentForm(user_reply, intent_answer, requestId, intent_form);
219
- console.log("got form reply", form_reply)
220
- //if (form_reply_message) {
221
- if (!form_reply.canceled && form_reply.message) {
222
- console.log("Form replying for next field...");
223
- if (log) {console.log("Sending form reply...", form_reply_message)}
224
- // reply with this message (ex. please enter your fullname)
225
- if (!form_reply.message.attributes) {
226
- form_reply.message.attributes = {}
227
- }
228
- form_reply.message.attributes.fillParams = true;
229
- form_reply.message.attributes.splits = true;
230
- apiext.sendSupportMessageExt(form_reply.message, projectId, requestId, token, () => {
231
- if (log) {console.log("FORM Message sent.", );}
232
- });
233
- return;
234
- }
235
- else if (form_reply.end) {
236
- console.log("Form end.");
237
- if (log) {console.log("unlocking intent for request", requestId);}
238
- unlockIntent(requestId);
239
- populatePrechatFormAndLead(message, projectId, token, APIURL);
240
- }
241
- else if (form_reply.canceled) {
242
- console.log("Form canceled.");
243
- if (log) {console.log("unlocking intent due to canceling, for request", requestId);}
244
- unlockIntent(requestId);
245
- if (log) {console.log("sending form 'cancel' reply...", form_reply.message)}
246
- // reply with this message (ex. please enter your fullname)
247
- if (!form_reply.message.attributes) {
248
- form_reply.message.attributes = {}
249
- }
250
- form_reply.message.attributes.fillParams = true;
251
- form_reply.message.attributes.splits = true;
252
- apiext.sendSupportMessageExt(form_reply.message, projectId, requestId, token, () => {
253
- if (log) {console.log("FORM Message sent.", );}
254
- });
255
- return;
256
- }
257
- console.log("form_reply is", form_reply)
258
- }
259
-
260
- // FORM END
261
-
262
- const context = {
263
- payload: {
264
- botId: botId,
265
- bot: bot,
266
- message: message, // USER MESSAGE (JSON)
267
- intent: answerObj
268
- },
269
- token: token
270
- };
271
- const static_bot_answer = { // static design of the chatbot reply
272
- //type: answerObj.type,
273
- text: answerObj.answer,
274
- attributes: answerObj.attributes,
275
- metadata: answerObj.metadata,
276
- // language: ?
277
- // channel: ? whatsapp|telegram|facebook...
278
- };
279
- if (!static_bot_answer.attributes) {
280
- static_bot_answer.attributes = {}
281
- }
282
-
283
- var timestamp = Date.now();
284
- static_bot_answer.attributes['clienttimestamp'] = timestamp;
285
- if (answerObj && answerObj._id) {
286
- static_bot_answer.attributes._answerid = answerObj._id.toString();
287
- }
288
- // DECORATES THE FINAL ANSWER
289
- // question_payload = clone of user's original message
290
- let question_payload = Object.assign({}, message);
291
- delete question_payload.request;
292
- let clonedfaqs = faqs.slice();
293
- if (clonedfaqs && clonedfaqs.length > 0) {
294
- clonedfaqs = clonedfaqs.shift()
295
- }
296
- const intent_info = {
297
- intent_name: answerObj.intent_display_name,
298
- is_fallback: false,
299
- confidence: answerObj.score,
300
- question_payload: question_payload,
301
- others: clonedfaqs
302
- }
303
- static_bot_answer.attributes.intent_info = intent_info;
304
-
305
- static_bot_answer.attributes.directives = true;
306
- static_bot_answer.attributes.splits = true;
307
- static_bot_answer.attributes.markbot = true;
308
- static_bot_answer.attributes.fillParams = true;
309
- static_bot_answer.attributes.webhook = answerObj.webhook_enabled;
310
-
311
- // exec webhook (only)
312
- const bot_answer = await execPipeline(static_bot_answer, message, bot, context, token);
313
-
314
- //bot_answer.text = await fillWithRequestParams(bot_answer.text, requestId); // move to "ext" pipeline
315
-
316
- apiext.sendSupportMessageExt(bot_answer, projectId, requestId, token, () => {
317
- if (log) {console.log("Message sent");}
318
- });
319
-
320
- }*/
321
-
322
- /*
323
- async function populatePrechatFormAndLead(message, projectId, token, APIURL, callback) {
324
- const tdclient = new TiledeskClient({
325
- projectId: projectId,
326
- token: token,
327
- APIURL: APIURL,
328
- APIKEY: "___"
329
- });
330
-
331
- const leadId = message.request.lead._id;
332
- const requestId = message.request.request_id;
333
-
334
- const parameters_key = "tilebot:requests:" + requestId + ":parameters";
335
- const all_parameters = await tdcache.hgetall(parameters_key);
336
- if (all_parameters) {
337
- //console.log("all_parameters['userEmail']", all_parameters['userEmail'])
338
- //console.log("all_parameters['userFullname']", all_parameters['userFullname']);
339
- tdclient.updateLeadEmailFullname(leadId, null, all_parameters['userFullname'], () => {
340
- if (log) {console.log("lead updated.")}
341
- tdclient.updateRequestAttributes(requestId, {
342
- preChatForm: all_parameters,
343
- updated: Date.now
344
- }, () => {
345
- if (log) {console.log("prechat updated.");}
346
- if (callback) {
347
- callback();
348
- }
349
- });
350
- });
351
- };
352
- }
353
-
354
- */
355
-
356
-
357
- /** TEST BUG
358
- function fixToken(token) {
359
- if (token.startsWith('JWT ')) {
360
- return token;
361
- }
362
- else {
363
- return 'JWT ' + token;
364
- }
365
- }
366
-
367
- function sendSupportMessageExt(message, projectId, requestId, token, callback) {
368
- let extEndpoint = `${APIURL}/modules/tilebot/`;
369
- if (process.env.TYBOT_ENDPOINT) {
370
- extEndpoint = `${process.env.TYBOT_ENDPOINT}`;
371
- }
372
- const jwt_token = fixToken(token);
373
- const url = `${extEndpoint}/ext/${projectId}/requests/${requestId}/messages`;
374
- if (this.log) {console.log("sendSupportMessageExt URL", url);}
375
- //console.log("sendSupportMessageExt:", url);
376
- const HTTPREQUEST = {
377
- url: url,
378
- headers: {
379
- 'Content-Type' : 'application/json',
380
- 'Authorization': jwt_token
381
- },
382
- json: message,
383
- method: 'POST'
384
- };
385
- myrequest(
386
- HTTPREQUEST,
387
- function(err, resbody) {
388
- //console.log("sendSupportMessageExt resbody:", resbody);
389
- if (err) {
390
- //console.error("sendSupportMessageExt error:", err)
391
- if (callback) {
392
- callback(err);
393
- }
394
- }
395
- else {
396
- if (callback) {
397
- console.log("resbody:", resbody);
398
- callback(null, resbody);
399
- }
400
- }
401
- }, this.log
402
- );
403
- }
404
-
405
-
406
- function myrequest(options, callback, log) {
407
- //console.log("API URL:", options.url);
408
- axios(
409
- {
410
- url: options.url,
411
- method: options.method,
412
- data: options.json,
413
- params: options.params,
414
- headers: options.headers
415
- })
416
- .then((res) => {
417
- //console.log("Response for url:", options.url);
418
- //console.log("Response headers:\n", res.headers);
419
- if (res && res.status == 200 && res.data) {
420
- if (callback) {
421
- callback(null, res.data);
422
- }
423
- }
424
- else {
425
- if (callback) {
426
- callback(TiledeskClient.getErr({message: "Response status not 200"}, options, res), null, null);
427
- }
428
- }
429
- })
430
- .catch( (error) => {
431
- console.error("An error occurred:", error);
432
- if (callback) {
433
- callback(error, null, null);
434
- }
435
- });
436
- }
437
- *** END TEST*/
438
-
439
-
440
-
441
-
442
-
443
- /*
444
- async function execIntentForm(userInputReply, original_intent_answer_text, requestId, form) {
445
- if (log) {console.log("executing intent form...")}
446
- let intentForm = new IntentForm({form: form, requestId: requestId, db: tdcache, log: log});
447
- let message = await intentForm.getMessage(userInputReply, original_intent_answer_text);
448
- return message;
449
- }
450
- */
451
-
452
- /*
453
- async function lockIntent(requestId, intent_name) {
454
- await tdcache.set("tilebot:requests:" + requestId + ":locked", intent_name);
455
- //console.log("locked.", intent_name);
456
- }
457
-
458
- async function currentLockedIntent(requestId) {
459
- return await tdcache.get("tilebot:requests:" + requestId + ":locked");
460
- }
461
-
462
- async function unlockIntent(requestId) {
463
- await tdcache.del("tilebot:requests:" + requestId + ":locked");
464
- }
465
- */
466
-
467
- /*function validMessage(message) {
468
- console.log("validating message", message)
469
- if (!message.type) {
470
- message.type = 'text';
471
- }
472
- if (message.text === '' && message.type === 'text') {
473
- console.log("in-valid message", message)
474
- return false;
475
- }
476
- console.log("valid message", message)
477
- return true;
478
- }*/
479
-
480
- /*
481
- async function execPipeline(static_bot_answer, message, bot, context, token) {
482
- const messagePipeline = new MessagePipeline(static_bot_answer, context);
483
- const webhookurl = bot.webhook_url;
484
- messagePipeline.addPlug(new WebhookChatbotPlug(message.request, webhookurl, token));
485
- //messagePipeline.addPlug(directivesPlug);
486
- //messagePipeline.addPlug(new SplitsChatbotPlug(log));
487
- //messagePipeline.addPlug(new MarkbotChatbotPlug(log));
488
- const bot_answer = await messagePipeline.exec();
489
- //if (log) {console.log("End pipeline, bot_answer:", JSON.stringify(bot_answer));}
490
- return bot_answer;
491
- }
492
- */
493
-
494
- /*
495
- function getIntentByDisplayName(name, bot) {
496
- return new Promise(function(resolve, reject) {
497
- var query = { "id_project": bot.id_project, "id_faq_kb": bot._id, "intent_display_name": name};
498
- if (log) {console.debug('query', query);}
499
- Faq.find(query).lean().exec(function (err, faqs) {
500
- if (err) {
501
- return reject();
502
- }
503
- if (log) {console.debug("faqs", faqs);}
504
- if (faqs && faqs.length > 0) {
505
- const intent = faqs[0];
506
- return resolve(intent);
507
- }
508
- else {
509
- return resolve(null);
510
- }
511
- });
512
- });
513
- }
514
- */
515
-
516
- // Tiledesk Resolution-bot webhook endpoint
517
- /*app.post('/bot', async (req, res) => {
518
- console.log("Webhook. Request body: " + JSON.stringify(req.body));
519
- // INTENTS
520
- let intent = null;
521
- intent = req.body.payload.intent.intent_display_name;
522
- //const projectId = req.body.payload.bot.id_project;
523
- const token = req.body.token;
524
- const request = req.body.payload.message.request;
525
- //const requestId = request.request_id;
526
- console.log("Got intent:", intent);
527
- API_URL = apiurl();
528
- const original_answer = req.body.payload.intent.answer;
529
-
530
- const messagePipeline = new MessagePipeline();
531
- messagePipeline.addPlug(new DirectivesChatbotPlug(request, API_URL, token));
532
- messagePipeline.addPlug(new SplitsChatbotPlug());
533
- messagePipeline.addPlug(new MarkbotChatbotPlug());
534
-
535
- const message = {
536
- text: original_answer
537
- };
538
- messagePipeline.execOn(message, (_message) => {
539
- res.json(_message);
540
- console.log("End pipeline.");
541
- });
542
-
543
- });*/
544
-
545
-
546
-
547
-
548
-
549
- /*
550
- function apiurl() {
551
- const server = "pre";
552
- //const server = "prod";
553
- const API_URL_PRE = 'https://tiledesk-server-pre.herokuapp.com';
554
- const API_URL_PROD = 'https://api.tiledesk.com/v2';
555
- // choose a server
556
- let API_URL = API_URL_PROD;
557
- if (server === 'pre') {
558
- API_URL = API_URL_PRE;
559
- }
560
- return API_URL;
561
- }
562
- */
563
-
564
182
  router.get('/message/context/:messageid', async (req, res) => {
565
183
  const messageid = req.params.messageid;
566
184
  const message_key = "tiledesk:messages:context:" + messageid;
@@ -579,22 +197,19 @@ router.get('/', (req, res) => {
579
197
  });
580
198
 
581
199
  function startApp(settings, completionCallback) {
582
- console.log("Starting Tybot with Settings:", settings);
200
+ console.log("Starting Tilebot with Settings:", settings);
583
201
 
584
202
  if (!settings.MONGODB_URI) {
585
- throw new Error("settings.MONGODB_URI is mandatory");
203
+ throw new Error("settings.MONGODB_URI is mandatory.");
586
204
  }
587
205
  if (!settings.API_ENDPOINT) {
588
- throw new Error("settings.API_ENDPOINT is mandatory");
206
+ throw new Error("settings.API_ENDPOINT is mandatory.");
589
207
  }
590
208
  else {
591
209
  APIURL = settings.API_ENDPOINT;
592
- console.log("(Tybot) settings.API_ENDPOINT:", APIURL);
210
+ console.log("(Tilebot) settings.API_ENDPOINT:", APIURL);
593
211
  }
594
- if (!settings.REDIS_HOST && !settings.REDIS_PORT) {
595
- throw new Error("settings.REDIS_HOST && settings.REDIS_PORT are mandatory");
596
- }
597
- else { // (settings.REDIS_HOST && settings.REDIS_PORT) { // && settings.CACHE_ENABLED) {
212
+ if (settings.REDIS_HOST && settings.REDIS_PORT) {
598
213
  tdcache = new TdCache({
599
214
  host: settings.REDIS_HOST,
600
215
  port: settings.REDIS_PORT,
@@ -608,29 +223,29 @@ function startApp(settings, completionCallback) {
608
223
  else {
609
224
  log = true;
610
225
  }
611
- console.log("(Tybot) log:", log);
226
+ console.log("(Tilebot) log:", log);
612
227
  var pjson = require('./package.json');
613
- console.log("(Tybot) Starting Tilebot connector v" + pjson.version);
614
- console.log("(Tybot) Connecting to mongodb...");
228
+ console.log("(Tilebot) Starting Tilebot connector v" + pjson.version);
229
+ console.log("(Tilebot) Connecting to mongodb...");
615
230
 
616
231
  connection = mongoose.connect(settings.MONGODB_URI, { "useNewUrlParser": true, "autoIndex": false }, async (err) => {
617
232
  if (err) {
618
- console.error('(Tybot) Failed to connect to MongoDB on ' + settings.MONGODB_URI + " ", err);
233
+ console.error('(Tilebot) Failed to connect to MongoDB on ' + settings.MONGODB_URI + " ", err);
619
234
  //process.exit(1); // add => exitOnFail: true
620
235
  }
621
236
  else {
622
- console.log("(Tybot) mongodb connection ok.");
237
+ console.log("(Tilebot) mongodb connection ok.");
623
238
  if (tdcache) {
624
239
  try {
625
240
  await tdcache.connect();
626
241
  }
627
242
  catch (error) {
628
243
  tdcache = null;
629
- console.error("(Tybot) tdcache (Redis) connection error:", error);
244
+ console.error("(Tilebot) tdcache (Redis) connection error:", error);
630
245
  }
631
- console.log("(Tybot) tdcache (Redis) connected.");
246
+ console.log("(Tilebot) tdcache (Redis) connected.");
632
247
  }
633
- console.info("Tybot started.");
248
+ console.info("Tilebot started.");
634
249
  if (completionCallback) {
635
250
  completionCallback();
636
251
  }
@@ -638,22 +253,4 @@ function startApp(settings, completionCallback) {
638
253
  });
639
254
  }
640
255
 
641
- /*
642
- var connection = mongoose.connect(process.env.mongoUrl, { "useNewUrlParser": true, "autoIndex": false }, function(err) {
643
- if (err) {
644
- console.error('Failed to connect to MongoDB on ' + process.env.mongoUrl + " ", err);
645
- //process.exit(1);
646
- }
647
- else {
648
- console.info("Mongodb connected")
649
- }
650
- });
651
- */
652
-
653
- /*
654
- app.listen(3000, () => {
655
- console.log('server started');
656
- });
657
- */
658
-
659
256
  module.exports = { router: router, startApp: startApp};
@@ -170,20 +170,19 @@ class IntentForm {
170
170
  return rg.test(text);
171
171
  }
172
172
 
173
- /*async printdb() {
174
- console.log("****** PRINT DB ***********************")
175
- const current_field_value = this.getValue("CURRENT_FIELD");
176
- console.log("Current edit field:", current_field_value);
177
- const form = await this.getValue("CURRENT_FORM");
178
- for (let i = 0; i < form.length; i++) {
179
- const field_id = i;
180
- console.log("field_id:", field_id);
181
- const field_value_key = field_id;
182
- console.log("field value key:", field_value_key);
183
- console.log("field value:", await this.getValue(field_value_key));
173
+ static isValidForm(form) {
174
+ let is_valid = true;
175
+ if (!form) {
176
+ is_valid = false;
184
177
  }
185
- console.log("****** PRINT DB END ***********************");
186
- }*/
178
+ else if (form && !form.fields) {
179
+ is_valid = false;
180
+ }
181
+ else if (form && form.fields.length === 0) {
182
+ is_valid = false;
183
+ }
184
+ return is_valid;
185
+ }
187
186
 
188
187
  }
189
188