@tiledesk/tiledesk-voice-twilio-connector 0.1.27 → 0.1.28

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/index.js CHANGED
@@ -157,32 +157,48 @@ router.post('/webhook/:id_project', async (req, res) => {
157
157
  }
158
158
  let end2 = new Date().getTime();
159
159
 
160
- //let conversation_id = await tdChannel.getConversation(ani, callId, user.token);
161
- let conversation_id = await tdChannel.generateConversation(from, callSid, user.token);
160
+ // Parallelizza generateConversation e recupero chiavi di integrazione
161
+ let [conversation_id, openaiKeyResult, elevenLabsKey] = await Promise.all([
162
+ //let conversation_id = await tdChannel.getConversation(ani, callId, user.token);
163
+ tdChannel.generateConversation(from, callSid, user.token),
164
+
165
+ // Recupero chiave OpenAI (con fallback sequenziale)
166
+ (async () => {
167
+ try {
168
+ let key = await integrationService.getKeyFromIntegrations(project_id, 'openai', settings.token)
169
+ if (!key) {
170
+ winston.debug("(voice) - Key not found in Integrations. Searching in kb settings...");
171
+ key = await integrationService.getKeyFromKbSettings(project_id, settings.token);
172
+ }
173
+ if (!key) {
174
+ winston.debug("(voice) - Retrieve public gptkey")
175
+ key = GPT_KEY;
176
+ return { key, publicKey: true };
177
+ }
178
+ return { key, publicKey: false };
179
+ } catch (error) {
180
+ winston.error('(voice) - Error retrieving OpenAI key:', error);
181
+ return { key: GPT_KEY, publicKey: true };
182
+ }
183
+ })(),
184
+
185
+ // Recupero chiave ElevenLabs in parallelo
186
+ integrationService.getKeyFromIntegrations(project_id, 'elevenlabs', settings.token).catch((error) => {
187
+ winston.error('(voice) - Error retrieving ElevenLabs key:', error);
188
+ return null;
189
+ })
190
+ ]);
191
+
162
192
  winston.debug("(voice) conversation returned:"+ conversation_id);
163
193
 
164
- let integrations = [], publicKey = false;
165
- try {
166
- //GET AND SAVE GPT-KET IF
167
- let key = await integrationService.getKeyFromIntegrations(project_id, 'openai', settings.token)
168
- if (!key) {
169
- winston.debug("(voice) - Key not found in Integrations. Searching in kb settings...");
170
- key = await integrationService.getKeyFromKbSettings(project_id, settings.token);
171
- }
172
- if (!key) {
173
- winston.debug("(voice) - Retrieve public gptkey")
174
- key = GPT_KEY;
175
- publicKey = true;
176
- }
177
- integrations.push({type: 'openai', key: key, publicKey: publicKey})
178
-
179
- let eleven_labs = await integrationService.getKeyFromIntegrations(project_id, 'elevenlabs', settings.token)
180
- if (eleven_labs) {
181
- winston.debug("(voice) - Key found in Integrations: "+ eleven_labs);
182
- integrations.push({type: 'elevenlabs', key: eleven_labs, publicKey: false})
183
- }
184
- } catch (error) {
185
- winston.error('(voice) - Error retrieving integrations keys:', error);
194
+ // Costruisci array integrations
195
+ let integrations = [];
196
+ if (openaiKeyResult && openaiKeyResult.key) {
197
+ integrations.push({type: 'openai', key: openaiKeyResult.key, publicKey: openaiKeyResult.publicKey})
198
+ }
199
+ if (elevenLabsKey) {
200
+ winston.debug("(voice) - Key found in Integrations: "+ elevenLabsKey);
201
+ integrations.push({type: 'elevenlabs', key: elevenLabsKey, publicKey: false})
186
202
  }
187
203
 
188
204
  //save data to redis
@@ -232,12 +248,16 @@ router.post('/webhook/:id_project', async (req, res) => {
232
248
  let messageToVXML = await tdTranslator.toVXML(message, callSid, vxmlAttributes, session_data)
233
249
  winston.debug('(voice) /webhook/:id_project messageVXML-->'+ messageToVXML)
234
250
 
235
- let end_call = new Date().getTime();
236
- winston.info(`Time to respond to /webhook/${project_id}: ${(end_call-start_call)}[ms]`)
251
+ let end_call1 = new Date().getTime();
252
+ winston.info(`Time to respond to /webhook/${project_id} before response: ${(end_call1-start_call)}[ms]`)
237
253
 
254
+
238
255
  // Render the response as XML in reply to the webhook request
239
256
  res.set('Content-Type', 'text/xml');
240
257
  res.status(200).send(messageToVXML);
258
+
259
+ let end_call2 = new Date().getTime();
260
+ winston.info(`Time to respond to /webhook/${project_id}: ${(end_call2-start_call)}[ms]`)
241
261
  });
242
262
 
243
263
 
@@ -1446,12 +1466,22 @@ async function startApp(settings, callback) {
1446
1466
  if(settings.OPENAI_ENDPOINT){
1447
1467
  OPENAI_ENDPOINT = settings.OPENAI_ENDPOINT
1448
1468
  }
1449
- if(settings.ELEVENLABS_ENDPOINT){
1450
- ELEVENLABS_ENDPOINT = settings.ELEVENLABS_ENDPOINT
1469
+
1470
+ // Read ELEVENLABS_ENDPOINT from process.env (not from settings)
1471
+ if(process.env.ELEVENLABS_ENDPOINT){
1472
+ ELEVENLABS_ENDPOINT = process.env.ELEVENLABS_ENDPOINT
1473
+ } else {
1474
+ ELEVENLABS_ENDPOINT = "https://api.elevenlabs.io" // default
1451
1475
  }
1452
1476
 
1453
- if(settings.MAX_POLLING_TIME){
1454
- MAX_POLLING_TIME = settings.MAX_POLLING_TIME/1000; //convert in seconds
1477
+ // Read MAX_POLLING_TIME from process.env (not from settings)
1478
+ if(process.env.MAX_POLLING_TIME){
1479
+ MAX_POLLING_TIME = process.env.MAX_POLLING_TIME/1000; //convert in seconds
1480
+ }
1481
+
1482
+ // Read BASE_POOLING_DELAY from process.env (not from settings)
1483
+ if(process.env.BASE_POOLING_DELAY){
1484
+ BASE_POOLING_DELAY = process.env.BASE_POOLING_DELAY;
1455
1485
  }
1456
1486
 
1457
1487
  if (settings.REDIS_HOST && settings.REDIS_PORT) {
@@ -1463,9 +1493,12 @@ async function startApp(settings, callback) {
1463
1493
  winston.error("(voice) Missing redis parameters --> REDIS_HOST and REDIS_PORT");
1464
1494
  }
1465
1495
 
1496
+
1497
+
1498
+
1466
1499
  //init VOICE CHANNEL
1467
1500
  voiceChannel = new VoiceChannel({
1468
- BASE_POOLING_DELAY: settings.BASE_POOLING_DELAY || BASE_POOLING_DELAY,
1501
+ BASE_POOLING_DELAY: BASE_POOLING_DELAY,
1469
1502
  redis_client: redis_client,
1470
1503
  })
1471
1504
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tiledesk/tiledesk-voice-twilio-connector",
3
- "version": "0.1.27",
3
+ "version": "0.1.28",
4
4
  "description": "Tiledesk VOICE Twilio connector",
5
5
  "license": "MIT",
6
6
  "author": "Gabriele Panico",
@@ -306,9 +306,19 @@ class TiledeskTwilioTranslator {
306
306
 
307
307
  async playPromptVXMLConverter(rootEle, message, xmlAttributes){
308
308
 
309
+ const gather = rootEle.ele("Gather", { input: "speech"})
310
+
311
+ const queryUrl = '?intentName='+ querystring.encode(xmlAttributes.intentName) + '&previousIntentTimestamp='+Date.now();
312
+ gather.att("action", this.BASE_URL + '/nextBlock/' + xmlAttributes.callSid + queryUrl)
313
+ // gather.att("action", this.BASE_URL + '/speechresult/' + xmlAttributes.callSid + queryUrl)
314
+ .att("method", "POST")
315
+ .att("language", xmlAttributes.TTS_VOICE_LANGUAGE)
316
+ .att('speechTimeout', "auto")
317
+ .att("enhanced", "true") // enable enhanced recognition
318
+
309
319
  const prompt = await this.promptVXML(rootEle, message, xmlAttributes);
310
320
 
311
- const queryUrl = '?intentName='+ querystring.encode(xmlAttributes.intentName) + '&previousIntentTimestamp='+Date.now();
321
+ /** fallback se non parla --> redirige alla nextblock */
312
322
  rootEle.ele("Redirect", {method: "POST"}, this.BASE_URL + '/nextblock/' + xmlAttributes.callSid + queryUrl).up()
313
323
  //prompt.ele("submit", { fetchhint: "safe", expr: "proxyBaseUrl +'/nextblock/' + session.connection.calltoken", method: "post", namelist: "usertext session intentName previousIntentTimestamp" });
314
324