@tiledesk/tiledesk-tybot-connector 0.2.58-rc1 → 0.2.58-rc2

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
@@ -7,6 +7,10 @@ available on:
7
7
 
8
8
  # v0.2.58 (next-release)
9
9
  - Added support for DTMF Menu (Reply for voice channel)
10
+ - Added Brevo Action
11
+
12
+ # v0.2.58-rc2
13
+ - Added Brevo Action
10
14
 
11
15
  # v0.2.58-rc1
12
16
  - Added support for DTMF Menu (Reply for voice channel)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.58-rc1",
3
+ "version": "0.2.58-rc2",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -44,6 +44,7 @@ const { DirMake } = require('./directives/DirMake');
44
44
  const { DirReplaceBotV2 } = require('./directives/DirReplaceBotV2');
45
45
  const { DirHubspot } = require('./directives/DirHubspot');
46
46
  const { DirCustomerio } = require('./directives/DirCustomerio');
47
+ const { DirBrevo } = require('./directives/DirBrevo');
47
48
  const { DirAskGPTV2 } = require('./directives/DirAskGPTV2');
48
49
 
49
50
  class DirectivesChatbotPlug {
@@ -706,6 +707,12 @@ class DirectivesChatbotPlug {
706
707
  this.process(next_dir);
707
708
  })
708
709
  }
710
+ else if (directive_name === Directives.BREVO) {
711
+ new DirBrevo(context).execute(directive, async () => {
712
+ let next_dir = await this.nextDirective(this.directives);
713
+ this.process(next_dir);
714
+ })
715
+ }
709
716
  else {
710
717
  //console.log("Unhandled Post-message Directive:", directive_name);
711
718
  let next_dir = await this.nextDirective(this.directives);
@@ -0,0 +1,356 @@
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.log = context.log;
20
+ }
21
+
22
+ execute(directive, callback) {
23
+ if (this.log) { console.log("DirBrevo directive: ", directive); }
24
+ let action;
25
+ if (directive.action) {
26
+ action = directive.action;
27
+ }
28
+ else {
29
+ console.error("DirBrevo Incorrect directive: ", JSON.stringify(directive));
30
+ callback();
31
+ return;
32
+ }
33
+ this.go(action, (stop) => {
34
+ callback(stop);
35
+ })
36
+ }
37
+
38
+ async go(action, callback) {
39
+ if (this.log) { console.log("DirBrevo action:", JSON.stringify(action)); }
40
+ if (!this.tdcache) {
41
+ console.error("Error: DirBrevo tdcache is mandatory");
42
+ callback();
43
+ return;
44
+ }
45
+
46
+ let trueIntent = action.trueIntent;
47
+ let falseIntent = action.falseIntent;
48
+ let trueIntentAttributes = action.trueIntentAttributes;
49
+ let falseIntentAttributes = action.falseIntentAttributes;
50
+
51
+ if (this.log) {
52
+ console.log("DirBrevo trueIntent", trueIntent)
53
+ console.log("DirBrevo falseIntent", falseIntent)
54
+ console.log("DirBrevo trueIntentAttributes", trueIntentAttributes)
55
+ console.log("DirBrevo falseIntentAttributes", falseIntentAttributes)
56
+ }
57
+
58
+ let requestVariables = null;
59
+ requestVariables =
60
+ await TiledeskChatbot.allParametersStatic(
61
+ this.tdcache, this.requestId
62
+ )
63
+
64
+ //let token = action.token;
65
+ let bodyParameters = action.bodyParameters;
66
+ if (this.log) { console.log("DirBrevo bodyParameters: ", bodyParameters); }
67
+
68
+ if (!bodyParameters || bodyParameters === '') {
69
+ if (this.log) { console.error("DirBrevo ERROR - bodyParameters is undefined or null or empty string") };
70
+ callback();
71
+ return;
72
+ }
73
+
74
+ const server_base_url = process.env.API_ENDPOINT || process.env.API_URL;
75
+ const brevo_base_url = process.env.BREVO_ENDPOINT || "https://api.brevo.com/v3"
76
+ if (this.log) {
77
+ console.log("DirBrevo server_base_url ", server_base_url);
78
+ console.log("DirBrevo brevo_base_url ", brevo_base_url);
79
+ }
80
+
81
+ let key = await this.getKeyFromIntegrations(server_base_url);
82
+ if (this.log) { console.log('DirBrevo key Debug1: ', key) }
83
+ // ONLY FOR DEBUG CANCELLARE!!!!!
84
+ // if (process.env.BREVO_DEBUG == '1') {
85
+ // key = process.env.BREVO_TOKEN;
86
+ // }
87
+ if (!key) {
88
+ if (this.log) { console.log("DirBrevo - Key not found in Integrations."); }
89
+ if (falseIntent) {
90
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
91
+ callback(true);
92
+ return;
93
+ }
94
+ }
95
+
96
+ const filler = new Filler();
97
+ for (const [key, value] of Object.entries(bodyParameters)) {
98
+ if (this.log) { console.log("bodyParam:", key, "value:", value) }
99
+ let filled_value = filler.fill(value, requestVariables);
100
+ bodyParameters[key] = filled_value;
101
+ }
102
+ if (this.log) { console.log('DirBrevo bodyParameters filler: ', bodyParameters) }
103
+
104
+ // CREATE THE JSON FOR BREVO
105
+ let brevo_email = '';
106
+ let brevo_bodyParameters = {};
107
+ for (const [key, value] of Object.entries(bodyParameters)) {
108
+ if (this.log) { console.log("bodyParam:", key, "value:", value) }
109
+ if (key === 'email') {brevo_email = value}
110
+ else { brevo_bodyParameters[key] = value;}
111
+ }
112
+ if (this.log) { console.log('DirBrevo brevo_email: ', brevo_email) }
113
+ if (this.log) { console.log('DirBrevo brevo_bodyParameters: ', brevo_bodyParameters) }
114
+
115
+
116
+ let json = {
117
+ email: brevo_email,
118
+ attributes: brevo_bodyParameters,
119
+ "emailBlacklisted": false,
120
+ "smsBlacklisted": false,
121
+ "listIds": [
122
+ 0
123
+ ],
124
+ "updateEnabled": false,
125
+ "smtpBlacklistSender": [
126
+ "info@mytest.com"
127
+ ]
128
+ }
129
+ if (this.log) { console.log('DirBrevo key Debug2: ', key) }
130
+ //----------------
131
+ if (this.log) {console.log("DirBrevo brevo_base_url ",brevo_base_url);}
132
+ if (this.log) { console.log('DirBrevo json: ', json) }
133
+ const BREVO_HTTPREQUEST = {
134
+ url: brevo_base_url + '/contacts',
135
+ headers: {
136
+ 'api-key': key,
137
+ 'Content-Type': 'application/json',
138
+ 'Accept': 'application/json'
139
+ },
140
+ json: json,
141
+ method: "POST"
142
+ }
143
+ if (this.log) { console.log("DirBrevo BREVO_HTTPREQUEST", JSON.stringify(BREVO_HTTPREQUEST)); }
144
+
145
+ this.#myrequest(
146
+ BREVO_HTTPREQUEST, async (err, resbody) => {
147
+ if (err) {
148
+ if (callback) {
149
+ if (this.log) {
150
+ console.error("(httprequest) DirBrevo err response:", err.response)
151
+ console.error("(httprequest) DirBrevo err data:", err.response.data)
152
+ };
153
+
154
+ let result = null;
155
+ let status = null;
156
+ let error;
157
+
158
+ if (err.response &&
159
+ err.response.status) {
160
+ status = err.response.status;
161
+ }
162
+
163
+ if (err.response &&
164
+ err.response.data &&
165
+ err.response.data.message) {
166
+ error = err.response.data.message;
167
+ }
168
+
169
+ if (this.log) {
170
+ console.error("(httprequest) DirBrevo err data result:", result); // CONTROLLA IL VALORE
171
+ console.error("(httprequest) DirBrevo err data status:", status);
172
+ console.error("(httprequest) DirBrevo err data error:", error);
173
+ }
174
+
175
+ await this.#assignAttributes(action, status, result, error);
176
+ if (falseIntent) {
177
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
178
+ callback(true);
179
+ return;
180
+ }
181
+ callback();
182
+ return;
183
+ }
184
+ } else if (callback) {
185
+ if (this.log) { console.log("DirBrevo resbody: ", JSON.stringify(resbody, null, 2).slice(2, -1)); }
186
+
187
+ let status = 201;
188
+ let error = null;
189
+ let result = JSON.stringify(resbody, null, 2).slice(2, -1);
190
+ await this.#assignAttributes(action, status, result, error);
191
+ if (trueIntent) {
192
+ await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes)
193
+ callback(true);
194
+ return;
195
+ }
196
+ callback();
197
+ return;
198
+ }
199
+ }
200
+ );
201
+
202
+ }
203
+
204
+ async #assignAttributes(action, status, result, error) {
205
+ if (this.log) {
206
+ console.log("DirBrevo assignAttributes action:", action)
207
+ console.log("DirBrevo assignAttributes status:", status)
208
+ console.log("DirBrevo assignAttributes result:", result)
209
+ console.log("DirBrevo assignAttributes error:", error)
210
+ }
211
+ if (this.context.tdcache) {
212
+ if (action.assignStatusTo) {
213
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignStatusTo, status);
214
+ }
215
+ if (action.assignResultTo) {
216
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignResultTo, result);
217
+ }
218
+ if (action.assignErrorTo) {
219
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignErrorTo, error);
220
+ }
221
+
222
+ // Debug log
223
+ if (this.log) {
224
+ const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
225
+ for (const [key, value] of Object.entries(all_parameters)) {
226
+ if (this.log) { console.log("DirBrevo request parameter:", key, "value:", value, "type:", typeof value) }
227
+ }
228
+ }
229
+ }
230
+ }
231
+
232
+ #myrequest(options, callback) {
233
+ if (this.log) {
234
+ console.log("** API URL:", options.url);
235
+ console.log("** Options:", JSON.stringify(options));
236
+ }
237
+ let axios_options = {
238
+ url: options.url,
239
+ method: options.method,
240
+ params: options.params,
241
+ headers: options.headers
242
+ }
243
+ if (options.json !== null) {
244
+ axios_options.data = options.json
245
+ }
246
+ if (this.log) {
247
+ console.log("axios_options:", JSON.stringify(axios_options));
248
+ }
249
+ if (options.url.startsWith("https:")) {
250
+ const httpsAgent = new https.Agent({
251
+ rejectUnauthorized: false,
252
+ });
253
+ axios_options.httpsAgent = httpsAgent;
254
+ }
255
+ axios(axios_options)
256
+ .then((res) => {
257
+ if (this.log) {
258
+ console.log("Response for url:", options.url);
259
+ console.log("Response headers:\n", JSON.stringify(res.headers));
260
+ console.log("Response status:", JSON.stringify(res.status));
261
+ }
262
+ if (res && (res.status == 200 || res.status == 201) && res.data) {
263
+ if (callback) {
264
+ callback(null, res.data);
265
+ }
266
+ }
267
+ else {
268
+ if (callback) {
269
+ callback(new Error("Response status is not 200"), null);
270
+ }
271
+ }
272
+ })
273
+ .catch((error) => {
274
+ if (callback) {
275
+ callback(error, null);
276
+ }
277
+ });
278
+ }
279
+
280
+ async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
281
+ let trueIntentDirective = null;
282
+
283
+ if (trueIntent) {
284
+ trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
285
+ }
286
+ let falseIntentDirective = null;
287
+ if (falseIntent) {
288
+ falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
289
+ }
290
+ if (this.log) { console.log('DirBrevo executeCondition/result', result) }
291
+ if (result === true) {
292
+ if (trueIntentDirective) {
293
+ this.intentDir.execute(trueIntentDirective, () => {
294
+ if (callback) {
295
+ callback();
296
+ }
297
+ });
298
+ }
299
+ else {
300
+ if (this.log) { console.log("No trueIntentDirective specified"); }
301
+ if (callback) {
302
+ callback();
303
+ }
304
+ }
305
+ }
306
+ else {
307
+ if (falseIntentDirective) {
308
+ this.intentDir.execute(falseIntentDirective, () => {
309
+ if (callback) {
310
+ callback();
311
+ }
312
+ });
313
+ }
314
+ else {
315
+ if (this.log) { console.log("No falseIntentDirective specified"); }
316
+ if (callback) {
317
+ callback();
318
+ }
319
+ }
320
+ }
321
+ }
322
+
323
+ async getKeyFromIntegrations(server_base_url) {
324
+ return new Promise((resolve) => {
325
+
326
+ const INTEGRATIONS_HTTPREQUEST = {
327
+ url: server_base_url + "/" + this.context.projectId + "/integration/name/Brevo",
328
+ headers: {
329
+ 'Content-Type': 'application/json',
330
+ 'Authorization': 'JWT ' + this.context.token
331
+ },
332
+ method: "GET"
333
+ }
334
+ if (this.log) { console.log("Brevo INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
335
+
336
+ this.#myrequest(
337
+ INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
338
+ if (err) {
339
+ resolve(null);
340
+ } else {
341
+
342
+ if (integration &&
343
+ integration.value) {
344
+ resolve(integration.value.apikey)
345
+ }
346
+ else {
347
+ resolve(null)
348
+ }
349
+ }
350
+ })
351
+ })
352
+ }
353
+
354
+ }
355
+
356
+ module.exports = { DirBrevo }
@@ -39,6 +39,7 @@ class Directives {
39
39
  static MAKE = 'make';
40
40
  static HUBSPOT = 'hubspot';
41
41
  static CUSTOMERIO = 'customerio';
42
+ static BREVO = 'brevo';
42
43
  /**** VOICE CHANNEL ****/
43
44
  static DTMF_FORM = 'dtmf_form';
44
45
  static DTMF_MENU = 'dtmf_menu';