@tiledesk/tiledesk-tybot-connector 0.1.21 → 0.1.23
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 +10 -1
- package/ExtApi.js +37 -0
- package/index backup.js +656 -0
- package/index.js +58 -461
- package/models/IntentForm.js +12 -13
- package/models/MockBotsDataSource.js +74 -0
- package/models/MockIntentsMachine.js +39 -0
- package/models/MongodbBotsDataSource.js +73 -0
- package/models/MongodbIntentsMachine.js +55 -0
- package/models/{TiledeskChatbot_Intents_Adapter.js → TiledeskChatbot backup.js } +0 -1
- package/models/TiledeskChatbot.js +206 -180
- package/models/TiledeskIntentsMachine.js +90 -0
- package/models/faqKbService.js +27 -0
- package/models/faqService.js +22 -0
- package/models/faq_kb.js +11 -28
- package/package.json +2 -2
- package/test/close_directive_test.js +49 -0
- package/test/directives_test.js +24 -26
- package/test/disable_input_text_directive_test.js +88 -0
- package/test/mock_query_test.js +276 -0
- package/test/single_test.sh +4 -0
- package/test/when_open_directive_test.js +167 -0
- package/tiledeskChatbotPlugs/CHANGELOG.md +2 -0
- package/tiledeskChatbotPlugs/DirectivesChatbotPlug.js +66 -46
- package/tiledeskChatbotPlugs/directives/DirClose.js +25 -0
- package/tiledeskChatbotPlugs/directives/DirDeflectToHelpCenter.js +6 -6
- package/tiledeskChatbotPlugs/directives/DirDepartment.js +52 -0
- package/tiledeskChatbotPlugs/directives/DirDisableInputText.js +45 -0
- package/tiledeskChatbotPlugs/directives/DirIntent.js +50 -0
- package/tiledeskChatbotPlugs/directives/DirMessage.js +5 -4
- package/tiledeskChatbotPlugs/directives/DirWhenOpen.js +95 -0
- package/tiledeskChatbotPlugs/directives/Directives.js +3 -0
- package/tiledeskChatbotPlugs/package.json +1 -1
- package/models/MongoDBIntentsDataSource.js +0 -22
- package/models/StaticIntentsDataSource.js +0 -110
- package/test/chatbot_query_test.js_ +0 -41
package/index backup.js
ADDED
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
const express = require('express');
|
|
2
|
+
const router = express.Router();
|
|
3
|
+
const bodyParser = require('body-parser');
|
|
4
|
+
//var cors = require('cors');
|
|
5
|
+
//let path = require("path");
|
|
6
|
+
//let fs = require('fs');
|
|
7
|
+
const { TiledeskChatbotClient } = require('@tiledesk/tiledesk-chatbot-client');
|
|
8
|
+
const { TiledeskClient } = require('@tiledesk/tiledesk-client');
|
|
9
|
+
//const jwt = require('jsonwebtoken');
|
|
10
|
+
//const { v4: uuidv4 } = require('uuid');
|
|
11
|
+
const { ExtApi } = require('./ExtApi.js');
|
|
12
|
+
const { ExtUtil } = require('./ExtUtil.js');
|
|
13
|
+
const { TdCache } = require('./TdCache.js');
|
|
14
|
+
//const { IntentForm } = require('./IntentForm.js');
|
|
15
|
+
const { TiledeskChatbot } = require('./models/TiledeskChatbot.js');
|
|
16
|
+
|
|
17
|
+
//router.use(cors());
|
|
18
|
+
router.use(bodyParser.json({limit: '50mb'}));
|
|
19
|
+
router.use(bodyParser.urlencoded({ extended: true , limit: '50mb'}));
|
|
20
|
+
|
|
21
|
+
let log = false;
|
|
22
|
+
let tdcache = null;
|
|
23
|
+
|
|
24
|
+
// DEV
|
|
25
|
+
const { MessagePipeline } = require('./tiledeskChatbotPlugs/MessagePipeline');
|
|
26
|
+
const { DirectivesChatbotPlug } = require('./tiledeskChatbotPlugs/DirectivesChatbotPlug');
|
|
27
|
+
/*const { SplitsChatbotPlug } = require('./tiledeskChatbotPlugs/SplitsChatbotPlug');
|
|
28
|
+
const { MarkbotChatbotPlug } = require('./tiledeskChatbotPlugs/MarkbotChatbotPlug');*/
|
|
29
|
+
const { WebhookChatbotPlug } = require('./tiledeskChatbotPlugs/WebhookChatbotPlug');
|
|
30
|
+
|
|
31
|
+
// PROD
|
|
32
|
+
/*const { MessagePipeline } = require('@tiledesk/tiledesk-chatbot-plugs/MessagePipeline');
|
|
33
|
+
const { DirectivesChatbotPlug } = require('@tiledesk/tiledesk-chatbot-plugs/DirectivesChatbotPlug');
|
|
34
|
+
const { SplitsChatbotPlug } = require('@tiledesk/tiledesk-chatbot-plugs/SplitsChatbotPlug');
|
|
35
|
+
const { MarkbotChatbotPlug } = require('@tiledesk/tiledesk-chatbot-plugs/MarkbotChatbotPlug');
|
|
36
|
+
const { WebhookChatbotPlug } = require('@tiledesk/tiledesk-chatbot-plugs/WebhookChatbotPlug');*/
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
// THE IMPORT
|
|
41
|
+
let mongoose = require('mongoose');
|
|
42
|
+
//let Faq = require('./models/faq');
|
|
43
|
+
//let Faq_kb = require('./models/faq_kb');
|
|
44
|
+
let connection;
|
|
45
|
+
let APIURL = null;
|
|
46
|
+
|
|
47
|
+
router.post('/ext/:botid', async (req, res) => {
|
|
48
|
+
if (log) {console.log("REQUEST BODY:", JSON.stringify(req.body));}
|
|
49
|
+
res.status(200).send({"success":true});
|
|
50
|
+
|
|
51
|
+
const botId = req.params.botid;
|
|
52
|
+
if (log) {console.log("query botId:", botId);}
|
|
53
|
+
const message = req.body.payload;
|
|
54
|
+
const messageId = message._id;
|
|
55
|
+
const faq_kb = req.body.hook;
|
|
56
|
+
const token = req.body.token;
|
|
57
|
+
const requestId = message.request.request_id;
|
|
58
|
+
const projectId = message.id_project;
|
|
59
|
+
|
|
60
|
+
const message_context = {
|
|
61
|
+
projectId: projectId,
|
|
62
|
+
requestId: requestId,
|
|
63
|
+
token: token
|
|
64
|
+
}
|
|
65
|
+
const message_context_key = "tiledesk:messages:context:" + messageId;
|
|
66
|
+
await tdcache.set(
|
|
67
|
+
message_context_key,
|
|
68
|
+
JSON.stringify(message_context),
|
|
69
|
+
{EX: 86400}
|
|
70
|
+
);
|
|
71
|
+
if (log) {console.log("message context saved for messageid:", message_context_key)}
|
|
72
|
+
// provide a http method for set/get message context, authenticated with tiledesk token and APIKEY.
|
|
73
|
+
|
|
74
|
+
const chatbot = new TiledeskChatbot({
|
|
75
|
+
botId: botId,
|
|
76
|
+
token: token,
|
|
77
|
+
faq_kb: faq_kb,
|
|
78
|
+
APIURL: APIURL,
|
|
79
|
+
APIKEY: "___",
|
|
80
|
+
tdcache: tdcache,
|
|
81
|
+
requestId: requestId,
|
|
82
|
+
projectId: projectId,
|
|
83
|
+
log: log
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const parameters_key = "tilebot:requests:" + requestId + ":parameters";
|
|
87
|
+
await chatbot.addParameter(requestId, "tdMessageId", messageId);
|
|
88
|
+
//all_params = await chatbot.allParameters(requestId);
|
|
89
|
+
//console.log("Allparams", all_params);
|
|
90
|
+
let reply = await chatbot.replyToMessage(message);
|
|
91
|
+
reply.triggeredByMessageId = messageId;
|
|
92
|
+
console.log("reply ok", reply);
|
|
93
|
+
let extEndpoint = `${APIURL}/modules/tilebot/`;
|
|
94
|
+
if (process.env.TYBOT_ENDPOINT) {
|
|
95
|
+
extEndpoint = `${process.env.TYBOT_ENDPOINT}`;
|
|
96
|
+
}
|
|
97
|
+
const apiext = new ExtApi({
|
|
98
|
+
ENDPOINT: extEndpoint,
|
|
99
|
+
log: log
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
apiext.sendSupportMessageExt(reply, projectId, requestId, token, () => {
|
|
103
|
+
if (log) {console.log("FORM Message sent.", );}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
router.post('/ext/:projectId/requests/:requestId/messages', async (req, res) => {
|
|
109
|
+
res.json({success:true});
|
|
110
|
+
const projectId = req.params.projectId;
|
|
111
|
+
const requestId = req.params.requestId;
|
|
112
|
+
const token = req.headers["authorization"];
|
|
113
|
+
let answer = req.body;
|
|
114
|
+
const tdclient = new TiledeskClient({
|
|
115
|
+
projectId: projectId,
|
|
116
|
+
token: token,
|
|
117
|
+
APIURL: APIURL,
|
|
118
|
+
APIKEY: "___",
|
|
119
|
+
log: false
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
let request;
|
|
123
|
+
const request_key = "tilebot:" + requestId;
|
|
124
|
+
if (tdcache) {
|
|
125
|
+
request = await tdcache.getJSON(request_key)
|
|
126
|
+
if (log) {console.log("HIT! Request from cache:", request.request_id);}
|
|
127
|
+
if (!request) {
|
|
128
|
+
if (log) {console.log("!Request from cache", requestId);}
|
|
129
|
+
request = await tdclient.getRequestById(requestId);
|
|
130
|
+
if (log) {console.log("Got request with APIs (after no cache hit)");}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
if (log) {console.log("No tdcache. Getting request with APIs", requestId);}
|
|
135
|
+
request = await tdclient.getRequestById(requestId);
|
|
136
|
+
if (log) {console.log("(No tdcache) Got request with APIs");}
|
|
137
|
+
}
|
|
138
|
+
let directivesPlug = new DirectivesChatbotPlug({supportRequest: request, TILEDESK_API_ENDPOINT: APIURL, token: token, log: log, HELP_CENTER_API_ENDPOINT: process.env.HELP_CENTER_API_ENDPOINT, cache: tdcache});
|
|
139
|
+
// PIPELINE-EXT
|
|
140
|
+
const bot_answer = await ExtUtil.execPipelineExt(request, answer, directivesPlug, tdcache, log);
|
|
141
|
+
//const bot_answer = answer;
|
|
142
|
+
tdclient.sendSupportMessage(requestId, bot_answer, () => {
|
|
143
|
+
directivesPlug.processDirectives(() => {
|
|
144
|
+
if (log) {console.log("After message execute directives end.");}
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
/*
|
|
150
|
+
async function execFaq(req, res, faqs, botId, message, token, bot) {
|
|
151
|
+
answerObj = faqs[0];
|
|
152
|
+
//console.log("answerObj:", answerObj)
|
|
153
|
+
let sender = 'bot_' + botId;
|
|
154
|
+
var answerObj;
|
|
155
|
+
answerObj.score = 100; //exact search not set score
|
|
156
|
+
|
|
157
|
+
const requestId = message.request.request_id;
|
|
158
|
+
const projectId = message.id_project;
|
|
159
|
+
//console.log("requestId:", requestId)
|
|
160
|
+
//console.log("token:", token)
|
|
161
|
+
//console.log("projectId:", projectId)
|
|
162
|
+
|
|
163
|
+
if (tdcache) {
|
|
164
|
+
const requestKey = "tilebot:" + requestId
|
|
165
|
+
//console.log("Setting request key:", requestKey)
|
|
166
|
+
// best effort, do not "await", go on, trust redis speed.
|
|
167
|
+
tdcache.setJSON(requestKey, message.request);
|
|
168
|
+
//await tdcache.setJSON(requestKey, message.request);
|
|
169
|
+
}
|
|
170
|
+
// /ext/:projectId/requests/:requestId/messages ENDPOINT COINCIDES
|
|
171
|
+
// with API_ENDPOINT (APIRURL) ONLY WHEN THE TYBOT ROUTE IS HOSTED
|
|
172
|
+
// ON THE MAIN SERVER. OTHERWISE WE USE TYBOT_ROUTE TO SPECIFY
|
|
173
|
+
// THE ALTERNATIVE ROUTE.
|
|
174
|
+
let extEndpoint = `${APIURL}/modules/tilebot/`;
|
|
175
|
+
if (process.env.TYBOT_ENDPOINT) {
|
|
176
|
+
extEndpoint = `${process.env.TYBOT_ENDPOINT}`;
|
|
177
|
+
}
|
|
178
|
+
const apiext = new ExtApi({
|
|
179
|
+
ENDPOINT: extEndpoint,
|
|
180
|
+
log: log
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
// THE FORM
|
|
185
|
+
let intent_form = answerObj.form;
|
|
186
|
+
let intent_name = answerObj.intent_display_name
|
|
187
|
+
if (intent_name === "test_form_intent") {
|
|
188
|
+
intent_form = {
|
|
189
|
+
"name": "form_name",
|
|
190
|
+
"id": "form_id",
|
|
191
|
+
"cancelCommands": ['annulla', 'cancella', 'reset', 'cancel'],
|
|
192
|
+
"cancelReply": "Form Annullata",
|
|
193
|
+
"fields": [
|
|
194
|
+
{
|
|
195
|
+
"name": "userFullname",
|
|
196
|
+
"type": "text",
|
|
197
|
+
"label": "What is your name?"
|
|
198
|
+
},{
|
|
199
|
+
"name": "companyName",
|
|
200
|
+
"type": "text",
|
|
201
|
+
"label": "Thank you ${userFullname}! What is your Company name?"
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
"name": "userEmail",
|
|
205
|
+
"type": "text",
|
|
206
|
+
"regex": "/^(?=.{1,254}$)(?=.{1,64}@)[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+(.[-!#$%&'*+/0-9=?A-Z^_`a-z{|}~]+)*@[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?(.[A-Za-z0-9]([A-Za-z0-9-]{0,61}[A-Za-z0-9])?)+$/",
|
|
207
|
+
"label": "Hi ${userFullname} from ${companyName}\n\nJust one last question\n\nYour email 🙂",
|
|
208
|
+
"errorLabel": "${userFullname} this email address is invalid\n\nCan you insert a correct email address?"
|
|
209
|
+
}
|
|
210
|
+
]
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (intent_form) {
|
|
215
|
+
await lockIntent(requestId, intent_name);
|
|
216
|
+
const user_reply = message.text;
|
|
217
|
+
const intent_answer = req.body.payload.text;
|
|
218
|
+
let form_reply = await execIntentForm(user_reply, intent_answer, requestId, intent_form);
|
|
219
|
+
console.log("got form reply", form_reply)
|
|
220
|
+
//if (form_reply_message) {
|
|
221
|
+
if (!form_reply.canceled && form_reply.message) {
|
|
222
|
+
console.log("Form replying for next field...");
|
|
223
|
+
if (log) {console.log("Sending form reply...", form_reply_message)}
|
|
224
|
+
// reply with this message (ex. please enter your fullname)
|
|
225
|
+
if (!form_reply.message.attributes) {
|
|
226
|
+
form_reply.message.attributes = {}
|
|
227
|
+
}
|
|
228
|
+
form_reply.message.attributes.fillParams = true;
|
|
229
|
+
form_reply.message.attributes.splits = true;
|
|
230
|
+
apiext.sendSupportMessageExt(form_reply.message, projectId, requestId, token, () => {
|
|
231
|
+
if (log) {console.log("FORM Message sent.", );}
|
|
232
|
+
});
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
else if (form_reply.end) {
|
|
236
|
+
console.log("Form end.");
|
|
237
|
+
if (log) {console.log("unlocking intent for request", requestId);}
|
|
238
|
+
unlockIntent(requestId);
|
|
239
|
+
populatePrechatFormAndLead(message, projectId, token, APIURL);
|
|
240
|
+
}
|
|
241
|
+
else if (form_reply.canceled) {
|
|
242
|
+
console.log("Form canceled.");
|
|
243
|
+
if (log) {console.log("unlocking intent due to canceling, for request", requestId);}
|
|
244
|
+
unlockIntent(requestId);
|
|
245
|
+
if (log) {console.log("sending form 'cancel' reply...", form_reply.message)}
|
|
246
|
+
// reply with this message (ex. please enter your fullname)
|
|
247
|
+
if (!form_reply.message.attributes) {
|
|
248
|
+
form_reply.message.attributes = {}
|
|
249
|
+
}
|
|
250
|
+
form_reply.message.attributes.fillParams = true;
|
|
251
|
+
form_reply.message.attributes.splits = true;
|
|
252
|
+
apiext.sendSupportMessageExt(form_reply.message, projectId, requestId, token, () => {
|
|
253
|
+
if (log) {console.log("FORM Message sent.", );}
|
|
254
|
+
});
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
console.log("form_reply is", form_reply)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// FORM END
|
|
261
|
+
|
|
262
|
+
const context = {
|
|
263
|
+
payload: {
|
|
264
|
+
botId: botId,
|
|
265
|
+
bot: bot,
|
|
266
|
+
message: message, // USER MESSAGE (JSON)
|
|
267
|
+
intent: answerObj
|
|
268
|
+
},
|
|
269
|
+
token: token
|
|
270
|
+
};
|
|
271
|
+
const static_bot_answer = { // static design of the chatbot reply
|
|
272
|
+
//type: answerObj.type,
|
|
273
|
+
text: answerObj.answer,
|
|
274
|
+
attributes: answerObj.attributes,
|
|
275
|
+
metadata: answerObj.metadata,
|
|
276
|
+
// language: ?
|
|
277
|
+
// channel: ? whatsapp|telegram|facebook...
|
|
278
|
+
};
|
|
279
|
+
if (!static_bot_answer.attributes) {
|
|
280
|
+
static_bot_answer.attributes = {}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
var timestamp = Date.now();
|
|
284
|
+
static_bot_answer.attributes['clienttimestamp'] = timestamp;
|
|
285
|
+
if (answerObj && answerObj._id) {
|
|
286
|
+
static_bot_answer.attributes._answerid = answerObj._id.toString();
|
|
287
|
+
}
|
|
288
|
+
// DECORATES THE FINAL ANSWER
|
|
289
|
+
// question_payload = clone of user's original message
|
|
290
|
+
let question_payload = Object.assign({}, message);
|
|
291
|
+
delete question_payload.request;
|
|
292
|
+
let clonedfaqs = faqs.slice();
|
|
293
|
+
if (clonedfaqs && clonedfaqs.length > 0) {
|
|
294
|
+
clonedfaqs = clonedfaqs.shift()
|
|
295
|
+
}
|
|
296
|
+
const intent_info = {
|
|
297
|
+
intent_name: answerObj.intent_display_name,
|
|
298
|
+
is_fallback: false,
|
|
299
|
+
confidence: answerObj.score,
|
|
300
|
+
question_payload: question_payload,
|
|
301
|
+
others: clonedfaqs
|
|
302
|
+
}
|
|
303
|
+
static_bot_answer.attributes.intent_info = intent_info;
|
|
304
|
+
|
|
305
|
+
static_bot_answer.attributes.directives = true;
|
|
306
|
+
static_bot_answer.attributes.splits = true;
|
|
307
|
+
static_bot_answer.attributes.markbot = true;
|
|
308
|
+
static_bot_answer.attributes.fillParams = true;
|
|
309
|
+
static_bot_answer.attributes.webhook = answerObj.webhook_enabled;
|
|
310
|
+
|
|
311
|
+
// exec webhook (only)
|
|
312
|
+
const bot_answer = await execPipeline(static_bot_answer, message, bot, context, token);
|
|
313
|
+
|
|
314
|
+
//bot_answer.text = await fillWithRequestParams(bot_answer.text, requestId); // move to "ext" pipeline
|
|
315
|
+
|
|
316
|
+
apiext.sendSupportMessageExt(bot_answer, projectId, requestId, token, () => {
|
|
317
|
+
if (log) {console.log("Message sent");}
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
}*/
|
|
321
|
+
|
|
322
|
+
/*
|
|
323
|
+
async function populatePrechatFormAndLead(message, projectId, token, APIURL, callback) {
|
|
324
|
+
const tdclient = new TiledeskClient({
|
|
325
|
+
projectId: projectId,
|
|
326
|
+
token: token,
|
|
327
|
+
APIURL: APIURL,
|
|
328
|
+
APIKEY: "___"
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
const leadId = message.request.lead._id;
|
|
332
|
+
const requestId = message.request.request_id;
|
|
333
|
+
|
|
334
|
+
const parameters_key = "tilebot:requests:" + requestId + ":parameters";
|
|
335
|
+
const all_parameters = await tdcache.hgetall(parameters_key);
|
|
336
|
+
if (all_parameters) {
|
|
337
|
+
//console.log("all_parameters['userEmail']", all_parameters['userEmail'])
|
|
338
|
+
//console.log("all_parameters['userFullname']", all_parameters['userFullname']);
|
|
339
|
+
tdclient.updateLeadEmailFullname(leadId, null, all_parameters['userFullname'], () => {
|
|
340
|
+
if (log) {console.log("lead updated.")}
|
|
341
|
+
tdclient.updateRequestAttributes(requestId, {
|
|
342
|
+
preChatForm: all_parameters,
|
|
343
|
+
updated: Date.now
|
|
344
|
+
}, () => {
|
|
345
|
+
if (log) {console.log("prechat updated.");}
|
|
346
|
+
if (callback) {
|
|
347
|
+
callback();
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
});
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
*/
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
/** TEST BUG
|
|
358
|
+
function fixToken(token) {
|
|
359
|
+
if (token.startsWith('JWT ')) {
|
|
360
|
+
return token;
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
return 'JWT ' + token;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
function sendSupportMessageExt(message, projectId, requestId, token, callback) {
|
|
368
|
+
let extEndpoint = `${APIURL}/modules/tilebot/`;
|
|
369
|
+
if (process.env.TYBOT_ENDPOINT) {
|
|
370
|
+
extEndpoint = `${process.env.TYBOT_ENDPOINT}`;
|
|
371
|
+
}
|
|
372
|
+
const jwt_token = fixToken(token);
|
|
373
|
+
const url = `${extEndpoint}/ext/${projectId}/requests/${requestId}/messages`;
|
|
374
|
+
if (this.log) {console.log("sendSupportMessageExt URL", url);}
|
|
375
|
+
//console.log("sendSupportMessageExt:", url);
|
|
376
|
+
const HTTPREQUEST = {
|
|
377
|
+
url: url,
|
|
378
|
+
headers: {
|
|
379
|
+
'Content-Type' : 'application/json',
|
|
380
|
+
'Authorization': jwt_token
|
|
381
|
+
},
|
|
382
|
+
json: message,
|
|
383
|
+
method: 'POST'
|
|
384
|
+
};
|
|
385
|
+
myrequest(
|
|
386
|
+
HTTPREQUEST,
|
|
387
|
+
function(err, resbody) {
|
|
388
|
+
//console.log("sendSupportMessageExt resbody:", resbody);
|
|
389
|
+
if (err) {
|
|
390
|
+
//console.error("sendSupportMessageExt error:", err)
|
|
391
|
+
if (callback) {
|
|
392
|
+
callback(err);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
if (callback) {
|
|
397
|
+
console.log("resbody:", resbody);
|
|
398
|
+
callback(null, resbody);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
}, this.log
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
function myrequest(options, callback, log) {
|
|
407
|
+
//console.log("API URL:", options.url);
|
|
408
|
+
axios(
|
|
409
|
+
{
|
|
410
|
+
url: options.url,
|
|
411
|
+
method: options.method,
|
|
412
|
+
data: options.json,
|
|
413
|
+
params: options.params,
|
|
414
|
+
headers: options.headers
|
|
415
|
+
})
|
|
416
|
+
.then((res) => {
|
|
417
|
+
//console.log("Response for url:", options.url);
|
|
418
|
+
//console.log("Response headers:\n", res.headers);
|
|
419
|
+
if (res && res.status == 200 && res.data) {
|
|
420
|
+
if (callback) {
|
|
421
|
+
callback(null, res.data);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
else {
|
|
425
|
+
if (callback) {
|
|
426
|
+
callback(TiledeskClient.getErr({message: "Response status not 200"}, options, res), null, null);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
})
|
|
430
|
+
.catch( (error) => {
|
|
431
|
+
console.error("An error occurred:", error);
|
|
432
|
+
if (callback) {
|
|
433
|
+
callback(error, null, null);
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
*** END TEST*/
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
/*
|
|
444
|
+
async function execIntentForm(userInputReply, original_intent_answer_text, requestId, form) {
|
|
445
|
+
if (log) {console.log("executing intent form...")}
|
|
446
|
+
let intentForm = new IntentForm({form: form, requestId: requestId, db: tdcache, log: log});
|
|
447
|
+
let message = await intentForm.getMessage(userInputReply, original_intent_answer_text);
|
|
448
|
+
return message;
|
|
449
|
+
}
|
|
450
|
+
*/
|
|
451
|
+
|
|
452
|
+
/*
|
|
453
|
+
async function lockIntent(requestId, intent_name) {
|
|
454
|
+
await tdcache.set("tilebot:requests:" + requestId + ":locked", intent_name);
|
|
455
|
+
//console.log("locked.", intent_name);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
async function currentLockedIntent(requestId) {
|
|
459
|
+
return await tdcache.get("tilebot:requests:" + requestId + ":locked");
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
async function unlockIntent(requestId) {
|
|
463
|
+
await tdcache.del("tilebot:requests:" + requestId + ":locked");
|
|
464
|
+
}
|
|
465
|
+
*/
|
|
466
|
+
|
|
467
|
+
/*function validMessage(message) {
|
|
468
|
+
console.log("validating message", message)
|
|
469
|
+
if (!message.type) {
|
|
470
|
+
message.type = 'text';
|
|
471
|
+
}
|
|
472
|
+
if (message.text === '' && message.type === 'text') {
|
|
473
|
+
console.log("in-valid message", message)
|
|
474
|
+
return false;
|
|
475
|
+
}
|
|
476
|
+
console.log("valid message", message)
|
|
477
|
+
return true;
|
|
478
|
+
}*/
|
|
479
|
+
|
|
480
|
+
/*
|
|
481
|
+
async function execPipeline(static_bot_answer, message, bot, context, token) {
|
|
482
|
+
const messagePipeline = new MessagePipeline(static_bot_answer, context);
|
|
483
|
+
const webhookurl = bot.webhook_url;
|
|
484
|
+
messagePipeline.addPlug(new WebhookChatbotPlug(message.request, webhookurl, token));
|
|
485
|
+
//messagePipeline.addPlug(directivesPlug);
|
|
486
|
+
//messagePipeline.addPlug(new SplitsChatbotPlug(log));
|
|
487
|
+
//messagePipeline.addPlug(new MarkbotChatbotPlug(log));
|
|
488
|
+
const bot_answer = await messagePipeline.exec();
|
|
489
|
+
//if (log) {console.log("End pipeline, bot_answer:", JSON.stringify(bot_answer));}
|
|
490
|
+
return bot_answer;
|
|
491
|
+
}
|
|
492
|
+
*/
|
|
493
|
+
|
|
494
|
+
/*
|
|
495
|
+
function getIntentByDisplayName(name, bot) {
|
|
496
|
+
return new Promise(function(resolve, reject) {
|
|
497
|
+
var query = { "id_project": bot.id_project, "id_faq_kb": bot._id, "intent_display_name": name};
|
|
498
|
+
if (log) {console.debug('query', query);}
|
|
499
|
+
Faq.find(query).lean().exec(function (err, faqs) {
|
|
500
|
+
if (err) {
|
|
501
|
+
return reject();
|
|
502
|
+
}
|
|
503
|
+
if (log) {console.debug("faqs", faqs);}
|
|
504
|
+
if (faqs && faqs.length > 0) {
|
|
505
|
+
const intent = faqs[0];
|
|
506
|
+
return resolve(intent);
|
|
507
|
+
}
|
|
508
|
+
else {
|
|
509
|
+
return resolve(null);
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
*/
|
|
515
|
+
|
|
516
|
+
// Tiledesk Resolution-bot webhook endpoint
|
|
517
|
+
/*app.post('/bot', async (req, res) => {
|
|
518
|
+
console.log("Webhook. Request body: " + JSON.stringify(req.body));
|
|
519
|
+
// INTENTS
|
|
520
|
+
let intent = null;
|
|
521
|
+
intent = req.body.payload.intent.intent_display_name;
|
|
522
|
+
//const projectId = req.body.payload.bot.id_project;
|
|
523
|
+
const token = req.body.token;
|
|
524
|
+
const request = req.body.payload.message.request;
|
|
525
|
+
//const requestId = request.request_id;
|
|
526
|
+
console.log("Got intent:", intent);
|
|
527
|
+
API_URL = apiurl();
|
|
528
|
+
const original_answer = req.body.payload.intent.answer;
|
|
529
|
+
|
|
530
|
+
const messagePipeline = new MessagePipeline();
|
|
531
|
+
messagePipeline.addPlug(new DirectivesChatbotPlug(request, API_URL, token));
|
|
532
|
+
messagePipeline.addPlug(new SplitsChatbotPlug());
|
|
533
|
+
messagePipeline.addPlug(new MarkbotChatbotPlug());
|
|
534
|
+
|
|
535
|
+
const message = {
|
|
536
|
+
text: original_answer
|
|
537
|
+
};
|
|
538
|
+
messagePipeline.execOn(message, (_message) => {
|
|
539
|
+
res.json(_message);
|
|
540
|
+
console.log("End pipeline.");
|
|
541
|
+
});
|
|
542
|
+
|
|
543
|
+
});*/
|
|
544
|
+
|
|
545
|
+
|
|
546
|
+
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
/*
|
|
550
|
+
function apiurl() {
|
|
551
|
+
const server = "pre";
|
|
552
|
+
//const server = "prod";
|
|
553
|
+
const API_URL_PRE = 'https://tiledesk-server-pre.herokuapp.com';
|
|
554
|
+
const API_URL_PROD = 'https://api.tiledesk.com/v2';
|
|
555
|
+
// choose a server
|
|
556
|
+
let API_URL = API_URL_PROD;
|
|
557
|
+
if (server === 'pre') {
|
|
558
|
+
API_URL = API_URL_PRE;
|
|
559
|
+
}
|
|
560
|
+
return API_URL;
|
|
561
|
+
}
|
|
562
|
+
*/
|
|
563
|
+
|
|
564
|
+
router.get('/message/context/:messageid', async (req, res) => {
|
|
565
|
+
const messageid = req.params.messageid;
|
|
566
|
+
const message_key = "tiledesk:messages:context:" + messageid;
|
|
567
|
+
const message_context_s = await tdcache.get(message_key);
|
|
568
|
+
if (message_context_s) {
|
|
569
|
+
const message_context = JSON.parse(message_context_s);
|
|
570
|
+
res.send(message_context);
|
|
571
|
+
}
|
|
572
|
+
else {
|
|
573
|
+
res.send(null);
|
|
574
|
+
}
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
router.get('/', (req, res) => {
|
|
578
|
+
res.send('Hello Tilebot!');
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
function startApp(settings, completionCallback) {
|
|
582
|
+
console.log("Starting Tybot with Settings:", settings);
|
|
583
|
+
|
|
584
|
+
if (!settings.MONGODB_URI) {
|
|
585
|
+
throw new Error("settings.MONGODB_URI is mandatory.");
|
|
586
|
+
}
|
|
587
|
+
if (!settings.API_ENDPOINT) {
|
|
588
|
+
throw new Error("settings.API_ENDPOINT is mandatory.");
|
|
589
|
+
}
|
|
590
|
+
else {
|
|
591
|
+
APIURL = settings.API_ENDPOINT;
|
|
592
|
+
console.log("(Tybot) settings.API_ENDPOINT:", APIURL);
|
|
593
|
+
}
|
|
594
|
+
if (settings.REDIS_HOST && settings.REDIS_PORT && settings.CACHE_ENABLED) {
|
|
595
|
+
tdcache = new TdCache({
|
|
596
|
+
host: settings.REDIS_HOST,
|
|
597
|
+
port: settings.REDIS_PORT,
|
|
598
|
+
password: settings.REDIS_PASSWORD
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
if (!settings.log) {
|
|
603
|
+
log = false;
|
|
604
|
+
}
|
|
605
|
+
else {
|
|
606
|
+
log = true;
|
|
607
|
+
}
|
|
608
|
+
console.log("(Tybot) log:", log);
|
|
609
|
+
var pjson = require('./package.json');
|
|
610
|
+
console.log("(Tybot) Starting Tilebot connector v" + pjson.version);
|
|
611
|
+
console.log("(Tybot) Connecting to mongodb...");
|
|
612
|
+
|
|
613
|
+
connection = mongoose.connect(settings.MONGODB_URI, { "useNewUrlParser": true, "autoIndex": false }, async (err) => {
|
|
614
|
+
if (err) {
|
|
615
|
+
console.error('(Tybot) Failed to connect to MongoDB on ' + settings.MONGODB_URI + " ", err);
|
|
616
|
+
//process.exit(1); // add => exitOnFail: true
|
|
617
|
+
}
|
|
618
|
+
else {
|
|
619
|
+
console.log("(Tybot) mongodb connection ok.");
|
|
620
|
+
if (tdcache) {
|
|
621
|
+
try {
|
|
622
|
+
await tdcache.connect();
|
|
623
|
+
}
|
|
624
|
+
catch (error) {
|
|
625
|
+
tdcache = null;
|
|
626
|
+
console.error("(Tybot) tdcache (Redis) connection error:", error);
|
|
627
|
+
}
|
|
628
|
+
console.log("(Tybot) tdcache (Redis) connected.");
|
|
629
|
+
}
|
|
630
|
+
console.info("Tybot started.");
|
|
631
|
+
if (completionCallback) {
|
|
632
|
+
completionCallback();
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
});
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/*
|
|
639
|
+
var connection = mongoose.connect(process.env.mongoUrl, { "useNewUrlParser": true, "autoIndex": false }, function(err) {
|
|
640
|
+
if (err) {
|
|
641
|
+
console.error('Failed to connect to MongoDB on ' + process.env.mongoUrl + " ", err);
|
|
642
|
+
//process.exit(1);
|
|
643
|
+
}
|
|
644
|
+
else {
|
|
645
|
+
console.info("Mongodb connected")
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
*/
|
|
649
|
+
|
|
650
|
+
/*
|
|
651
|
+
app.listen(3000, () => {
|
|
652
|
+
console.log('server started');
|
|
653
|
+
});
|
|
654
|
+
*/
|
|
655
|
+
|
|
656
|
+
module.exports = { router: router, startApp: startApp};
|