@tiledesk/tiledesk-tybot-connector 0.5.0-rc2 → 0.5.1-rc1
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 +7 -0
- package/ExtApi.js +2 -71
- package/Logger.js +32 -5
- package/{models → engine}/IntentsMachineFactory.js +1 -1
- package/{models → engine}/MongodbBotsDataSource.js +2 -2
- package/{models → engine}/MongodbIntentsMachine.js +1 -1
- package/{models → engine}/TiledeskChatbot.js +3 -1
- package/{models → engine}/TiledeskIntentsMachine.js +1 -2
- package/{models → engine/mock}/MockBotsDataSource.js +1 -2
- package/index.js +23 -111
- package/logs/app.log +3041 -4469
- package/logs/app3.log +2822 -0
- package/logs/app4.log +46163 -0
- package/logs/app5.log +33173 -0
- package/models/faq.js +2 -5
- package/package.json +1 -1
- package/{TiledeskServices → services}/AIService.js +4 -3
- package/{models/faqKbService.js → services/FaqKbService.js} +1 -1
- 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 +2 -3
- package/tiledeskChatbotPlugs/FillParamsChatbotPlug.js +1 -1
- package/tiledeskChatbotPlugs/TiledeskRequestVariables.js +1 -1
- package/tiledeskChatbotPlugs/directives/DEPRECATED_DirSetAttribute.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirAddTags.js +8 -42
- package/tiledeskChatbotPlugs/directives/DirAiPrompt.js +42 -128
- package/tiledeskChatbotPlugs/directives/DirAskGPT.js +9 -74
- package/tiledeskChatbotPlugs/directives/DirAskGPTV2.js +14 -83
- package/tiledeskChatbotPlugs/directives/DirAssign.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirAssignFromFunction.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirAssistant.js +13 -106
- package/tiledeskChatbotPlugs/directives/DirBrevo.js +5 -34
- package/tiledeskChatbotPlugs/directives/DirCaptureUserReply.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirClearTranscript.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirClose.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirCode.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirCondition.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +3 -72
- package/tiledeskChatbotPlugs/directives/DirContactUpdate.js +3 -3
- package/tiledeskChatbotPlugs/directives/DirCustomerio.js +5 -33
- package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +2 -2
- package/tiledeskChatbotPlugs/directives/DirDeleteVariable.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirForm.js +2 -2
- package/tiledeskChatbotPlugs/directives/DirGptTask.js +13 -76
- package/tiledeskChatbotPlugs/directives/DirGptTask_OLD.js +8 -53
- package/tiledeskChatbotPlugs/directives/DirHubspot.js +5 -34
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgentsV2.js +3 -39
- package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +2 -38
- package/tiledeskChatbotPlugs/directives/DirIntent.js +2 -77
- package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirMake.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirMessage.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +3 -72
- package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +2 -2
- package/tiledeskChatbotPlugs/directives/DirMoveToUnassigned.js +2 -2
- package/tiledeskChatbotPlugs/directives/DirQapla.js +7 -72
- package/tiledeskChatbotPlugs/directives/DirRandomReply.js +2 -2
- package/tiledeskChatbotPlugs/directives/DirReplaceBot.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirReplaceBotV2.js +3 -38
- package/tiledeskChatbotPlugs/directives/DirReplaceBotV3.js +3 -39
- package/tiledeskChatbotPlugs/directives/DirReply.js +2 -2
- package/tiledeskChatbotPlugs/directives/DirReplyV2.js +3 -3
- package/tiledeskChatbotPlugs/directives/DirSendEmail.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirSendWhatsapp.js +3 -29
- package/tiledeskChatbotPlugs/directives/DirSetAttribute.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirSetAttributeV2.js +3 -38
- package/tiledeskChatbotPlugs/directives/DirWait.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirWebRequest.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2.js +32 -22
- package/tiledeskChatbotPlugs/directives/DirWebRequestV2_old.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirWebResponse.js +23 -64
- package/tiledeskChatbotPlugs/directives/DirWhatsappByAttribute.js +3 -29
- package/utils/HttpUtils.js +128 -0
- package/{models → utils}/TiledeskChatbotUtil.js +8 -50
- package/TiledeskServices/utils.js +0 -99
- /package/{models → engine}/IntentForm.js +0 -0
- /package/{models → engine}/TiledeskChatbotConst.js +0 -0
- /package/{models → engine/mock}/MockIntentsMachine.js +0 -0
- /package/{models → engine/mock}/MockTdCache.js +0 -0
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
|
+
# 0.5.1-rc1
|
|
9
|
+
- added json_buttons
|
|
10
|
+
- added winston log with code refactoring
|
|
11
|
+
|
|
12
|
+
# v0.5.0
|
|
13
|
+
- added: AI_ENDPOINT env var
|
|
14
|
+
|
|
8
15
|
# v0.5.0-rc1
|
|
9
16
|
- added: ability to get 'none' as bodytype in webresponse
|
|
10
17
|
|
package/ExtApi.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
let axios = require('axios');
|
|
2
2
|
let https = require("https");
|
|
3
3
|
const winston = require('./utils/winston');
|
|
4
|
+
const httpUtils = require('./utils/HttpUtils');
|
|
4
5
|
|
|
5
6
|
class ExtApi {
|
|
6
7
|
|
|
@@ -49,7 +50,7 @@ class ExtApi {
|
|
|
49
50
|
json: message,
|
|
50
51
|
method: 'POST'
|
|
51
52
|
};
|
|
52
|
-
|
|
53
|
+
httpUtils.request(
|
|
53
54
|
HTTPREQUEST,
|
|
54
55
|
function(err, resbody) {
|
|
55
56
|
if (err) {
|
|
@@ -66,76 +67,6 @@ class ExtApi {
|
|
|
66
67
|
);
|
|
67
68
|
}
|
|
68
69
|
|
|
69
|
-
/**
|
|
70
|
-
* A stub to send message to the "ext/botId" endpoint, hosted by tilebot on:
|
|
71
|
-
* /${TILEBOT_ROUTE}/ext/${botId}
|
|
72
|
-
*
|
|
73
|
-
* @param {Object} message. The message to send
|
|
74
|
-
* @param {string} botId. Tiledesk botId
|
|
75
|
-
* @param {string} token. User token
|
|
76
|
-
*/
|
|
77
|
-
// sendMessageToBot(message, botId, token, callback) {
|
|
78
|
-
// const url = `${this.ENDPOINT}/ext/${botId}`;
|
|
79
|
-
// const HTTPREQUEST = {
|
|
80
|
-
// url: url,
|
|
81
|
-
// headers: {
|
|
82
|
-
// 'Content-Type' : 'application/json',
|
|
83
|
-
// 'Authorization': jwt_token
|
|
84
|
-
// },
|
|
85
|
-
// json: message,
|
|
86
|
-
// method: 'POST'
|
|
87
|
-
// };
|
|
88
|
-
// this.myrequest(
|
|
89
|
-
// HTTPREQUEST,
|
|
90
|
-
// function(err, resbody) {
|
|
91
|
-
// if (err) {
|
|
92
|
-
// if (callback) {
|
|
93
|
-
// callback(err);
|
|
94
|
-
// }
|
|
95
|
-
// }
|
|
96
|
-
// else {
|
|
97
|
-
// if (callback) {
|
|
98
|
-
// callback(null, resbody);
|
|
99
|
-
// }
|
|
100
|
-
// }
|
|
101
|
-
// }, this.log
|
|
102
|
-
// );
|
|
103
|
-
// }
|
|
104
|
-
|
|
105
|
-
myrequest(options, callback, log) {
|
|
106
|
-
let axios_options = {
|
|
107
|
-
url: options.url,
|
|
108
|
-
method: options.method,
|
|
109
|
-
data: options.json,
|
|
110
|
-
params: options.params,
|
|
111
|
-
headers: options.headers
|
|
112
|
-
}
|
|
113
|
-
if (options.url.startsWith("https:")) {
|
|
114
|
-
const httpsAgent = new https.Agent({
|
|
115
|
-
rejectUnauthorized: false,
|
|
116
|
-
});
|
|
117
|
-
axios_options.httpsAgent = httpsAgent;
|
|
118
|
-
}
|
|
119
|
-
axios(axios_options)
|
|
120
|
-
.then((res) => {
|
|
121
|
-
if (res && res.status == 200 && res.data) {
|
|
122
|
-
if (callback) {
|
|
123
|
-
callback(null, res.data);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
else {
|
|
127
|
-
if (callback) {
|
|
128
|
-
callback(TiledeskClient.getErr({message: "Response status not 200"}, options, res), null, null);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
})
|
|
132
|
-
.catch( (error) => {
|
|
133
|
-
winston.error("(ExtApi) An error occurred:", JSON.stringify(error));
|
|
134
|
-
if (callback) {
|
|
135
|
-
callback(error, null, null);
|
|
136
|
-
}
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
70
|
}
|
|
140
71
|
|
|
141
72
|
module.exports = { ExtApi };
|
package/Logger.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
let { Publisher } = require("@tiledesk/tiledesk-multi-worker");
|
|
2
2
|
|
|
3
|
+
const FLOW_LOGS_ENABLED = process.env.FLOW_LOGS_ENABLED;
|
|
3
4
|
const AMQP_MANAGER_URL = process.env.AMQP_MANAGER_URL;
|
|
4
5
|
let publisher = new Publisher(AMQP_MANAGER_URL, {
|
|
5
6
|
debug: false,
|
|
@@ -8,6 +9,8 @@ let publisher = new Publisher(AMQP_MANAGER_URL, {
|
|
|
8
9
|
topic: "logs",
|
|
9
10
|
})
|
|
10
11
|
|
|
12
|
+
const levels = { error: 0, warn: 1, info: 2, debug: 3 };
|
|
13
|
+
|
|
11
14
|
class Logger {
|
|
12
15
|
|
|
13
16
|
constructor(config) {
|
|
@@ -21,17 +24,27 @@ class Logger {
|
|
|
21
24
|
//throw new Error('config.request_id is mandatory');
|
|
22
25
|
}
|
|
23
26
|
|
|
27
|
+
if (!FLOW_LOGS_ENABLED || FLOW_LOGS_ENABLED === false || FLOW_LOGS_ENABLED === 'false') {
|
|
28
|
+
console.warn("(Logger) Flow logs disabled");
|
|
29
|
+
this._disableMethods();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (!AMQP_MANAGER_URL) {
|
|
33
|
+
console.warn("(Logger) No AQMP Manager url provided. Flow logs disabled");
|
|
34
|
+
this._disableMethods();
|
|
35
|
+
}
|
|
36
|
+
|
|
24
37
|
this.request_id = config.request_id;
|
|
25
38
|
this.dev = false;
|
|
26
39
|
if (config.dev && config.dev === true) {
|
|
27
40
|
this.dev = true;
|
|
28
41
|
}
|
|
29
42
|
|
|
30
|
-
if (!AMQP_MANAGER_URL) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
}
|
|
43
|
+
// if (!AMQP_MANAGER_URL) {
|
|
44
|
+
// console.error('AMQP_MANAGER_URL is undefined. Logger not available...');
|
|
45
|
+
// return;
|
|
46
|
+
// //throw new Error("Error starting logger: AMQP_MANAGER_URL is undefined.")
|
|
47
|
+
// }
|
|
35
48
|
|
|
36
49
|
}
|
|
37
50
|
|
|
@@ -65,6 +78,7 @@ class Logger {
|
|
|
65
78
|
request_id: this.request_id,
|
|
66
79
|
text: text,
|
|
67
80
|
level: level,
|
|
81
|
+
nlevel: levels[level],
|
|
68
82
|
timestamp: new Date(),
|
|
69
83
|
dev: this.dev
|
|
70
84
|
}
|
|
@@ -81,6 +95,19 @@ class Logger {
|
|
|
81
95
|
.join(" ")
|
|
82
96
|
}
|
|
83
97
|
|
|
98
|
+
// Substitute methods with empty function if flow flogs are disabled
|
|
99
|
+
_disableMethods() {
|
|
100
|
+
const methods = ['error', 'warn', 'info', 'debug'];
|
|
101
|
+
methods.forEach(method => {
|
|
102
|
+
this[method] = () => {};
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
_disableDebugMethod() {
|
|
107
|
+
const method = 'debug';
|
|
108
|
+
this[method] = () => {};
|
|
109
|
+
}
|
|
110
|
+
|
|
84
111
|
}
|
|
85
112
|
|
|
86
113
|
module.exports = { Logger }
|
|
@@ -18,7 +18,7 @@ class IntentsMachineFactory {
|
|
|
18
18
|
machine = new MongodbIntentsMachine({projectId: projectId, language: bot.language, log});
|
|
19
19
|
}
|
|
20
20
|
else {
|
|
21
|
-
|
|
21
|
+
winston.error("bot is null for: " + botId + " on projectId: " + projectId);
|
|
22
22
|
}
|
|
23
23
|
return machine;
|
|
24
24
|
}
|
|
@@ -5,7 +5,7 @@ const { MessagePipeline } = require('../tiledeskChatbotPlugs/MessagePipeline');
|
|
|
5
5
|
const { WebhookChatbotPlug } = require('../tiledeskChatbotPlugs/WebhookChatbotPlug');
|
|
6
6
|
const { TiledeskClient } = require('@tiledesk/tiledesk-client');
|
|
7
7
|
const { IntentForm } = require('./IntentForm.js');
|
|
8
|
-
const { TiledeskChatbotUtil } = require('
|
|
8
|
+
const { TiledeskChatbotUtil } = require('../utils/TiledeskChatbotUtil.js');
|
|
9
9
|
const { DirLockIntent } = require('../tiledeskChatbotPlugs/directives/DirLockIntent');
|
|
10
10
|
const { DirUnlockIntent } = require('../tiledeskChatbotPlugs/directives/DirUnlockIntent');
|
|
11
11
|
const winston = require('../utils/winston');
|
|
@@ -146,6 +146,7 @@ class TiledeskChatbot {
|
|
|
146
146
|
let reply;
|
|
147
147
|
if (!intent || (intent && !intent.name)) {
|
|
148
148
|
winston.verbose("(TiledeskChatbot) Invalid intent:", explicit_intent_name)
|
|
149
|
+
reply = { "text": "Invalid intent: *" + explicit_intent_name + "*" }
|
|
149
150
|
resolve();
|
|
150
151
|
}
|
|
151
152
|
else {
|
|
@@ -171,6 +172,7 @@ class TiledeskChatbot {
|
|
|
171
172
|
}
|
|
172
173
|
else {
|
|
173
174
|
winston.verbose("(TiledeskChatbot) Intent not found: " + explicit_intent_name);
|
|
175
|
+
reply = { "text": "Intent not found: " + explicit_intent_name }
|
|
174
176
|
resolve()
|
|
175
177
|
}
|
|
176
178
|
}
|
|
@@ -40,11 +40,10 @@ class TiledeskIntentsMachine {
|
|
|
40
40
|
HTTPREQUEST,
|
|
41
41
|
(err, resbody) => {
|
|
42
42
|
if (err) {
|
|
43
|
-
// console.error("An error occurred on /model/parse:", err)
|
|
44
43
|
reject(err);
|
|
45
44
|
}
|
|
46
45
|
else {
|
|
47
|
-
winston.
|
|
46
|
+
winston.debug("(TiledeskIntentsMachine) Tiledesk AI replied: ", resbody)
|
|
48
47
|
resolve(this.translateForTiledesk(resbody));
|
|
49
48
|
}
|
|
50
49
|
}, false
|
|
@@ -24,7 +24,6 @@ class MockBotsDataSource {
|
|
|
24
24
|
}catch(err){
|
|
25
25
|
reject(err);
|
|
26
26
|
}
|
|
27
|
-
|
|
28
27
|
})
|
|
29
28
|
}
|
|
30
29
|
|
|
@@ -64,7 +63,7 @@ class MockBotsDataSource {
|
|
|
64
63
|
}
|
|
65
64
|
}
|
|
66
65
|
catch(err) {
|
|
67
|
-
|
|
66
|
+
winston.error("(MockBotsDataSource) Error getByIntentDisplayName: ", err);
|
|
68
67
|
}
|
|
69
68
|
return intent;
|
|
70
69
|
}
|
package/index.js
CHANGED
|
@@ -6,13 +6,11 @@ const { TiledeskClient } = require('@tiledesk/tiledesk-client');
|
|
|
6
6
|
const { ExtApi } = require('./ExtApi.js');
|
|
7
7
|
const { ExtUtil } = require('./ExtUtil.js');
|
|
8
8
|
const { TdCache } = require('./TdCache.js');
|
|
9
|
-
const { TiledeskChatbot } = require('./
|
|
10
|
-
const { MongodbBotsDataSource } = require('./
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const {
|
|
14
|
-
const { TiledeskChatbotConst } = require('./models/TiledeskChatbotConst');
|
|
15
|
-
const { IntentsMachineFactory } = require('./models/IntentsMachineFactory');
|
|
9
|
+
const { TiledeskChatbot } = require('./engine/TiledeskChatbot.js');
|
|
10
|
+
const { MongodbBotsDataSource } = require('./engine/MongodbBotsDataSource.js');
|
|
11
|
+
const { MockBotsDataSource } = require('./engine/mock/MockBotsDataSource.js');
|
|
12
|
+
const { TiledeskChatbotConst } = require('./engine/TiledeskChatbotConst.js');
|
|
13
|
+
const { IntentsMachineFactory } = require('./engine/IntentsMachineFactory.js');
|
|
16
14
|
const { v4: uuidv4 } = require('uuid');
|
|
17
15
|
let axios = require('axios');
|
|
18
16
|
// let parser = require('accept-language-parser');
|
|
@@ -33,8 +31,11 @@ const { DirectivesChatbotPlug } = require('./tiledeskChatbotPlugs/DirectivesChat
|
|
|
33
31
|
// THE IMPORT
|
|
34
32
|
let mongoose = require('mongoose');
|
|
35
33
|
// const { Directives } = require('./tiledeskChatbotPlugs/directives/Directives.js');
|
|
36
|
-
const { TiledeskChatbotUtil } = require('./
|
|
37
|
-
|
|
34
|
+
const { TiledeskChatbotUtil } = require('./utils/TiledeskChatbotUtil.js'); //require('@tiledesk/tiledesk-chatbot-util');
|
|
35
|
+
|
|
36
|
+
const AiService = require('./services/AIService.js');
|
|
37
|
+
const tilebotService = require('./services/TilebotService.js');
|
|
38
|
+
|
|
38
39
|
let API_ENDPOINT = null;
|
|
39
40
|
let TILEBOT_ENDPOINT = null;
|
|
40
41
|
let staticBots;
|
|
@@ -51,7 +52,6 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
51
52
|
}
|
|
52
53
|
winston.verbose("(tybotRoute) Request Body: ", req.body);
|
|
53
54
|
|
|
54
|
-
|
|
55
55
|
const message = req.body.payload;
|
|
56
56
|
const messageId = message._id;
|
|
57
57
|
//const faq_kb = req.body.hook; now it is "bot"
|
|
@@ -75,7 +75,7 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
75
75
|
let isAudio = TiledeskChatbotUtil.isAudioMessage(message)
|
|
76
76
|
if(isAudio){
|
|
77
77
|
let responseText = await aiService.speechToText(message.metadata.src).catch(err => {
|
|
78
|
-
|
|
78
|
+
winston.error('(index.js) aiService.speechToText error: ', err)
|
|
79
79
|
})
|
|
80
80
|
if(responseText && responseText.text)
|
|
81
81
|
message.text = responseText.text
|
|
@@ -113,20 +113,8 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
113
113
|
return;
|
|
114
114
|
});
|
|
115
115
|
|
|
116
|
-
winston.debug("(tybotRoute) Bot found:"
|
|
117
|
-
|
|
118
|
-
// try {
|
|
119
|
-
// // bot = await botsDS.getBotById(botId);
|
|
120
|
-
// // bot = await botById(botId, projectId, tdcache, botsDS);
|
|
121
|
-
// bot = await botsDS.getBotByIdCache(botId, tdcache);
|
|
122
|
-
// // console.log("getBotByIdCache ---> bot: ", JSON.stringify(bot, null, 2))
|
|
123
|
-
// }
|
|
124
|
-
// catch(error) {
|
|
125
|
-
// console.error("Error getting botId:", botId);
|
|
126
|
-
// console.error("Error getting bot was:", error);
|
|
127
|
-
// return;
|
|
128
|
-
// }
|
|
129
|
-
// if (log) {console.log("bot found:", JSON.stringify(bot));}
|
|
116
|
+
winston.debug("(tybotRoute) Bot found: ", bot)
|
|
117
|
+
|
|
130
118
|
|
|
131
119
|
let intentsMachine;
|
|
132
120
|
let backupMachine;
|
|
@@ -171,7 +159,7 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
171
159
|
return;
|
|
172
160
|
}
|
|
173
161
|
if (!reply) {
|
|
174
|
-
|
|
162
|
+
winston.verbose("(tybotRoute) No reply. Stop flow.")
|
|
175
163
|
return;
|
|
176
164
|
}
|
|
177
165
|
|
|
@@ -449,84 +437,45 @@ router.post('/block/:project_id/:bot_id/:block_id', async (req, res) => {
|
|
|
449
437
|
}
|
|
450
438
|
|
|
451
439
|
if (async) {
|
|
452
|
-
|
|
453
|
-
sendMessageToBot(
|
|
440
|
+
winston.verbose("Async webhook");
|
|
441
|
+
tilebotService.sendMessageToBot(message, bot_id, (err, resbody) => {
|
|
454
442
|
if (err) {
|
|
455
|
-
|
|
443
|
+
winston.error("Async webhook err:\n", err);
|
|
456
444
|
return res.status(500).send({ success: false, error: err });
|
|
457
445
|
}
|
|
458
446
|
return res.status(200).send({ success: true });
|
|
459
447
|
})
|
|
460
448
|
} else {
|
|
461
449
|
|
|
462
|
-
|
|
450
|
+
winston.verbose("Sync webhook. Subscribe and await for reply...")
|
|
463
451
|
const topic = `/webhooks/${request_id}`;
|
|
464
452
|
|
|
465
453
|
try {
|
|
466
454
|
|
|
467
455
|
const listener = async (message, topic) => {
|
|
468
|
-
|
|
456
|
+
winston.debug("Web response is: " + JSON.stringify(message) + " for topic " + topic);
|
|
469
457
|
await tdcache.unsubscribe(topic, listener);
|
|
470
458
|
|
|
471
459
|
let json = JSON.parse(message);
|
|
472
460
|
let status = json.status ? json.status : 200;
|
|
473
|
-
|
|
461
|
+
winston.debug("Web response status: " + status);
|
|
474
462
|
|
|
475
463
|
return res.status(status).send(json.payload);
|
|
476
464
|
}
|
|
477
465
|
await tdcache.subscribe(topic, listener);
|
|
478
466
|
|
|
479
467
|
} catch(err) {
|
|
480
|
-
|
|
468
|
+
winston.error("Error cache subscribe ", err);
|
|
481
469
|
return res.status(500).send({ success: false, error: "Error during cache subscription"})
|
|
482
470
|
}
|
|
483
471
|
|
|
484
|
-
sendMessageToBot(
|
|
485
|
-
|
|
472
|
+
tilebotService.sendMessageToBot(message, bot_id, () => {
|
|
473
|
+
winston.debug("Sync webhook message sent: ", message);
|
|
486
474
|
})
|
|
487
475
|
}
|
|
488
476
|
|
|
489
477
|
});
|
|
490
478
|
|
|
491
|
-
// draft webhook
|
|
492
|
-
// router.post('/block/:project_id/:bot_id/:block_id', async (req, res) => {
|
|
493
|
-
// const project_id = req.params['project_id'];
|
|
494
|
-
// const bot_id = req.params['bot_id'];
|
|
495
|
-
// const block_id = req.params['block_id'];
|
|
496
|
-
// const body = req.body;
|
|
497
|
-
// if (this.log) {
|
|
498
|
-
// console.log("/block/ .heders:", JSON.stringify(req.headers));
|
|
499
|
-
// console.log("/block/ .body:", JSON.stringify(body));
|
|
500
|
-
// }
|
|
501
|
-
|
|
502
|
-
// // console.log('/block/:project_id/:bot_id/:block_id:', project_id, "/", bot_id, "/", block_id);
|
|
503
|
-
// // console.log('/block/:project_id/:bot_id/:block_id.body', body);
|
|
504
|
-
|
|
505
|
-
// // invoke block
|
|
506
|
-
// // unique ID for each execution
|
|
507
|
-
// const execution_id = uuidv4().replace(/-/g, '');
|
|
508
|
-
// const request_id = "automation-request-" + project_id + "-" + execution_id;
|
|
509
|
-
// const command = "/" + block_id;
|
|
510
|
-
// let request = {
|
|
511
|
-
// "payload": {
|
|
512
|
-
// "recipient": request_id,
|
|
513
|
-
// "text": command,
|
|
514
|
-
// "id_project": project_id,
|
|
515
|
-
// "request": {
|
|
516
|
-
// "request_id": request_id
|
|
517
|
-
// },
|
|
518
|
-
// "attributes": {
|
|
519
|
-
// "payload": body
|
|
520
|
-
// }
|
|
521
|
-
// },
|
|
522
|
-
// "token": "NO-TOKEN"
|
|
523
|
-
// }
|
|
524
|
-
// if (this.log) {console.log("sendMessageToBot()...", JSON.stringify(request));}
|
|
525
|
-
// sendMessageToBot(TILEBOT_ENDPOINT, request, bot_id, async () => {
|
|
526
|
-
// res.status(200).send({"success":true});
|
|
527
|
-
// return;
|
|
528
|
-
// });
|
|
529
|
-
// });
|
|
530
479
|
|
|
531
480
|
async function startApp(settings, completionCallback) {
|
|
532
481
|
winston.info("(Tilebot) Starting Tilebot..")
|
|
@@ -649,43 +598,6 @@ async function checkRequest(request_id, id_project) {
|
|
|
649
598
|
// WARNING! Move this function in models/TiledeskChatbotUtil.js
|
|
650
599
|
}
|
|
651
600
|
|
|
652
|
-
/**
|
|
653
|
-
* A stub to send message to the "ext/botId" endpoint, hosted by tilebot on:
|
|
654
|
-
* /${TILEBOT_ROUTE}/ext/${botId}
|
|
655
|
-
*
|
|
656
|
-
* @param {Object} message. The message to send
|
|
657
|
-
* @param {string} botId. Tiledesk botId
|
|
658
|
-
* @param {string} token. User token
|
|
659
|
-
*/
|
|
660
|
-
function sendMessageToBot(TILEBOT_ENDPOINT, message, botId, callback) {
|
|
661
|
-
|
|
662
|
-
const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
|
|
663
|
-
winston.verbose("sendMessageToBot URL" + url);
|
|
664
|
-
const HTTPREQUEST = {
|
|
665
|
-
url: url,
|
|
666
|
-
headers: {
|
|
667
|
-
'Content-Type': 'application/json'
|
|
668
|
-
},
|
|
669
|
-
json: message,
|
|
670
|
-
method: 'POST'
|
|
671
|
-
};
|
|
672
|
-
myrequest(
|
|
673
|
-
HTTPREQUEST,
|
|
674
|
-
function (err, resbody) {
|
|
675
|
-
if (err) {
|
|
676
|
-
if (callback) {
|
|
677
|
-
callback(err);
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
else {
|
|
681
|
-
if (callback) {
|
|
682
|
-
callback(null, resbody);
|
|
683
|
-
}
|
|
684
|
-
}
|
|
685
|
-
}, false
|
|
686
|
-
);
|
|
687
|
-
}
|
|
688
|
-
|
|
689
601
|
function myrequest(options, callback, log) {
|
|
690
602
|
winston.verbose("(tybotRoute) myrequest API URL:" + options.url);
|
|
691
603
|
winston.debug("(tybotRoute) myrequest Options:", options);
|