@tiledesk/tiledesk-tybot-connector 0.2.601-rc1 → 0.3.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.
Files changed (71) hide show
  1. package/CHANGELOG.md +378 -1
  2. package/ExtApi.js +6 -6
  3. package/TdCache copy.js +242 -0
  4. package/TdCache.js +81 -176
  5. package/TdCache_v3.js +261 -0
  6. package/TiledeskExpression.js +7 -3
  7. package/index.js +290 -43
  8. package/logs/app.log +279 -0
  9. package/models/IntentsMachineFactory.js +5 -2
  10. package/models/MockBotsDataSource.js +19 -11
  11. package/models/TiledeskChatbot.js +97 -79
  12. package/models/TiledeskChatbotConst.js +12 -17
  13. package/models/TiledeskChatbotUtil.js +353 -109
  14. package/models/TiledeskIntentsMachine.js +1 -1
  15. package/models/faqKbService.js +1 -1
  16. package/package.json +7 -6
  17. package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +172 -106
  18. package/tiledeskChatbotPlugs/Filler.js +13 -2
  19. package/tiledeskChatbotPlugs/TildeskContextForCodeOrchestrator.js +8 -0
  20. package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +13 -7
  21. package/tiledeskChatbotPlugs/directives/DirAddTags.js +374 -0
  22. package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +476 -0
  23. package/tiledeskChatbotPlugs/directives/DirAskGPT.js +16 -19
  24. package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +221 -34
  25. package/tiledeskChatbotPlugs/directives/DirAssign.js +0 -11
  26. package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +11 -21
  27. package/tiledeskChatbotPlugs/directives/DirAssistant.js +728 -0
  28. package/tiledeskChatbotPlugs/directives/DirBrevo.js +353 -0
  29. package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +3 -30
  30. package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +22 -0
  31. package/tiledeskChatbotPlugs/directives/DirClose.js +16 -3
  32. package/tiledeskChatbotPlugs/directives/DirCode.js +1 -1
  33. package/tiledeskChatbotPlugs/directives/DirCondition.js +0 -26
  34. package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +183 -0
  35. package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +121 -0
  36. package/tiledeskChatbotPlugs/directives/DirCustomerio.js +5 -8
  37. package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +11 -1
  38. package/tiledeskChatbotPlugs/directives/DirDepartment.js +15 -6
  39. package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +17 -6
  40. package/tiledeskChatbotPlugs/directives/DirForm.js +12 -2
  41. package/tiledeskChatbotPlugs/directives/DirGptTask.js +83 -38
  42. package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +4 -7
  43. package/tiledeskChatbotPlugs/directives/DirHubspot.js +5 -8
  44. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +14 -27
  45. package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +278 -0
  46. package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +147 -51
  47. package/tiledeskChatbotPlugs/directives/DirIfOpenHours_OLD.js +125 -0
  48. package/tiledeskChatbotPlugs/directives/DirIntent.js +8 -36
  49. package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +5 -26
  50. package/tiledeskChatbotPlugs/directives/DirMessage.js +19 -17
  51. package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +136 -0
  52. package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +20 -87
  53. package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +59 -0
  54. package/tiledeskChatbotPlugs/directives/DirQapla.js +6 -9
  55. package/tiledeskChatbotPlugs/directives/DirRandomReply.js +17 -7
  56. package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +17 -7
  57. package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +11 -2
  58. package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +135 -21
  59. package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +163 -0
  60. package/tiledeskChatbotPlugs/directives/DirReply.js +42 -9
  61. package/tiledeskChatbotPlugs/directives/DirReplyV2.js +347 -0
  62. package/tiledeskChatbotPlugs/directives/DirSendEmail.js +13 -23
  63. package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +247 -0
  64. package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +202 -15
  65. package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +13 -4
  66. package/tiledeskChatbotPlugs/directives/DirWait.js +21 -4
  67. package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -2
  68. package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +166 -103
  69. package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +2 -60
  70. package/tiledeskChatbotPlugs/directives/Directives.js +16 -1
  71. /package/tiledeskChatbotPlugs/directives/{DirOfflineHours.js → DEPRECATED_DirOfflineHours.js} +0 -0
@@ -0,0 +1,125 @@
1
+ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
+ const { DirIntent } = require('./DirIntent');
3
+ const ms = require('minimist-string');
4
+
5
+ class DirIfOpenHours {
6
+
7
+ constructor(context) {
8
+ if (!context) {
9
+ throw new Error('context object is mandatory.');
10
+ }
11
+
12
+ this.context = context;
13
+ this.intentDir = new DirIntent(context);
14
+ this.log = context.log;
15
+
16
+ this.API_ENDPOINT = context.API_ENDPOINT;
17
+ this.tdClient = new TiledeskClient({
18
+ projectId: this.context.projectId,
19
+ token: this.context.token,
20
+ APIURL: this.API_ENDPOINT,
21
+ APIKEY: "___",
22
+ log: this.log
23
+ });
24
+ }
25
+
26
+ execute(directive, callback) {
27
+ let action;
28
+ if (directive.action) {
29
+ action = directive.action
30
+ }
31
+ else if (directive.parameter) {
32
+ let params;
33
+ params = this.parseParams(directive.parameter);
34
+ if (!params.trueIntent && !params.falseIntent) {
35
+ if (this.log) {
36
+ console.log("missing both params.trueIntent & params.falseIntent");
37
+ }
38
+ callback();
39
+ return;
40
+ }
41
+ action = {
42
+ trueIntent: params.trueIntent,
43
+ falseIntent: params.falseIntent
44
+ }
45
+ }
46
+ else {
47
+ callback();
48
+ return;
49
+ }
50
+ this.go(action, (stop) => {
51
+ callback(stop);
52
+ });
53
+ }
54
+
55
+ go(action, callback) {
56
+
57
+ const trueIntent = action.trueIntent;
58
+ const falseIntent = action.falseIntent;
59
+ const trueIntentAttributes = action.trueIntentAttributes;
60
+ const falseIntentAttributes = action.falseIntentAttributes;
61
+ const stopOnConditionMet = action.stopOnConditionMet;
62
+
63
+ if (trueIntent && trueIntent.trim() === "") {
64
+ trueIntent = null;
65
+ }
66
+ if (falseIntent && falseIntent.trim() === "") {
67
+ falseIntent = null;
68
+ }
69
+ if (this.log) {console.log("condition action:", action);}
70
+ if (!trueIntent && !falseIntent) {
71
+ if (this.log) {console.log("Invalid condition, no intents specified");}
72
+ callback();
73
+ return;
74
+ }
75
+ this.tdClient.openNow((err, result) => {
76
+ if (this.log) {console.log("openNow():", result);}
77
+ if (err) {
78
+ console.error("*** DirIfOpenHours Error:", err);
79
+ callback();
80
+ }
81
+ else if (result && result.isopen) {
82
+ if (trueIntent) {
83
+ let intentDirective = DirIntent.intentDirectiveFor(trueIntent);
84
+ if (this.log) {console.log("DirIfOpenHours (openHours) => trueIntent");}
85
+ this.intentDir.execute(intentDirective, () => {
86
+ callback(stopOnConditionMet);
87
+ });
88
+ }
89
+ else {
90
+ callback();
91
+ return;
92
+ }
93
+ }
94
+ else if (falseIntent) {
95
+ let intentDirective = DirIntent.intentDirectiveFor(falseIntent);
96
+ if (this.log) {console.log("DirIfOpenHours (openHours) => falseIntent", falseIntent);}
97
+ this.intentDir.execute(intentDirective, () => {
98
+ callback(stopOnConditionMet);
99
+ });
100
+ }
101
+ else {
102
+ callback();
103
+ }
104
+ });
105
+ }
106
+
107
+ parseParams(directive_parameter) {
108
+ let trueIntent = null;
109
+ let falseIntent = null;
110
+ const params = ms(directive_parameter);
111
+ if (params.trueIntent) {
112
+ trueIntent = params.trueIntent;
113
+ }
114
+ if (params.falseIntent) {
115
+ falseIntent = params.falseIntent;
116
+ }
117
+ return {
118
+ trueIntent: trueIntent,
119
+ falseIntent: falseIntent
120
+ }
121
+ }
122
+
123
+ }
124
+
125
+ module.exports = { DirIfOpenHours };
@@ -1,4 +1,3 @@
1
- //const { ExtApi } = require('../../ExtApi.js');
2
1
  let axios = require('axios');
3
2
  let https = require("https");
4
3
  const { v4: uuidv4 } = require('uuid');
@@ -6,38 +5,16 @@ const ms = require('minimist-string');
6
5
 
7
6
  class DirIntent {
8
7
 
9
- // constructor(settings) {
10
- // if (!settings.API_ENDPOINT) {
11
- // throw new Error("settings.API_ENDPOINT is mandatory!");
12
- // }
13
- // this.API_ENDPOINT = settings.API_ENDPOINT;
14
- // this.TILEBOT_ENDPOINT = settings.TILEBOT_ENDPOINT;
15
- // this.log = settings.log;
16
- // this.supportRequest = settings.supportRequest;
17
- // this.token = settings.token;
18
- // }
19
8
  constructor(context) {
20
9
  if (!context) {
21
10
  throw new Error('context object is mandatory.');
22
11
  }
23
12
  this.context = context;
24
- this.API_ENDPOINT = context.TILEDESK_APIURL,
13
+ this.API_ENDPOINT = context.API_ENDPOINT,
25
14
  this.TILEBOT_ENDPOINT = context.TILEBOT_ENDPOINT;
26
15
  this.supportRequest = context.supportRequest;
27
16
  this.token = context.token;
28
17
  this.log = context.log;
29
- // let context = {
30
- // projectId: projectId,
31
- // token: token,
32
- // supportRequest: supportRequest,
33
- // requestId: supportRequest.request_id,
34
- // TILEDESK_APIURL: API_URL,
35
- // TILEBOT_ENDPOINT: TILEBOT_ENDPOINT,
36
- // departmentId: depId,
37
- // tdcache: tdcache,
38
- // tdclient: tdclient,
39
- // log: true
40
- // }
41
18
  }
42
19
 
43
20
  execute(directive, callback) {
@@ -78,7 +55,7 @@ class DirIntent {
78
55
  // intent_command = "/#" + intentId;
79
56
  // }
80
57
  else {
81
- console.error("(DirIntent) No intent name found in action");
58
+ // console.error("(DirIntent) No intent name found in action");
82
59
  callback();
83
60
  return;
84
61
  }
@@ -103,14 +80,9 @@ class DirIntent {
103
80
  "token": this.token
104
81
  }
105
82
  if (this.log) {console.log("move to intent message:", intent_command_request);}
106
- let TILEBOT_ENDPOINT;
107
- if (this.TILEBOT_ENDPOINT) {
108
- TILEBOT_ENDPOINT = this.TILEBOT_ENDPOINT;
109
- }
110
- else {
111
- TILEBOT_ENDPOINT = `${this.API_ENDPOINT}/modules/tilebot`
112
- }
113
- this.sendMessageToBot(TILEBOT_ENDPOINT, intent_command_request, botId, () => {
83
+
84
+
85
+ this.sendMessageToBot(this.TILEBOT_ENDPOINT, intent_command_request, botId, () => {
114
86
  // console.log("sendMessageToBot() req_body sent:", intent_command_request);
115
87
  callback(true);
116
88
  });
@@ -160,9 +132,9 @@ class DirIntent {
160
132
  * @param {string} botId. Tiledesk botId
161
133
  * @param {string} token. User token
162
134
  */
163
- sendMessageToBot(CHATBOT_ENDPOINT, message, botId, callback) {
135
+ sendMessageToBot(TILEBOT_ENDPOINT, message, botId, callback) {
164
136
  // const jwt_token = this.fixToken(token);
165
- const url = `${CHATBOT_ENDPOINT}/ext/${botId}`;
137
+ const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
166
138
  // console.log("sendMessageToBot URL", url);
167
139
  const HTTPREQUEST = {
168
140
  url: url,
@@ -225,7 +197,7 @@ class DirIntent {
225
197
  }
226
198
  })
227
199
  .catch( (error) => {
228
- console.error("An error occurred:", error);
200
+ console.error("(DirIntent) Axios error: ", JSON.stringify(error));
229
201
  if (callback) {
230
202
  callback(error, null, null);
231
203
  }
@@ -9,33 +9,9 @@ class DirJSONCondition {
9
9
  throw new Error('context object is mandatory.');
10
10
  }
11
11
  this.context = context;
12
- // let context = {
13
- // projectId: projectId,
14
- // token: token,
15
- // supportRequest: supportRequest,
16
- // requestId: supportRequest.request_id,
17
- // TILEDESK_APIURL: API_URL,
18
- // TILEBOT_ENDPOINT:TILEBOT_ENDPOINT,
19
- // departmentId: depId,
20
- // tdcache: tdcache,
21
- // log: false
22
- // }
23
- // this.tdclient = new TiledeskClient({
24
- // projectId: context.projectId,
25
- // token: context.token,
26
- // APIURL: context.TILEDESK_APIURL,
27
- // APIKEY: "___",
28
- // log: context.log
29
- // });
12
+ this.chatbot = context.chatbot;
13
+ this.chatbot = context.chatbot;
30
14
  this.intentDir = new DirIntent(context);
31
- // {
32
- // API_ENDPOINT: context.TILEDESK_APIURL,
33
- // TILEBOT_ENDPOINT: context.TILEBOT_ENDPOINT,
34
- // supportRequest: context.supportRequest,
35
- // token: context.token,
36
- // log: context.log
37
- // }
38
- // );
39
15
  this.log = context.log;
40
16
  }
41
17
 
@@ -119,6 +95,9 @@ class DirJSONCondition {
119
95
  }
120
96
  }
121
97
  else {
98
+ if (result === null) {
99
+ await this.chatbot.addParameter("flowError", "An error occurred evaluating condition: result === null");
100
+ }
122
101
  if (falseIntentDirective) {
123
102
  this.intentDir.execute(falseIntentDirective, () => {
124
103
  // console.log("result === false. stopOnConditionMet?", stopOnConditionMet);
@@ -3,35 +3,25 @@ const { Directives } = require('./Directives.js');
3
3
 
4
4
  class DirMessage {
5
5
 
6
- // constructor(settings) {
7
- // if (!settings.API_ENDPOINT) {
8
- // throw new Error("settings.API_ENDPOINT is mandatory!");
9
- // }
10
- // this.API_ENDPOINT = settings.API_ENDPOINT;
11
- // this.TILEBOT_ENDPOINT = settings.TILEBOT_ENDPOINT;
12
- // this.projectId = settings.projectId;
13
- // this.requestId = settings.requestId;
14
- // this.token = settings.token;
15
- // }
16
-
17
6
  constructor(context) {
18
7
  if (!context) {
19
8
  throw new Error('context object is mandatory.');
20
9
  }
21
10
  this.context = context;
22
- this.API_ENDPOINT = context.TILEDESK_APIURL,
11
+ this.API_ENDPOINT = context.API_ENDPOINT,
23
12
  this.TILEBOT_ENDPOINT = context.TILEBOT_ENDPOINT;
24
13
  this.projectId = context.projectId;
25
14
  this.requestId = context.requestId;
26
15
  this.token = context.token;
27
16
  this.log = this.context.log;
17
+ this.supportRequest = this.context.supportRequest
28
18
  }
29
19
 
30
20
  execute(directive, callback) {
31
21
  let action;
32
22
  if (directive.action) {
33
- if (this.log) {console.log("got action:", JSON.stringify(action));}
34
23
  action = directive.action;
24
+ if (this.log) {console.log("got action:", JSON.stringify(action));}
35
25
  if (!action.attributes) {
36
26
  action.attributes = {}
37
27
  }
@@ -56,10 +46,12 @@ class DirMessage {
56
46
  // }
57
47
  // console.log("final message action:", JSON.stringify(action));
58
48
  }
49
+ // DEPRECATED
59
50
  else if (directive.parameter) {
60
51
  let text = directive.parameter.trim();
61
52
  action = {
62
53
  text: text,
54
+ isInfo: true,
63
55
  attributes: {
64
56
  directives: false,
65
57
  splits: true,
@@ -69,6 +61,7 @@ class DirMessage {
69
61
  }
70
62
  if (directive.name === Directives.HMESSAGE) {
71
63
  action.attributes.subtype = "info";
64
+ // this.hMessage = true;
72
65
  }
73
66
  // if (directive.name === Directives.HMESSAGE) {
74
67
  // action.sender = "tiledesk";
@@ -93,12 +86,21 @@ class DirMessage {
93
86
  // const message = action.body.message;
94
87
  const message = action;
95
88
  if (this.log) {console.log("Message to extEndpoint:", JSON.stringify(message))};
96
- let extEndpoint = `${this.API_ENDPOINT}/modules/tilebot`;
97
- if (this.TILEBOT_ENDPOINT) {
98
- extEndpoint = `${this.TILEBOT_ENDPOINT}`;
89
+
90
+ if(!action.isInfo && this.supportRequest && !this.supportRequest.draft){
91
+ callback();
92
+ return;
99
93
  }
94
+ delete action.isInfo
95
+ // if (this.projectId === "656054000410fa00132e5dcc") {
96
+ // if (!message.text.startsWith('/')) {
97
+ // callback();
98
+ // return;
99
+ // }
100
+ // }
101
+
100
102
  const apiext = new ExtApi({
101
- ENDPOINT: extEndpoint,
103
+ TILEBOT_ENDPOINT: this.TILEBOT_ENDPOINT,
102
104
  log: false
103
105
  });
104
106
  if (message.text) {
@@ -0,0 +1,136 @@
1
+ let axios = require('axios');
2
+ let https = require("https");
3
+ const { v4: uuidv4 } = require('uuid');
4
+
5
+ class DirMessageToBot {
6
+
7
+ constructor(context) {
8
+ if (!context) {
9
+ throw new Error('context object is mandatory.');
10
+ }
11
+ this.context = context;
12
+ this.API_ENDPOINT = context.API_ENDPOINT,
13
+ this.TILEBOT_ENDPOINT = context.TILEBOT_ENDPOINT;
14
+ this.supportRequest = context.supportRequest;
15
+ this.token = context.token;
16
+ this.log = context.log;
17
+ }
18
+
19
+ execute(directive, callback) {
20
+ // console.log("exec intent:", JSON.stringify(directive));
21
+ let action;
22
+ if (directive.action) {
23
+ // console.log("got intent action:", JSON.stringify(directive.action));
24
+ action = directive.action;
25
+ }
26
+ else {
27
+ console.error("Incorrect directive:", directive);
28
+ callback();
29
+ return;
30
+ }
31
+ this.go(action, (stop) => {
32
+ callback(stop);
33
+ });
34
+ }
35
+
36
+ go(action, callback) {
37
+ // console.log("message action intent:", action);
38
+ const message = action.message;
39
+ // const projectId = this.supportRequest.id_project;
40
+ // const requestId = this.supportRequest.request_id;
41
+ const botId = this.supportRequest.bot_id;
42
+
43
+ let outgoing_message = {
44
+ "payload": message,
45
+ "token": this.token
46
+ }
47
+ if (this.log) {console.log("sending message:", JSON.stringify(outgoing_message));}
48
+
49
+ this.sendMessageToBot(this.TILEBOT_ENDPOINT, outgoing_message, botId, () => {
50
+ // console.log("(DirMessageToBot) sendMessageToBot() req_body sent");
51
+ callback(true);
52
+ });
53
+ }
54
+
55
+ /**
56
+ * A stub to send message to the "ext/botId" endpoint, hosted by tilebot on:
57
+ * /${TILEBOT_ROUTE}/ext/${botId}
58
+ *
59
+ * @param {Object} message. The message to send
60
+ * @param {string} botId. Tiledesk botId
61
+ * @param {string} token. User token
62
+ */
63
+ sendMessageToBot(TILEBOT_ENDPOINT, message, botId, callback) {
64
+ // const jwt_token = this.fixToken(token);
65
+ const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
66
+ // console.log("sendMessageToBot URL", url);
67
+ const HTTPREQUEST = {
68
+ url: url,
69
+ headers: {
70
+ 'Content-Type' : 'application/json'
71
+ },
72
+ json: message,
73
+ method: 'POST'
74
+ };
75
+ this.myrequest(
76
+ HTTPREQUEST,
77
+ function(err, resbody) {
78
+ if (err) {
79
+ if (callback) {
80
+ callback(err);
81
+ }
82
+ }
83
+ else {
84
+ if (callback) {
85
+ callback(null, resbody);
86
+ }
87
+ }
88
+ }, false
89
+ );
90
+ }
91
+
92
+ myrequest(options, callback, log) {
93
+ if (this.log) {
94
+ console.log("API URL:", options.url);
95
+ console.log("** Options:", JSON.stringify(options));
96
+ }
97
+ let axios_options = {
98
+ url: options.url,
99
+ method: options.method,
100
+ data: options.json,
101
+ params: options.params,
102
+ headers: options.headers
103
+ }
104
+ if (options.url.startsWith("https:")) {
105
+ const httpsAgent = new https.Agent({
106
+ rejectUnauthorized: false,
107
+ });
108
+ axios_options.httpsAgent = httpsAgent;
109
+ }
110
+ axios(axios_options)
111
+ .then((res) => {
112
+ if (this.log) {
113
+ console.log("Response for url:", options.url);
114
+ console.log("Response headers:\n", JSON.stringify(res.headers));
115
+ }
116
+ if (res && res.status == 200 && res.data) {
117
+ if (callback) {
118
+ callback(null, res.data);
119
+ }
120
+ }
121
+ else {
122
+ if (callback) {
123
+ callback(TiledeskClient.getErr({message: "Response status not 200"}, options, res), null, null);
124
+ }
125
+ }
126
+ })
127
+ .catch( (error) => {
128
+ console.error("(DirMessageToBot) Axios error: ", JSON.stringify(error));
129
+ if (callback) {
130
+ callback(error, null, null);
131
+ }
132
+ });
133
+ }
134
+ }
135
+
136
+ module.exports = { DirMessageToBot };
@@ -2,6 +2,7 @@
2
2
  const { Directives } = require('./Directives');
3
3
  const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
4
4
  const { TiledeskChatbotConst } = require('../../models/TiledeskChatbotConst');
5
+ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
5
6
 
6
7
  class DirMoveToAgent {
7
8
 
@@ -10,106 +11,38 @@ class DirMoveToAgent {
10
11
  throw new Error('context object is mandatory.');
11
12
  }
12
13
  this.context = context;
13
- // let context = {
14
- // projectId: projectId,
15
- // token: token,
16
- // supportRequest: supportRequest,
17
- // requestId: supportRequest.request_id,
18
- // TILEDESK_APIURL: API_URL,
19
- // TILEBOT_ENDPOINT:TILEBOT_ENDPOINT,
20
- // departmentId: depId,
21
- // tdcache: tdcache,
22
- // log: false
23
- // }
24
- // new TiledeskClient({
25
- // projectId: context.projectId,
26
- // token: context.token,
27
- // APIURL: context.TILEDESK_APIURL,
28
- // APIKEY: "___",
29
- // log: context.log
30
- // });
31
- this.tdclient = context.tdclient;
32
14
  this.tdcache = context.tdcache;
33
15
  this.requestId = context.requestId;
34
- // this.depId = context.departmentId;
35
16
  this.log = context.log;
17
+
18
+ this.API_ENDPOINT = context.API_ENDPOINT;
19
+ this.tdClient = new TiledeskClient({
20
+ projectId: this.context.projectId,
21
+ token: this.context.token,
22
+ APIURL: this.API_ENDPOINT,
23
+ APIKEY: "___",
24
+ log: this.log
25
+ });
26
+
36
27
  }
37
28
 
38
29
  execute(directive, callback) {
39
30
  directive.action = {};
40
- // if (directive.name === Directives.WHEN_ONLINE_MOVE_TO_AGENT) { // TEMP
41
- // directive.action = {
42
- // whenOnlineOnly: true
43
- // }
44
- // }
45
31
  this.go(directive.action, () => {
46
32
  callback();
47
33
  });
48
34
  }
49
35
 
50
36
  async go(action, callback) {
51
- // let depId = null;
52
- // if (this.log) {console.log("DirMoveToAgent this.context.departmentId:", this.context.departmentId);}
53
- // if (this.context.departmentId) {
54
- // depId = this.context.departmentId
55
- // if (this.log) {console.log("DirMoveToAgent depId:", depId);}
56
- // }
57
- // else if (this.tdcache) {
58
- // depId =
59
- // await TiledeskChatbot.getParameterStatic(
60
- // this.tdcache, this.requestId, TiledeskChatbotConst.REQ_DEPARTMENT_ID_KEY
61
- // );
62
- // if (this.log) {console.log("DirMoveToAgent depId (cache):", depId);}
63
- // }
64
- // if (this.log) {console.log("DirMoveToAgent anyway depId is:", depId);}
65
- // if (action.whenOnlineOnly === true) {
66
- // this.tdclient.openNow( async (err, result) => {
67
- // if (err) {
68
- // console.error("Agent in DirOfflineHours Error:", err);
69
- // callback();
70
- // }
71
- // else {
72
- // if (result && result.isopen) {
73
- // // if (depId) {
74
- // // this.tdclient.agent(this.requestId, depId, (err) => {
75
- // this.tdclient.moveToAgent(this.requestId, (err) => {
76
- // if (err) {
77
- // console.error("Error moving to agent during online hours:", err);
78
- // }
79
- // else {
80
- // console.log("Successfully moved to agent during online hours");
81
- // }
82
- // callback();
83
- // });
84
- // // }
85
- // // else {
86
- // // callback();
87
- // // }
88
- // }
89
- // else {
90
- // callback();
91
- // }
92
- // }
93
- // });
94
- // }
95
- // else {
96
- // if (depId) {
97
- // this.tdclient.agent(this.requestId, depId, (err) => {
98
- this.tdclient.moveToAgent(this.requestId, (err) => {
99
- if (err) {
100
- console.error("Error moving to agent:", err);
101
- }
102
- else {
103
- // console.log("Successfully moved to agent");
104
- }
105
- callback();
106
- });
107
- // }
108
- // else {
109
- // callback();
110
- // }
111
-
112
- // }
37
+ this.tdClient.moveToAgent(this.requestId, (err) => {
38
+ if (err) {
39
+ console.error("Error moving to agent:", err);
40
+ }
41
+ else {
42
+ // console.log("Successfully moved to agent");
43
+ }
44
+ callback();
45
+ });
113
46
  }
114
47
 
115
48
  }
@@ -0,0 +1,59 @@
1
+ // const { TiledeskClient } = require('@tiledesk/tiledesk-client');
2
+ const { Directives } = require('./Directives');
3
+ const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
4
+ const { TiledeskChatbotConst } = require('../../models/TiledeskChatbotConst');
5
+ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
6
+
7
+ class DirMoveToUnassigned {
8
+
9
+ constructor(context) {
10
+ if (!context) {
11
+ throw new Error('context object is mandatory.');
12
+ }
13
+ this.context = context;
14
+ this.requestId = context.requestId;
15
+ this.log = context.log;
16
+
17
+ this.API_ENDPOINT = context.API_ENDPOINT;
18
+ this.tdClient = new TiledeskClient({
19
+ projectId: this.context.projectId,
20
+ token: this.context.token,
21
+ APIURL: this.API_ENDPOINT,
22
+ APIKEY: "___",
23
+ log: this.log
24
+ });
25
+ }
26
+
27
+ execute(directive, callback) {
28
+ directive.action = {};
29
+ this.go(directive.action, () => {
30
+ callback();
31
+ });
32
+ }
33
+
34
+ async go(action, callback) {
35
+ this.moveToUnassigned(this.requestId, (err) => {
36
+ if (err) {
37
+ console.error("Error moving to unassigned:", err);
38
+ }
39
+ else {
40
+ // console.log("Successfully moved to unassigned");
41
+ }
42
+ callback();
43
+ });
44
+ }
45
+
46
+ async moveToUnassigned(requestId, callback) {
47
+ const empty_participants = [] // STATUS WILL MOVE AUTOMATICALLY TO UNASSIGNED (100)
48
+ this.tdClient.updateRequestParticipants(requestId, empty_participants, (err) => {
49
+ if (callback) {
50
+ callback(err);
51
+ }
52
+ });
53
+ }
54
+
55
+
56
+
57
+ }
58
+
59
+ module.exports = { DirMoveToUnassigned };