@tiledesk/tiledesk-tybot-connector 0.2.37 → 0.2.38

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,9 +5,12 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ # v0.2.38
9
+ - improved Qapla' action with condition
10
+
8
11
  # v0.2.37
9
- - improved GptTask action
10
- - improved AskGPT action
12
+ - improved GptTask action with condition
13
+ - improved AskGPT action with condition
11
14
 
12
15
  # v0.2.36
13
16
  - Fixes json conversion in GptTask issue
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.37",
3
+ "version": "0.2.38",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -619,9 +619,16 @@ class DirectivesChatbotPlug {
619
619
  });
620
620
  }
621
621
  else if (directive_name === Directives.QAPLA) {
622
- new DirQapla(context).execute(directive, async () => {
623
- let next_dir = await this.nextDirective(this.directives);
624
- this.process(next_dir);
622
+ new DirQapla(context).execute(directive, async (stop) => {
623
+ if (context.log) { console.log("DirQapla stop?", stop);}
624
+ if (stop == true) {
625
+ if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
626
+ this.theend();
627
+ }
628
+ else {
629
+ let next_dir = await this.nextDirective(this.directives);
630
+ this.process(next_dir);
631
+ }
625
632
  })
626
633
  }
627
634
  else if (directive_name === Directives.MAKE) {
@@ -2,6 +2,7 @@ const axios = require("axios").default;
2
2
  const { TiledeskChatbot } = require("../../models/TiledeskChatbot");
3
3
  const { Filler } = require("../Filler");
4
4
  let https = require("https");
5
+ const { DirIntent } = require("./DirIntent");
5
6
  require('dotenv').config();
6
7
 
7
8
  class DirQapla {
@@ -13,6 +14,7 @@ class DirQapla {
13
14
  this.context = context;
14
15
  this.tdcache = this.context.tdcache;
15
16
  this.requestId = this.context.requestId;
17
+ this.intentDir = new DirIntent(context);
16
18
  this.log = context.log;
17
19
  }
18
20
 
@@ -27,8 +29,8 @@ class DirQapla {
27
29
  callback();
28
30
  return;
29
31
  }
30
- this.go(action, () => {
31
- callback();
32
+ this.go(action, (stop) => {
33
+ callback(stop);
32
34
  })
33
35
  }
34
36
 
@@ -40,34 +42,44 @@ class DirQapla {
40
42
  return;
41
43
  }
42
44
 
45
+ let trueIntent = action.trueIntent;
46
+ let falseIntent = action.falseIntent;
47
+ let trueIntentAttributes = action.trueIntentAttributes;
48
+ let falseIntentAttributes = action.falseIntentAttributes;
49
+
50
+ if (this.log) {
51
+ console.log("DirAskGPT trueIntent", trueIntent)
52
+ console.log("DirAskGPT falseIntent", falseIntent)
53
+ console.log("DirAskGPT trueIntentAttributes", trueIntentAttributes)
54
+ console.log("DirAskGPT falseIntentAttributes", falseIntentAttributes)
55
+ }
56
+
57
+ // let default values??
58
+ let status = null;
59
+ let result = null;
60
+ let error;
61
+
43
62
  let requestVariables = null;
44
63
  requestVariables =
45
64
  await TiledeskChatbot.allParametersStatic(
46
65
  this.tdcache, this.requestId
47
66
  )
48
67
 
49
- if (this.log) {
50
- const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
51
- for (const [key, value] of Object.entries(all_parameters)) {
52
- if (this.log) { console.log("DirQapla request parameter:", key, "value:", value, "type:", typeof value) }
53
- }
54
- }
55
-
56
68
  const filler = new Filler();
57
69
  const tracking_number = filler.fill(action.trackingNumber, requestVariables);
58
70
  if (this.log) { console.log("DirQapla tracking number: ", tracking_number); }
59
71
 
60
72
  if (!tracking_number || tracking_number === '') {
61
- console.error("DirQapla ERROR - tracking number is undefined or null or empty string");
62
- callback();
63
- }
64
-
65
- const key = action.apiKey;
66
- console.log("action.key: ", action.apiKey)
67
-
68
- if (!key) {
69
- console.error("DirQapla ERROR - key is undefined");
73
+ console.error("Error: DirQapla tracking number is undefined or null or empty string");
74
+ error = "Tracking number is not defined";
75
+ await this.#assignAttributes(action, status, result, error);
76
+ if (falseIntent) {
77
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
78
+ callback(true);
79
+ return;
80
+ }
70
81
  callback();
82
+ return;
71
83
  }
72
84
 
73
85
  const server_base_url = process.env.API_ENDPOINT || process.env.API_URL;
@@ -76,6 +88,27 @@ class DirQapla {
76
88
  console.log("DirQapla server_base_url: ", qapla_base_url);
77
89
  console.log("DirQapla qapla_base_url: ", qapla_base_url);
78
90
  }
91
+
92
+ let key = action.apiKey;
93
+
94
+ if (!key) {
95
+ if (this.log) { console.log("DirQapla - Key not found into action. Searching in integrations..."); }
96
+ key = await this.getKeyFromIntegrations(server_base_url);
97
+ }
98
+
99
+ if (!key) {
100
+ console.error("Error: DirQapla api key is mandatory");
101
+ error = "Invalid or empty ApiKey";
102
+ await this.#assignAttributes(action, status, result, error);
103
+ if (falseIntent) {
104
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
105
+ callback(true);
106
+ return;
107
+ }
108
+ callback();
109
+ return;
110
+ }
111
+
79
112
  const QAPLA_HTTPREQUEST = {
80
113
  url: qapla_base_url + "/getShipment/",
81
114
  headers: {
@@ -94,14 +127,18 @@ class DirQapla {
94
127
  if (err) {
95
128
  if (callback) {
96
129
  console.error("(httprequest) DirQapla getShipment err:", err);
130
+ error = "Unable to get shipment";
131
+ await this.#assignAttributes(action, status, result, error);
132
+ if (falseIntent) {
133
+ await this.#executeCondition(false, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
134
+ callback(true);
135
+ return;
136
+ }
97
137
  callback();
138
+ return;
98
139
  }
99
140
  } else if (callback) {
100
- if (this.log) { console.log("DirQapla getShipment resbody: ", resbody); }
101
-
102
- let status = null;;
103
- let result;
104
- let error;
141
+ if (this.log) { console.log("DirQapla getShipment resbody: ", JSON.stringify(resbody, null, 2)); }
105
142
 
106
143
  if (resbody.getShipment &&
107
144
  resbody.getShipment.shipments &&
@@ -111,16 +148,66 @@ class DirQapla {
111
148
  resbody.getShipment.shipments[0].status.qaplaStatus.status) {
112
149
  status = resbody.getShipment.shipments[0].status.qaplaStatus.status;
113
150
  }
151
+ // status = resbody.getShipment?.shipments[0]?.status?.qaplaStatus?.status; // doesn't works
152
+
153
+ if (resbody.getShipment &&
154
+ resbody.getShipment.result)
155
+ result = resbody.getShipment?.result;
156
+ error = resbody.getShipment?.error;
114
157
 
115
- result = resbody.getShipment.result;
116
- error = resbody.getShipment.error;
117
158
  await this.#assignAttributes(action, status, result, error);
159
+ if (trueIntent) {
160
+ await this.#executeCondition(true, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes);
161
+ callback(true);
162
+ return;
163
+ }
118
164
  callback();
165
+ return;
119
166
  }
120
167
  }
121
168
  )
122
169
  }
123
170
 
171
+ async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
172
+ let trueIntentDirective = null;
173
+ if (trueIntent) {
174
+ trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
175
+ }
176
+ let falseIntentDirective = null;
177
+ if (falseIntent) {
178
+ falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
179
+ }
180
+ if (result === true) {
181
+ if (trueIntentDirective) {
182
+ this.intentDir.execute(trueIntentDirective, () => {
183
+ if (callback) {
184
+ callback();
185
+ }
186
+ })
187
+ }
188
+ else {
189
+ if (this.log) { console.log("No trueIntentDirective specified"); }
190
+ if (callback) {
191
+ callback();
192
+ }
193
+ }
194
+ }
195
+ else {
196
+ if (falseIntentDirective) {
197
+ this.intentDir.execute(falseIntentDirective, () => {
198
+ if (callback) {
199
+ callback();
200
+ }
201
+ });
202
+ }
203
+ else {
204
+ if (this.log) { console.log("No falseIntentDirective specified"); }
205
+ if (callback) {
206
+ callback();
207
+ }
208
+ }
209
+ }
210
+ }
124
211
 
125
212
  async #assignAttributes(action, status, result, error) {
126
213
  if (this.log) {
@@ -197,6 +284,38 @@ class DirQapla {
197
284
  }
198
285
  });
199
286
  }
287
+
288
+ async getKeyFromIntegrations(server_base_url) {
289
+ return new Promise((resolve) => {
290
+
291
+ const INTEGRATIONS_HTTPREQUEST = {
292
+ url: server_base_url + "/" + this.context.projectId + "/integration/name/openai",
293
+ headers: {
294
+ 'Content-Type': 'application/json',
295
+ 'Authorization': 'JWT ' + this.context.token
296
+ },
297
+ method: "GET"
298
+ }
299
+ if (this.log) { console.log("DirGptTask INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
300
+
301
+ this.#myrequest(
302
+ INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
303
+ if (err) {
304
+ resolve(null);
305
+ } else {
306
+
307
+ if (integration &&
308
+ integration.value) {
309
+ resolve(integration.value.apikey)
310
+ }
311
+ else {
312
+ resolve(null)
313
+ }
314
+ }
315
+ })
316
+ })
317
+ }
318
+
200
319
  }
201
320
 
202
321
  module.exports = { DirQapla }