@tiledesk/tiledesk-tybot-connector 0.5.1-rc1 → 2.0.0

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": "0.5.1-rc1",
3
+ "version": "2.0.0",
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
  }
@@ -3,8 +3,8 @@ const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
3
3
  const { TiledeskChatbotUtil } = require('../../utils/TiledeskChatbotUtil');
4
4
  let axios = require('axios');
5
5
  const { TiledeskClient } = require('@tiledesk/tiledesk-client');
6
- const winston = require('../../utils/winston');
7
6
  const { Logger } = require('../../Logger');
7
+ const winston = require('../../utils/winston')
8
8
 
9
9
  class DirReply {
10
10
 
@@ -18,7 +18,7 @@ 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;
21
+ console.log("is draft request: ", this.context.supportRequest.draft);
22
22
  this.logger = new Logger({ request_id: this.requestId, dev: this.context.supportRequest.draft });
23
23
 
24
24
  this.API_ENDPOINT = context.API_ENDPOINT;
@@ -45,10 +45,10 @@ class DirReply {
45
45
  callback();
46
46
  return;
47
47
  }
48
- this.logger.info("Executing Action Reply ", directive.action)
48
+ this.logger.info("1 Execute action reply for " + directive.action.text)
49
49
 
50
50
  this.go(action, () => {
51
- this.logger.info("Action Reply terminated")
51
+ this.logger.info("6 End of action reply " + directive.action.text + " -> callback")
52
52
  callback();
53
53
  });
54
54
  }
@@ -69,16 +69,15 @@ class DirReply {
69
69
  const filler = new Filler();
70
70
  // fill text attribute
71
71
  message.text = filler.fill(message.text, requestAttributes);
72
+ this.logger.info("2 Sending reply " + message.text);
72
73
 
73
74
  if (message.metadata) {
74
75
  winston.debug("DirReply filling message 'metadata':", message.metadata);
75
76
  if (message.metadata.src) {
76
77
  message.metadata.src = filler.fill(message.metadata.src, requestAttributes);
77
- this.logger.debug("Filled metadata.src with ", message.metadata.src);
78
78
  }
79
79
  if (message.metadata.name) {
80
80
  message.metadata.name = filler.fill(message.metadata.name, requestAttributes);
81
- this.logger.debug("Filled metadata.name with ", message.metadata.name);
82
81
  }
83
82
  }
84
83
  winston.debug("DirReply filling commands'. Message:", message);
@@ -91,7 +90,6 @@ class DirReply {
91
90
  let command = commands[i];
92
91
  if (command.type === 'message' && command.message && command.message.text) {
93
92
  command.message.text = filler.fill(command.message.text, requestAttributes);
94
- this.logger.debug("Filled message.text with ", command.message.text)
95
93
  TiledeskChatbotUtil.fillCommandAttachments(command, requestAttributes, this.log);
96
94
  winston.debug("DirReply command filled: " + command.message.text);
97
95
  }
@@ -146,6 +144,15 @@ class DirReply {
146
144
  }
147
145
 
148
146
  let cleanMessage = message;
147
+ this.logger.info("3 Sending reply (text) " + cleanMessage.text);
148
+ this.logger.info("4 Sending reply with clean message " + JSON.stringify(cleanMessage));
149
+ // cleanMessage = TiledeskChatbotUtil.removeEmptyReplyCommands(message);
150
+ // if (!TiledeskChatbotUtil.isValidReply(cleanMessage)) {
151
+ // console.log("invalid message", cleanMessage);
152
+ // callback(); // cancel reply operation
153
+ // return;
154
+ // }
155
+
149
156
  cleanMessage.senderFullname = this.context.chatbot.bot.name;
150
157
  winston.debug("DirReply reply with clean message: ", cleanMessage);
151
158
 
@@ -156,8 +163,10 @@ class DirReply {
156
163
  (err) => {
157
164
  if (err) {
158
165
  winston.error("DirReply Error sending reply: ", err);
166
+ this.logger.error("Error sending reply: " + err);
159
167
  }
160
168
  winston.verbose("DirReply reply message sent")
169
+ this.logger.info("5 Reply message sent");
161
170
  const delay = TiledeskChatbotUtil.totalMessageWait(cleanMessage);
162
171
  if (delay > 0 && delay <= 30000) { // prevent long delays
163
172
  setTimeout(() => {
@@ -1,7 +1,6 @@
1
1
  const { Filler } = require('../Filler');
2
2
  const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
3
3
  const { TiledeskChatbotUtil } = require('../../utils/TiledeskChatbotUtil');
4
- const winston = require('../../utils/winston');
5
4
  let axios = require('axios');
6
5
 
7
6
  class DirWebResponse {
@@ -38,24 +37,25 @@ class DirWebResponse {
38
37
  async go(action, callback) {
39
38
  winston.debug("DirWebResponse action: ", action);
40
39
 
41
- if (!this.tdcache) {
42
- winston.error("DirWebResponse Error: tdcache is mandatory");
43
- callback();
44
- return;
45
- }
46
-
47
40
  let requestAttributes = null;
48
- requestAttributes =
49
- await TiledeskChatbot.allParametersStatic(
50
- this.tdcache, this.requestId
51
- );
52
-
53
- const filler = new Filler();
54
- const filled_status = filler.fill(action.status, requestAttributes);
55
- const json = await this.getJsonFromAction(action, filler, requestAttributes)
41
+ let status = null;
42
+ if (this.tdcache) {
43
+ requestAttributes =
44
+ await TiledeskChatbot.allParametersStatic(this.tdcache, this.requestId);
45
+ const filler = new Filler();
56
46
 
47
+ try {
48
+ status = filler.fill(action.status, requestAttributes);
49
+ }
50
+ catch(e) {
51
+ winston.error("DirWebResponse Error: ", e)
52
+ }
53
+
54
+ }
55
+
56
+ const json = await this.getJsonFromAction(action, filler, requestAttributes)
57
57
  let webResponse = {
58
- status: filled_status,
58
+ status: status,
59
59
  payload: json
60
60
  }
61
61
 
@@ -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
  }