@tiledesk/tiledesk-tybot-connector 0.2.28 → 0.2.30

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,13 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
+ ### v0.2.30
9
+ - Updated WhatsappByAttribute action
10
+ - Fixed bug: WhatsappByAttribute action pointed to whatsapp pre
11
+
12
+ ### v0.2.29
13
+ - Added Make action
14
+
8
15
  ### v0.2.28
9
16
  - DirSetAttribute, added support for attribute-filling in constants
10
17
  - DirSetAttribute, added function JSONparse
package/index.js CHANGED
@@ -37,7 +37,6 @@ router.post('/ext/:botid', async (req, res) => {
37
37
  console.log("Removed req.body.payload.request.snapshot field");
38
38
  }
39
39
  if (log) {console.log("REQUEST BODY:", JSON.stringify(req.body));}
40
- res.status(200).send({"success":true});
41
40
 
42
41
  const botId = req.params.botid;
43
42
  if (log) {console.log(" :", botId);}
@@ -53,6 +52,14 @@ router.post('/ext/:botid', async (req, res) => {
53
52
  if (message.request.id_project === null || message.request.id_project === undefined) {
54
53
  message.request.id_project = projectId;
55
54
  }
55
+
56
+ // let request_check = checkRequest(message.request.request_id, message.id_project);
57
+ // if (request_check === true) {
58
+ // res.status(200).send({ "successs": true });
59
+ // } else {
60
+ // return res.status(400).send({ "success": false, "message": "Invalid request_id"})
61
+ // }
62
+ res.status(200).send({"success":true});
56
63
 
57
64
  const request_botId_key = "tilebot:botId_requests:" + requestId;
58
65
  await tdcache.set(
@@ -541,4 +548,23 @@ async function connectRedis() {
541
548
  return;
542
549
  }
543
550
 
551
+ async function checkRequest(request_id, id_project) {
552
+ // TO DO CHECK
553
+
554
+ // if (request_id startsWith "support-request-{$project_id}")
555
+ // if (project_id is equal to the id_project)
556
+ // return true;
557
+ // else
558
+ // return (false, motivation)
559
+ // else if (request_id startsWith "automation-request-{$project_id}")
560
+ // if (project_id is equal to the id_project)
561
+ // return true;
562
+ // else
563
+ // return (false, motivation)
564
+ // else
565
+ // return (false, motivation);
566
+
567
+ // WARNING! Move this function in models/TiledeskChatbotUtil.js
568
+ }
569
+
544
570
  module.exports = { router: router, startApp: startApp};
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.30",
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 }
@@ -134,10 +134,23 @@ class DirSetAttributeV2 {
134
134
  attributes.TiledeskString = TiledeskString;
135
135
  const result = new TiledeskExpression().evaluateJavascriptExpression(expression, attributes);
136
136
  console.log("filling in setattribute, result:", result);
137
- await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.destination, result);
137
+ let destination = await this.fillDestination(action.destination);
138
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, destination, result);
138
139
  callback();
139
140
  }
140
141
 
142
+ async fillDestination(destination) {
143
+ if (this.tdcache) {
144
+ // console.log("tdcache in setattribute...", this.tdcache);
145
+ const requestAttributes =
146
+ await TiledeskChatbot.allParametersStatic(this.tdcache, this.context.requestId);
147
+ // console.log("requestAttributes in setattribute...", requestAttributes);
148
+ const filler = new Filler();
149
+ destination = filler.fill(destination, requestAttributes);
150
+ console.log("setattribute, final destination:", destination);
151
+ }
152
+ return destination
153
+ }
141
154
  async fillValues(operands) {
142
155
  // operation: {
143
156
  // operators: ["addAsNumber", "subtractAsNumber", "divideAsNumber", "multiplyAsNumber"],
@@ -30,65 +30,126 @@ class DirWhatsappByAttribute {
30
30
  }
31
31
 
32
32
  async go(action, callback) {
33
+
33
34
  if (this.log) {
34
35
  console.log("whatsapp by attributes action: ", JSON.stringify(action))
35
36
  }
36
37
 
37
- if (process.env.API_URL) {
38
- whatsapp_api_url = "https://tiledesk-whatsapp-connector.giovannitroisi3.repl.co/api";
39
- // whatsapp_api_url = process.env.API_URL + "/modules/whatsapp";
40
- console.log("(Tilebot) DirWhatsappByAttribute whatsapp_api_url: ", whatsapp_api_url);
38
+ ///////////
39
+
40
+ const whatsapp_api_url_pre = process.env.WHATSAPP_ENDPOINT;
41
+ const server_base_url = process.env.API_URL || process.env.API_ENDPOINT;
42
+
43
+ if (whatsapp_api_url_pre) {
44
+ whatsapp_api_url = whatsapp_api_url_pre;
41
45
  } else {
42
- console.error("(Tilebot) ERROR Missing whatsapp_api_url. Unable to use action WhatsApp By Attributes");
46
+ whatsapp_api_url = server_base_url + "/modules/whatsapp/api"
47
+ }
48
+ console.log("DirWhatsappByAttribute whatsapp_api_url: ", whatsapp_api_url);
49
+
50
+ if (!action.attributeName) {
51
+ console.error("DirWhatsappByAttribute attributeName is mandatory")
43
52
  callback();
44
53
  return;
45
54
  }
55
+ if (this.log) { console.log("DirWhatsappByAttribute attributeName: ", action.attributeName )};
46
56
 
47
- if (action.attributeName) {
48
- if (this.log) { console.log("whatsapp attributeName:", action.attributeName); }
49
- let attribute_value = null;
50
- if (this.context.tdcache) {
57
+ const attribute_value = await TiledeskChatbot.getParameterStatic(this.context.tdcache, this.context.requestId, action.attributeName)
58
+ if (this.log) { console.log("attribute_value:", JSON.stringify(attribute_value)); }
51
59
 
52
- const attribute_value = await TiledeskChatbot.getParameterStatic(this.context.tdcache, this.context.requestId, action.attributeName)
53
- if (this.log) { console.log("attribute_value:", JSON.stringify(attribute_value)); }
60
+ if (attribute_value == null) {
61
+ console.error("DirWhatsappByAttribute attribute_value is undefined");
62
+ callback();
63
+ return;
64
+ }
54
65
 
55
- if (attribute_value == null) {
56
- console.error("(Tilebot) attribute_value is undefined");
57
- callback();
58
- return;
59
- }
66
+ attribute_value.transaction_id = this.context.requestId;
60
67
 
61
- const URL = whatsapp_api_url + '/tiledesk/broadcast';
62
- const HTTPREQUEST = {
63
- url: URL,
64
- headers: {
65
- 'Content-Type': 'application/json'
66
- },
67
- json: attribute_value,
68
- method: 'POST'
69
- };
70
- let promise = new Promise((resolve, reject) => {
71
- DirWhatsappByAttribute.myrequest(
72
- HTTPREQUEST,
73
- function (err, resbody) {
74
- if (err) {
75
- if (callback) {
76
- callback(err);
77
- }
78
- reject(err);
79
- }
80
- else {
81
- if (callback) {
82
- callback(null, resbody);
83
- }
84
- console.log("(tybot) broadcast sent: ", resbody);
85
- resolve(resbody);
86
- }
87
- }, true);
88
- })
89
- return promise;
90
- }
68
+ const HTTPREQUEST = {
69
+ url: whatsapp_api_url + "/tiledesk/broadcast",
70
+ headers: {
71
+ 'Content-Type': 'application/json'
72
+ },
73
+ json: attribute_value,
74
+ method: 'POST'
91
75
  }
76
+
77
+ return new Promise((resolve, reject) => {
78
+ DirWhatsappByAttribute.myrequest(
79
+ HTTPREQUEST,
80
+ function (err, resbody) {
81
+ if (err) {
82
+ if (callback) {
83
+ callback(err);
84
+ }
85
+ reject(err);
86
+ }
87
+ else {
88
+ if (callback) {
89
+ callback(null, resbody);
90
+ }
91
+ console.log("(tybot) broadcast sent: ", resbody);
92
+ resolve(resbody);
93
+ }
94
+ }, true);
95
+ })
96
+
97
+
98
+ // if (process.env.API_URL) {
99
+ // whatsapp_api_url = "https://tiledesk-whatsapp-connector.giovannitroisi3.repl.co/api";
100
+ // // whatsapp_api_url = process.env.API_URL + "/modules/whatsapp";
101
+ // console.log("(Tilebot) DirWhatsappByAttribute whatsapp_api_url: ", whatsapp_api_url);
102
+ // } else {
103
+ // console.error("(Tilebot) ERROR Missing whatsapp_api_url. Unable to use action WhatsApp By Attributes");
104
+ // callback();
105
+ // return;
106
+ // }
107
+ // if (action.attributeName) {
108
+ // if (this.log) { console.log("whatsapp attributeName:", action.attributeName); }
109
+ // let attribute_value = null;
110
+ // if (this.context.tdcache) {
111
+
112
+ // const attribute_value = await TiledeskChatbot.getParameterStatic(this.context.tdcache, this.context.requestId, action.attributeName)
113
+ // if (this.log) { console.log("attribute_value:", JSON.stringify(attribute_value)); }
114
+
115
+ // if (attribute_value == null) {
116
+ // console.error("(Tilebot) attribute_value is undefined");
117
+ // callback();
118
+ // return;
119
+ // }
120
+
121
+ // const URL = whatsapp_api_url + '/tiledesk/broadcast';
122
+ // const HTTPREQUEST = {
123
+ // url: URL,
124
+ // headers: {
125
+ // 'Content-Type': 'application/json',
126
+
127
+ // },
128
+ // json: attribute_value,
129
+ // method: 'POST'
130
+ // };
131
+ // let promise = new Promise((resolve, reject) => {
132
+ // DirWhatsappByAttribute.myrequest(
133
+ // HTTPREQUEST,
134
+ // function (err, resbody) {
135
+ // if (err) {
136
+ // if (callback) {
137
+ // callback(err);
138
+ // }
139
+ // reject(err);
140
+ // }
141
+ // else {
142
+ // if (callback) {
143
+ // callback(null, resbody);
144
+ // }
145
+ // console.log("(tybot) broadcast sent: ", resbody);
146
+ // resolve(resbody);
147
+ // }
148
+ // }, true);
149
+ // })
150
+ // return promise;
151
+ // }
152
+ // }
92
153
  }
93
154
 
94
155
  // HTTP REQUEST
@@ -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