@tiledesk/tiledesk-tybot-connector 0.1.62 → 0.1.64
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 +17 -0
- package/index.js +5 -1
- package/models/MockBotsDataSource.js +10 -1
- package/models/MongodbBotsDataSource.js +7 -4
- package/models/TiledeskChatbot.js +2 -2
- package/models/TiledeskChatbotUtil.js +3 -0
- package/package.json +1 -1
- package/test/conversation-form-test.js +45 -45
- package/test/json_condition-actions_bot.js +7 -12
- package/test/json_condition-conversation_test.js +1 -1
- package/test/json_condition-with-intent-params-conversation_test.js +282 -0
- package/test/json_condition-with-intent-params_bot.js +159 -0
- package/test/web_request-conversation_test.js +1 -1
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +113 -98
- package/tiledeskChatbotPlugs/SplitsChatbotPlug.js +1 -1
- package/tiledeskChatbotPlugs/WebhookChatbotPlug.js +7 -7
- package/tiledeskChatbotPlugs/directives/DirDepartment.js +1 -1
- package/tiledeskChatbotPlugs/directives/DirIfOnlineAgents.js +10 -18
- package/tiledeskChatbotPlugs/directives/DirIfOpenHours.js +4 -2
- package/tiledeskChatbotPlugs/directives/DirIntent.js +23 -1
- package/tiledeskChatbotPlugs/directives/DirJSONCondition.js +8 -6
- package/tiledeskChatbotPlugs/directives/DirMoveToAgent.js +37 -22
- /package/test/{disable_input_text_directive_test.js → DEPRECATED-disable_input_text_directive_test.js_} +0 -0
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,23 @@
|
|
|
5
5
|
available on:
|
|
6
6
|
▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
|
|
7
7
|
|
|
8
|
+
### 0.1.64 - online
|
|
9
|
+
- Added logs for bug wa-wh test
|
|
10
|
+
|
|
11
|
+
### 0.1.63 - online
|
|
12
|
+
- Added stopOnConditionMet to DirIfOnlineAgents
|
|
13
|
+
- Removed deprecated Directive Directives.WHEN_OFFLINE_HOURS
|
|
14
|
+
- Removed deprecated Directive Directives.DISABLE_INPUT_TEXT
|
|
15
|
+
- Removed deprecated Directive Directives.WHEN_OPEN
|
|
16
|
+
- Removed deprecated Directive Directives.WHEN_CLOSED
|
|
17
|
+
- Removed deprecated Directive Directives.IF_AGENTS
|
|
18
|
+
- Removed deprecated Directive Directives.IF_NO_AGENTS
|
|
19
|
+
- Added support for intent parameters in DirJSONCondition DirIfOnlineAgents DirIfOpeningHours
|
|
20
|
+
- Added JSONCondition with intent parameters test
|
|
21
|
+
|
|
22
|
+
### 0.1.63 - online
|
|
23
|
+
- Added support for depId-from-supportRequest in DirMoveToAgent. This fixes no-handoff in test-it-out and from Dialogflow connector
|
|
24
|
+
|
|
8
25
|
### 0.1.62
|
|
9
26
|
- Added TiledeskChatbotUtil.fillCommandAttachments() replacing vars in commands.button.link
|
|
10
27
|
|
package/index.js
CHANGED
|
@@ -390,7 +390,11 @@ router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) =>
|
|
|
390
390
|
id_project: projectId
|
|
391
391
|
}
|
|
392
392
|
}
|
|
393
|
-
|
|
393
|
+
if (log) {
|
|
394
|
+
console.log("/ext request....", JSON.stringify(request));
|
|
395
|
+
console.log("/ext APIURL....", APIURL);
|
|
396
|
+
console.log("/ext process.env.TYBOT_ENDPOINT....", process.env.TYBOT_ENDPOINT);
|
|
397
|
+
}
|
|
394
398
|
let directivesPlug = new DirectivesChatbotPlug({supportRequest: request, TILEDESK_API_ENDPOINT: APIURL, TILEBOT_ENDPOINT:process.env.TYBOT_ENDPOINT, token: token, log: log, HELP_CENTER_API_ENDPOINT: process.env.HELP_CENTER_API_ENDPOINT, cache: tdcache});
|
|
395
399
|
// PIPELINE-EXT
|
|
396
400
|
// if (log) {console.log("answer to process:", JSON.stringify(answer));}
|
|
@@ -64,7 +64,16 @@ class MockBotsDataSource {
|
|
|
64
64
|
faq = await this.getByIntentDisplayName(botId, key);
|
|
65
65
|
// console.log("faq found in datasource..:", JSON.stringify(faq));
|
|
66
66
|
}
|
|
67
|
-
|
|
67
|
+
// clones the faq to avoid modifying original object
|
|
68
|
+
// console.log("faq is:", faq)
|
|
69
|
+
let json_faq;
|
|
70
|
+
if (faq !== null && faq !== undefined) {
|
|
71
|
+
let string_faq = JSON.stringify(faq)
|
|
72
|
+
// console.log("string faq is:", string_faq)
|
|
73
|
+
json_faq = JSON.parse(string_faq);
|
|
74
|
+
// console.log("json faq is:", json_faq)
|
|
75
|
+
}
|
|
76
|
+
return json_faq;
|
|
68
77
|
}
|
|
69
78
|
|
|
70
79
|
/**
|
|
@@ -85,31 +85,34 @@ class MongodbBotsDataSource {
|
|
|
85
85
|
* @returns a single Intent
|
|
86
86
|
*/
|
|
87
87
|
async getByIntentDisplayName(botId, key) {
|
|
88
|
+
if (this.log) {console.log("Quering intent by botId:", botId, "key:", key );}
|
|
88
89
|
return new Promise((resolve, reject) => {
|
|
89
90
|
// var query = { "id_project": this.projectId, "id_faq_kb": botId, "intent_display_name": name};
|
|
90
91
|
let query = null;
|
|
91
92
|
key = key.trim();
|
|
92
93
|
if (key.startsWith("#")) {
|
|
93
94
|
let intent_id = key.substring(key.indexOf("#") + 1);
|
|
94
|
-
|
|
95
|
+
if (this.log) {console.log("Query by intent_id:", intent_id );}
|
|
95
96
|
query = { "id_faq_kb": botId, "intent_id": intent_id };
|
|
96
97
|
}
|
|
97
98
|
else {
|
|
98
|
-
|
|
99
|
+
if (this.log) {console.log("Query by intent name:", key);}
|
|
99
100
|
query = { "id_faq_kb": botId, "intent_display_name": key };
|
|
100
|
-
}
|
|
101
|
-
|
|
101
|
+
}
|
|
102
102
|
if (this.log) {console.debug('query', query);}
|
|
103
103
|
Faq.find(query).lean().exec( (err, faqs) => {
|
|
104
104
|
if (err) {
|
|
105
|
+
console.error("error getting faqs", err);
|
|
105
106
|
return reject(err);
|
|
106
107
|
}
|
|
107
108
|
if (this.log) {console.debug("getByIntentDisplayName faqs", JSON.stringify(faqs));}
|
|
108
109
|
if (faqs && faqs.length > 0) {
|
|
109
110
|
const intent = faqs[0];
|
|
111
|
+
if (this.log) {console.debug("intent found:", JSON.stringify(intent));}
|
|
110
112
|
return resolve(intent);
|
|
111
113
|
}
|
|
112
114
|
else {
|
|
115
|
+
if (this.log) {console.debug("No intent found");}
|
|
113
116
|
return resolve(null);
|
|
114
117
|
}
|
|
115
118
|
});
|
|
@@ -580,7 +580,7 @@ class TiledeskChatbot {
|
|
|
580
580
|
if (static_bot_answer.attributes && static_bot_answer.attributes.webhook && static_bot_answer.attributes.webhook === true) {
|
|
581
581
|
const variables = await this.allParameters();
|
|
582
582
|
context.variables = variables;
|
|
583
|
-
if (this.log) {console.log("adding variables to webhook context:", context.variables);}
|
|
583
|
+
if (this.log) {console.log("adding variables to webhook context:", JSON.stringify(context.variables));}
|
|
584
584
|
}
|
|
585
585
|
const messagePipeline = new MessagePipeline(static_bot_answer, context);
|
|
586
586
|
const webhookurl = bot.webhook_url;
|
|
@@ -625,7 +625,7 @@ class TiledeskChatbot {
|
|
|
625
625
|
});
|
|
626
626
|
// const parameters_key = "tilebot:requests:" + requestId + ":parameters";
|
|
627
627
|
const all_parameters = await this.allParameters();//this.tdcache.hgetall(parameters_key);
|
|
628
|
-
if (this.log) {console.log("(populatePrechatFormAndLead) parameters_key:", all_parameters);}
|
|
628
|
+
if (this.log) {console.log("(populatePrechatFormAndLead) parameters_key:", JSON.stringify(all_parameters));}
|
|
629
629
|
if (all_parameters) {
|
|
630
630
|
if (this.log) {console.log("(populatePrechatFormAndLead) userEmail:", all_parameters['userEmail']);}
|
|
631
631
|
if (this.log) {console.log("(populatePrechatFormAndLead) userFullname:", all_parameters['userFullname']);}
|
package/package.json
CHANGED
|
@@ -138,55 +138,55 @@ describe('Conversation1 - Form filling', async () => {
|
|
|
138
138
|
});
|
|
139
139
|
});
|
|
140
140
|
|
|
141
|
-
it('/disable_input', (done) => {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
141
|
+
// it('/disable_input', (done) => {
|
|
142
|
+
// // console.log("/disable_input...");
|
|
143
|
+
// let listener;
|
|
144
|
+
// let endpointServer = express();
|
|
145
|
+
// endpointServer.use(bodyParser.json());
|
|
146
|
+
// endpointServer.post('/:projectId/requests/:requestId/messages', function (req, res) {
|
|
147
|
+
// // console.log("req.body....:", JSON.stringify(req.body));
|
|
148
|
+
// res.send({ success: true });
|
|
149
|
+
// const message = req.body;
|
|
150
150
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
151
|
+
// assert(message["text"] !== "");
|
|
152
|
+
// assert(message["attributes"] !== "");
|
|
153
|
+
// assert(message["attributes"]["disableInputMessage"] === true);
|
|
154
154
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
155
|
+
// listener.close(() => {
|
|
156
|
+
// // console.log('closed.');
|
|
157
|
+
// done();
|
|
158
|
+
// });
|
|
159
159
|
|
|
160
|
-
|
|
160
|
+
// });
|
|
161
161
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
});
|
|
162
|
+
// listener = endpointServer.listen(10002, '0.0.0.0', function () {
|
|
163
|
+
// // console.log('endpointServer started', listener.address());
|
|
164
|
+
// // const botId = process.env.TEST_BOT_ID;
|
|
165
|
+
// // const PROJECT_ID = process.env.TEST_PROJECT_ID;
|
|
166
|
+
// // console.log("botId:", botId);
|
|
167
|
+
// // console.log("REQUEST_ID:", REQUEST_ID);
|
|
168
|
+
// let request = {
|
|
169
|
+
// "payload": {
|
|
170
|
+
// "_id": uuidv4(),
|
|
171
|
+
// "senderFullname": "guest#367e",
|
|
172
|
+
// "type": "text",
|
|
173
|
+
// "sender": "A-SENDER",
|
|
174
|
+
// "recipient": REQUEST_ID,
|
|
175
|
+
// "text": "/disable_input",
|
|
176
|
+
// "id_project": PROJECT_ID,
|
|
177
|
+
// "metadata": "",
|
|
178
|
+
// "request": {
|
|
179
|
+
// "request_id": REQUEST_ID,
|
|
180
|
+
// "id_project": PROJECT_ID
|
|
181
|
+
// }
|
|
182
|
+
// },
|
|
183
|
+
// "token": CHATBOT_TOKEN
|
|
184
|
+
// }
|
|
185
|
+
// sendMessageToBot(request, BOT_ID, CHATBOT_TOKEN, () => {
|
|
186
|
+
// // console.log("Message sent.");
|
|
187
|
+
// });
|
|
188
|
+
// });
|
|
189
|
+
// });
|
|
190
190
|
|
|
191
191
|
it('/good_form', (done) => {
|
|
192
192
|
// console.log("/good_form...");
|
|
@@ -213,8 +213,7 @@ const bot = {
|
|
|
213
213
|
"actions": [{
|
|
214
214
|
"_tdActionTitle": null,
|
|
215
215
|
"_tdActionType": "jsoncondition",
|
|
216
|
-
"
|
|
217
|
-
"groups": [{
|
|
216
|
+
"groups": [{
|
|
218
217
|
"type": "expression",
|
|
219
218
|
"conditions": [{
|
|
220
219
|
"type": "condition",
|
|
@@ -226,15 +225,13 @@ const bot = {
|
|
|
226
225
|
"name": ""
|
|
227
226
|
}
|
|
228
227
|
}]
|
|
229
|
-
|
|
230
|
-
},
|
|
228
|
+
}],
|
|
231
229
|
"stopOnConditionMet": true,
|
|
232
230
|
"trueIntent": "#d19b1b7a-7146-481d-a7d5-6b9cce8fcb50"
|
|
233
231
|
}, {
|
|
234
232
|
"_tdActionTitle": null,
|
|
235
233
|
"_tdActionType": "jsoncondition",
|
|
236
|
-
"
|
|
237
|
-
"groups": [{
|
|
234
|
+
"groups": [{
|
|
238
235
|
"type": "expression",
|
|
239
236
|
"conditions": [{
|
|
240
237
|
"type": "condition",
|
|
@@ -246,15 +243,13 @@ const bot = {
|
|
|
246
243
|
"name": ""
|
|
247
244
|
}
|
|
248
245
|
}]
|
|
249
|
-
|
|
250
|
-
},
|
|
246
|
+
}],
|
|
251
247
|
"stopOnConditionMet": true,
|
|
252
248
|
"trueIntent": "#16240c0b-9618-40cd-87e7-6319e5c3e392"
|
|
253
249
|
}, {
|
|
254
250
|
"_tdActionTitle": null,
|
|
255
251
|
"_tdActionType": "jsoncondition",
|
|
256
|
-
"
|
|
257
|
-
"groups": [{
|
|
252
|
+
"groups": [{
|
|
258
253
|
"type": "expression",
|
|
259
254
|
"conditions": [{
|
|
260
255
|
"type": "condition",
|
|
@@ -278,8 +273,8 @@ const bot = {
|
|
|
278
273
|
"name": ""
|
|
279
274
|
}
|
|
280
275
|
}]
|
|
281
|
-
|
|
282
|
-
|
|
276
|
+
}],
|
|
277
|
+
|
|
283
278
|
"stopOnConditionMet": false,
|
|
284
279
|
"trueIntent": "#98dd1994-83ef-4863-8179-07c48d2194b9"
|
|
285
280
|
}],
|
|
@@ -59,7 +59,7 @@ describe('Conversation for JSONCondition test', async () => {
|
|
|
59
59
|
let endpointServer = express();
|
|
60
60
|
endpointServer.use(bodyParser.json());
|
|
61
61
|
endpointServer.post('/:projectId/requests/:requestId/messages', function (req, res) {
|
|
62
|
-
|
|
62
|
+
console.log("...req.body:", JSON.stringify(req.body));
|
|
63
63
|
res.send({ success: true });
|
|
64
64
|
const message = req.body;
|
|
65
65
|
assert(message.attributes.commands !== null);
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
var assert = require('assert');
|
|
2
|
+
let axios = require('axios');
|
|
3
|
+
const tybot = require("../");
|
|
4
|
+
const tybotRoute = tybot.router;
|
|
5
|
+
var express = require('express');
|
|
6
|
+
var app = express();
|
|
7
|
+
app.use("/", tybotRoute);
|
|
8
|
+
app.use((err, req, res, next) => {
|
|
9
|
+
console.error("General error", err);
|
|
10
|
+
});
|
|
11
|
+
require('dotenv').config();
|
|
12
|
+
const bodyParser = require('body-parser');
|
|
13
|
+
const { v4: uuidv4 } = require('uuid');
|
|
14
|
+
const bots_data = require('./json_condition-with-intent-params_bot.js').bots_data;
|
|
15
|
+
|
|
16
|
+
const PROJECT_ID = "projectID"; //process.env.TEST_ACTIONS_PROJECT_ID;
|
|
17
|
+
const REQUEST_ID = "support-group-" + PROJECT_ID + "-" + uuidv4().replace(/-/g, "");
|
|
18
|
+
const BOT_ID = "botID"; //process.env.TEST_ACTIONS_BOT_ID;
|
|
19
|
+
const CHATBOT_TOKEN = "XXX"; //process.env.ACTIONS_CHATBOT_TOKEN;
|
|
20
|
+
|
|
21
|
+
describe('Conversation for JSONCondition with intent params test', async () => {
|
|
22
|
+
|
|
23
|
+
let app_listener;
|
|
24
|
+
|
|
25
|
+
before(() => {
|
|
26
|
+
return new Promise(async (resolve, reject) => {
|
|
27
|
+
console.log("Starting tilebot server...");
|
|
28
|
+
tybot.startApp(
|
|
29
|
+
{
|
|
30
|
+
// MONGODB_URI: process.env.mongoUrl,
|
|
31
|
+
bots: bots_data,
|
|
32
|
+
API_ENDPOINT: process.env.API_ENDPOINT,
|
|
33
|
+
REDIS_HOST: process.env.REDIS_HOST,
|
|
34
|
+
REDIS_PORT: process.env.REDIS_PORT,
|
|
35
|
+
REDIS_PASSWORD: process.env.REDIS_PASSWORD,
|
|
36
|
+
log: process.env.API_LOG
|
|
37
|
+
}, () => {
|
|
38
|
+
console.log("Tilebot route successfully started.");
|
|
39
|
+
var port = process.env.PORT || 10001;
|
|
40
|
+
app_listener = app.listen(port, () => {
|
|
41
|
+
console.log('Tilebot connector listening on port ', port);
|
|
42
|
+
resolve();
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
})
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
after(function (done) {
|
|
49
|
+
app_listener.close(() => {
|
|
50
|
+
// console.log('ACTIONS app_listener closed.');
|
|
51
|
+
done();
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('/condition with params{"star_type":"supernova"}', (done) => {
|
|
56
|
+
console.log('/condition with params{"star_type":"supernova"}');
|
|
57
|
+
// let message_id = uuidv4();
|
|
58
|
+
let listener;
|
|
59
|
+
let endpointServer = express();
|
|
60
|
+
endpointServer.use(bodyParser.json());
|
|
61
|
+
endpointServer.post('/:projectId/requests/:requestId/messages', function (req, res) {
|
|
62
|
+
console.log("...req.body:", JSON.stringify(req.body));
|
|
63
|
+
res.send({ success: true });
|
|
64
|
+
const message = req.body;
|
|
65
|
+
assert(message.attributes.commands !== null);
|
|
66
|
+
assert(message.attributes.commands.length === 2);
|
|
67
|
+
const command2 = message.attributes.commands[1];
|
|
68
|
+
|
|
69
|
+
assert(command2.type === "message");
|
|
70
|
+
assert(command2.message.text === "My name is supernova and I'm 2B km large");
|
|
71
|
+
getChatbotParameters(REQUEST_ID, (err, params) => {
|
|
72
|
+
if (err) {
|
|
73
|
+
assert.ok(false);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
assert(params);
|
|
77
|
+
// assert(params["last_message_id"] === message_id);
|
|
78
|
+
assert(params["project_id"] === PROJECT_ID);
|
|
79
|
+
assert(params["star_type"] === "supernova");
|
|
80
|
+
assert(params["my_name"] === "supernova");
|
|
81
|
+
assert(params["size"] === "2B");
|
|
82
|
+
listener.close(() => {
|
|
83
|
+
done();
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
listener = endpointServer.listen(10002, '0.0.0.0', () => {
|
|
91
|
+
// console.log('endpointServer started', listener.address());
|
|
92
|
+
let request = {
|
|
93
|
+
"payload": {
|
|
94
|
+
// "_id": message_id,
|
|
95
|
+
"senderFullname": "guest#367e",
|
|
96
|
+
"type": "text",
|
|
97
|
+
"sender": "A-SENDER",
|
|
98
|
+
"recipient": REQUEST_ID,
|
|
99
|
+
"text": '/condition with params{"star_type":"supernova"}',
|
|
100
|
+
"id_project": PROJECT_ID,
|
|
101
|
+
"metadata": "",
|
|
102
|
+
"request": {
|
|
103
|
+
"request_id": REQUEST_ID
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
"token": CHATBOT_TOKEN
|
|
107
|
+
}
|
|
108
|
+
sendMessageToBot(request, BOT_ID, () => {
|
|
109
|
+
// console.log("Message sent:\n", request);
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('/condition with params{"star_type":"nebula"}', (done) => {
|
|
115
|
+
console.log('/condition with params{"star_type":"nebula"}');
|
|
116
|
+
// let message_id = uuidv4();
|
|
117
|
+
let listener;
|
|
118
|
+
let endpointServer = express();
|
|
119
|
+
endpointServer.use(bodyParser.json());
|
|
120
|
+
endpointServer.post('/:projectId/requests/:requestId/messages', function (req, res) {
|
|
121
|
+
console.log("...req.body:", JSON.stringify(req.body));
|
|
122
|
+
res.send({ success: true });
|
|
123
|
+
const message = req.body;
|
|
124
|
+
assert(message.attributes.commands !== null);
|
|
125
|
+
assert(message.attributes.commands.length === 2);
|
|
126
|
+
const command2 = message.attributes.commands[1];
|
|
127
|
+
|
|
128
|
+
assert(command2.type === "message");
|
|
129
|
+
assert(command2.message.text === "My name is nebula and I'm 500k km large");
|
|
130
|
+
getChatbotParameters(REQUEST_ID, (err, params) => {
|
|
131
|
+
if (err) {
|
|
132
|
+
assert.ok(false);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
console.log("params:", params)
|
|
136
|
+
assert(params);
|
|
137
|
+
// assert(params["last_message_id"] === message_id);
|
|
138
|
+
assert(params["project_id"] === PROJECT_ID);
|
|
139
|
+
assert(params["star_type"] === "nebula");
|
|
140
|
+
assert(params["my_name"] === "nebula");
|
|
141
|
+
assert(params["size"] === "500k");
|
|
142
|
+
listener.close(() => {
|
|
143
|
+
done();
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
listener = endpointServer.listen(10002, '0.0.0.0', () => {
|
|
151
|
+
// console.log('endpointServer started', listener.address());
|
|
152
|
+
let request = {
|
|
153
|
+
"payload": {
|
|
154
|
+
// "_id": message_id,
|
|
155
|
+
"senderFullname": "guest#367e",
|
|
156
|
+
"type": "text",
|
|
157
|
+
"sender": "A-SENDER",
|
|
158
|
+
"recipient": REQUEST_ID,
|
|
159
|
+
"text": '/condition with params{"star_type":"nebula"}',
|
|
160
|
+
"id_project": PROJECT_ID,
|
|
161
|
+
"metadata": "",
|
|
162
|
+
"request": {
|
|
163
|
+
"request_id": REQUEST_ID
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
"token": CHATBOT_TOKEN
|
|
167
|
+
}
|
|
168
|
+
sendMessageToBot(request, BOT_ID, () => {
|
|
169
|
+
// console.log("Message sent:\n", request);
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* A stub to send message to the "ext/botId" endpoint, hosted by tilebot on:
|
|
178
|
+
* /${TILEBOT_ROUTE}/ext/${botId}
|
|
179
|
+
*
|
|
180
|
+
* @param {Object} message. The message to send
|
|
181
|
+
* @param {string} botId. Tiledesk botId
|
|
182
|
+
* @param {string} token. User token
|
|
183
|
+
*/
|
|
184
|
+
function sendMessageToBot(message, botId, callback) {
|
|
185
|
+
// const jwt_token = this.fixToken(token);
|
|
186
|
+
const url = `${process.env.TYBOT_ENDPOINT}/ext/${botId}`;
|
|
187
|
+
// console.log("sendMessageToBot URL", url);
|
|
188
|
+
const HTTPREQUEST = {
|
|
189
|
+
url: url,
|
|
190
|
+
headers: {
|
|
191
|
+
'Content-Type': 'application/json'
|
|
192
|
+
},
|
|
193
|
+
json: message,
|
|
194
|
+
method: 'POST'
|
|
195
|
+
};
|
|
196
|
+
myrequest(
|
|
197
|
+
HTTPREQUEST,
|
|
198
|
+
function (err, resbody) {
|
|
199
|
+
if (err) {
|
|
200
|
+
if (callback) {
|
|
201
|
+
callback(err);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
if (callback) {
|
|
206
|
+
callback(null, resbody);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}, false
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* A stub to get the request parameters, hosted by tilebot on:
|
|
215
|
+
* /${TILEBOT_ROUTE}/ext/parameters/requests/${requestId}?all
|
|
216
|
+
*
|
|
217
|
+
* @param {string} requestId. Tiledesk chatbot/requestId parameters
|
|
218
|
+
*/
|
|
219
|
+
function getChatbotParameters(requestId, callback) {
|
|
220
|
+
// const jwt_token = this.fixToken(token);
|
|
221
|
+
const url = `${process.env.TYBOT_ENDPOINT}/ext/parameters/requests/${requestId}?all`;
|
|
222
|
+
const HTTPREQUEST = {
|
|
223
|
+
url: url,
|
|
224
|
+
headers: {
|
|
225
|
+
'Content-Type': 'application/json'
|
|
226
|
+
},
|
|
227
|
+
method: 'get'
|
|
228
|
+
};
|
|
229
|
+
myrequest(
|
|
230
|
+
HTTPREQUEST,
|
|
231
|
+
function (err, resbody) {
|
|
232
|
+
if (err) {
|
|
233
|
+
if (callback) {
|
|
234
|
+
callback(err);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
if (callback) {
|
|
239
|
+
callback(null, resbody);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}, false
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function myrequest(options, callback, log) {
|
|
247
|
+
if (log) {
|
|
248
|
+
console.log("API URL:", options.url);
|
|
249
|
+
console.log("** Options:", JSON.stringify(options));
|
|
250
|
+
}
|
|
251
|
+
axios(
|
|
252
|
+
{
|
|
253
|
+
url: options.url,
|
|
254
|
+
method: options.method,
|
|
255
|
+
data: options.json,
|
|
256
|
+
params: options.params,
|
|
257
|
+
headers: options.headers
|
|
258
|
+
})
|
|
259
|
+
.then((res) => {
|
|
260
|
+
if (log) {
|
|
261
|
+
console.log("Response for url:", options.url);
|
|
262
|
+
console.log("Response headers:\n", JSON.stringify(res.headers));
|
|
263
|
+
//console.log("******** Response for url:", res);
|
|
264
|
+
}
|
|
265
|
+
if (res && res.status == 200 && res.data) {
|
|
266
|
+
if (callback) {
|
|
267
|
+
callback(null, res.data);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
if (callback) {
|
|
272
|
+
callback(TiledeskClient.getErr({ message: "Response status not 200" }, options, res), null, null);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
})
|
|
276
|
+
.catch((error) => {
|
|
277
|
+
console.error("An error occurred:", error);
|
|
278
|
+
if (callback) {
|
|
279
|
+
callback(error, null, null);
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
}
|