@tiledesk/tiledesk-tybot-connector 0.2.25 → 0.2.27

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.27
9
+ - Added support for removing empty text from replies: TiledeskChatbotUtil.removeEmptyReplyCommands()
10
+ - Removed non-user-defined attributes from /ext/parameters method
11
+
12
+ ### v0.2.26
13
+ - Updated Qapla Action (tracking number from filler)
14
+
8
15
  ### v0.2.25
9
16
  - added support for native connect to block
10
17
  - added TiledeskChatbotUtil.addConnectAction()
package/index.js CHANGED
@@ -221,6 +221,11 @@ router.post('/ext/:botid', async (req, res) => {
221
221
 
222
222
  });
223
223
 
224
+ router.post('/hooks/:hookid', async (req, res) => {
225
+ // chatbot_id + intend_id
226
+
227
+ });
228
+
224
229
  router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) => {
225
230
  res.json({success:true});
226
231
  const projectId = req.params.projectId;
@@ -359,7 +364,18 @@ router.get('/ext/parameters/requests/:requestid', async (req, res) => {
359
364
  TiledeskChatbotConst.REQ_REQUEST_ID_KEY,
360
365
  TiledeskChatbotConst.REQ_USER_AGENT_KEY,
361
366
  TiledeskChatbotConst.REQ_USER_LANGUAGE_KEY,
362
- TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY
367
+ TiledeskChatbotConst.REQ_USER_SOURCE_PAGE_KEY,
368
+ TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_TYPE_KEY,
369
+ TiledeskChatbotConst.REQ_TRANSCRIPT_KEY,
370
+ TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_KEY,
371
+ "lastUserImageURL", // image
372
+ "lastUserImageName", // image
373
+ "lastUserImageWidth", // image
374
+ "lastUserImageHeight", // image
375
+ "lastUserImageType", // image
376
+ "lastUserDocumentURL", // file
377
+ "lastUserDocumentName", // file
378
+ "lastUserDocumentType" // file
363
379
  ]
364
380
  let userParams = {};
365
381
  if (parameters) {
@@ -5,6 +5,7 @@ class TiledeskChatbotConst {
5
5
  static REQ_REQUEST_ID_KEY = "conversation_id";
6
6
  static REQ_CHATBOT_NAME_KEY = "chatbot_name";
7
7
  static REQ_LAST_USER_TEXT_KEY = "last_user_text";
8
+ static REQ_LAST_USER_TEXT_v2_KEY = "lastUserText";
8
9
  static REQ_LAST_MESSAGE_ID_KEY = "last_message_id";
9
10
  static REQ_COUNTRY_KEY = "user_country";
10
11
  static REQ_CITY_KEY = "user_city";
@@ -15,6 +16,10 @@ class TiledeskChatbotConst {
15
16
  static REQ_END_USER_ID_KEY = "user_id";
16
17
  static REQ_END_USER_IP_ADDRESS_KEY = "user_ip_address";
17
18
  static REQ_CHAT_URL = "chat_url";
19
+ static REQ_LAST_USER_MESSAGE_TYPE_KEY = "lastUserMessageType";
20
+ static REQ_TRANSCRIPT_KEY = "transcript";
21
+ static REQ_LAST_USER_MESSAGE_KEY = "lastUserMessage";
22
+ static REQ_CHAT_CHANNEL = "chatChannel";
18
23
 
19
24
  // static REQ_DEPARTMENT_ID_KEY = "tdDepartmentId";
20
25
  // static REQ_PROJECT_ID_KEY = "projectId";
@@ -237,6 +237,57 @@ class TiledeskChatbotUtil {
237
237
  }
238
238
  }
239
239
 
240
+ static removeEmptyReplyCommands(message) {
241
+ try {
242
+ if (message && message.attributes && message.attributes.commands && message.attributes.commands.length > 0) {
243
+ let commands = message.attributes.commands;
244
+
245
+ for (let i = commands.length - 1; i >= 0; i--) {
246
+ // console.log("...commands[" + i + "]");
247
+ if (commands[i].type === "message") { // is a message, not a "wait"
248
+ // console.log("commands[i]:", commands[i].message.text);
249
+ // let textEmpty = false;
250
+ if (commands[i].message) {
251
+ if (commands[i].message.type === "text") { // check text commands
252
+ if (( commands[i].message.text && commands[i].message.text.trim() === "") || !commands[i].message.text) {
253
+ console.log("deleting command:", commands[i]);
254
+ commands.splice(i, 1);
255
+ if (commands[i-1]) {
256
+ if (commands[i-1].type === "wait") {
257
+ commands.splice(i-1, 1);
258
+ i--;
259
+ console.log("deleted wait");
260
+ }
261
+ }
262
+ }
263
+ }
264
+ }
265
+ }
266
+ }
267
+ // for (let i = 0; i < commands.length; i++) {
268
+ // if (commands[i].type === 'message' && commands[i].message && commands[i].message.text) {
269
+ // if (this.log) {console.log("[" + commands[i].message.lang + "]commands[i].message.text:", commands[i].message.text);}
270
+ // }
271
+ // }
272
+ }
273
+ }
274
+ catch(error) {
275
+ log.error("error while checking", error)
276
+ }
277
+ return message;
278
+ }
279
+
280
+ /*
281
+ returns true if a valid message for a reply (i.e. at least one valid - non empty - message command)
282
+ */
283
+ static isValidReply(message) {
284
+ if (message && message.attributes && message.attributes.commands && message.attributes.commands.length > 0) {
285
+ return true;
286
+ } else {
287
+ return false;
288
+ }
289
+ }
290
+
240
291
  static totalMessageWait(message) {
241
292
  if (!message) {
242
293
  return;
@@ -337,17 +388,17 @@ class TiledeskChatbotUtil {
337
388
 
338
389
  if (message.text && message.sender !== "_tdinternal") {
339
390
  await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY, message.text); // DEPRECATED
340
- await chatbot.addParameter("lastUserText", message.text);
391
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_TEXT_v2_KEY, message.text);
341
392
  if (message.channel) {
342
393
  if (message.channel.name === "chat21") {
343
- await chatbot.addParameter("chatChannel", "web"); // renames the channel in chat21
394
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_CHANNEL, "web"); // renames the channel in chat21
344
395
  }
345
396
  else {
346
- await chatbot.addParameter("chatChannel", message.channel.name);
397
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_CHAT_CHANNEL, message.channel.name);
347
398
  }
348
399
  }
349
- await chatbot.addParameter("lastUserMessageType", message.type);
350
- await chatbot.addParameter("lastUserMessage", TiledeskChatbotUtil.lastUserMessageFrom(message)); // JSON TYPE *NEW
400
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_TYPE_KEY, message.type);
401
+ await chatbot.addParameter(TiledeskChatbotConst.REQ_LAST_USER_MESSAGE_KEY, TiledeskChatbotUtil.lastUserMessageFrom(message)); // JSON TYPE *NEW
351
402
  // get image
352
403
  if (message.type && message.type === "image" && message.metadata) {
353
404
  // "text": "\nimage text",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.25",
3
+ "version": "0.2.27",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -53,16 +53,17 @@ class DirQapla {
53
53
  }
54
54
  }
55
55
 
56
- let tracking_number = await this.context.chatbot.getParameter(action.trackingNumber);
56
+ const filler = new Filler();
57
+ const tracking_number = filler.fill(action.trackingNumber, requestVariables);
58
+ // let tracking_number = await this.context.chatbot.getParameter(action.trackingNumber);
57
59
  if (this.log) {console.log("DirQapla tracking number: ", tracking_number); }
58
60
 
59
- if (!tracking_number) {
60
- console.error("DirQapla ERROR - tracking number is undefined or null");
61
+ if (!tracking_number || tracking_number === '') {
62
+ console.error("DirQapla ERROR - tracking number is undefined or null or empty string");
61
63
  callback();
62
64
  }
63
65
 
64
66
  const qapla_base_url = process.env.QAPLA_ENDPOINT || "https://api.qapla.it/1.2"
65
- // https://api.qapla.it/1.2/getShipment/?apiKey=3b9839c954168e861f2b63d79920ec3a3ff92aab674de9f3930df60b8a40c495&trackingNumber=123456
66
67
  if (this.log) { console.log("DirQapla QaplaEndpoint URL: ", qapla_base_url); }
67
68
  const QAPLA_HTTPREQUEST = {
68
69
  url: qapla_base_url + "/getShipment/",
@@ -97,18 +97,24 @@ class DirReply {
97
97
  }
98
98
  }
99
99
  // send!
100
- message.senderFullname = this.context.chatbot.bot.name;
101
- if (this.log) {console.log("Reply:", JSON.stringify(message))};
102
- await TiledeskChatbotUtil.updateConversationTranscript(this.context.chatbot, message);
100
+ const cleanMessage = TiledeskChatbotUtil.removeEmptyReplyCommands(message);
101
+ if (!TiledeskChatbotUtil.isValidReply(cleanMessage)) {
102
+ console.log("invalid message", cleanMessage);
103
+ callback(); // cancel reply operation
104
+ }
105
+ // console.log("valid message!", cleanMessage);
106
+ cleanMessage.senderFullname = this.context.chatbot.bot.name;
107
+ if (this.log) {console.log("Reply:", JSON.stringify(cleanMessage))};
108
+ await TiledeskChatbotUtil.updateConversationTranscript(this.context.chatbot, cleanMessage);
103
109
  this.context.tdclient.sendSupportMessage(
104
110
  this.requestId,
105
- message,
111
+ cleanMessage,
106
112
  (err) => {
107
113
  if (err) {
108
114
  console.error("Error sending reply:", err);
109
115
  }
110
116
  if (this.log) {console.log("Reply message sent");}
111
- const delay = TiledeskChatbotUtil.totalMessageWait(message);
117
+ const delay = TiledeskChatbotUtil.totalMessageWait(cleanMessage);
112
118
  // console.log("got total delay:", delay)
113
119
  if (delay > 0 && delay <= 30000) { // prevent long delays
114
120
  setTimeout(() => {