@tiledesk/tiledesk-tybot-connector 0.2.153-rc8 → 0.3.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/CHANGELOG.md CHANGED
@@ -5,14 +5,8 @@
5
5
  available on:
6
6
  ▶️ https://www.npmjs.com/package/@tiledesk/tiledesk-tybot-connector
7
7
 
8
- # v0.2.153-rc8
9
- - added: fixToken function in TiledeskService utils class
10
-
11
- # v0.2.153-rc4
12
- - log added
13
-
14
- # v0.2.153-rc3
15
- - added: specchToText function to transcript audio file
8
+ # v0.3.0
9
+ - bug-fixed: callback is not defined
16
10
 
17
11
  # v0.2.153-rc1
18
12
  - changed: context for gpt-40 and gpt-40-mini
package/ExtApi.js CHANGED
@@ -76,6 +76,7 @@ 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);
79
80
  // const url = `${this.ENDPOINT}/ext/${botId}`;
80
81
  // if (this.log) {console.log("sendMessageToBot URL", url);}
81
82
  // const HTTPREQUEST = {
@@ -0,0 +1,242 @@
1
+ const redis = require('redis');
2
+
3
+ class TdCache {
4
+
5
+ constructor(config) {
6
+ this.redis_host = config.host;
7
+ this.redis_port = config.port;
8
+ this.redis_password = config.password;
9
+ this.client = null;
10
+ }
11
+
12
+ async connect(callback) {
13
+ // client = redis.createClient();
14
+ return new Promise( async (resolve, reject) => {
15
+ this.client = redis.createClient(
16
+ {
17
+ host: this.redis_host,
18
+ port: this.redis_port,
19
+ password: this.redis_password
20
+ });
21
+ this.client.on('error', err => {
22
+ reject(err);
23
+ if (callback) {
24
+ callback(err);
25
+ }
26
+ });
27
+ // this.client.on('connect', function() {
28
+ // console.log('Redis Connected!');
29
+ // });
30
+ this.client.on('ready',function() {
31
+ console.log("connected")
32
+ resolve();
33
+ if (callback) {
34
+ callback();
35
+ }
36
+ //console.log("Redis is ready.");
37
+ });
38
+ });
39
+ }
40
+
41
+ async set(key, value, options) {
42
+ //console.log("setting key value", key, value)
43
+ if (!options) {
44
+ options = {EX: 86400}
45
+ }
46
+ return new Promise( async (resolve, reject) => {
47
+ if (options && options.EX) {
48
+ //console.log("expires:", options.EX)
49
+ try {
50
+ await this.client.set(
51
+ key,
52
+ value,
53
+ 'EX', options.EX);
54
+ }
55
+ catch(error) {
56
+ reject(error)
57
+ }
58
+ }
59
+ else {
60
+ try {
61
+ //console.log("setting here...key", key, value)
62
+ await this.client.set(
63
+ key,
64
+ value);
65
+ }
66
+ catch(error) {
67
+ console.error("Error", error);
68
+ reject(error)
69
+ }
70
+ }
71
+ if (options && options.callback) {
72
+ options.callback();
73
+ }
74
+ //console.log("resolving...", key);
75
+ return resolve();
76
+ });
77
+ }
78
+
79
+ async incr(key) {
80
+ // console.log("incr key:", key)
81
+ return new Promise( async (resolve, reject) => {
82
+ try {
83
+ // console.log("incr here...key", key)
84
+ await this.client.incr(key);
85
+ }
86
+ catch(error) {
87
+ console.error("Error on incr:", error);
88
+ reject(error)
89
+ }
90
+ return resolve();
91
+ });
92
+ }
93
+
94
+ async hset(dict_key, key, value, options) {
95
+ //console.log("hsetting dict_key key value", dict_key, key, value)
96
+ return new Promise( async (resolve, reject) => {
97
+ if (options && options.EX) {
98
+ //console.log("expires:", options.EX)
99
+ try {
100
+ await this.client.hset(
101
+ dict_key,
102
+ key,
103
+ value,
104
+ 'EX', options.EX);
105
+ }
106
+ catch(error) {
107
+ reject(error)
108
+ }
109
+ }
110
+ else {
111
+ try {
112
+ //console.log("setting here...key", key, value)
113
+ await this.client.hset(
114
+ dict_key,
115
+ key,
116
+ value);
117
+ }
118
+ catch(error) {
119
+ console.error("Error", error);
120
+ reject(error)
121
+ }
122
+ }
123
+ if (options && options.callback) {
124
+ options.callback();
125
+ }
126
+ return resolve();
127
+ });
128
+ }
129
+
130
+ async hdel(dict_key, key, options) {
131
+ //console.log("hsetting dict_key key value", dict_key, key, value)
132
+ return new Promise( async (resolve, reject) => {
133
+ if (options && options.EX) {
134
+ //console.log("expires:", options.EX)
135
+ try {
136
+ await this.client.hdel(
137
+ dict_key,
138
+ key,
139
+ 'EX', options.EX);
140
+ }
141
+ catch(error) {
142
+ reject(error)
143
+ }
144
+ }
145
+ else {
146
+ try {
147
+ //console.log("setting here...key", key, value)
148
+ await this.client.hdel(
149
+ dict_key,
150
+ key);
151
+ }
152
+ catch(error) {
153
+ console.error("Error", error);
154
+ reject(error);
155
+ }
156
+ }
157
+ if (options && options.callback) {
158
+ options.callback();
159
+ }
160
+ return resolve();
161
+ });
162
+ }
163
+
164
+ async setJSON(key, value, options) {
165
+ const _string = JSON.stringify(value);
166
+ return await this.set(key, _string, options);
167
+ }
168
+
169
+ async get(key, callback) {
170
+ //console.log("getting key", key)
171
+ return new Promise( async (resolve, reject) => {
172
+ this.client.get(key, (err, value) => {
173
+ if (err) {
174
+ reject(err);
175
+ }
176
+ else {
177
+ if (callback) {
178
+ callback(value);
179
+ }
180
+ return resolve(value);
181
+ }
182
+ });
183
+ });
184
+ }
185
+
186
+ async hgetall(dict_key, callback) {
187
+ //console.log("hgetting dics", dict_key);
188
+ return new Promise( async (resolve, reject) => {
189
+ this.client.hgetall(dict_key, (err, value) => {
190
+ if (err) {
191
+ reject(err);
192
+ if (callback) {
193
+ callback(err, null);
194
+ }
195
+ }
196
+ else {
197
+ if (callback) {
198
+ callback(null, value);
199
+ }
200
+ resolve(value);
201
+ }
202
+ });
203
+ });
204
+ }
205
+
206
+ async hget(dict_key, key, callback) {
207
+ //console.log("hgetting dics", dict_key);
208
+ return new Promise( async (resolve, reject) => {
209
+ this.client.hget(dict_key, key, (err, value) => {
210
+ if (err) {
211
+ reject(err);
212
+ if (callback) {
213
+ callback(err, null);
214
+ }
215
+ }
216
+ else {
217
+ if (callback) {
218
+ callback(null, value);
219
+ }
220
+ resolve(value);
221
+ }
222
+ });
223
+ });
224
+ }
225
+
226
+ async getJSON(key, callback) {
227
+ const value = await this.get(key);
228
+ return JSON.parse(value);
229
+ }
230
+
231
+ async del(key, callback) {
232
+ return new Promise( async (resolve, reject) => {
233
+ await this.client.del(key);
234
+ if (callback) {
235
+ callback();
236
+ }
237
+ return resolve();
238
+ })
239
+ }
240
+ }
241
+
242
+ module.exports = { TdCache };
package/index.js CHANGED
@@ -32,7 +32,6 @@ 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');
36
35
  let API_ENDPOINT = null;
37
36
  let TILEBOT_ENDPOINT = null;
38
37
  let staticBots;
@@ -64,21 +63,13 @@ router.post('/ext/:botid', async (req, res) => {
64
63
  message.request.id_project = projectId;
65
64
  }
66
65
 
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
- console.log('(chatbot index) is audio', message.metadata)
76
- let responseText = await aiService.speechToText(message.metadata.src).catch(err => {
77
- console.log('errrr', err)
78
- return res.status(400).send({"success": false, error: "Unable to translate audio message for request: " + requestId})
79
- })
80
- message.text= responseText.text
81
- }
66
+ // let request_check = checkRequest(message.request.request_id, message.id_project);
67
+ // if (request_check === true) {
68
+ // res.status(200).send({ "successs": true });
69
+ // } else {
70
+ // return res.status(400).send({ "success": false, "message": "Invalid request_id"})
71
+ // }
72
+ // res.status(200).send({"success":true});
82
73
 
83
74
  // validate reuqestId
84
75
  let isValid = TiledeskChatbotUtil.validateRequestId(requestId, projectId);
@@ -205,12 +196,10 @@ router.post('/ext/:botid', async (req, res) => {
205
196
  }
206
197
  catch(err) {
207
198
  console.error("(tybotRoute) An error occurred replying to message:", JSON.stringify(message), "\nError:", err );
208
- callback();
209
199
  return;
210
200
  }
211
201
  if (!reply) {
212
202
  if (log) { console.log("(tybotRoute) No reply. Stop flow.") }
213
- callback();
214
203
  return;
215
204
  }
216
205
 
@@ -771,6 +760,7 @@ async function checkRequest(request_id, id_project) {
771
760
  * @param {string} token. User token
772
761
  */
773
762
  function sendMessageToBot(TILEBOT_ENDPOINT, message, botId, callback) {
763
+ // const jwt_token = this.fixToken(token);
774
764
  const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
775
765
  console.log("sendMessageToBot URL", url);
776
766
  const HTTPREQUEST = {
@@ -480,13 +480,6 @@ 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
-
490
483
  static lastUserMessageFrom(msg) {
491
484
  let message = {};
492
485
  message["senderFullname"] = msg["senderFullname"]; // ex. "Bot"
@@ -933,6 +926,7 @@ class TiledeskChatbotUtil {
933
926
  * @param {string} requestId. Tiledesk chatbot/requestId parameters
934
927
  */
935
928
  getChatbotParameters(requestId, callback) {
929
+ // const jwt_token = this.fixToken(token);
936
930
  const url = `${process.env.TILEBOT_ENDPOINT}/ext/reserved/parameters/requests/${requestId}?all`;
937
931
  const HTTPREQUEST = {
938
932
  url: url,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-tybot-connector",
3
- "version": "0.2.153-rc8",
3
+ "version": "0.3.0",
4
4
  "description": "Tiledesk Tybot connector",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -15,7 +15,6 @@
15
15
  "@tiledesk/tiledesk-chatbot-client": "^0.5.30",
16
16
  "@tiledesk/tiledesk-chatbot-util": "^0.8.39",
17
17
  "@tiledesk/tiledesk-client": "^0.10.13",
18
- "@tiledesk/tiledesk-multi-worker": "^0.1.14",
19
18
  "accept-language-parser": "^1.5.0",
20
19
  "axios": "^1.7.7",
21
20
  "body-parser": "^1.19.0",
@@ -108,6 +108,7 @@ 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);
111
112
  const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
112
113
  // console.log("sendMessageToBot URL", url);
113
114
  const HTTPREQUEST = {
@@ -133,6 +133,7 @@ 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);
136
137
  const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
137
138
  // console.log("sendMessageToBot URL", url);
138
139
  const HTTPREQUEST = {
@@ -61,6 +61,7 @@ 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);
64
65
  const url = `${TILEBOT_ENDPOINT}/ext/${botId}`;
65
66
  // console.log("sendMessageToBot URL", url);
66
67
  const HTTPREQUEST = {
@@ -3,7 +3,6 @@ const { TiledeskChatbot } = require('../../models/TiledeskChatbot');
3
3
  const { TiledeskChatbotUtil } = require('../../models/TiledeskChatbotUtil');
4
4
  let axios = require('axios');
5
5
  const { TiledeskClient } = require('@tiledesk/tiledesk-client');
6
- const { Logger } = require('../../Logger');
7
6
 
8
7
  class DirReply {
9
8
 
@@ -17,7 +16,6 @@ class DirReply {
17
16
  this.token = context.token;
18
17
  this.tdcache = context.tdcache;
19
18
  this.log = context.log;
20
- this.logger = new Logger({ request_id: this.requestId, dev: this.context.supportRequest.draft });
21
19
 
22
20
  this.API_ENDPOINT = context.API_ENDPOINT;
23
21
  this.tdClient = new TiledeskClient({
@@ -43,10 +41,7 @@ class DirReply {
43
41
  callback();
44
42
  return;
45
43
  }
46
- this.logger.info("1 Execute action reply for " + directive.action.text)
47
-
48
44
  this.go(action, () => {
49
- this.logger.info("6 End of action reply " + directive.action.text + " -> callback")
50
45
  callback();
51
46
  });
52
47
  }
@@ -69,8 +64,6 @@ class DirReply {
69
64
  const filler = new Filler();
70
65
  // fill text attribute
71
66
  message.text = filler.fill(message.text, requestAttributes);
72
- this.logger.info("2 Sending reply " + message.text);
73
-
74
67
  if (message.metadata) {
75
68
  if (this.log) {console.log("filling message 'metadata':", JSON.stringify(message.metadata));}
76
69
  if (message.metadata.src) {
@@ -146,8 +139,6 @@ class DirReply {
146
139
  }
147
140
  // send!
148
141
  let cleanMessage = message;
149
- this.logger.info("3 Sending reply (text) " + cleanMessage.text);
150
- this.logger.info("4 Sending reply with clean message " + JSON.stringify(cleanMessage));
151
142
  // cleanMessage = TiledeskChatbotUtil.removeEmptyReplyCommands(message);
152
143
  // if (!TiledeskChatbotUtil.isValidReply(cleanMessage)) {
153
144
  // console.log("invalid message", cleanMessage);
@@ -165,10 +156,8 @@ class DirReply {
165
156
  (err) => {
166
157
  if (err) {
167
158
  console.error("Error sending reply:", err);
168
- this.logger.error("Error sending reply: " + err);
169
159
  }
170
160
  if (this.log) {console.log("Reply message sent:", JSON.stringify(cleanMessage));}
171
- this.logger.info("5 Reply message sent");
172
161
  const delay = TiledeskChatbotUtil.totalMessageWait(cleanMessage);
173
162
  // console.log("got total delay:", delay)
174
163
  if (delay > 0 && delay <= 30000) { // prevent long delays
package/Logger.js DELETED
@@ -1,74 +0,0 @@
1
- let { Publisher } = require("@tiledesk/tiledesk-multi-worker");
2
-
3
- const AMQP_MANAGER_URL = process.env.AMQP_MANAGER_URL;
4
- let publisher = new Publisher(AMQP_MANAGER_URL, {
5
- debug: false,
6
- queueName: "logs_queue",
7
- exchange: "tiledesk-multi",
8
- topic: "logs",
9
- })
10
-
11
- class Logger {
12
-
13
- constructor(config) {
14
-
15
- if (!config) {
16
- throw new Error('config is mandatory');
17
- }
18
-
19
- if (!config.request_id) {
20
- console.error('config.request_id is mandatory');
21
- //throw new Error('config.request_id is mandatory');
22
- }
23
-
24
- this.request_id = config.request_id;
25
- this.dev = config.dev;
26
-
27
- if (!AMQP_MANAGER_URL) {
28
- console.error('AMQP_MANAGER_URL is undefined. Logger not available...');
29
- return;
30
- //throw new Error("Error starting logger: AMQP_MANAGER_URL is undefined.")
31
- }
32
-
33
- }
34
-
35
- error(text) {
36
- if (!this.request_id || !publisher) {
37
- console.log("Return because request or publisher is undefined", this.request_id, publisher);
38
- return;
39
- }
40
-
41
- let data = {
42
- request_id: this.request_id,
43
- text: text,
44
- level: "error",
45
- timestamp: new Date()
46
- }
47
- publisher.publish(data, (err, ok) => {
48
- if (err) console.warn("publish log fail: ", err);
49
- return;
50
- })
51
- }
52
-
53
- info(text) {
54
- if (!this.request_id || !publisher) {
55
- console.log("Return because request or publisher is undefined", this.request_id, publisher);
56
- return;
57
- }
58
-
59
- let data = {
60
- request_id: this.request_id,
61
- text: text,
62
- level: "info",
63
- timestamp: new Date()
64
- }
65
-
66
- publisher.publish(data, (err, ok) => {
67
- if (err) console.warn("publish log fail: ", err);
68
- return;
69
- })
70
- }
71
-
72
- }
73
-
74
- module.exports = { Logger }
@@ -1,43 +0,0 @@
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
@@ -1,99 +0,0 @@
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