@tiledesk/tiledesk-tybot-connector 0.2.28 → 0.2.29

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.29
9
+ - Added Make action
10
+
8
11
  ### v0.2.28
9
12
  - DirSetAttribute, added support for attribute-filling in constants
10
13
  - DirSetAttribute, added function JSONparse
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.28",
3
+ "version": "0.2.29",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -40,6 +40,7 @@ const { DirRandomReply } = require('./directives/DirRandomReply');
40
40
  const { DirGptTask } = require('./directives/DirGptTask');
41
41
  const { DirForm } = require('./directives/DirForm');
42
42
  const { DirCaptureUserReply } = require('./directives/DirCaptureUserReply');
43
+ const { DirMake } = require('./directives/DirMake');
43
44
 
44
45
  class DirectivesChatbotPlug {
45
46
 
@@ -609,6 +610,12 @@ class DirectivesChatbotPlug {
609
610
  this.process(next_dir);
610
611
  })
611
612
  }
613
+ else if (directive_name === Directives.MAKE) {
614
+ new DirMake(context).execute(directive, async () => {
615
+ let next_dir = await this.nextDirective(this.directives);
616
+ this.process(next_dir);
617
+ })
618
+ }
612
619
  else {
613
620
  //console.log("Unhandled Post-message Directive:", directive_name);
614
621
  let next_dir = await this.nextDirective(this.directives);
@@ -0,0 +1,257 @@
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 DirMake {
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.log = context.log;
19
+ this.intentDir = new DirIntent(context);
20
+ }
21
+
22
+ execute(directive, callback) {
23
+ if (this.log) { console.log("DirMake directive: ", directive); }
24
+ let action;
25
+ if (directive.action) {
26
+ action = directive.action;
27
+ }
28
+ else {
29
+ console.error("DirMake Incorrect directive: ", JSON.stringify(directive));
30
+ callback();
31
+ return;
32
+ }
33
+ this.go(action, () => {
34
+ callback();
35
+ })
36
+ }
37
+
38
+ async go(action, callback) {
39
+ if (this.log) { console.log("DirMake action:", JSON.stringify(action)); }
40
+ if (!this.tdcache) {
41
+ console.error("Error: DirMake tdcache is mandatory");
42
+ callback();
43
+ return;
44
+ }
45
+ //console.log('DirMake work!');
46
+ let requestVariables = null;
47
+ requestVariables =
48
+ await TiledeskChatbot.allParametersStatic(
49
+ this.tdcache, this.requestId
50
+ )
51
+
52
+ if (this.log) {
53
+ const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
54
+ for (const [key, value] of Object.entries(all_parameters)) {
55
+ if (this.log) { console.log("DirMake request parameter:", key, "value:", value, "type:", typeof value) }
56
+ }
57
+ }
58
+
59
+ let webhook_url = action.url;
60
+ let bodyParameters = action.bodyParameters;
61
+ if (this.log) {
62
+ console.log("DirMake webhook_url: ", webhook_url);
63
+ console.log("DirMake bodyParameters: ", bodyParameters);
64
+ }
65
+ if (!bodyParameters || bodyParameters === '') {
66
+ console.error("DirMake ERROR - bodyParameters is undefined or null or empty string");
67
+ callback();
68
+ return;
69
+ }
70
+ if (!webhook_url || webhook_url === '') {
71
+ console.error("DirMake ERROR - webhook_url is undefined or null or empty string");
72
+ callback();
73
+ return;
74
+ }
75
+ let url;
76
+ try {
77
+ console.log('CIAOkkkk',bodyParameters)
78
+ let make_base_url = process.env.MAKE_ENDPOINT;
79
+ if (make_base_url) {
80
+ url = make_base_url + "/make/";
81
+ } else {
82
+ url = action.url;
83
+ }
84
+ console.log('Make url: ', url);
85
+ const filler = new Filler();
86
+
87
+
88
+ for (const [key, value] of Object.entries(bodyParameters)) {
89
+ if (this.log) {console.log("bodyParam:", key, "value:", value)}
90
+ let filled_value = filler.fill(value, requestVariables);
91
+ bodyParameters[key] = filled_value;
92
+ }
93
+
94
+ console.log('CIAO',bodyParameters)
95
+
96
+
97
+
98
+
99
+ // Condition branches
100
+ let trueIntent = action.trueIntent;
101
+ let falseIntent = action.falseIntent;
102
+ console.log('trueIntent',trueIntent)
103
+
104
+ if (this.log) { console.log("DirMake MakeEndpoint URL: ", make_base_url); }
105
+ const MAKE_HTTPREQUEST = {
106
+ url: url,
107
+ headers: {
108
+ 'Content-Type': 'application/json'
109
+ },
110
+ json: bodyParameters,
111
+ method: "POST"
112
+ }
113
+
114
+ if (this.log) { console.log("myrequest/DirMake MAKE_HTTPREQUEST", MAKE_HTTPREQUEST); }
115
+ this.#myrequest(
116
+ MAKE_HTTPREQUEST, async (err, resbody) => {
117
+ if (err) {
118
+ if (callback) {
119
+ console.error("myrequest/(httprequest) DirMake make err:", err);
120
+ let status = 404;
121
+ let error = 'Not found';
122
+ await this.#assignAttributes(action, status, error);
123
+ this.#executeCondition(false, trueIntent, null, falseIntent, null, () => {
124
+ callback(false); // continue the flow
125
+ });
126
+ callback();
127
+ }
128
+ } else if (callback) {
129
+ if (this.log) { console.log("myrequest/DirMake Make resbody: ", resbody); }
130
+ let status = 200;
131
+ let error = null;
132
+ await this.#assignAttributes(action, status, error);
133
+ await this.#executeCondition(true, trueIntent, null, falseIntent, null, () => {
134
+ callback(); // stop the flow
135
+ });
136
+ console.log('myrequest/status: ',status)
137
+ callback();
138
+ }
139
+ }
140
+ );
141
+ } catch(e) {
142
+ console.error('error: ', e)
143
+ }
144
+ }
145
+
146
+ async #assignAttributes(action, status, error) {
147
+ if (this.log) {
148
+ console.log("DirMake assignAttributes action:", action)
149
+ console.log("DirMake assignAttributes status:", status)
150
+ console.log("DirMake assignAttributes error:", error)
151
+ }
152
+ if (this.context.tdcache) {
153
+ if (action.assignStatusTo) {
154
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignStatusTo, status);
155
+ }
156
+ if (action.assignErrorTo) {
157
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignErrorTo, error);
158
+ }
159
+
160
+ // Debug log
161
+ if (this.log) {
162
+ const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
163
+ for (const [key, value] of Object.entries(all_parameters)) {
164
+ if (this.log) { console.log("DirMake request parameter:", key, "value:", value, "type:", typeof value) }
165
+ }
166
+ }
167
+ }
168
+ }
169
+
170
+ #myrequest(options, callback) {
171
+ if (this.log) {
172
+ console.log("API URL:", options.url);
173
+ console.log("** Options:", JSON.stringify(options));
174
+ }
175
+ let axios_options = {
176
+ url: options.url,
177
+ method: options.method,
178
+ params: options.params,
179
+ headers: options.headers
180
+ }
181
+ if (options.json !== null) {
182
+ axios_options.data = options.json
183
+ }
184
+ if (this.log) {
185
+ console.log("axios_options:", JSON.stringify(axios_options));
186
+ }
187
+ if (options.url.startsWith("https:")) {
188
+ const httpsAgent = new https.Agent({
189
+ rejectUnauthorized: false,
190
+ });
191
+ axios_options.httpsAgent = httpsAgent;
192
+ }
193
+ axios(axios_options)
194
+ .then((res) => {
195
+ if (this.log) {
196
+ console.log("Response for url:", options.url);
197
+ console.log("Response status:", res.status);
198
+ console.log("Response headers:\n", JSON.stringify(res.headers));
199
+ }
200
+ if (res && res.status == 200 && res.data) {
201
+ if (callback) {
202
+ callback(null, res.data);
203
+ }
204
+ }
205
+ else {
206
+ if (callback) {
207
+ callback(new Error("Response status is not 200"), null);
208
+ }
209
+ }
210
+ })
211
+ .catch((error) => {
212
+ // console.error("An error occurred:", JSON.stringify(error.data));
213
+ if (callback) {
214
+ callback(error, null);
215
+ }
216
+ });
217
+ }
218
+ async #executeCondition(result, trueIntent, trueIntentAttributes, falseIntent, falseIntentAttributes, callback) {
219
+ let trueIntentDirective = null;
220
+
221
+ if (trueIntent) {
222
+ //console.log('executeCondition/trueIntent',trueIntent)
223
+ trueIntentDirective = DirIntent.intentDirectiveFor(trueIntent, trueIntentAttributes);
224
+ //console.log('executeCondition/trueIntentDirective',trueIntentDirective)
225
+ //console.log('executeCondition/trueIntentAttributes',trueIntentAttributes)
226
+ }
227
+ let falseIntentDirective = null;
228
+ if (falseIntent) {
229
+ falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
230
+ }
231
+ if (this.log) {console.log('DirMake executeCondition/result',result)}
232
+ if (result === true) {
233
+ if (trueIntentDirective) {
234
+ this.intentDir.execute(trueIntentDirective, () => {
235
+ callback();
236
+ });
237
+ }
238
+ else {
239
+ if (this.log) {console.log("No trueIntentDirective specified");}
240
+ callback();
241
+ }
242
+ }
243
+ else {
244
+ if (falseIntentDirective) {
245
+ this.intentDir.execute(falseIntentDirective, () => {
246
+ callback();
247
+ });
248
+ }
249
+ else {
250
+ if (this.log) {console.log("No falseIntentDirective specified");}
251
+ callback();
252
+ }
253
+ }
254
+ }
255
+ }
256
+
257
+ module.exports = { DirMake }
@@ -32,6 +32,7 @@ class Directives {
32
32
  static FORM = "form";
33
33
  static CAPTURE_USER_REPLY = "capture_user_reply";
34
34
  static QAPLA = 'qapla';
35
+ static MAKE = 'make';
35
36
 
36
37
  // static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
37
38
  // static WHEN_OFFLINE_HOURS = "whenofflinehours"; // DEPRECATED // adds a message on top of the original message when offline hours opts: --replace