-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·97 lines (79 loc) · 2.45 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
'use strict';
/*
Instalar dependencias con npm:
1. ari-client
2. mysql
3. google-tts-api
4. sox-audio
5. path
*/
const rutaAudioMapu = '/var/lib/asterisk/sounds/custom/citasGoogle';
const generarAudio = require('./js/googleTTS.js');
const convertirAudio = require('./js/sox');
const path = require('path');
const ari = require('ari-client');
const mysql = require('mysql');
// Configuración de la conexión MySQL
const dbConfig = {
host: 'localhost',
user: 'root',
password: '',
database: 'clinic'
};
const dbConnection = mysql.createConnection(dbConfig);
dbConnection.connect();
// Función para obtener las citas disponibles
function obtenerCitasDisponibles() {
return new Promise((resolve, reject) => {
const query = 'SELECT * FROM appointments ORDER BY id';
dbConnection.query(query, (error, results) => {
if (error) {
reject(error);
} else {
resolve(results);
}
});
});
}
ari.connect('http://localhost:8088', 'adminari', '1234', clientLoaded);
async function clientLoaded(err, client) {
if (err) {
throw err;
}
/** Listener Start Stasis */
client.on('StasisStart', async (event, incoming) => {
// Send a Playback
incoming.answer((err, channel) => {
// Playback
play(incoming, `sound:${rutaAudioMapu}`, (err) => {
// Playback Completed - Send a Hangup Channel
incoming.hangup((err) => {});
});
});
});
const citas = await obtenerCitasDisponibles();
let citasDisponibles = '';
if(citas.length === 0){
citasDisponibles = 'No hay citas disponibles en este momento';
}
else{
//citasDisponibles = 'Las citas disponibles para esta semana son las siguientes: '
citas
citas.forEach(cita => {
citasDisponibles += `${cita.date} a las ${cita.time}, `;
});
}
await generarAudio(citasDisponibles);
await convertirAudio();
/** Function for Playback a Sound */
function play(channel, sound, callback) {
const playback = client.Playback();
playback.once('PlaybackFinished', (event, instance) => {
if (callback) {
callback(null);
}
});
channel.play({ media: sound }, playback, (err, playback) => {});
}
client.start('cita-ivr');
}