@tiledesk/tiledesk-tybot-connector 0.2.62-rc1 → 0.2.62-rc3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,6 +5,9 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ # v0.2.62-rc3
9
+ - Added n8n action (webrequest)
10
+
8
11
  # v0.2.62-rc1
9
12
  - Added VXML actions (speech_form, play_prompt)
10
13
 
@@ -29,6 +32,9 @@ available on:
29
32
  - added flow attribute "decodedCustomJWT"
30
33
 
31
34
  # v0.2.57
35
+ - Changed env variables for AskGPT actions
36
+ - Added support for temperature, top_k, max_tokens and context for AskGPTV2 action
37
+ - Added support for context with variables for GptTask action
32
38
  - fixed catching reply error in index.js: reply = await chatbot.replyToMessage(message);
33
39
  - Added context and top_k parameter in DirAskGPTV2
34
40
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.62-rc1",
3
+ "version": "0.2.62-rc3",
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 {
@@ -720,6 +721,25 @@ class DirectivesChatbotPlug {
720
721
  this.process(next_dir);
721
722
  })
722
723
  }
724
+ else if (directive_name === Directives.BREVO) {
725
+ new DirBrevo(context).execute(directive, async () => {
726
+ let next_dir = await this.nextDirective(this.directives);
727
+ this.process(next_dir);
728
+ })
729
+ }
730
+ else if (directive_name === Directives.N8N) {
731
+ new DirWebRequestV2(context).execute(directive, async (stop) => {
732
+ if (context.log) { console.log("stop on condition?", stop);}
733
+ if (stop == true) {
734
+ if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
735
+ this.theend();
736
+ }
737
+ else {
738
+ let next_dir = await this.nextDirective(this.directives);
739
+ this.process(next_dir);
740
+ }
741
+ });
742
+ }
723
743
  else {
724
744
  //console.log("Unhandled Post-message Directive:", directive_name);
725
745
  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,8 @@ class Directives {
39
39
  static MAKE = 'make';
40
40
  static HUBSPOT = 'hubspot';
41
41
  static CUSTOMERIO = 'customerio';
42
+ static BREVO = 'brevo';
43
+ static N8N = 'n8n';
42
44
  /**** VOICE CHANNEL ****/
43
45
  static DTMF_FORM = 'dtmf_form';
44
46
  static DTMF_MENU = 'dtmf_menu';