-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoiceapi.js
More file actions
83 lines (70 loc) · 1.99 KB
/
Copy pathvoiceapi.js
File metadata and controls
83 lines (70 loc) · 1.99 KB
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
// core modules
const { request } = require('https');
// modules installed from npm
const btoa = require('btoa');
require('dotenv').config();
// application modules
const logger = require('./logger');
// EnableX server REST API call default options
const httpOptions = {
host: 'api.enablex.io',
port: 443,
headers: {
Authorization: `Basic ${btoa(`${process.env.ENABLEX_APP_ID}:${process.env.ENABLEX_APP_KEY}`)}`,
'Content-Type': 'application/json',
},
};
// To initiate Rest API Call to EnableX Server API
const connectEnablexServer = (data, callback) => {
logger.info(`REQ URI:- ${httpOptions.method} ${httpOptions.host}:${httpOptions.port}${httpOptions.path}`);
logger.info(`REQ PARAM:- ${data}`);
const req = request(httpOptions, (res) => {
let body = '';
res.on('data', (response) => {
body += response;
});
res.on('end', () => {
callback(body);
});
res.on('error', (e) => {
logger.info(`Got error: ${e.message}`);
});
});
if (data == null) {
req.end();
} else {
req.end(data);
}
};
// Voice API call to play IVR using TTS
function playVoiceIVR(callVoiceId, data, callback) {
httpOptions.path = `/voice/v1/call/${callVoiceId}/play`;
httpOptions.method = 'PUT';
connectEnablexServer(data, (response) => {
logger.info(`RESPONSE:- ${response}`);
callback(response);
});
}
// Voice API to accept incomingcall
function acceptCall(callVoiceId, callback) {
httpOptions.path = `/voice/v1/call/${callVoiceId}/accept`;
httpOptions.method = 'PUT';
connectEnablexServer('', (response) => {
logger.info(`RESPONSE:- ${response}`);
callback(response);
});
}
// Voice API call to hangup the call
function hangupCall(callVoiceId, callback) {
httpOptions.path = `/voice/v1/call/${callVoiceId}`;
httpOptions.method = 'DELETE';
connectEnablexServer('', (response) => {
logger.info(`RESPONSE:- ${response}`);
callback(response);
});
}
module.exports = {
playVoiceIVR,
hangupCall,
acceptCall,
};