@tiledesk/tiledesk-tybot-connector 0.5.0-rc2 → 0.5.1-rc1

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 (80) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/ExtApi.js +2 -71
  3. package/Logger.js +32 -5
  4. package/{models → engine}/IntentsMachineFactory.js +1 -1
  5. package/{models → engine}/MongodbBotsDataSource.js +2 -2
  6. package/{models → engine}/MongodbIntentsMachine.js +1 -1
  7. package/{models → engine}/TiledeskChatbot.js +3 -1
  8. package/{models → engine}/TiledeskIntentsMachine.js +1 -2
  9. package/{models → engine/mock}/MockBotsDataSource.js +1 -2
  10. package/index.js +23 -111
  11. package/logs/app.log +3041 -4469
  12. package/logs/app3.log +2822 -0
  13. package/logs/app4.log +46163 -0
  14. package/logs/app5.log +33173 -0
  15. package/models/faq.js +2 -5
  16. package/package.json +1 -1
  17. package/{TiledeskServices → services}/AIService.js +4 -3
  18. package/{models/faqKbService.js → services/FaqKbService.js} +1 -1
  19. package/{models/faqService.js → services/FaqService.js} +4 -3
  20. package/services/IntegrationService.js +43 -0
  21. package/services/TilebotService.js +47 -0
  22. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +2 -3
  23. package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +1 -1
  24. package/tiledeskChatbotPlugs/TiledeskRequestVariables.js +1 -1
  25. package/tiledeskChatbotPlugs/directives/DEPRECATED_DirSetAttribute.js +1 -1
  26. package/tiledeskChatbotPlugs/directives/DirAddTags.js +8 -42
  27. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +42 -128
  28. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +9 -74
  29. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +14 -83
  30. package/tiledeskChatbotPlugs/directives/DirAssign.js +1 -1
  31. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +1 -1
  32. package/tiledeskChatbotPlugs/directives/DirAssistant.js +13 -106
  33. package/tiledeskChatbotPlugs/directives/DirBrevo.js +5 -34
  34. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +1 -1
  35. package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +1 -1
  36. package/tiledeskChatbotPlugs/directives/DirClose.js +1 -1
  37. package/tiledeskChatbotPlugs/directives/DirCode.js +1 -1
  38. package/tiledeskChatbotPlugs/directives/DirCondition.js +1 -1
  39. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +3 -72
  40. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +3 -3
  41. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +5 -33
  42. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +2 -2
  43. package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +1 -1
  44. package/tiledeskChatbotPlugs/directives/DirForm.js +2 -2
  45. package/tiledeskChatbotPlugs/directives/DirGptTask.js +13 -76
  46. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +8 -53
  47. package/tiledeskChatbotPlugs/directives/DirHubspot.js +5 -34
  48. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +3 -39
  49. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +2 -38
  50. package/tiledeskChatbotPlugs/directives/DirIntent.js +2 -77
  51. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +1 -1
  52. package/tiledeskChatbotPlugs/directives/DirMake.js +1 -1
  53. package/tiledeskChatbotPlugs/directives/DirMessage.js +1 -1
  54. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +3 -72
  55. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +2 -2
  56. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +2 -2
  57. package/tiledeskChatbotPlugs/directives/DirQapla.js +7 -72
  58. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +2 -2
  59. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +1 -1
  60. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +3 -38
  61. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +3 -39
  62. package/tiledeskChatbotPlugs/directives/DirReply.js +2 -2
  63. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +3 -3
  64. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +1 -1
  65. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +3 -29
  66. package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +1 -1
  67. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +3 -38
  68. package/tiledeskChatbotPlugs/directives/DirWait.js +1 -1
  69. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -1
  70. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +32 -22
  71. package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +1 -1
  72. package/tiledeskChatbotPlugs/directives/DirWebResponse.js +23 -64
  73. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +3 -29
  74. package/utils/HttpUtils.js +128 -0
  75. package/{models → utils}/TiledeskChatbotUtil.js +8 -50
  76. package/TiledeskServices/utils.js +0 -99
  77. /package/{models → engine}/IntentForm.js +0 -0
  78. /package/{models → engine}/TiledeskChatbotConst.js +0 -0
  79. /package/{models → engine/mock}/MockIntentsMachine.js +0 -0
  80. /package/{models → engine/mock}/MockTdCache.js +0 -0
@@ -1,7 +1,7 @@
1
1
  let axios = require('axios');
2
2
  let https = require("https");
3
3
  const { Filler } = require('../Filler');
4
- const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
4
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
5
5
  const { DirIntent } = require('./DirIntent');
6
6
  const winston = require('../../utils/winston');
7
7
 
@@ -38,10 +38,9 @@ class DirWebRequestV2 {
38
38
  }
39
39
 
40
40
  async go(action, callback) {
41
- winston.debug("(DirWebRequestV2) Action: ", action);
42
-
41
+ winston.debug("DirWebRequestV2 action:", action);
43
42
  if (!this.tdcache) {
44
- console.error("Error: DirWebRequest tdcache is mandatory");
43
+ winston.error("Error: DirWebRequestV2 tdcache is mandatory");
45
44
  callback();
46
45
  return;
47
46
  }
@@ -51,12 +50,10 @@ class DirWebRequestV2 {
51
50
  let trueIntentAttributes = action.trueIntentAttributes;
52
51
  let falseIntentAttributes = action.falseIntentAttributes;
53
52
 
54
- if (this.log) {
55
- console.log("DirWebRequest trueIntent", trueIntent)
56
- console.log("DirWebRequest falseIntent", falseIntent)
57
- console.log("DirWebRequest trueIntentAttributes", trueIntentAttributes)
58
- console.log("DirWebRequest falseIntentAttributes", falseIntentAttributes)
59
- }
53
+ winston.debug("DirWebRequestV2 trueIntent " + trueIntent)
54
+ winston.debug("DirWebRequestV2 falseIntent " + falseIntent)
55
+ winston.debug("DirWebRequestV2 trueIntentAttributes " + trueIntentAttributes)
56
+ winston.debug("DirWebRequestV2 falseIntentAttributes " + falseIntentAttributes)
60
57
 
61
58
  let requestAttributes = null;
62
59
  requestAttributes =
@@ -91,7 +88,7 @@ class DirWebRequestV2 {
91
88
 
92
89
  let timeout = this.#webrequest_timeout(action, 20000, 1, 300000);
93
90
 
94
- if (this.log) {console.log("webRequest URL", url);}
91
+ winston.debug("DirWebRequestV2 webRequest URL " + url);
95
92
 
96
93
  const HTTPREQUEST = {
97
94
  url: url,
@@ -100,19 +97,19 @@ class DirWebRequestV2 {
100
97
  method: action.method,
101
98
  timeout: timeout
102
99
  };
103
- if (this.log) { console.log("webRequest HTTPREQUEST", HTTPREQUEST); }
100
+ winston.debug("DirWebRequestV2 HttpRequest: ", HTTPREQUEST);
104
101
 
105
102
  this.#myrequest(
106
103
  HTTPREQUEST, async (err, res) => {
107
- if (this.log) { console.log("DirWebRequest res:", res); }
104
+
108
105
  let resbody = res.data;
109
106
  let status = res.status;
110
107
  let error = res.error;
111
108
  await this.#assignAttributes(action, resbody, status, error)
112
- if (this.log) { console.log("webRequest resbody:", resbody); }
109
+ winston.debug("DirWebRequestV2 resbody:", resbody);
113
110
 
114
111
  if (err) {
115
- if (this.log) { console.error("webRequest error:", err); }
112
+ winston.log("webRequest error: ", err);
116
113
  if (callback) {
117
114
  if (falseIntent) {
118
115
  await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
@@ -177,7 +174,7 @@ class DirWebRequestV2 {
177
174
  resolve(json);
178
175
  }
179
176
  catch (err) {
180
- if (this.log) { console.error("Error parsing webRequest jsonBody:", jsonBody, err) };
177
+ winston.error("DirWebRequestV2 Error parsing webRequest jsonBody: " + JSON.stringify(jsonBody) + "\nError: " + JSON.stringify(err));
181
178
  reject("Error parsing jsonBody");
182
179
  }
183
180
  }
@@ -210,7 +207,7 @@ class DirWebRequestV2 {
210
207
  }
211
208
  resolve(json);
212
209
  } catch (err) {
213
- if (this.log) { console.error("Error parsing webRequest formData:", formData, err) };
210
+ winston.error("DirWebRequestV2 Error parsing webRequest formData: " + JSON.stringify(formData) + "\nError: " + JSON.stringify(err));
214
211
  reject("Error parsing formData");
215
212
  }
216
213
  }
@@ -238,7 +235,7 @@ class DirWebRequestV2 {
238
235
  });
239
236
  }
240
237
  else {
241
- winston.debug("(DirWebRequestV2) No trueIntentDirective specified");
238
+ winston.debug("DirWebRequestV2 No trueIntentDirective specified");
242
239
  if (callback) {
243
240
  callback();
244
241
  }
@@ -253,7 +250,7 @@ class DirWebRequestV2 {
253
250
  });
254
251
  }
255
252
  else {
256
- winston.debug("(DirWebRequestV2) No falseIntentDirective specified");
253
+ winston.debug("DirWebRequestV2 No falseIntentDirective specified");
257
254
  if (callback) {
258
255
  callback();
259
256
  }
@@ -305,6 +302,21 @@ class DirWebRequestV2 {
305
302
  }
306
303
  })
307
304
  .catch((err) => {
305
+ if (this.log) {
306
+ // FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - START
307
+ let cache = [];
308
+ let error_log = JSON.stringify(err, function (key, value) { // try to use a separate function
309
+ if (typeof value === 'object' && value != null) {
310
+ if (cache.indexOf(value) !== -1) {
311
+ return;
312
+ }
313
+ cache.push(value);
314
+ }
315
+ return value;
316
+ });
317
+ winston.error("(DirWebRequestv2) An error occurred: ", error_log);
318
+ // FIX THE STRINGIFY OF CIRCULAR STRUCTURE BUG - END
319
+ }
308
320
  if (callback) {
309
321
  let status = 1000;
310
322
  let cache = [];
@@ -340,7 +352,7 @@ class DirWebRequestV2 {
340
352
  });
341
353
  }
342
354
  catch (error) {
343
- winston.error("Error: ", error);
355
+ winston.error("DirWebRequestV2 Error:", error);
344
356
  }
345
357
  }
346
358
 
@@ -349,8 +361,6 @@ class DirWebRequestV2 {
349
361
  if (!action.settings) {
350
362
  return timeout;
351
363
  }
352
-
353
-
354
364
  if (action.settings.timeout) {
355
365
  if ((typeof action.settings.timeout === "number") && action.settings.timeout > min && action.settings.timeout < max) {
356
366
  timeout = Math.round(action.settings.timeout)
@@ -1,7 +1,7 @@
1
1
  let axios = require('axios');
2
2
  let https = require("https");
3
3
  const { Filler } = require('../Filler');
4
- const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
4
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
5
5
  const { DirIntent } = require('./DirIntent');
6
6
 
7
7
  class DirWebRequestV2 {
@@ -1,6 +1,7 @@
1
1
  const { Filler } = require('../Filler');
2
- const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
- const { TiledeskChatbotUtil } = require('../../models/TiledeskChatbotUtil');
2
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
3
+ const { TiledeskChatbotUtil } = require('../../utils/TiledeskChatbotUtil');
4
+ const winston = require('../../utils/winston');
4
5
  let axios = require('axios');
5
6
 
6
7
  class DirWebResponse {
@@ -18,12 +19,13 @@ class DirWebResponse {
18
19
  }
19
20
 
20
21
  execute(directive, callback) {
22
+ winston.debug("Execute WebResponse directive: ", directive);
21
23
  let action;
22
24
  if (directive.action) {
23
25
  action = directive.action;
24
26
  }
25
27
  else {
26
- console.error("Incorrect directive (no action provided):", directive);
28
+ winston.debug("DirWebResponse Incorrect directive: ", directive);
27
29
  callback();
28
30
  return;
29
31
  }
@@ -34,29 +36,26 @@ class DirWebResponse {
34
36
  }
35
37
 
36
38
  async go(action, callback) {
37
- if (this.log) {
38
- console.log("(DirWebResponse) action:", action);
39
+ winston.debug("DirWebResponse action: ", action);
40
+
41
+ if (!this.tdcache) {
42
+ winston.error("DirWebResponse Error: tdcache is mandatory");
43
+ callback();
44
+ return;
39
45
  }
40
46
 
41
47
  let requestAttributes = null;
42
- if (this.tdcache) {
43
- requestAttributes =
44
- await TiledeskChatbot.allParametersStatic(this.tdcache, this.requestId);
45
- const filler = new Filler();
46
-
47
- try {
48
- let status = action.status;
49
- status = filler.fill(status, requestAttributes);
50
- }
51
- catch(e) {
52
- console.error(e)
53
- }
54
-
55
- }
56
-
48
+ requestAttributes =
49
+ await TiledeskChatbot.allParametersStatic(
50
+ this.tdcache, this.requestId
51
+ );
52
+
53
+ const filler = new Filler();
54
+ const filled_status = filler.fill(action.status, requestAttributes);
57
55
  const json = await this.getJsonFromAction(action, filler, requestAttributes)
56
+
58
57
  let webResponse = {
59
- status: status,
58
+ status: filled_status,
60
59
  payload: json
61
60
  }
62
61
 
@@ -64,12 +63,10 @@ class DirWebResponse {
64
63
 
65
64
  try {
66
65
  this.tdcache.publish(topic, JSON.stringify(webResponse));
67
- if (this.log) {
68
- console.log("(DirWebResponse) Published webresponse to topic:", topic);
69
- }
66
+ winston.verbose("DirWebResponse Published webresponse to topic: " + topic);
70
67
  }
71
68
  catch(e) {
72
- console.error(e)
69
+ winston.error("DirWebResponse Error: ", e)
73
70
  }
74
71
 
75
72
  callback();
@@ -87,7 +84,7 @@ class DirWebResponse {
87
84
  resolve(json);
88
85
  }
89
86
  catch (err) {
90
- if (this.log) { console.error("Error parsing webRequest jsonBody:", jsonBody, err) };
87
+ winston.error("Error parsing webRequest jsonBody: " + JSON.stringify(jsonBody) + "\nError: " + JSON.stringify(err));
91
88
  reject("Error parsing jsonBody");
92
89
  }
93
90
  }
@@ -99,42 +96,4 @@ class DirWebResponse {
99
96
 
100
97
  }
101
98
 
102
-
103
-
104
- /**
105
- * A stub to send message to the "ext/botId" endpoint, hosted by tilebot on:
106
- * /${TILEBOT_ROUTE}/ext/${botId}
107
- *
108
- * @param {Object} webResponse. The webhook response to send back
109
- * @param {Object} projectId. The projectId
110
- * @param {string} botId. Tiledesk botId
111
- * @param {string} token. User token
112
- */
113
- // function sendResponse(webResponse, projectId, botId, callback) {
114
- // const url = `${WEBHOOK_URL}/${projectId}/${botId}`;
115
- // const HTTPREQUEST = {
116
- // url: url,
117
- // headers: {
118
- // 'Content-Type': 'application/json'
119
- // },
120
- // json: webResponse,
121
- // method: 'POST'
122
- // };
123
- // myrequest(
124
- // HTTPREQUEST,
125
- // function (err, resbody) {
126
- // if (err) {
127
- // if (callback) {
128
- // callback(err);
129
- // }
130
- // }
131
- // else {
132
- // if (callback) {
133
- // callback(null, resbody);
134
- // }
135
- // }
136
- // }, false
137
- // );
138
- // }
139
-
140
99
  module.exports = { DirWebResponse };
@@ -1,5 +1,6 @@
1
1
  const axios = require("axios").default;
2
- const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
2
+ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
3
+ const httpUtils = require("../../utils/HttpUtils");
3
4
  const winston = require('../../utils/winston');
4
5
 
5
6
  let whatsapp_api_url;
@@ -71,7 +72,7 @@ class DirWhatsappByAttribute {
71
72
  }
72
73
 
73
74
  return new Promise((resolve, reject) => {
74
- DirWhatsappByAttribute.myrequest(
75
+ httpUtils.request(
75
76
  HTTPREQUEST,
76
77
  function (err, resbody) {
77
78
  if (err) {
@@ -91,33 +92,6 @@ class DirWhatsappByAttribute {
91
92
  })
92
93
 
93
94
  }
94
-
95
- // HTTP REQUEST
96
- static async myrequest(options, callback, log) {
97
- return await axios({
98
- url: options.url,
99
- method: options.method,
100
- data: options.json,
101
- params: options.params,
102
- headers: options.headers
103
- }).then((res) => {
104
- if (res && res.status == 200 && res.data) {
105
- if (callback) {
106
- callback(null, res.data);
107
- }
108
- }
109
- else {
110
- if (callback) {
111
- callback(TiledeskClient.getErr({ message: "Response status not 200" }, options, res), null, null);
112
- }
113
- }
114
- }).catch((err) => {
115
- winston.error("(DirWhatsappByAttribute) An error occured: ", err);
116
- if (callback) {
117
- callback(err, null, null);
118
- }
119
- })
120
- }
121
95
  }
122
96
 
123
97
  module.exports = { DirWhatsappByAttribute }
@@ -0,0 +1,128 @@
1
+ let axios = require('axios');
2
+ let https = require("https");
3
+ const winston = require('./winston');
4
+
5
+ class HttpUtils {
6
+
7
+ constructor() { }
8
+
9
+ request(options, callback) {
10
+ let axios_options = {
11
+ url: options.url,
12
+ method: options.method,
13
+ params: options.params,
14
+ headers: options.headers
15
+ }
16
+ if (options.json !== null) {
17
+ axios_options.data = options.json
18
+ }
19
+ if (options.url.startsWith("https:")) {
20
+ const httpsAgent = new https.Agent({
21
+ rejectUnauthorized: false,
22
+ });
23
+ axios_options.httpsAgent = httpsAgent;
24
+ }
25
+ axios(axios_options)
26
+ .then((res) => {
27
+ if (res && res.status == 200 && res.data) {
28
+ if (callback) {
29
+ callback(null, res.data);
30
+ }
31
+ }
32
+ else {
33
+ if (callback) {
34
+ callback(new Error("Response status is not 200"), null);
35
+ }
36
+ }
37
+ })
38
+ .catch((error) => {
39
+ if (error.response.data) {
40
+ if (typeof error.response.data === 'string') {
41
+ winston.error("Axios error response data: " + error.response.data);
42
+ } else {
43
+ winston.error("Axios error response data: ", error.response.data);
44
+ }
45
+ } else if (error.response) {
46
+ winston.error("Axios error response: ", error.response);
47
+ } else {
48
+ winston.error("Axios error: ", error);
49
+ }
50
+ if (callback) {
51
+ callback(error, null);
52
+ }
53
+ });
54
+ }
55
+
56
+ // static myrequest(options, callback, log) {
57
+ // winston.verbose("** API URL: " + options.url);
58
+ // winston.debug("** Options: ", options);
59
+ // let axios_settings = {
60
+ // url: options.url,
61
+ // method: options.method,
62
+ // data: options.json,
63
+ // params: options.params,
64
+ // headers: options.headers
65
+ // }
66
+
67
+ // if (options.url.startsWith("https:") && options.httpsOptions) {
68
+ // const httpsAgent = new https.Agent(options.httpsOptions);
69
+ // axios_settings.httpsAgent = httpsAgent;
70
+ // }
71
+ // else if (options.url.startsWith("https:") && !options.httpsOptions) {
72
+ // // HTTPS default is rejectUnauthorized: false
73
+ // const httpsAgent = new https.Agent({
74
+ // rejectUnauthorized: false,
75
+ // });
76
+ // axios_settings.httpsAgent = httpsAgent;
77
+ // }
78
+
79
+ // axios(axios_settings)
80
+ // .then(function (res) {
81
+ // winston.debug("Response for url: " + options.url);
82
+ // winston.debug("Response headers: ", res.headers);
83
+
84
+ // if (res && res.status == 200 && res.data) {
85
+ // if (callback) {
86
+ // callback(null, res.data);
87
+ // }
88
+ // }
89
+ // else {
90
+
91
+ // if (callback) {
92
+ // callback(Utils.getErr({message: "Response status not 200"}, options, res), null, null);
93
+ // }
94
+ // }
95
+ // })
96
+ // .catch(function (error) {
97
+ // winston.error("Request Error: ", error);
98
+ // if (callback) {
99
+ // callback(error, null, null);
100
+ // }
101
+ // });
102
+ // }
103
+
104
+ static getErr(err, request, response) {
105
+ let res_err = {}
106
+ res_err.http_err = err;
107
+ res_err.http_request = request;
108
+ res_err.http_response = response;
109
+ return res_err;
110
+ }
111
+
112
+ fixToken(token) {
113
+ if (token.startsWith('JWT ')) {
114
+ return token;
115
+ }
116
+ else {
117
+ return 'JWT ' + token;
118
+ }
119
+ }
120
+
121
+
122
+
123
+
124
+ }
125
+
126
+ const httpUtils = new HttpUtils();
127
+
128
+ module.exports = httpUtils;
@@ -1,12 +1,12 @@
1
1
  const { TiledeskExpression } = require('../TiledeskExpression');
2
2
  const { Filler } = require('../tiledeskChatbotPlugs/Filler');
3
- const { TiledeskChatbotConst } = require('./TiledeskChatbotConst');
4
- const { TiledeskChatbot } = require('./TiledeskChatbot.js');
3
+ const { TiledeskChatbotConst } = require('../engine/TiledeskChatbotConst');
4
+ const { TiledeskChatbot } = require('../engine/TiledeskChatbot.js');
5
5
  let parser = require('accept-language-parser');
6
6
  const { Directives } = require('../tiledeskChatbotPlugs/directives/Directives.js');
7
7
  require('dotenv').config();
8
8
  let axios = require('axios');
9
- const winston = require('../utils/winston');
9
+ const winston = require('./winston');
10
10
 
11
11
  class TiledeskChatbotUtil {
12
12
 
@@ -313,79 +313,37 @@ class TiledeskChatbotUtil {
313
313
  // fill buttons
314
314
  const filler = new Filler();
315
315
  json_buttons_string = filler.fill(json_buttons_string, flow_attributes);
316
- console.log("json_buttons_string:", json_buttons_string);
317
- console.log("type json_buttons_string:", typeof json_buttons_string);
318
- try {
319
- json_buttons = JSON.parse(json_buttons_string);
320
- console.log("json_buttons (parsed): ", json_buttons);
321
- } catch(err) {
322
- console.error("Error parsing json_buttons_string: ", err)
323
- }
324
-
325
- console.log("after try type json_buttons: ", typeof json_buttons, json_buttons);
326
- console.log("Array.isArray(json_buttons): ", Array.isArray(json_buttons));
316
+ // console.log("json_buttons_string:", json_buttons_string);
317
+ json_buttons = JSON.parse(json_buttons_string);
327
318
  if (Array.isArray(json_buttons)) {
328
- console.log("json_buttons is array");
329
319
  json_buttons.forEach(button => {
330
- console.log("analyze button:", button);
331
- console.log("button.value:", typeof button.value, button.value);
332
- console.log("button.type:", typeof button.type, button.type);
333
- console.log("button.action:", typeof button.action, button.action);
334
- console.log("final_buttons:", final_buttons);
335
320
  if (button.value && button.type === "action" && button.action) {
336
- console.log("Case 1");
337
321
  button.show_echo = true;
338
- // console.log("pushing:", button)
339
322
  final_buttons.push(button);
340
323
  }
341
324
  else if (button.value && button.type === "text") {
342
- console.log("Case 2");
343
325
  button.show_echo = true;
344
- // console.log("pushing:", button)
345
326
  final_buttons.push(button);
346
327
  }
347
328
  else if (button.value && button.type === "url" && button.link) {
348
- console.log("Case 3");
349
329
  button.show_echo = true;
350
- // console.log("pushing:", button)
351
330
  final_buttons.push(button);
352
331
  }
353
332
  else {
354
- console.log("Invalid button. Skipping:", JSON.stringify(button) );
333
+ winston.verbose("Invalid button. Skipping:", button);
355
334
  }
356
335
  });
357
336
  }
358
-
359
- // "buttons": [
360
- // {
361
- // "type": "action",
362
- // "value": "Button1", // obbligatorio sempre
363
- // "action": "#bb347206-d639-4926-94c9-e94930623dce", // mandatory
364
- // "show_echo": true, // lo inserisco sempre
365
- // "alias": "button1 alias"
366
- // },
367
- // {
368
- // "type": "text",
369
- // "value": "Button2 text", // obbligatorio sempre
370
- // "show_echo": true // lo inserisco sempre
371
- // },
372
- // {
373
- // "type": "url",
374
- // "value": "Button3 link", // obbligatorio sempre
375
- // "link": "http://", // obbligatorio
376
- // "show_echo": true // lo inserisco sempre
377
- // }
378
- // ]
379
337
  }
380
338
  catch(error) {
381
- console.error("Invalid json_buttons:", error)
339
+ winston.warn("Invalid json_buttons:", error)
382
340
  }
383
341
  if (final_buttons && final_buttons.length > 0) {
384
342
  command.message.attributes.attachment.buttons = final_buttons;
385
343
  delete command.message.attributes.attachment.json_buttons;
386
344
  }
387
345
  else {
388
- console.log("Invalid json buttons. Skipped:", JSON.stringify(final_buttons));
346
+ winston.verbose("Invalid json_buttons. Skipping...")
389
347
  }
390
348
  }
391
349
  }
@@ -1,99 +0,0 @@
1
- let axios = require('axios');
2
- let https = require("https");
3
-
4
- class Utils {
5
- constructor(){}
6
-
7
-
8
- static myrequest(options, callback, log) {
9
- if (log) {
10
- console.log("** API URL:", options.url);
11
- console.log("** Options:", JSON.stringify(options));
12
- }
13
- let axios_settings = {
14
- url: options.url,
15
- method: options.method,
16
- data: options.json,
17
- params: options.params,
18
- headers: options.headers
19
- }
20
- // console.log("options.url.startsWith(https:)", options.url.startsWith("https:"))
21
- // console.log("this.httpsOptions", this.httpsOptions)
22
-
23
- if (options.url.startsWith("https:") && options.httpsOptions) {
24
- // console.log("Tiledesk Client v 0.9.x: url.startsWith https: && httpsOptions");
25
- const httpsAgent = new https.Agent(options.httpsOptions);
26
- axios_settings.httpsAgent = httpsAgent;
27
- }
28
- else if (options.url.startsWith("https:") && !options.httpsOptions) {
29
- // HTTPS default is rejectUnauthorized: false
30
- // console.log("Tiledesk Client v 0.9.x: url.startsWith https: && NOT httpsOptions");
31
- const httpsAgent = new https.Agent({
32
- rejectUnauthorized: false,
33
- });
34
- axios_settings.httpsAgent = httpsAgent;
35
- }
36
-
37
-
38
-
39
- // console.log("Using axios settings:", axios_settings)
40
- // axios(
41
- // {
42
- // url: options.url,
43
- // method: options.method,
44
- // data: options.json,
45
- // params: options.params,
46
- // httpsAgent: httpsAgent,
47
- // headers: options.headers
48
- // })
49
- axios(axios_settings)
50
- .then(function (res) {
51
- if (log) {
52
- console.log("Response for url:", options.url);
53
- console.log("Response headers:\n", JSON.stringify(res.headers));
54
- // console.log("******** Response for url:", res);
55
- }
56
-
57
- if (res && res.status == 200 && res.data) {
58
- if (callback) {
59
- callback(null, res.data);
60
- }
61
- }
62
- else {
63
-
64
- if (callback) {
65
- callback(Utils.getErr({message: "Response status not 200"}, options, res), null, null);
66
- }
67
- }
68
- })
69
- .catch(function (error) {
70
- // console.error(error);
71
- if (callback) {
72
- callback(error, null, null);
73
- }
74
- });
75
- }
76
-
77
- static getErr(err, request, response) {
78
- let res_err = {}
79
- res_err.http_err = err;
80
- res_err.http_request = request;
81
- res_err.http_response = response;
82
- return res_err;
83
- }
84
-
85
- static fixToken(token) {
86
- if (token.startsWith('JWT ')) {
87
- return token;
88
- }
89
- else {
90
- return 'JWT ' + token;
91
- }
92
- }
93
-
94
-
95
-
96
-
97
- }
98
-
99
- module.exports = Utils
File without changes
File without changes
File without changes
File without changes