@tiledesk/tiledesk-tybot-connector 0.5.0 → 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/ExtApi.js +4 -83
- package/ExtUtil.js +0 -1
- package/Logger.js +105 -0
- package/TdCache.js +5 -3
- package/TiledeskClientTest.js +5 -28
- package/TiledeskExpression.js +6 -47
- package/{models → engine}/IntentForm.js +30 -44
- package/{models → engine}/IntentsMachineFactory.js +5 -4
- package/{models → engine}/MongodbBotsDataSource.js +31 -55
- package/{models → engine}/MongodbIntentsMachine.js +6 -5
- package/{models → engine}/TiledeskChatbot.js +82 -259
- package/{models → engine}/TiledeskChatbotConst.js +9 -0
- package/{models → engine}/TiledeskIntentsMachine.js +5 -15
- package/{models → engine/mock}/MockBotsDataSource.js +3 -19
- package/{models → engine/mock}/MockTdCache.js +0 -9
- package/index.js +103 -380
- package/logs/app.log +12723 -277
- package/logs/app1.log +62258 -0
- package/logs/app2.log +46280 -0
- package/logs/app3.log +31686 -0
- package/logs/app4.log +46163 -0
- package/logs/app5.log +33173 -0
- package/models/faq.js +2 -5
- package/package.json +5 -2
- package/{TiledeskServices → services}/AIService.js +4 -3
- package/{models/faqKbService.js → services/FaqKbService.js} +1 -2
- package/{models/faqService.js → services/FaqService.js} +4 -3
- package/services/IntegrationService.js +43 -0
- package/services/TilebotService.js +47 -0
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +49 -135
- package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +8 -14
- package/tiledeskChatbotPlugs/Filler.js +0 -1
- package/tiledeskChatbotPlugs/MarkbotChatbotPlug.js +12 -20
- package/tiledeskChatbotPlugs/MessagePipeline.js +5 -15
- package/tiledeskChatbotPlugs/SplitsChatbotPlug.js +4 -13
- package/tiledeskChatbotPlugs/TiledeskRequestVariables.js +1 -1
- package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +21 -89
- package/tiledeskChatbotPlugs/directives/DEPRECATED_DirSetAttribute.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirAddTags.js +23 -140
- package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +42 -128
- package/tiledeskChatbotPlugs/directives/DirAskGPT.js +37 -127
- package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +52 -145
- package/tiledeskChatbotPlugs/directives/DirAssign.js +12 -23
- package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +16 -12
- package/tiledeskChatbotPlugs/directives/DirAssistant.js +56 -198
- package/tiledeskChatbotPlugs/directives/DirBrevo.js +41 -101
- package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +9 -19
- package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirClose.js +4 -2
- package/tiledeskChatbotPlugs/directives/DirCode.js +16 -23
- package/tiledeskChatbotPlugs/directives/DirCondition.js +19 -25
- package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +9 -82
- package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +10 -22
- package/tiledeskChatbotPlugs/directives/DirCustomerio.js +34 -89
- package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +17 -22
- package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +8 -8
- package/tiledeskChatbotPlugs/directives/DirDepartment.js +13 -12
- package/tiledeskChatbotPlugs/directives/DirDisableInputText.js +2 -6
- package/tiledeskChatbotPlugs/directives/DirFireTiledeskEvent.js +4 -4
- package/tiledeskChatbotPlugs/directives/DirForm.js +17 -24
- package/tiledeskChatbotPlugs/directives/DirGptTask.js +49 -139
- package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +8 -53
- package/tiledeskChatbotPlugs/directives/DirHubspot.js +31 -91
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +19 -26
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +24 -78
- package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +17 -68
- package/tiledeskChatbotPlugs/directives/DirIntent.js +6 -100
- package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +15 -14
- package/tiledeskChatbotPlugs/directives/DirLockIntent.js +7 -12
- package/tiledeskChatbotPlugs/directives/DirMake.js +21 -53
- package/tiledeskChatbotPlugs/directives/DirMessage.js +35 -28
- package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +8 -87
- package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +6 -4
- package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +6 -4
- package/tiledeskChatbotPlugs/directives/DirQapla.js +26 -120
- package/tiledeskChatbotPlugs/directives/DirRandomReply.js +11 -19
- package/tiledeskChatbotPlugs/directives/DirRemoveCurrentBot.js +4 -0
- package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +5 -2
- package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +12 -58
- package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +11 -57
- package/tiledeskChatbotPlugs/directives/DirReply.js +36 -30
- package/tiledeskChatbotPlugs/directives/DirReplyV2.js +30 -72
- package/tiledeskChatbotPlugs/directives/DirSendEmail.js +9 -10
- package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +15 -40
- package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +8 -5
- package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +25 -113
- package/tiledeskChatbotPlugs/directives/DirSetConversationTags.js +14 -13
- package/tiledeskChatbotPlugs/directives/DirUnlockIntent.js +3 -3
- package/tiledeskChatbotPlugs/directives/DirWait.js +5 -6
- package/tiledeskChatbotPlugs/directives/DirWebRequest.js +14 -49
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +22 -77
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirWebResponse.js +11 -14
- package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +13 -42
- package/tiledeskChatbotPlugs/directives/Directives.js +0 -3
- package/utils/HttpUtils.js +128 -0
- package/{models → utils}/TiledeskChatbotUtil.js +164 -234
- package/utils/winston.js +42 -0
- package/TiledeskServices/utils.js +0 -99
- /package/{models → engine/mock}/MockIntentsMachine.js +0 -0
|
@@ -2,6 +2,8 @@ let axios = require('axios');
|
|
|
2
2
|
let https = require("https");
|
|
3
3
|
const { v4: uuidv4 } = require('uuid');
|
|
4
4
|
const ms = require('minimist-string');
|
|
5
|
+
const winston = require('../../utils/winston');
|
|
6
|
+
const tilebotService = require('../../services/TilebotService');
|
|
5
7
|
|
|
6
8
|
class DirConnectBlock {
|
|
7
9
|
constructor(context) {
|
|
@@ -17,12 +19,13 @@ class DirConnectBlock {
|
|
|
17
19
|
}
|
|
18
20
|
|
|
19
21
|
execute(directive, callback) {
|
|
22
|
+
winston.verbose("Execute ConnectBlock directive");
|
|
20
23
|
let action;
|
|
21
24
|
if (directive.action) {
|
|
22
25
|
action = directive.action;
|
|
23
26
|
}
|
|
24
27
|
else {
|
|
25
|
-
|
|
28
|
+
winston.warn("DirConnectBlock Incorrect directive: ", directive);
|
|
26
29
|
callback();
|
|
27
30
|
return;
|
|
28
31
|
}
|
|
@@ -32,6 +35,7 @@ class DirConnectBlock {
|
|
|
32
35
|
}
|
|
33
36
|
|
|
34
37
|
go(action, callback) {
|
|
38
|
+
winston.debug("(DirConnectBlock) Action: ", action);
|
|
35
39
|
const intentName = action.intentName;
|
|
36
40
|
const projectId = this.supportRequest.id_project;
|
|
37
41
|
const requestId = this.supportRequest.request_id;
|
|
@@ -61,9 +65,9 @@ class DirConnectBlock {
|
|
|
61
65
|
},
|
|
62
66
|
"token": this.token
|
|
63
67
|
}
|
|
64
|
-
|
|
68
|
+
winston.debug("(DirConnectBlock) move to intent message: ", intent_command_request);
|
|
65
69
|
|
|
66
|
-
|
|
70
|
+
tilebotService.sendMessageToBot(intent_command_request, botId, () => {
|
|
67
71
|
callback();
|
|
68
72
|
});
|
|
69
73
|
}
|
|
@@ -75,7 +79,7 @@ class DirConnectBlock {
|
|
|
75
79
|
string_params = JSON.stringify(json_params);
|
|
76
80
|
}
|
|
77
81
|
catch (error) {
|
|
78
|
-
|
|
82
|
+
winston.error("(DirConnectBlock) Error stringing JSON PARAMS: ", json_params);
|
|
79
83
|
}
|
|
80
84
|
}
|
|
81
85
|
if (string_params != null) {
|
|
@@ -99,84 +103,7 @@ class DirConnectBlock {
|
|
|
99
103
|
return intentDirective;
|
|
100
104
|
}
|
|
101
105
|
|
|
102
|
-
|
|
103
|
-
* A stub to send message to the "ext/botId" endpoint, hosted by tilebot on:
|
|
104
|
-
* /${TILEBOT_ROUTE}/ext/${botId}
|
|
105
|
-
*
|
|
106
|
-
* @param {Object} message. The message to send
|
|
107
|
-
* @param {string} botId. Tiledesk botId
|
|
108
|
-
* @param {string} token. User token
|
|
109
|
-
*/
|
|
110
|
-
sendMessageToBot(TILEBOT_ENDPOINT, message, botId, callback) {
|
|
111
|
-
const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
|
|
112
|
-
// console.log("sendMessageToBot URL", url);
|
|
113
|
-
const HTTPREQUEST = {
|
|
114
|
-
url: url,
|
|
115
|
-
headers: {
|
|
116
|
-
'Content-Type' : 'application/json'
|
|
117
|
-
},
|
|
118
|
-
json: message,
|
|
119
|
-
method: 'POST'
|
|
120
|
-
};
|
|
121
|
-
this.myrequest(
|
|
122
|
-
HTTPREQUEST,
|
|
123
|
-
function(err, resbody) {
|
|
124
|
-
if (err) {
|
|
125
|
-
if (callback) {
|
|
126
|
-
callback(err);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
else {
|
|
130
|
-
if (callback) {
|
|
131
|
-
callback(null, resbody);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}, false
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
myrequest(options, callback, log) {
|
|
139
|
-
if (this.log) {
|
|
140
|
-
console.log("API URL:", options.url);
|
|
141
|
-
console.log("** Options:", JSON.stringify(options));
|
|
142
|
-
}
|
|
143
|
-
let axios_options = {
|
|
144
|
-
url: options.url,
|
|
145
|
-
method: options.method,
|
|
146
|
-
data: options.json,
|
|
147
|
-
params: options.params,
|
|
148
|
-
headers: options.headers
|
|
149
|
-
}
|
|
150
|
-
if (options.url.startsWith("https:")) {
|
|
151
|
-
const httpsAgent = new https.Agent({
|
|
152
|
-
rejectUnauthorized: false,
|
|
153
|
-
});
|
|
154
|
-
axios_options.httpsAgent = httpsAgent;
|
|
155
|
-
}
|
|
156
|
-
axios(axios_options)
|
|
157
|
-
.then((res) => {
|
|
158
|
-
if (this.log) {
|
|
159
|
-
console.log("Response for url:", options.url);
|
|
160
|
-
console.log("Response headers:\n", JSON.stringify(res.headers));
|
|
161
|
-
}
|
|
162
|
-
if (res && res.status == 200 && res.data) {
|
|
163
|
-
if (callback) {
|
|
164
|
-
callback(null, res.data);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
else {
|
|
168
|
-
if (callback) {
|
|
169
|
-
callback(TiledeskClient.getErr({message: "Response status not 200"}, options, res), null, null);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
})
|
|
173
|
-
.catch( (error) => {
|
|
174
|
-
console.error("(DirConnectBlock) Axios error: ", JSON.stringify(error));
|
|
175
|
-
if (callback) {
|
|
176
|
-
callback(error, null, null);
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
}
|
|
106
|
+
|
|
180
107
|
}
|
|
181
108
|
|
|
182
109
|
module.exports = { DirConnectBlock };
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
const { Filler } = require('../Filler');
|
|
2
|
-
const { TiledeskChatbot } = require('../../
|
|
3
|
-
const { TiledeskChatbotUtil } = require('../../
|
|
2
|
+
const { TiledeskChatbot } = require('../../engine/TiledeskChatbot');
|
|
3
|
+
const { TiledeskChatbotUtil } = require('../../utils/TiledeskChatbotUtil');
|
|
4
4
|
let axios = require('axios');
|
|
5
|
-
const { TiledeskChatbotConst } = require('../../
|
|
5
|
+
const { TiledeskChatbotConst } = require('../../engine/TiledeskChatbotConst');
|
|
6
6
|
const { TiledeskClient } = require('@tiledesk/tiledesk-client');
|
|
7
|
+
const winston = require('../../utils/winston');
|
|
7
8
|
|
|
8
9
|
class DirContactUpdate {
|
|
9
10
|
|
|
@@ -30,6 +31,7 @@ class DirContactUpdate {
|
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
execute(directive, callback) {
|
|
34
|
+
winston.verbose("Execute ContactUpdate directive")
|
|
33
35
|
let action;
|
|
34
36
|
if (directive.action) {
|
|
35
37
|
action = directive.action;
|
|
@@ -39,7 +41,7 @@ class DirContactUpdate {
|
|
|
39
41
|
action.attributes.fillParams = true;
|
|
40
42
|
}
|
|
41
43
|
else {
|
|
42
|
-
|
|
44
|
+
winston.warn("DirContactUpdate Incorrect directive: ", directive);
|
|
43
45
|
callback();
|
|
44
46
|
return;
|
|
45
47
|
}
|
|
@@ -49,7 +51,7 @@ class DirContactUpdate {
|
|
|
49
51
|
}
|
|
50
52
|
|
|
51
53
|
async go(action, callback) {
|
|
52
|
-
|
|
54
|
+
winston.debug("(DirContactUpdate) Action: ", action);
|
|
53
55
|
const contactProperties = action.update;
|
|
54
56
|
|
|
55
57
|
// fill
|
|
@@ -64,32 +66,23 @@ class DirContactUpdate {
|
|
|
64
66
|
let updateProperties = {}
|
|
65
67
|
for (const [key, value] of Object.entries(contactProperties)) {
|
|
66
68
|
let filled_value = filler.fill(value, requestAttributes);
|
|
67
|
-
if (this.log) {console.log("(DirContactUpdate) setting property key:",key, "with value:", value, "filled value:", filled_value); }
|
|
68
69
|
updateProperties[key] = filled_value;
|
|
69
70
|
// it's important that all the lead's properties are immediatly updated in the current flow invocation so the updated values will be available in the next actions
|
|
70
71
|
if (key === "fullname") {
|
|
71
72
|
await this.context.chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, filled_value);
|
|
72
|
-
if (this.log) {console.log("(DirContactUpdate) updating attribute:",TiledeskChatbotConst.REQ_LEAD_USERFULLNAME_KEY, "with property key:", key, "and value:", filled_value); }
|
|
73
73
|
}
|
|
74
74
|
else if ( key === "email") {
|
|
75
75
|
await this.context.chatbot.addParameter(TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY, filled_value);
|
|
76
|
-
if (this.log) {console.log("(DirContactUpdate) updating attribute:",TiledeskChatbotConst.REQ_LEAD_EMAIL_KEY, "with property key:", key, "and value:", filled_value); }
|
|
77
76
|
}
|
|
78
|
-
// else if (key === "phone") {
|
|
79
|
-
// static REQ_USER_PHONE_KEY = "userPhone";
|
|
80
|
-
// }
|
|
81
|
-
if (this.log) {console.log("(DirContactUpdate) updating property:", key, "value:", filled_value); }
|
|
82
77
|
}
|
|
83
78
|
const leadId = requestAttributes[TiledeskChatbotConst.REQ_USER_LEAD_ID_KEY];
|
|
84
79
|
this.tdClient.updateLead(leadId, updateProperties, null, null, () => {
|
|
85
|
-
if (this.log) {console.log("(DirContactUpdate) Lead updated.", updateProperties);}
|
|
86
80
|
// send hidden info to update widget lead fullname only if it is a conversation!
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
81
|
+
winston.debug("(DirContactUpdate) requestId: " + this.requestId);
|
|
82
|
+
winston.debug("(DirContactUpdate) updateProperties: ", updateProperties);
|
|
83
|
+
winston.debug("(DirContactUpdate) updateProperties['fullname']: " + updateProperties['fullname']);
|
|
90
84
|
callback();
|
|
91
85
|
// if (this.requestId.startsWith("support-group") && updateProperties['userFullname']) {
|
|
92
|
-
// if (this.log) {console.log("(DirContactUpdate) send hidden info to update widget lead fullname"); }
|
|
93
86
|
// const userFullname = updateProperties['fullname'];
|
|
94
87
|
// const updateLeadDataOnWidgetMessage = {
|
|
95
88
|
// type: "text",
|
|
@@ -99,15 +92,10 @@ class DirContactUpdate {
|
|
|
99
92
|
// updateUserFullname: userFullname
|
|
100
93
|
// }
|
|
101
94
|
// };
|
|
102
|
-
// if (this.log) {console.log("(DirContactUpdate) sending updateLeadDataOnWidgetMessage:", updateLeadDataOnWidgetMessage); }
|
|
103
95
|
// this.tdClient.sendSupportMessage(
|
|
104
96
|
// this.requestId,
|
|
105
97
|
// updateLeadDataOnWidgetMessage,
|
|
106
98
|
// (err) => {
|
|
107
|
-
// if (err) {
|
|
108
|
-
// console.error("(DirContactUpdate) Error sending reply:", err);
|
|
109
|
-
// }
|
|
110
|
-
// if (this.log) {console.log("(DirContactUpdate) hidden message sent:", updateLeadDataOnWidgetMessage);}
|
|
111
99
|
// callback();
|
|
112
100
|
// });
|
|
113
101
|
// }
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
const axios = require("axios").default;
|
|
2
|
-
const { TiledeskChatbot } = require("../../
|
|
2
|
+
const { TiledeskChatbot } = require("../../engine/TiledeskChatbot");
|
|
3
3
|
const { Filler } = require("../Filler");
|
|
4
4
|
const { DirIntent } = require("./DirIntent");
|
|
5
5
|
let https = require("https");
|
|
6
6
|
require('dotenv').config();
|
|
7
|
-
|
|
7
|
+
const winston = require('../../utils/winston');
|
|
8
|
+
const integrationService = require("../../services/IntegrationService");
|
|
8
9
|
|
|
9
10
|
class DirCustomerio {
|
|
10
11
|
|
|
@@ -15,19 +16,21 @@ class DirCustomerio {
|
|
|
15
16
|
this.context = context;
|
|
16
17
|
this.tdcache = this.context.tdcache;
|
|
17
18
|
this.requestId = this.context.requestId;
|
|
19
|
+
this.projectId = this.context.projectId;
|
|
20
|
+
this.token = this.context.token;
|
|
18
21
|
this.intentDir = new DirIntent(context);
|
|
19
22
|
this.API_ENDPOINT = this.context.API_ENDPOINT;
|
|
20
23
|
this.log = context.log;
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
execute(directive, callback) {
|
|
24
|
-
|
|
27
|
+
winston.verbose("Execute Customerio directive");
|
|
25
28
|
let action;
|
|
26
29
|
if (directive.action) {
|
|
27
30
|
action = directive.action;
|
|
28
31
|
}
|
|
29
32
|
else {
|
|
30
|
-
|
|
33
|
+
winston.warn("DirCustomerio Incorrect directive: ", directive);
|
|
31
34
|
callback();
|
|
32
35
|
return;
|
|
33
36
|
}
|
|
@@ -37,19 +40,18 @@ class DirCustomerio {
|
|
|
37
40
|
}
|
|
38
41
|
|
|
39
42
|
async go(action, callback) {
|
|
40
|
-
|
|
43
|
+
winston.debug("(DirCustomerio) Action: ", action);
|
|
41
44
|
if (!this.tdcache) {
|
|
42
|
-
|
|
45
|
+
winston.error("(DirCustomerio) Error: tdcache is mandatory");
|
|
43
46
|
callback();
|
|
44
47
|
return;
|
|
45
48
|
}
|
|
46
49
|
|
|
47
50
|
let trueIntent = action.trueIntent;
|
|
48
51
|
let falseIntent = action.falseIntent;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
52
|
+
|
|
53
|
+
winston.debug("(DirCustomerio) trueIntent " + trueIntent)
|
|
54
|
+
winston.debug("(DirCustomerio) falseIntent " + falseIntent)
|
|
53
55
|
|
|
54
56
|
let requestVariables = null;
|
|
55
57
|
requestVariables =
|
|
@@ -59,23 +61,22 @@ class DirCustomerio {
|
|
|
59
61
|
|
|
60
62
|
let formid = action.formid;
|
|
61
63
|
let bodyParameters = action.bodyParameters;
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
64
|
+
|
|
65
|
+
winston.debug("(DirCustomerio) formid: " + formid);
|
|
66
|
+
winston.debug("(DirCustomerio) bodyParameters: ", bodyParameters);
|
|
66
67
|
|
|
67
68
|
if (!bodyParameters || bodyParameters === '') {
|
|
68
|
-
|
|
69
|
+
winston.debug("(DirCustomerio) Error: bodyParameters is undefined or null or empty string");
|
|
69
70
|
callback();
|
|
70
71
|
return;
|
|
71
72
|
}
|
|
72
73
|
|
|
73
74
|
const customerio_base_url = process.env.CUSTOMERIO_ENDPOINT || "https://track.customer.io/api/v1";
|
|
74
|
-
|
|
75
|
+
winston.debug("(DirCustomerio) customerio_base_url: " + customerio_base_url);
|
|
75
76
|
|
|
76
|
-
let key = await
|
|
77
|
+
let key = await integrationService.getKeyFromIntegrations(this.projectId, 'customerio', this.token);
|
|
77
78
|
if (!key) {
|
|
78
|
-
|
|
79
|
+
winston.debug("(DirCustomerio) - Key not found in Integrations.");
|
|
79
80
|
let status = 422;
|
|
80
81
|
let error = 'Missing customerio access token';
|
|
81
82
|
await this.#assignAttributes(action, status, error);
|
|
@@ -88,11 +89,11 @@ class DirCustomerio {
|
|
|
88
89
|
|
|
89
90
|
const filler = new Filler();
|
|
90
91
|
for (const [key, value] of Object.entries(bodyParameters)) {
|
|
91
|
-
|
|
92
|
+
winston.debug("(DirCustomerio) bodyParam: " + key + " value: " + value)
|
|
92
93
|
let filled_value = filler.fill(value, requestVariables);
|
|
93
94
|
bodyParameters[key] = filled_value;
|
|
94
95
|
}
|
|
95
|
-
|
|
96
|
+
winston.debug("(DirCustomerio) bodyParameters filler: ", bodyParameters)
|
|
96
97
|
|
|
97
98
|
let json = {
|
|
98
99
|
data: bodyParameters
|
|
@@ -110,16 +111,15 @@ class DirCustomerio {
|
|
|
110
111
|
json: json,
|
|
111
112
|
method: "POST"
|
|
112
113
|
}
|
|
113
|
-
|
|
114
|
+
winston.debug("(DirCustomerio) HttpRequest: ", CUSTOMERIO_HTTPREQUEST);
|
|
114
115
|
|
|
115
116
|
this.#myrequest(
|
|
116
117
|
CUSTOMERIO_HTTPREQUEST, async (err, resbody) => {
|
|
117
118
|
if (err) {
|
|
118
119
|
if (callback) {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
};
|
|
120
|
+
winston.debug("(DirCustomerio) err response:", err.response)
|
|
121
|
+
winston.debug("(DirCustomerio) err data:", err.response.data)
|
|
122
|
+
|
|
123
123
|
let status = null;
|
|
124
124
|
let error;
|
|
125
125
|
|
|
@@ -133,10 +133,9 @@ class DirCustomerio {
|
|
|
133
133
|
error = err.response.data.meta.error;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
}
|
|
136
|
+
winston.debug("(DirCustomerio) err data status: " + status);
|
|
137
|
+
winston.debug("(DirCustomerio) err data error: ", error);
|
|
138
|
+
|
|
140
139
|
await this.#assignAttributes(action, status, error);
|
|
141
140
|
if (falseIntent) {
|
|
142
141
|
await this.#executeCondition(false, trueIntent, null, falseIntent, null);
|
|
@@ -148,7 +147,7 @@ class DirCustomerio {
|
|
|
148
147
|
|
|
149
148
|
}
|
|
150
149
|
} else if (callback) {
|
|
151
|
-
|
|
150
|
+
winston.debug("(DirCustomerio) DirCustomerio resbody: ", resbody);
|
|
152
151
|
|
|
153
152
|
let status = 204;
|
|
154
153
|
let error = null;
|
|
@@ -167,11 +166,9 @@ class DirCustomerio {
|
|
|
167
166
|
}
|
|
168
167
|
|
|
169
168
|
async #assignAttributes(action, status, error) {
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
console.log("DirCustomerio assignAttributes error:", error)
|
|
174
|
-
}
|
|
169
|
+
winston.debug("(DirCustomerio) assignAttributes action: ", action)
|
|
170
|
+
winston.debug("(DirCustomerio) assignAttributes status: " + status)
|
|
171
|
+
winston.debug("(DirCustomerio) assignAttributes error: ", error)
|
|
175
172
|
if (this.context.tdcache) {
|
|
176
173
|
if (action.assignStatusTo) {
|
|
177
174
|
await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignStatusTo, status);
|
|
@@ -179,22 +176,10 @@ class DirCustomerio {
|
|
|
179
176
|
if (action.assignErrorTo) {
|
|
180
177
|
await TiledeskChatbot.addParameterStatic(this.context.tdcache, this.context.requestId, action.assignErrorTo, error);
|
|
181
178
|
}
|
|
182
|
-
|
|
183
|
-
// Debug log
|
|
184
|
-
if (this.log) {
|
|
185
|
-
const all_parameters = await TiledeskChatbot.allParametersStatic(this.context.tdcache, this.context.requestId);
|
|
186
|
-
for (const [key, value] of Object.entries(all_parameters)) {
|
|
187
|
-
if (this.log) { console.log("DirCustomerio request parameter:", key, "value:", value, "type:", typeof value) }
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
179
|
}
|
|
191
180
|
}
|
|
192
181
|
|
|
193
182
|
#myrequest(options, callback) {
|
|
194
|
-
if (this.log) {
|
|
195
|
-
console.log("** API URL:", options.url);
|
|
196
|
-
console.log("** Options:", JSON.stringify(options));
|
|
197
|
-
}
|
|
198
183
|
let axios_options = {
|
|
199
184
|
url: options.url,
|
|
200
185
|
method: options.method,
|
|
@@ -204,9 +189,6 @@ class DirCustomerio {
|
|
|
204
189
|
if (options.json !== null) {
|
|
205
190
|
axios_options.data = options.json
|
|
206
191
|
}
|
|
207
|
-
if (this.log) {
|
|
208
|
-
console.log("axios_options:", JSON.stringify(axios_options));
|
|
209
|
-
}
|
|
210
192
|
if (options.url.startsWith("https:")) {
|
|
211
193
|
const httpsAgent = new https.Agent({
|
|
212
194
|
rejectUnauthorized: false,
|
|
@@ -215,10 +197,6 @@ class DirCustomerio {
|
|
|
215
197
|
}
|
|
216
198
|
axios(axios_options)
|
|
217
199
|
.then((res) => {
|
|
218
|
-
if (this.log) {
|
|
219
|
-
console.log("Response for url:", options.url);
|
|
220
|
-
console.log("Response headers:\n", JSON.stringify(res.headers));
|
|
221
|
-
}
|
|
222
200
|
if (res && (res.status == 200 || res.status == 204) && (res.data || res.config.data)) {
|
|
223
201
|
if (callback) {
|
|
224
202
|
if (res.data) {
|
|
@@ -251,7 +229,6 @@ class DirCustomerio {
|
|
|
251
229
|
if (falseIntent) {
|
|
252
230
|
falseIntentDirective = DirIntent.intentDirectiveFor(falseIntent, falseIntentAttributes);
|
|
253
231
|
}
|
|
254
|
-
if (this.log) { console.log('DirCustomerio executeCondition/result', result) }
|
|
255
232
|
if (result === true) {
|
|
256
233
|
if (trueIntentDirective) {
|
|
257
234
|
this.intentDir.execute(trueIntentDirective, () => {
|
|
@@ -261,7 +238,7 @@ class DirCustomerio {
|
|
|
261
238
|
});
|
|
262
239
|
}
|
|
263
240
|
else {
|
|
264
|
-
|
|
241
|
+
winston.debug("(DirCustomerio) No trueIntentDirective specified");
|
|
265
242
|
if (callback) {
|
|
266
243
|
callback();
|
|
267
244
|
}
|
|
@@ -276,45 +253,13 @@ class DirCustomerio {
|
|
|
276
253
|
});
|
|
277
254
|
}
|
|
278
255
|
else {
|
|
279
|
-
|
|
256
|
+
winston.debug("(DirCustomerio) No falseIntentDirective specified");
|
|
280
257
|
if (callback) {
|
|
281
258
|
callback();
|
|
282
259
|
}
|
|
283
260
|
}
|
|
284
261
|
}
|
|
285
262
|
}
|
|
286
|
-
|
|
287
|
-
async getKeyFromIntegrations() {
|
|
288
|
-
return new Promise((resolve) => {
|
|
289
|
-
|
|
290
|
-
const INTEGRATIONS_HTTPREQUEST = {
|
|
291
|
-
url: this.API_ENDPOINT + "/" + this.context.projectId + "/integration/name/customerio",
|
|
292
|
-
headers: {
|
|
293
|
-
'Content-Type': 'application/json',
|
|
294
|
-
'Authorization': 'JWT ' + this.context.token
|
|
295
|
-
},
|
|
296
|
-
method: "GET"
|
|
297
|
-
}
|
|
298
|
-
if (this.log) { console.log("Customerio INTEGRATIONS_HTTPREQUEST ", INTEGRATIONS_HTTPREQUEST) }
|
|
299
|
-
|
|
300
|
-
this.#myrequest(
|
|
301
|
-
INTEGRATIONS_HTTPREQUEST, async (err, integration) => {
|
|
302
|
-
if (err) {
|
|
303
|
-
resolve(null);
|
|
304
|
-
} else {
|
|
305
|
-
if (this.log) { console.log('Integration: ', integration); }
|
|
306
|
-
if (integration &&
|
|
307
|
-
integration.value) {
|
|
308
|
-
resolve(integration.value.apikey)
|
|
309
|
-
}
|
|
310
|
-
else {
|
|
311
|
-
resolve(null)
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
})
|
|
315
|
-
})
|
|
316
|
-
}
|
|
317
|
-
|
|
318
263
|
}
|
|
319
264
|
|
|
320
265
|
module.exports = { DirCustomerio }
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
const { HelpCenterQuery } = require('@tiledesk/helpcenter-query-client');
|
|
2
|
-
const { TiledeskChatbot } = require('../../
|
|
3
|
-
const { TiledeskChatbotConst } = require('../../
|
|
2
|
+
const { TiledeskChatbot } = require('../../engine/TiledeskChatbot.js');
|
|
3
|
+
const { TiledeskChatbotConst } = require('../../engine/TiledeskChatbotConst.js');
|
|
4
4
|
const ms = require('minimist-string');
|
|
5
5
|
const { TiledeskClient } = require('@tiledesk/tiledesk-client');
|
|
6
|
+
const winston = require('../../utils/winston');
|
|
6
7
|
|
|
7
8
|
class DirDeflectToHelpCenter {
|
|
8
9
|
|
|
@@ -24,6 +25,7 @@ class DirDeflectToHelpCenter {
|
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
async execute(directive, callback) {
|
|
28
|
+
winston.verbose("Execute DeflectToHelpCenter directive");
|
|
27
29
|
let action;
|
|
28
30
|
if (directive.action) {
|
|
29
31
|
action = directive.action
|
|
@@ -60,12 +62,11 @@ class DirDeflectToHelpCenter {
|
|
|
60
62
|
url_target = action.urlTarget;
|
|
61
63
|
}
|
|
62
64
|
// let message = pipeline.message;
|
|
63
|
-
//console.log("help center message", JSON.stringify(message));
|
|
64
65
|
const last_user_text = await TiledeskChatbot.getParameterStatic(
|
|
65
66
|
this.context.tdcache,
|
|
66
67
|
this.context.requestId,
|
|
67
68
|
TiledeskChatbotConst.REQ_LAST_USER_TEXT_KEY);
|
|
68
|
-
|
|
69
|
+
winston.debug("(DirDeflectToHelpCenter) last_user_text", last_user_text);
|
|
69
70
|
if (last_user_text && last_user_text.trim() !== '') {
|
|
70
71
|
const helpcenter = new HelpCenterQuery({
|
|
71
72
|
APIKEY: "__",
|
|
@@ -76,30 +77,29 @@ class DirDeflectToHelpCenter {
|
|
|
76
77
|
helpcenter.APIURL = this.helpcenter_api_endpoint
|
|
77
78
|
}
|
|
78
79
|
if (!workspace_id) {
|
|
79
|
-
|
|
80
|
+
winston.debug("(DirDeflectToHelpCenter) No workspaces_id. Listing all workspaces to eventually select the first");
|
|
80
81
|
try {
|
|
81
82
|
// find/select the first workspace
|
|
82
83
|
const workspaces = await helpcenter.allWorkspaces();
|
|
83
84
|
if (workspaces.length > 0) {
|
|
84
85
|
workspace_id = workspaces[0]._id;
|
|
85
|
-
// console.log("First Workspace selected", workspaces[0]);
|
|
86
86
|
}
|
|
87
87
|
else {
|
|
88
|
-
|
|
88
|
+
winston.debug("(DirDeflectToHelpCenter) No Workspaces found");
|
|
89
89
|
callback(false);
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
catch(err) {
|
|
93
|
-
|
|
93
|
+
winston.error("(DirDeflectToHelpCenter) Error search workspaces: ", err);
|
|
94
94
|
callback(false);
|
|
95
95
|
}
|
|
96
96
|
}
|
|
97
|
-
|
|
97
|
+
winston.debug("(DirDeflectToHelpCenter) searching on workspace_id: " + workspace_id);
|
|
98
98
|
try {
|
|
99
99
|
const results = await helpcenter.search(workspace_id, last_user_text, maxresults);
|
|
100
100
|
if (results && results.length > 0) {
|
|
101
|
-
|
|
102
|
-
|
|
101
|
+
winston.debug("(DirDeflectToHelpCenter) Successfully got results ", results);
|
|
102
|
+
winston.debug("(DirDeflectToHelpCenter) Sending hcReply ", hc_reply);
|
|
103
103
|
// pipeline.message.text = hc_reply;
|
|
104
104
|
let buttons = [];
|
|
105
105
|
results.forEach(content => {
|
|
@@ -141,26 +141,26 @@ class DirDeflectToHelpCenter {
|
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
-
|
|
144
|
+
winston.debug("(DirDeflectToHelpCenter) HC reply: ", message)
|
|
145
145
|
this.tdClient.sendSupportMessage(
|
|
146
146
|
this.context.requestId,
|
|
147
147
|
message,
|
|
148
148
|
(err) => {
|
|
149
149
|
if (err) {
|
|
150
|
-
|
|
150
|
+
winston.error("(DirDeflectToHelpCenter) Error sending reply: " + err.message);
|
|
151
151
|
callback(false);
|
|
152
152
|
}
|
|
153
|
-
|
|
153
|
+
winston.debug("(DirDeflectToHelpCenter) Reply message sent.");
|
|
154
154
|
callback(true);
|
|
155
155
|
});
|
|
156
156
|
}
|
|
157
157
|
else {
|
|
158
|
-
|
|
158
|
+
winston.debug("(DirDeflectToHelpCenter) Nothing found in Help Center. projectId: " + project_id + " workspaceId: " + workspace_id);
|
|
159
159
|
callback(false);
|
|
160
160
|
}
|
|
161
161
|
}
|
|
162
162
|
catch(err) {
|
|
163
|
-
|
|
163
|
+
winston.error("(DirDeflectToHelpCenter) Error (searching results): ", err);
|
|
164
164
|
callback(false);
|
|
165
165
|
}
|
|
166
166
|
}
|
|
@@ -172,9 +172,8 @@ class DirDeflectToHelpCenter {
|
|
|
172
172
|
parseParams(directive_parameter) {
|
|
173
173
|
let workspace_id = null;
|
|
174
174
|
let hc_reply = null;
|
|
175
|
-
|
|
175
|
+
|
|
176
176
|
const params = ms(directive_parameter);
|
|
177
|
-
// console.log("ms decoded params:", params)
|
|
178
177
|
if (params.w) {
|
|
179
178
|
workspace_id = params.w
|
|
180
179
|
}
|
|
@@ -183,16 +182,12 @@ class DirDeflectToHelpCenter {
|
|
|
183
182
|
}
|
|
184
183
|
|
|
185
184
|
if (params.m) {
|
|
186
|
-
// console.log("_params.m:", params.m)
|
|
187
185
|
//hc_reply = params.m.replaceAll("\\n", "\n");
|
|
188
186
|
hc_reply = params.m.replace(/\\n/g, "\n");
|
|
189
|
-
// console.log("hc_reply with replaced slash n regex|replaceAll", hc_reply)
|
|
190
187
|
}
|
|
191
188
|
if (params.message) {
|
|
192
|
-
// console.log("_params.message:", params.message)
|
|
193
189
|
//hc_reply = params.message.replaceAll("\\n", "\n");
|
|
194
190
|
hc_reply = params.message.replace(/\\n/g, "\n");
|
|
195
|
-
// console.log("hc_reply -message with replaced slash n replace(/\\n/g", hc_reply)
|
|
196
191
|
}
|
|
197
192
|
return {
|
|
198
193
|
workspace_id: workspace_id,
|