@tiledesk/tiledesk-tybot-connector 0.2.29 → 0.2.31

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.31
9
+ - Removed patch: Added support for removing empty text from replies: TiledeskChatbotUtil.removeEmptyReplyCommands()
10
+
11
+ ### v0.2.30
12
+ - Updated WhatsappByAttribute action
13
+ - Fixed bug: WhatsappByAttribute action pointed to whatsapp pre
14
+
8
15
  ### v0.2.29
9
16
  - Added Make action
10
17
 
@@ -234,8 +234,8 @@ class TiledeskExpression {
234
234
  }
235
235
 
236
236
  evaluateJavascriptExpression(expression, context) {
237
- console.log("(evaluateJavascriptExpression) evaluating:", expression)
238
- console.log("context:", context)
237
+ // console.log("(evaluateJavascriptExpression) evaluating:", expression)
238
+ // console.log("context:", context)
239
239
  let res = null;
240
240
  try {
241
241
  const vm = new VM({
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};
@@ -80,7 +80,7 @@ class TiledeskChatbot {
80
80
  if (this.log) {console.log("RESETTING LOCKED INTENT. Intent was explicitly invoked with an action:", message.attributes.action);}
81
81
  await this.unlockIntent(this.requestId);
82
82
  await this.unlockAction(this.requestId);
83
- console.log("RESET LOCKED INTENT.");
83
+ // console.log("RESET LOCKED INTENT.");
84
84
  if (this.log) {console.log("RESET LOCKED INTENT. Intent was explicitly invoked with an action:", message.attributes.action);}
85
85
  }
86
86
  } catch(error) {
@@ -296,7 +296,7 @@ class TiledeskChatbot {
296
296
  }
297
297
  if (this.log) {console.log("NLP intents found:", intents);}
298
298
  if (intents && intents.length > 0) {
299
- console.log("Matching intents found.");
299
+ // console.log("Matching intents found.");
300
300
  // let faq = await this.botsDataSource.getByIntentDisplayName(this.botId, intents[0].intent_display_name);
301
301
  let faq = await this.botsDataSource.getByIntentDisplayNameCache(this.botId, intents[0].intent_display_name, this.tdcache);
302
302
  let reply;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.29",
3
+ "version": "0.2.31",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -74,14 +74,12 @@ class DirMake {
74
74
  }
75
75
  let url;
76
76
  try {
77
- console.log('CIAOkkkk',bodyParameters)
78
77
  let make_base_url = process.env.MAKE_ENDPOINT;
79
78
  if (make_base_url) {
80
79
  url = make_base_url + "/make/";
81
80
  } else {
82
81
  url = action.url;
83
82
  }
84
- console.log('Make url: ', url);
85
83
  const filler = new Filler();
86
84
 
87
85
 
@@ -97,11 +97,13 @@ class DirReply {
97
97
  }
98
98
  }
99
99
  // send!
100
- const cleanMessage = TiledeskChatbotUtil.removeEmptyReplyCommands(message);
101
- if (!TiledeskChatbotUtil.isValidReply(cleanMessage)) {
102
- console.log("invalid message", cleanMessage);
103
- callback(); // cancel reply operation
104
- }
100
+ let cleanMessage = message;
101
+ // cleanMessage = TiledeskChatbotUtil.removeEmptyReplyCommands(message);
102
+ // if (!TiledeskChatbotUtil.isValidReply(cleanMessage)) {
103
+ // console.log("invalid message", cleanMessage);
104
+ // callback(); // cancel reply operation
105
+ // return;
106
+ // }
105
107
  // console.log("valid message!", cleanMessage);
106
108
  cleanMessage.senderFullname = this.context.chatbot.bot.name;
107
109
  if (this.log) {console.log("Reply:", JSON.stringify(cleanMessage))};
@@ -128,86 +130,8 @@ class DirReply {
128
130
  }
129
131
  });
130
132
 
131
- // this.sendSupportMessage(
132
- // this.requestId,
133
- // message,
134
- // (err) => {
135
- // if (err) {
136
- // console.error("Error sending reply:", err);
137
- // }
138
- // // if (this.log) {console.log("Reply message sent.");}
139
- // console.log("Reply message sent.", JSON.stringify(message));
140
- // callback();
141
- // }
142
- // );
143
133
  }
144
134
 
145
- // sendSupportMessage(requestId, message, callback) {
146
- // const url = `${this.context.tdclient.APIURL}/${this.projectId}/requests/${requestId}/messages`
147
- // const HTTPREQUEST = {
148
- // url: url,
149
- // headers: {
150
- // 'Content-Type' : 'application/json',
151
- // 'Authorization': "JWT " + this.token
152
- // },
153
- // json: message,
154
- // method: 'POST'
155
- // };
156
- // this.myrequest(
157
- // HTTPREQUEST,
158
- // function(err, resbody) {
159
- // if (err) {
160
- // if (callback) {
161
- // callback(err);
162
- // }
163
- // }
164
- // else {
165
- // if (callback) {
166
- // callback(null, resbody);
167
- // }
168
- // }
169
- // }, this.log
170
- // );
171
- // }
172
-
173
- // myrequest(options, callback, log) {
174
- // console.log("API URL:", options.url);
175
- // console.log("** Options:", JSON.stringify(options));
176
- // console.log("** Sending reply json:", JSON.stringify(options.json));
177
- // axios(
178
- // {
179
- // url: options.url,
180
- // method: options.method,
181
- // data: options.json,
182
- // params: options.params,
183
- // headers: options.headers
184
- // })
185
- // .then((res) => {
186
- // console.log("Reply: Response for url:", options.url);
187
- // console.log("Reply: Response headers:\n", JSON.stringify(res.headers));
188
- // console.log("Reply: Status:", res.status);
189
- // console.log("Reply: Data:", JSON.stringify(res.data));
190
- // if (res && res.status == 200 && res.data) {
191
- // console.log("Status 200 OK");
192
- // if (callback) {
193
- // callback(null, res.data);
194
- // }
195
- // }
196
- // else {
197
- // console.error("Status ! 200");
198
- // if (callback) {
199
- // callback({ message: "Response status not 200" }, null);
200
- // }
201
- // }
202
- // })
203
- // .catch((error) => {
204
- // console.error("Reply error:", error);
205
- // if (callback) {
206
- // callback(error, null);
207
- // }
208
- // });
209
- // }
210
-
211
135
  }
212
136
 
213
137
  module.exports = { DirReply };
@@ -96,12 +96,12 @@ class DirSetAttributeV2 {
96
96
  }
97
97
 
98
98
  async go(action, callback) {
99
- console.log("(DirSetAttribute) action before filling:", JSON.stringify(action));
99
+ if (this.log) {console.log("(DirSetAttribute) action before filling:", JSON.stringify(action));}
100
100
  if (action && action.operation && action.operation.operands) {
101
- console.log("filling in setattribute...");
101
+ if (this.log) {console.log("filling in setattribute...");}
102
102
  await this.fillValues(action.operation.operands);
103
103
  }
104
- console.log("filled in setattribute:", action.operation);
104
+ if (this.log) {console.log("filled in setattribute:", action.operation);}
105
105
  // let res = validate(action, schema);
106
106
  // if (res.errors) {
107
107
  // console.log("(DirSetAttribute) failed validation action:", JSON.stringify(action));
@@ -126,18 +126,34 @@ class DirSetAttributeV2 {
126
126
  // console.log("filling in setattribute...");
127
127
  // await this.fillValues(action.operation.operands);
128
128
  // }
129
- console.log("dirsetattribute, action.operation.operands:", action.operation.operands);
129
+ // console.log("dirsetattribute, action.operation.operands:", action.operation.operands);
130
130
  const expression = TiledeskExpression.JSONOperationToExpression(action.operation.operators, action.operation.operands);
131
131
  const attributes = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
132
- console.log("dirsetattribute, attributes:", attributes);
132
+ // console.log("dirsetattribute, attributes:", attributes);
133
133
  attributes.TiledeskMath = TiledeskMath;
134
134
  attributes.TiledeskString = TiledeskString;
135
135
  const result = new TiledeskExpression().evaluateJavascriptExpression(expression, attributes);
136
- console.log("filling in setattribute, result:", result);
137
- await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.destination, result);
136
+ // console.log("filling in setattribute, result:", result);
137
+ // THE GOAL OF ATTRIBUTE-FILLING THE "DESTINATION" FIELD IS TO SUPPORT DYNAMIC ATTRIBUTES
138
+ // (ATTRS WHOSE NAME IS UNKNOWN AD DESIGN-TIME)
139
+ // STILL UNSUPPORTED IN UI
140
+ let destination = await this.fillDestination(action.destination);
141
+ await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, destination, result);
138
142
  callback();
139
143
  }
140
144
 
145
+ async fillDestination(destination) {
146
+ if (this.tdcache) {
147
+ // console.log("tdcache in setattribute...", this.tdcache);
148
+ const requestAttributes =
149
+ await TiledeskChatbot.allParametersStatic(this.tdcache, this.context.requestId);
150
+ // console.log("requestAttributes in setattribute...", requestAttributes);
151
+ const filler = new Filler();
152
+ destination = filler.fill(destination, requestAttributes);
153
+ // console.log("setattribute, final destination:", destination);
154
+ }
155
+ return destination
156
+ }
141
157
  async fillValues(operands) {
142
158
  // operation: {
143
159
  // operators: ["addAsNumber", "subtractAsNumber", "divideAsNumber", "multiplyAsNumber"],
@@ -173,11 +189,11 @@ class DirSetAttributeV2 {
173
189
  // console.log("requestAttributes in setattribute...", requestAttributes);
174
190
  const filler = new Filler();
175
191
  operands.forEach(operand => {
176
- if (!operand.isVariable) {
177
- console.log("setattribute, liquid operand:", operand);
192
+ // if (!operand.isVariable) {
193
+ // console.log("setattribute, liquid operand:", operand);
178
194
  operand.value = filler.fill(operand.value, requestAttributes);
179
- console.log("setattribute, final operand:", operand);
180
- }
195
+ // console.log("setattribute, final operand:", operand);
196
+ // }
181
197
  });
182
198
  }
183
199
  }
@@ -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