@tiledesk/tiledesk-tybot-connector 0.2.601-rc1 → 0.3.1

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 (73) hide show
  1. package/CHANGELOG.md +387 -1
  2. package/ExtApi.js +6 -7
  3. package/Logger.js +74 -0
  4. package/TdCache.js +81 -176
  5. package/TdCache_v3.js +261 -0
  6. package/TiledeskExpression.js +7 -3
  7. package/TiledeskServices/AIService.js +43 -0
  8. package/TiledeskServices/utils.js +99 -0
  9. package/index.js +305 -50
  10. package/logs/app.log +279 -0
  11. package/models/IntentsMachineFactory.js +5 -2
  12. package/models/MockBotsDataSource.js +19 -11
  13. package/models/TiledeskChatbot.js +97 -79
  14. package/models/TiledeskChatbotConst.js +12 -17
  15. package/models/TiledeskChatbotUtil.js +359 -109
  16. package/models/TiledeskIntentsMachine.js +1 -1
  17. package/models/faqKbService.js +1 -1
  18. package/package.json +8 -6
  19. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +172 -106
  20. package/tiledeskChatbotPlugs/Filler.js +13 -2
  21. package/tiledeskChatbotPlugs/TildeskContextForCodeOrchestrator.js +8 -0
  22. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +13 -7
  23. package/tiledeskChatbotPlugs/directives/DirAddTags.js +374 -0
  24. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +476 -0
  25. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +16 -19
  26. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +221 -34
  27. package/tiledeskChatbotPlugs/directives/DirAssign.js +0 -11
  28. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +11 -21
  29. package/tiledeskChatbotPlugs/directives/DirAssistant.js +728 -0
  30. package/tiledeskChatbotPlugs/directives/DirBrevo.js +353 -0
  31. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +3 -30
  32. package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +22 -0
  33. package/tiledeskChatbotPlugs/directives/DirClose.js +16 -3
  34. package/tiledeskChatbotPlugs/directives/DirCode.js +1 -1
  35. package/tiledeskChatbotPlugs/directives/DirCondition.js +0 -26
  36. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +182 -0
  37. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +121 -0
  38. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +5 -8
  39. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +11 -1
  40. package/tiledeskChatbotPlugs/directives/DirDepartment.js +15 -6
  41. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +17 -6
  42. package/tiledeskChatbotPlugs/directives/DirForm.js +12 -2
  43. package/tiledeskChatbotPlugs/directives/DirGptTask.js +83 -38
  44. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +4 -7
  45. package/tiledeskChatbotPlugs/directives/DirHubspot.js +5 -8
  46. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +14 -27
  47. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +278 -0
  48. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +147 -51
  49. package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +125 -0
  50. package/tiledeskChatbotPlugs/directives/DirIntent.js +8 -37
  51. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +5 -26
  52. package/tiledeskChatbotPlugs/directives/DirMessage.js +19 -17
  53. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +135 -0
  54. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +20 -87
  55. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +59 -0
  56. package/tiledeskChatbotPlugs/directives/DirQapla.js +6 -9
  57. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +17 -7
  58. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +17 -7
  59. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +11 -2
  60. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +135 -21
  61. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +163 -0
  62. package/tiledeskChatbotPlugs/directives/DirReply.js +53 -9
  63. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +347 -0
  64. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +13 -23
  65. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +247 -0
  66. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +202 -15
  67. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +13 -4
  68. package/tiledeskChatbotPlugs/directives/DirWait.js +21 -4
  69. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -2
  70. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +166 -103
  71. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +2 -60
  72. package/tiledeskChatbotPlugs/directives/Directives.js +16 -1
  73. /package/tiledeskChatbotPlugs/directives/{DirOfflineHours.js → DEPRECATED_DirOfflineHours.js} +0 -0
@@ -0,0 +1,353 @@
1
+ const axios = require("axios").default;
2
+ const { TiledeskChatbot } = require("../../models/TiledeskChatbot");
3
+ const { Filler } = require("../Filler");
4
+ const { DirIntent } = require("./DirIntent");
5
+ let https = require("https");
6
+ require('dotenv').config();
7
+
8
+
9
+ class DirBrevo {
10
+
11
+ constructor(context) {
12
+ if (!context) {
13
+ throw new Error('context object is mandatory');
14
+ }
15
+ this.context = context;
16
+ this.tdcache = this.context.tdcache;
17
+ this.requestId = this.context.requestId;
18
+ this.intentDir = new DirIntent(context);
19
+ this.API_ENDPOINT = this.context.API_ENDPOINT;
20
+ this.log = context.log;
21
+ }
22
+
23
+ execute(directive, callback) {
24
+ if (this.log) { console.log("DirBrevo directive: ", directive); }
25
+ let action;
26
+ if (directive.action) {
27
+ action = directive.action;
28
+ }
29
+ else {
30
+ console.error("DirBrevo Incorrect directive: ", JSON.stringify(directive));
31
+ callback();
32
+ return;
33
+ }
34
+ this.go(action, (stop) => {
35
+ callback(stop);
36
+ })
37
+ }
38
+
39
+ async go(action, callback) {
40
+ if (this.log) { console.log("DirBrevo action:", JSON.stringify(action)); }
41
+ if (!this.tdcache) {
42
+ console.error("Error: DirBrevo tdcache is mandatory");
43
+ callback();
44
+ return;
45
+ }
46
+
47
+ let trueIntent = action.trueIntent;
48
+ let falseIntent = action.falseIntent;
49
+ let trueIntentAttributes = action.trueIntentAttributes;
50
+ let falseIntentAttributes = action.falseIntentAttributes;
51
+
52
+ if (this.log) {
53
+ console.log("DirBrevo trueIntent", trueIntent)
54
+ console.log("DirBrevo falseIntent", falseIntent)
55
+ console.log("DirBrevo trueIntentAttributes", trueIntentAttributes)
56
+ console.log("DirBrevo falseIntentAttributes", falseIntentAttributes)
57
+ }
58
+
59
+ let requestVariables = null;
60
+ requestVariables =
61
+ await TiledeskChatbot.allParametersStatic(
62
+ this.tdcache, this.requestId
63
+ )
64
+
65
+ //let token = action.token;
66
+ let bodyParameters = action.bodyParameters;
67
+ if (this.log) { console.log("DirBrevo bodyParameters: ", bodyParameters); }
68
+
69
+ if (!bodyParameters || bodyParameters === '') {
70
+ if (this.log) { console.error("DirBrevo ERROR - bodyParameters is undefined or null or empty string") };
71
+ callback();
72
+ return;
73
+ }
74
+
75
+ const brevo_base_url = process.env.BREVO_ENDPOINT || "https://api.brevo.com/v3"
76
+ if (this.log) { console.log("DirBrevo brevo_base_url ", brevo_base_url); }
77
+
78
+ let key = await this.getKeyFromIntegrations();
79
+ if (this.log) { console.log('DirBrevo key Debug1: ', key) }
80
+ // ONLY FOR DEBUG CANCELLARE!!!!!
81
+ // if (process.env.BREVO_DEBUG == '1') {
82
+ // key = process.env.BREVO_TOKEN;
83
+ // }
84
+ if (!key) {
85
+ if (this.log) { console.log("DirBrevo - Key not found in Integrations."); }
86
+ if (falseIntent) {
87
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
88
+ callback(true);
89
+ return;
90
+ }
91
+ }
92
+
93
+ const filler = new Filler();
94
+ for (const [key, value] of Object.entries(bodyParameters)) {
95
+ if (this.log) { console.log("bodyParam:", key, "value:", value) }
96
+ let filled_value = filler.fill(value, requestVariables);
97
+ bodyParameters[key] = filled_value;
98
+ }
99
+ if (this.log) { console.log('DirBrevo bodyParameters filler: ', bodyParameters) }
100
+
101
+ // CREATE THE JSON FOR BREVO
102
+ let brevo_email = '';
103
+ let brevo_bodyParameters = {};
104
+ for (const [key, value] of Object.entries(bodyParameters)) {
105
+ if (this.log) { console.log("bodyParam:", key, "value:", value) }
106
+ if (key === 'email') {brevo_email = value}
107
+ else { brevo_bodyParameters[key] = value;}
108
+ }
109
+ if (this.log) { console.log('DirBrevo brevo_email: ', brevo_email) }
110
+ if (this.log) { console.log('DirBrevo brevo_bodyParameters: ', brevo_bodyParameters) }
111
+
112
+
113
+ let json = {
114
+ email: brevo_email,
115
+ attributes: brevo_bodyParameters,
116
+ "emailBlacklisted": false,
117
+ "smsBlacklisted": false,
118
+ "listIds": [
119
+ 0
120
+ ],
121
+ "updateEnabled": false,
122
+ "smtpBlacklistSender": [
123
+ "info@mytest.com"
124
+ ]
125
+ }
126
+ if (this.log) { console.log('DirBrevo key Debug2: ', key) }
127
+ //----------------
128
+ if (this.log) {console.log("DirBrevo brevo_base_url ",brevo_base_url);}
129
+ if (this.log) { console.log('DirBrevo json: ', json) }
130
+ const BREVO_HTTPREQUEST = {
131
+ url: brevo_base_url + '/contacts',
132
+ headers: {
133
+ 'api-key': key,
134
+ 'Content-Type': 'application/json',
135
+ 'Accept': 'application/json'
136
+ },
137
+ json: json,
138
+ method: "POST"
139
+ }
140
+ if (this.log) { console.log("DirBrevo BREVO_HTTPREQUEST", JSON.stringify(BREVO_HTTPREQUEST)); }
141
+
142
+ this.#myrequest(
143
+ BREVO_HTTPREQUEST, async (err, resbody) => {
144
+ if (err) {
145
+ if (callback) {
146
+ if (this.log) {
147
+ console.error("(httprequest) DirBrevo err response:", err.response)
148
+ console.error("(httprequest) DirBrevo err data:", err.response.data)
149
+ };
150
+
151
+ let result = null;
152
+ let status = null;
153
+ let error;
154
+
155
+ if (err.response &&
156
+ err.response.status) {
157
+ status = err.response.status;
158
+ }
159
+
160
+ if (err.response &&
161
+ err.response.data &&
162
+ err.response.data.message) {
163
+ error = err.response.data.message;
164
+ }
165
+
166
+ if (this.log) {
167
+ console.error("(httprequest) DirBrevo err data result:", result); // CONTROLLA IL VALORE
168
+ console.error("(httprequest) DirBrevo err data status:", status);
169
+ console.error("(httprequest) DirBrevo err data error:", error);
170
+ }
171
+
172
+ await this.#assignAttributes(action, status, result, error);
173
+ if (falseIntent) {
174
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
175
+ callback(true);
176
+ return;
177
+ }
178
+ callback();
179
+ return;
180
+ }
181
+ } else if (callback) {
182
+ if (this.log) { console.log("DirBrevo resbody: ", JSON.stringify(resbody, null, 2).slice(2, -1)); }
183
+
184
+ let status = 201;
185
+ let error = null;
186
+ let result = JSON.stringify(resbody, null, 2).slice(2, -1);
187
+ await this.#assignAttributes(action, status, result, error);
188
+ if (trueIntent) {
189
+ await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes)
190
+ callback(true);
191
+ return;
192
+ }
193
+ callback();
194
+ return;
195
+ }
196
+ }
197
+ );
198
+
199
+ }
200
+
201
+ async #assignAttributes(action, status, result, error) {
202
+ if (this.log) {
203
+ console.log("DirBrevo assignAttributes action:", action)
204
+ console.log("DirBrevo assignAttributes status:", status)
205
+ console.log("DirBrevo assignAttributes result:", result)
206
+ console.log("DirBrevo assignAttributes error:", error)
207
+ }
208
+ if (this.context.tdcache) {
209
+ if (action.assignStatusTo) {
210
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignStatusTo, status);
211
+ }
212
+ if (action.assignResultTo) {
213
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignResultTo, result);
214
+ }
215
+ if (action.assignErrorTo) {
216
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignErrorTo, error);
217
+ }
218
+
219
+ // Debug log
220
+ if (this.log) {
221
+ const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
222
+ for (const [key, value] of Object.entries(all_parameters)) {
223
+ if (this.log) { console.log("DirBrevo request parameter:", key, "value:", value, "type:", typeof value) }
224
+ }
225
+ }
226
+ }
227
+ }
228
+
229
+ #myrequest(options, callback) {
230
+ if (this.log) {
231
+ console.log("** API URL:", options.url);
232
+ console.log("** Options:", JSON.stringify(options));
233
+ }
234
+ let axios_options = {
235
+ url: options.url,
236
+ method: options.method,
237
+ params: options.params,
238
+ headers: options.headers
239
+ }
240
+ if (options.json !== null) {
241
+ axios_options.data = options.json
242
+ }
243
+ if (this.log) {
244
+ console.log("axios_options:", JSON.stringify(axios_options));
245
+ }
246
+ if (options.url.startsWith("https:")) {
247
+ const httpsAgent = new https.Agent({
248
+ rejectUnauthorized: false,
249
+ });
250
+ axios_options.httpsAgent = httpsAgent;
251
+ }
252
+ axios(axios_options)
253
+ .then((res) => {
254
+ if (this.log) {
255
+ console.log("Response for url:", options.url);
256
+ console.log("Response headers:\n", JSON.stringify(res.headers));
257
+ console.log("Response status:", JSON.stringify(res.status));
258
+ }
259
+ if (res && (res.status == 200 || res.status == 201) && res.data) {
260
+ if (callback) {
261
+ callback(null, res.data);
262
+ }
263
+ }
264
+ else {
265
+ if (callback) {
266
+ callback(new Error("Response status is not 200"), null);
267
+ }
268
+ }
269
+ })
270
+ .catch((error) => {
271
+ if (callback) {
272
+ callback(error, null);
273
+ }
274
+ });
275
+ }
276
+
277
+ async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
278
+ let trueIntentDirective = null;
279
+
280
+ if (trueIntent) {
281
+ trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
282
+ }
283
+ let falseIntentDirective = null;
284
+ if (falseIntent) {
285
+ falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
286
+ }
287
+ if (this.log) { console.log('DirBrevo executeCondition/result', result) }
288
+ if (result === true) {
289
+ if (trueIntentDirective) {
290
+ this.intentDir.execute(trueIntentDirective, () => {
291
+ if (callback) {
292
+ callback();
293
+ }
294
+ });
295
+ }
296
+ else {
297
+ if (this.log) { console.log("No trueIntentDirective specified"); }
298
+ if (callback) {
299
+ callback();
300
+ }
301
+ }
302
+ }
303
+ else {
304
+ if (falseIntentDirective) {
305
+ this.intentDir.execute(falseIntentDirective, () => {
306
+ if (callback) {
307
+ callback();
308
+ }
309
+ });
310
+ }
311
+ else {
312
+ if (this.log) { console.log("No falseIntentDirective specified"); }
313
+ if (callback) {
314
+ callback();
315
+ }
316
+ }
317
+ }
318
+ }
319
+
320
+ async getKeyFromIntegrations() {
321
+ return new Promise((resolve) => {
322
+
323
+ const INTEGRATIONS_HTTPREQUEST = {
324
+ url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/Brevo",
325
+ headers: {
326
+ 'Content-Type': 'application/json',
327
+ 'Authorization': 'JWT ' + this.context.token
328
+ },
329
+ method: "GET"
330
+ }
331
+ if (this.log) { console.log("Brevo INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
332
+
333
+ this.#myrequest(
334
+ INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
335
+ if (err) {
336
+ resolve(null);
337
+ } else {
338
+
339
+ if (integration &&
340
+ integration.value) {
341
+ resolve(integration.value.apikey)
342
+ }
343
+ else {
344
+ resolve(null)
345
+ }
346
+ }
347
+ })
348
+ })
349
+ }
350
+
351
+ }
352
+
353
+ module.exports = { DirBrevo }
@@ -7,37 +7,10 @@ class DirCaptureUserReply {
7
7
  if (!context) {
8
8
  throw new Error('context object is mandatory.');
9
9
  }
10
+
10
11
  this.context = context;
11
12
  this.reply = context.reply;
12
- // reply = {
13
- // actions: [
14
- // {
15
- // _tdActionType: 'askgpt',
16
- // _tdActionTitle: 'gpt action',
17
- // assignReplyTo: 'gpt_reply',
18
- // assignSourceTo: 'gpt_source',
19
- // kbid: 'XXX',
20
- // trueIntent: '#SUCCESS',
21
- // falseIntent: '#FAILURE',
22
- // question: 'this is the question: ${last_user_message}'
23
- // }
24
- // ],
25
- // attributes: {
26
- // clienttimestamp: 1695548792706,
27
- // intent_info: {
28
- // intent_name: 'gpt success',
29
- // intent_id: '00f93b97-89ee-466d-a09c-e47a18943057',
30
- // is_fallback: false,
31
- // confidence: undefined,
32
- // question_payload: [Object],
33
- // botId: 'botID',
34
- // bot: [Object]
35
- // },
36
- // webhook: false
37
- // }
38
- // }
39
13
  this.message = context.message;
40
- this.tdclient = context.tdclient;
41
14
  this.chatbot = context.chatbot;
42
15
  this.tdcache = context.tdcache;
43
16
  this.requestId = context.requestId;
@@ -75,8 +48,8 @@ class DirCaptureUserReply {
75
48
  // console.log("(DirCaptureUserReply) lockIntent");
76
49
  await this.chatbot.lockAction(this.requestId, actionId);
77
50
  // console.log("(DirCaptureUserReply) lockAction");
78
- let _lockedAction = await this.chatbot.currentLockedAction(this.requestId);
79
- let _lockedIntent = await this.chatbot.currentLockedIntent(this.requestId);
51
+ // let _lockedAction = await this.chatbot.currentLockedAction(this.requestId);
52
+ // let _lockedIntent = await this.chatbot.currentLockedIntent(this.requestId);
80
53
  // console.log("(DirCaptureUserReply) _lockedAction", _lockedAction)
81
54
  // console.log("(DirCaptureUserReply) _lockedIntent", _lockedIntent)
82
55
  callback();
@@ -0,0 +1,22 @@
1
+
2
+ const { TiledeskChatbotUtil } = require('../../models/TiledeskChatbotUtil');
3
+
4
+ class DirClearTranscript {
5
+
6
+ constructor(context) {
7
+ if (!context) {
8
+ throw new Error('context object is mandatory.');
9
+ }
10
+ this.context = context;
11
+ this.requestId = context.requestId;
12
+ }
13
+
14
+ execute(directive, callback) {
15
+ TiledeskChatbotUtil.clearConversationTranscript(this.context.chatbot, () => {
16
+ callback();
17
+ });
18
+ }
19
+
20
+ }
21
+
22
+ module.exports = { DirClearTranscript };
@@ -1,4 +1,7 @@
1
1
 
2
+ const { TiledeskClient } = require("@tiledesk/tiledesk-client");
3
+ const { TiledeskChatbotConst } = require("../../models/TiledeskChatbotConst");
4
+
2
5
  class DirClose {
3
6
 
4
7
  constructor(context) {
@@ -6,17 +9,27 @@ class DirClose {
6
9
  throw new Error('context object is mandatory.');
7
10
  }
8
11
  this.context = context;
9
- this.tdclient = context.tdclient;
10
12
  this.requestId = context.requestId;
13
+ this.chatbot = context.chatbot;
14
+ this.API_ENDPOINT = context.API_ENDPOINT;
15
+ this.log = context.log;
16
+
17
+ this.tdClient = new TiledeskClient({
18
+ projectId: this.context.projectId,
19
+ token: this.context.token,
20
+ APIURL: this.API_ENDPOINT,
21
+ APIKEY: "___",
22
+ log: this.log
23
+ });
11
24
  }
12
25
 
13
26
  execute(directive, callback) {
14
- this.tdclient.closeRequest(this.requestId, (err) => {
27
+ this.tdClient.closeRequest(this.requestId, async (err) => {
15
28
  if (err) {
16
29
  console.error("Error in 'close directive':", err);
17
30
  }
18
31
  else {
19
- // console.log("Successfully closed on close()");
32
+ await this.chatbot.deleteParameter(TiledeskChatbotConst.USER_INPUT);
20
33
  }
21
34
  callback();
22
35
  });
@@ -81,7 +81,7 @@ class DirCode {
81
81
  // console.log("newvars_del:", newvars_del);
82
82
  }
83
83
  catch(err) {
84
- console.error("An error occurred:", err);
84
+ console.error("(DirCode) An error occurred:", err);
85
85
  }
86
86
  callback();
87
87
  return;
@@ -10,33 +10,7 @@ class DirCondition {
10
10
  throw new Error('context object is mandatory.');
11
11
  }
12
12
  this.context = context;
13
- // let context = {
14
- // projectId: projectId,
15
- // token: token,
16
- // supportRequest: supportRequest,
17
- // requestId: supportRequest.request_id,
18
- // TILEDESK_APIURL: API_URL,
19
- // TILEBOT_ENDPOINT:TILEBOT_ENDPOINT,
20
- // departmentId: depId,
21
- // tdcache: tdcache,
22
- // log: false
23
- // }
24
- // this.tdclient = new TiledeskClient({
25
- // projectId: context.projectId,
26
- // token: context.token,
27
- // APIURL: context.TILEDESK_APIURL,
28
- // APIKEY: "___",
29
- // log: context.log
30
- // });
31
13
  this.intentDir = new DirIntent(context);
32
- // {
33
- // API_ENDPOINT: context.TILEDESK_APIURL,
34
- // TILEBOT_ENDPOINT: context.TILEBOT_ENDPOINT,
35
- // supportRequest: context.supportRequest,
36
- // token: context.token,
37
- // log: context.log
38
- // }
39
- // );
40
14
  this.log = context.log;
41
15
  }
42
16
 
@@ -0,0 +1,182 @@
1
+ let axios = require('axios');
2
+ let https = require("https");
3
+ const { v4: uuidv4 } = require('uuid');
4
+ const ms = require('minimist-string');
5
+
6
+ class DirConnectBlock {
7
+ constructor(context) {
8
+ if (!context) {
9
+ throw new Error('context object is mandatory.');
10
+ }
11
+ this.context = context;
12
+ this.API_ENDPOINT = context.API_ENDPOINT,
13
+ this.TILEBOT_ENDPOINT = context.TILEBOT_ENDPOINT;
14
+ this.supportRequest = context.supportRequest;
15
+ this.token = context.token;
16
+ this.log = context.log;
17
+ }
18
+
19
+ execute(directive, callback) {
20
+ let action;
21
+ if (directive.action) {
22
+ action = directive.action;
23
+ }
24
+ else {
25
+ console.error("Incorrect DirConnectBlock directive:", directive);
26
+ callback();
27
+ return;
28
+ }
29
+ this.go(action, () => {
30
+ callback();
31
+ });
32
+ }
33
+
34
+ go(action, callback) {
35
+ const intentName = action.intentName;
36
+ const projectId = this.supportRequest.id_project;
37
+ const requestId = this.supportRequest.request_id;
38
+ const botId = this.supportRequest.bot_id;
39
+ let intent_command;
40
+ if (intentName) {
41
+ intent_command = "/" + intentName;
42
+ }
43
+ else {
44
+ callback();
45
+ return;
46
+ }
47
+
48
+ let intent_command_request = {
49
+ "payload": {
50
+ "_id": uuidv4(),
51
+ "senderFullname": "_tdinternal",
52
+ "type": "text",
53
+ "sender": "_tdinternal",
54
+ "recipient": requestId,
55
+ "text": intent_command,
56
+ "id_project": projectId,
57
+ "request": {
58
+ "request_id": requestId,
59
+ "id_project": projectId
60
+ }
61
+ },
62
+ "token": this.token
63
+ }
64
+ if (this.log) {console.log("move to intent message:", intent_command_request);}
65
+
66
+ this.sendMessageToBot(this.TILEBOT_ENDPOINT, intent_command_request, botId, () => {
67
+ callback();
68
+ });
69
+ }
70
+
71
+ static intentDirectiveFor(intent, json_params) {
72
+ let string_params = null;
73
+ if (json_params) {
74
+ try {
75
+ string_params = JSON.stringify(json_params);
76
+ }
77
+ catch (error) {
78
+ console.error("Error stringigying JSON PARAMS", json_params);
79
+ }
80
+ }
81
+ if (string_params != null) {
82
+ intent += string_params
83
+ }
84
+ let intentDirective = {
85
+ action: {
86
+ intentName: intent
87
+ }
88
+ }
89
+ return intentDirective;
90
+ }
91
+
92
+ static fullIntentDirectiveFor(intent, json_params) {
93
+ let string_params = JSON.stringify(params);
94
+ let intentDirective = {
95
+ action: {
96
+ intentName: intent
97
+ }
98
+ }
99
+ return intentDirective;
100
+ }
101
+
102
+ /**
103
+ * A stub to send message to the "ext/botId" endpoint, hosted by tilebot on:
104
+ * /${TILEBOT_ROUTE}/ext/${botId}
105
+ *
106
+ * @param {Object} message. The message to send
107
+ * @param {string} botId. Tiledesk botId
108
+ * @param {string} token. User token
109
+ */
110
+ sendMessageToBot(TILEBOT_ENDPOINT, message, botId, callback) {
111
+ const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
112
+ // console.log("sendMessageToBot URL", url);
113
+ const HTTPREQUEST = {
114
+ url: url,
115
+ headers: {
116
+ 'Content-Type' : 'application/json'
117
+ },
118
+ json: message,
119
+ method: 'POST'
120
+ };
121
+ this.myrequest(
122
+ HTTPREQUEST,
123
+ function(err, resbody) {
124
+ if (err) {
125
+ if (callback) {
126
+ callback(err);
127
+ }
128
+ }
129
+ else {
130
+ if (callback) {
131
+ callback(null, resbody);
132
+ }
133
+ }
134
+ }, false
135
+ );
136
+ }
137
+
138
+ myrequest(options, callback, log) {
139
+ if (this.log) {
140
+ console.log("API URL:", options.url);
141
+ console.log("** Options:", JSON.stringify(options));
142
+ }
143
+ let axios_options = {
144
+ url: options.url,
145
+ method: options.method,
146
+ data: options.json,
147
+ params: options.params,
148
+ headers: options.headers
149
+ }
150
+ if (options.url.startsWith("https:")) {
151
+ const httpsAgent = new https.Agent({
152
+ rejectUnauthorized: false,
153
+ });
154
+ axios_options.httpsAgent = httpsAgent;
155
+ }
156
+ axios(axios_options)
157
+ .then((res) => {
158
+ if (this.log) {
159
+ console.log("Response for url:", options.url);
160
+ console.log("Response headers:\n", JSON.stringify(res.headers));
161
+ }
162
+ if (res && res.status == 200 && res.data) {
163
+ if (callback) {
164
+ callback(null, res.data);
165
+ }
166
+ }
167
+ else {
168
+ if (callback) {
169
+ callback(TiledeskClient.getErr({message: "Response status not 200"}, options, res), null, null);
170
+ }
171
+ }
172
+ })
173
+ .catch( (error) => {
174
+ console.error("(DirConnectBlock) Axios error: ", JSON.stringify(error));
175
+ if (callback) {
176
+ callback(error, null, null);
177
+ }
178
+ });
179
+ }
180
+ }
181
+
182
+ module.exports = { DirIntent };