@tiledesk/tiledesk-tybot-connector 0.3.0 → 0.3.2
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 +2 -2
- package/ExtApi.js +0 -1
- package/TiledeskServices/AIService.js +43 -0
- package/TiledeskServices/utils.js +99 -0
- package/index.js +14 -8
- package/models/TiledeskChatbotUtil.js +7 -1
- package/package.json +1 -1
- package/tiledeskChatbotPlugs/directives/DirConnectBlock.js +0 -1
- package/tiledeskChatbotPlugs/directives/DirIntent.js +0 -1
- package/tiledeskChatbotPlugs/directives/DirMessageToBot.js +0 -1
package/CHANGELOG.md
CHANGED
package/ExtApi.js
CHANGED
|
@@ -76,7 +76,6 @@ class ExtApi {
|
|
|
76
76
|
* @param {string} token. User token
|
|
77
77
|
*/
|
|
78
78
|
// sendMessageToBot(message, botId, token, callback) {
|
|
79
|
-
// const jwt_token = this.fixToken(token);
|
|
80
79
|
// const url = `${this.ENDPOINT}/ext/${botId}`;
|
|
81
80
|
// if (this.log) {console.log("sendMessageToBot URL", url);}
|
|
82
81
|
// const HTTPREQUEST = {
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
const Utils = require('./utils')
|
|
2
|
+
class AiService {
|
|
3
|
+
|
|
4
|
+
constructor(options){
|
|
5
|
+
this.APIURL = options.API_ENDPOINT
|
|
6
|
+
this.TOKEN = options.TOKEN
|
|
7
|
+
this.PROJECT_ID = options.PROJECT_ID
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
async speechToText(url){
|
|
11
|
+
return new Promise((resolve, reject)=> {
|
|
12
|
+
const HTTPREQUEST = {
|
|
13
|
+
url: `${this.APIURL}/${this.PROJECT_ID}/llm/transcription`,
|
|
14
|
+
headers: {
|
|
15
|
+
'Content-Type' : 'application/json',
|
|
16
|
+
'Authorization': Utils.fixToken(this.TOKEN)
|
|
17
|
+
},
|
|
18
|
+
json: {
|
|
19
|
+
url: url
|
|
20
|
+
},
|
|
21
|
+
method: 'POST',
|
|
22
|
+
httpsOptions: this.httpsOptions
|
|
23
|
+
};
|
|
24
|
+
Utils.myrequest(
|
|
25
|
+
HTTPREQUEST,
|
|
26
|
+
function(err, resbody) {
|
|
27
|
+
if (err) {
|
|
28
|
+
reject(err)
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
resolve(resbody)
|
|
32
|
+
}
|
|
33
|
+
}, this.LOG
|
|
34
|
+
);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
module.exports= AiService
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
let axios = require('axios');
|
|
2
|
+
let https = require("https");
|
|
3
|
+
|
|
4
|
+
class Utils {
|
|
5
|
+
constructor(){}
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
static myrequest(options, callback, log) {
|
|
9
|
+
if (log) {
|
|
10
|
+
console.log("** API URL:", options.url);
|
|
11
|
+
console.log("** Options:", JSON.stringify(options));
|
|
12
|
+
}
|
|
13
|
+
let axios_settings = {
|
|
14
|
+
url: options.url,
|
|
15
|
+
method: options.method,
|
|
16
|
+
data: options.json,
|
|
17
|
+
params: options.params,
|
|
18
|
+
headers: options.headers
|
|
19
|
+
}
|
|
20
|
+
// console.log("options.url.startsWith(https:)", options.url.startsWith("https:"))
|
|
21
|
+
// console.log("this.httpsOptions", this.httpsOptions)
|
|
22
|
+
|
|
23
|
+
if (options.url.startsWith("https:") && options.httpsOptions) {
|
|
24
|
+
// console.log("Tiledesk Client v 0.9.x: url.startsWith https: && httpsOptions");
|
|
25
|
+
const httpsAgent = new https.Agent(options.httpsOptions);
|
|
26
|
+
axios_settings.httpsAgent = httpsAgent;
|
|
27
|
+
}
|
|
28
|
+
else if (options.url.startsWith("https:") && !options.httpsOptions) {
|
|
29
|
+
// HTTPS default is rejectUnauthorized: false
|
|
30
|
+
// console.log("Tiledesk Client v 0.9.x: url.startsWith https: && NOT httpsOptions");
|
|
31
|
+
const httpsAgent = new https.Agent({
|
|
32
|
+
rejectUnauthorized: false,
|
|
33
|
+
});
|
|
34
|
+
axios_settings.httpsAgent = httpsAgent;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
// console.log("Using axios settings:", axios_settings)
|
|
40
|
+
// axios(
|
|
41
|
+
// {
|
|
42
|
+
// url: options.url,
|
|
43
|
+
// method: options.method,
|
|
44
|
+
// data: options.json,
|
|
45
|
+
// params: options.params,
|
|
46
|
+
// httpsAgent: httpsAgent,
|
|
47
|
+
// headers: options.headers
|
|
48
|
+
// })
|
|
49
|
+
axios(axios_settings)
|
|
50
|
+
.then(function (res) {
|
|
51
|
+
if (log) {
|
|
52
|
+
console.log("Response for url:", options.url);
|
|
53
|
+
console.log("Response headers:\n", JSON.stringify(res.headers));
|
|
54
|
+
// console.log("******** Response for url:", res);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (res && res.status == 200 && res.data) {
|
|
58
|
+
if (callback) {
|
|
59
|
+
callback(null, res.data);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
|
|
64
|
+
if (callback) {
|
|
65
|
+
callback(Utils.getErr({message: "Response status not 200"}, options, res), null, null);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
.catch(function (error) {
|
|
70
|
+
// console.error(error);
|
|
71
|
+
if (callback) {
|
|
72
|
+
callback(error, null, null);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
static getErr(err, request, response) {
|
|
78
|
+
let res_err = {}
|
|
79
|
+
res_err.http_err = err;
|
|
80
|
+
res_err.http_request = request;
|
|
81
|
+
res_err.http_response = response;
|
|
82
|
+
return res_err;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
static fixToken(token) {
|
|
86
|
+
if (token.startsWith('JWT ')) {
|
|
87
|
+
return token;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
return 'JWT ' + token;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = Utils
|
package/index.js
CHANGED
|
@@ -32,6 +32,7 @@ const { DirectivesChatbotPlug } = require('./tiledeskChatbotPlugs/DirectivesChat
|
|
|
32
32
|
let mongoose = require('mongoose');
|
|
33
33
|
// const { Directives } = require('./tiledeskChatbotPlugs/directives/Directives.js');
|
|
34
34
|
const { TiledeskChatbotUtil } = require('./models/TiledeskChatbotUtil.js'); //require('@tiledesk/tiledesk-chatbot-util');
|
|
35
|
+
const AiService = require('./TiledeskServices/AIService.js');
|
|
35
36
|
let API_ENDPOINT = null;
|
|
36
37
|
let TILEBOT_ENDPOINT = null;
|
|
37
38
|
let staticBots;
|
|
@@ -63,13 +64,19 @@ router.post('/ext/:botid', async (req, res) => {
|
|
|
63
64
|
message.request.id_project = projectId;
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
67
|
+
/** MANAGE AUDIO FILE MESSAGE */
|
|
68
|
+
let aiService = new AiService({
|
|
69
|
+
API_ENDPOINT: API_ENDPOINT,
|
|
70
|
+
TOKEN: token,
|
|
71
|
+
PROJECT_ID: projectId
|
|
72
|
+
})
|
|
73
|
+
let isAudio = TiledeskChatbotUtil.isAudioMessage(message)
|
|
74
|
+
if(isAudio){
|
|
75
|
+
let responseText = await aiService.speechToText(message.metadata.src).catch(err => {
|
|
76
|
+
if(log) console.log('(index.js) aiService.speechToText error: ', err)
|
|
77
|
+
})
|
|
78
|
+
message.text= responseText.text
|
|
79
|
+
}
|
|
73
80
|
|
|
74
81
|
// validate reuqestId
|
|
75
82
|
let isValid = TiledeskChatbotUtil.validateRequestId(requestId, projectId);
|
|
@@ -760,7 +767,6 @@ async function checkRequest(request_id, id_project) {
|
|
|
760
767
|
* @param {string} token. User token
|
|
761
768
|
*/
|
|
762
769
|
function sendMessageToBot(TILEBOT_ENDPOINT, message, botId, callback) {
|
|
763
|
-
// const jwt_token = this.fixToken(token);
|
|
764
770
|
const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
|
|
765
771
|
console.log("sendMessageToBot URL", url);
|
|
766
772
|
const HTTPREQUEST = {
|
|
@@ -480,6 +480,13 @@ class TiledeskChatbotUtil {
|
|
|
480
480
|
return false;
|
|
481
481
|
}
|
|
482
482
|
|
|
483
|
+
static isAudioMessage(message){
|
|
484
|
+
if (message && message.type && message.type === 'file' && message.metadata && message.metadata.src && message.metadata.type.includes('audio') ) {
|
|
485
|
+
return true;
|
|
486
|
+
}
|
|
487
|
+
return false;
|
|
488
|
+
}
|
|
489
|
+
|
|
483
490
|
static lastUserMessageFrom(msg) {
|
|
484
491
|
let message = {};
|
|
485
492
|
message["senderFullname"] = msg["senderFullname"]; // ex. "Bot"
|
|
@@ -926,7 +933,6 @@ class TiledeskChatbotUtil {
|
|
|
926
933
|
* @param {string} requestId. Tiledesk chatbot/requestId parameters
|
|
927
934
|
*/
|
|
928
935
|
getChatbotParameters(requestId, callback) {
|
|
929
|
-
// const jwt_token = this.fixToken(token);
|
|
930
936
|
const url = `${process.env.TILEBOT_ENDPOINT}/ext/reserved/parameters/requests/${requestId}?all`;
|
|
931
937
|
const HTTPREQUEST = {
|
|
932
938
|
url: url,
|
package/package.json
CHANGED
|
@@ -108,7 +108,6 @@ class DirConnectBlock {
|
|
|
108
108
|
* @param {string} token. User token
|
|
109
109
|
*/
|
|
110
110
|
sendMessageToBot(TILEBOT_ENDPOINT, message, botId, callback) {
|
|
111
|
-
// const jwt_token = this.fixToken(token);
|
|
112
111
|
const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
|
|
113
112
|
// console.log("sendMessageToBot URL", url);
|
|
114
113
|
const HTTPREQUEST = {
|
|
@@ -133,7 +133,6 @@ class DirIntent {
|
|
|
133
133
|
* @param {string} token. User token
|
|
134
134
|
*/
|
|
135
135
|
sendMessageToBot(TILEBOT_ENDPOINT, message, botId, callback) {
|
|
136
|
-
// const jwt_token = this.fixToken(token);
|
|
137
136
|
const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
|
|
138
137
|
// console.log("sendMessageToBot URL", url);
|
|
139
138
|
const HTTPREQUEST = {
|
|
@@ -61,7 +61,6 @@ class DirMessageToBot {
|
|
|
61
61
|
* @param {string} token. User token
|
|
62
62
|
*/
|
|
63
63
|
sendMessageToBot(TILEBOT_ENDPOINT, message, botId, callback) {
|
|
64
|
-
// const jwt_token = this.fixToken(token);
|
|
65
64
|
const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
|
|
66
65
|
// console.log("sendMessageToBot URL", url);
|
|
67
66
|
const HTTPREQUEST = {
|