@tiledesk/tiledesk-tybot-connector 2.0.7-rc1 → 2.0.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "2.0.7-rc1",
3
+ "version": "2.0.7",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -222,13 +222,14 @@ class DirectivesChatbotPlug {
222
222
  directive_name = directive.name.toLowerCase();
223
223
  }
224
224
  if (directive && directive.action) {
225
- const action_id = directive.action["_tdActionId"];
226
- const locked_action_id = await this.chatbot.currentLockedAction(this.supportRequest.request_id);
227
- if ( locked_action_id && (locked_action_id !== action_id) ) {
228
- let next_dir = await this.nextDirective(this.directives);
229
- this.process(next_dir);
230
- return;
231
- }
225
+ const action_id = directive.action["_tdActionId"];
226
+ const locked_action_id = await this.chatbot.currentLockedAction(this.supportRequest.request_id);
227
+ if ( locked_action_id && (locked_action_id !== action_id) ) {
228
+ let next_dir = await this.nextDirective(this.directives);
229
+ this.process(next_dir);
230
+ return;
231
+ }
232
+
232
233
  }
233
234
  if (directive == null || (directive !== null && directive["name"] === undefined)) {
234
235
  winston.debug("(DirectivesChatbotPlug) stop process(). directive is (null?): ", directive);
@@ -700,19 +701,6 @@ class DirectivesChatbotPlug {
700
701
  }
701
702
  });
702
703
  }
703
- else if (directive_name === Directives.WEBHOOK) {
704
- // console.log(".....DirIntent")
705
- new DirIntent(context).execute(directive, async (stop) => {
706
- if (stop) {
707
- if (context.log) { console.log("Stopping Actions on:", JSON.stringify(directive));}
708
- this.theend();
709
- }
710
- else {
711
- let next_dir = await this.nextDirective(this.directives);
712
- this.process(next_dir);
713
- }
714
- });
715
- }
716
704
  else if (directive_name === Directives.WEB_RESPONSE) {
717
705
  new DirWebResponse(context).execute(directive, async () => {
718
706
  let next_dir = await this.nextDirective(this.directives);
@@ -62,12 +62,10 @@ class DirIntent {
62
62
  "recipient": requestId,
63
63
  "text": intent_command,
64
64
  "id_project": projectId,
65
- "request": this.supportRequest,
66
- // "request": {
67
- // "request_id": requestId,
68
- // "id_project": projectId
69
- // // "bot_id": botId
70
- // }
65
+ "request": {
66
+ "request_id": requestId,
67
+ "id_project": projectId
68
+ }
71
69
  },
72
70
  "token": this.token
73
71
  }
@@ -18,7 +18,6 @@ class DirReply {
18
18
  this.token = context.token;
19
19
  this.tdcache = context.tdcache;
20
20
  this.log = context.log;
21
- this.supportRequest = this.context.supportRequest;
22
21
  this.logger = new Logger({ request_id: this.requestId, dev: this.context.supportRequest.draft });
23
22
 
24
23
  this.API_ENDPOINT = context.API_ENDPOINT;
@@ -45,10 +44,10 @@ class DirReply {
45
44
  callback();
46
45
  return;
47
46
  }
48
- this.logger.info("Executing Action Reply ", directive.action)
47
+ this.logger.info("1 Execute action reply for " + directive.action.text)
49
48
 
50
49
  this.go(action, () => {
51
- this.logger.info("Action Reply terminated")
50
+ this.logger.info("6 End of action reply " + directive.action.text + " -> callback")
52
51
  callback();
53
52
  });
54
53
  }
@@ -69,16 +68,15 @@ class DirReply {
69
68
  const filler = new Filler();
70
69
  // fill text attribute
71
70
  message.text = filler.fill(message.text, requestAttributes);
71
+ this.logger.info("2 Sending reply " + message.text);
72
72
 
73
73
  if (message.metadata) {
74
74
  winston.debug("DirReply filling message 'metadata':", message.metadata);
75
75
  if (message.metadata.src) {
76
76
  message.metadata.src = filler.fill(message.metadata.src, requestAttributes);
77
- this.logger.debug("Filled metadata.src with ", message.metadata.src);
78
77
  }
79
78
  if (message.metadata.name) {
80
79
  message.metadata.name = filler.fill(message.metadata.name, requestAttributes);
81
- this.logger.debug("Filled metadata.name with ", message.metadata.name);
82
80
  }
83
81
  }
84
82
  winston.debug("DirReply filling commands'. Message:", message);
@@ -91,7 +89,6 @@ class DirReply {
91
89
  let command = commands[i];
92
90
  if (command.type === 'message' && command.message && command.message.text) {
93
91
  command.message.text = filler.fill(command.message.text, requestAttributes);
94
- this.logger.debug("Filled message.text with ", command.message.text)
95
92
  TiledeskChatbotUtil.fillCommandAttachments(command, requestAttributes, this.log);
96
93
  winston.debug("DirReply command filled: " + command.message.text);
97
94
  }
@@ -146,6 +143,15 @@ class DirReply {
146
143
  }
147
144
 
148
145
  let cleanMessage = message;
146
+ this.logger.info("3 Sending reply (text) " + cleanMessage.text);
147
+ this.logger.info("4 Sending reply with clean message " + JSON.stringify(cleanMessage));
148
+ // cleanMessage = TiledeskChatbotUtil.removeEmptyReplyCommands(message);
149
+ // if (!TiledeskChatbotUtil.isValidReply(cleanMessage)) {
150
+ // console.log("invalid message", cleanMessage);
151
+ // callback(); // cancel reply operation
152
+ // return;
153
+ // }
154
+
149
155
  cleanMessage.senderFullname = this.context.chatbot.bot.name;
150
156
  winston.debug("DirReply reply with clean message: ", cleanMessage);
151
157
 
@@ -156,8 +162,10 @@ class DirReply {
156
162
  (err) => {
157
163
  if (err) {
158
164
  winston.error("DirReply Error sending reply: ", err);
165
+ this.logger.error("Error sending reply: " + err);
159
166
  }
160
167
  winston.verbose("DirReply reply message sent")
168
+ this.logger.info("5 Reply message sent");
161
169
  const delay = TiledeskChatbotUtil.totalMessageWait(cleanMessage);
162
170
  if (delay > 0 && delay <= 30000) { // prevent long delays
163
171
  setTimeout(() => {
@@ -96,4 +96,42 @@ class DirWebResponse {
96
96
 
97
97
  }
98
98
 
99
+
100
+
101
+ /**
102
+ * A stub to send message to the "ext/botId" endpoint, hosted by tilebot on:
103
+ * /${TILEBOT_ROUTE}/ext/${botId}
104
+ *
105
+ * @param {Object} webResponse. The webhook response to send back
106
+ * @param {Object} projectId. The projectId
107
+ * @param {string} botId. Tiledesk botId
108
+ * @param {string} token. User token
109
+ */
110
+ // function sendResponse(webResponse, projectId, botId, callback) {
111
+ // const url = `${WEBHOOK_URL}/${projectId}/${botId}`;
112
+ // const HTTPREQUEST = {
113
+ // url: url,
114
+ // headers: {
115
+ // 'Content-Type': 'application/json'
116
+ // },
117
+ // json: webResponse,
118
+ // method: 'POST'
119
+ // };
120
+ // myrequest(
121
+ // HTTPREQUEST,
122
+ // function (err, resbody) {
123
+ // if (err) {
124
+ // if (callback) {
125
+ // callback(err);
126
+ // }
127
+ // }
128
+ // else {
129
+ // if (callback) {
130
+ // callback(null, resbody);
131
+ // }
132
+ // }
133
+ // }, false
134
+ // );
135
+ // }
136
+
99
137
  module.exports = { DirWebResponse };
@@ -58,7 +58,6 @@ class Directives {
58
58
  static MOVE_TO_UNASSIGNED = "move_to_unassigned";
59
59
  static CONNECT_BLOCK = "connect_block";
60
60
  static ADD_TAGS = 'add_tags'
61
- static WEBHOOK = 'webhook';
62
61
  static WEB_RESPONSE = "web_response";
63
62
 
64
63
  // static WHEN_ONLINE_MOVE_TO_AGENT = "whenonlinemovetoagent"; // DEPRECATED?
@@ -319,31 +319,55 @@ class TiledeskChatbotUtil {
319
319
  json_buttons.forEach(button => {
320
320
  if (button.value && button.type === "action" && button.action) {
321
321
  button.show_echo = true;
322
+ // console.log("pushing:", button)
322
323
  final_buttons.push(button);
323
324
  }
324
325
  else if (button.value && button.type === "text") {
325
326
  button.show_echo = true;
327
+ // console.log("pushing:", button)
326
328
  final_buttons.push(button);
327
329
  }
328
330
  else if (button.value && button.type === "url" && button.link) {
329
331
  button.show_echo = true;
332
+ // console.log("pushing:", button)
330
333
  final_buttons.push(button);
331
334
  }
332
335
  else {
333
- winston.verbose("Invalid button. Skipping:", button);
336
+ console.log("Invalid button. Skipping:", JSON.stringify(button) );
334
337
  }
335
338
  });
336
339
  }
340
+
341
+ // "buttons": [
342
+ // {
343
+ // "type": "action",
344
+ // "value": "Button1", // obbligatorio sempre
345
+ // "action": "#bb347206-d639-4926-94c9-e94930623dce", // mandatory
346
+ // "show_echo": true, // lo inserisco sempre
347
+ // "alias": "button1 alias"
348
+ // },
349
+ // {
350
+ // "type": "text",
351
+ // "value": "Button2 text", // obbligatorio sempre
352
+ // "show_echo": true // lo inserisco sempre
353
+ // },
354
+ // {
355
+ // "type": "url",
356
+ // "value": "Button3 link", // obbligatorio sempre
357
+ // "link": "http://", // obbligatorio
358
+ // "show_echo": true // lo inserisco sempre
359
+ // }
360
+ // ]
337
361
  }
338
362
  catch(error) {
339
- winston.warn("Invalid json_buttons:", error)
363
+ console.error("Invalid json_buttons:", error)
340
364
  }
341
365
  if (final_buttons && final_buttons.length > 0) {
342
366
  command.message.attributes.attachment.buttons = final_buttons;
343
367
  delete command.message.attributes.attachment.json_buttons;
344
368
  }
345
369
  else {
346
- winston.verbose("Invalid json_buttons. Skipping...")
370
+ console.log("Invalid json_buttons. Skipping")
347
371
  }
348
372
  }
349
373
  }