-
Notifications
You must be signed in to change notification settings - Fork 0
/
inboundxml.js
210 lines (190 loc) · 5.42 KB
/
inboundxml.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const cpaas = require('@avaya/cpaas'); //Avaya cloud
var enums = cpaas.enums;
var ix = cpaas.inboundXml;
// Replace the following with your own values
const HOST = "xx.xx.xx.xx"; // Application IP address
const URL_PORT = xxxx; // Application port
const TRANSFER_NUMBER = "+1xxxxxxxxxx"; // Ten digit tranfser telephone number
const REQUEST_URL = "http://" + HOST + ":" + URL_PORT.toString() + "/cpaas-request/";
const REDIRECT_URL = "http://" + HOST + ":" + URL_PORT.toString() + "/cpaas-redirect/";
var app = express();
// Middleware to parse JSON
app.use(bodyParser.urlencoded({
extended : true
}));
app.use(bodyParser.json());
app.use(cors());
// Tell server to listen on port
var server = app.listen(URL_PORT, function ()
{
var host = server.address().address;
var port = server.address().port;
console.log(`The transfer agent is listening at http://%s:%s`, HOST, URL_PORT)
});
// Entry point for a GET from a web browser
app.get('/', function (req, res)
{
res.send("The CPaaS virtual agent is running.");
});
//Entry point for voice
app.post('/cpaas-request/', function (req, res) {
//CPaaS Inbound XML Parameters
var from = req.body.From;
var to = req.body.To;
var hangup = false;
var prompt = "Say something";
if ("SpeechResult" in req.body) {
prompt = "I heard you say " + req.body.SpeechResult;
if (req.body.SpeechResult == "goodbye") {
hangup = true;
prompt = "Goodbye";
} else if (req.body.SpeechResult == "transfer") {
processTransfer("You are about to be transfered.", res);
return;
} else if (req.body.SpeechResult == "redirect") {
processRedirect(res);
return;
} else if (req.body.SpeechResult == "play") {
processPlay(res);
return;
} else if (req.body.SpeechResult == "dial tone") {
processDialTone(res);
return;
}
}
sendResponseToCPaaS(prompt, hangup, res);
});
//Entry point for redirect
app.post('/cpaas-redirect/', function (req, res) {
sendResponseToCPaaS("This is the redirect XML web hook. Goodbye!", true, res);
});
async function processRedirect(res) {
var xmlDefinition = generateXMLRedirect();
var serverResponse = await buildCPaaSResponse(xmlDefinition);
res.type('application/xml');
res.send(serverResponse);
}
function generateXMLRedirect() {
var xml_content = [];
var redirect = ix.redirect({
url: REDIRECT_URL
});
xml_content.push(redirect);
var xmlDefinition = ix.response({content: xml_content});
return xmlDefinition;
}
async function processDialTone(res) {
var xmlDefinition = generateDialTone();
var serverResponse = await buildCPaaSResponse(xmlDefinition);
res.type('application/xml');
res.send(serverResponse);
}
function generateDialTone() {
var xml_content = [];
var gather = ix.gather({
action : REQUEST_URL,
method : "POST",
input : "speech",
timeout : "20",
content : [
ix.play({
loop: 1,
url: "tone_stream://%(2000,0,350,440)"
}),
ix.say({
language: enums.Language.EN,
text: "Say something",
voice : enums.Voice.FEMALE
})
]
});
xml_content.push(gather);
var xmlDefinition = ix.response({content: xml_content});
return xmlDefinition;
}
async function processPlay(res) {
var xmlDefinition = generateXMLPlay();
var serverResponse = await buildCPaaSResponse(xmlDefinition);
res.type('application/xml');
res.send(serverResponse);
}
function generateXMLPlay() {
var xml_content = [];
var play = ix.play({
loop : 0,
url: "http://www.hochmuth.com/mp3/Haydn_Adagio.mp3"
});
xml_content.push(play);
var xmlDefinition = ix.response({content: xml_content});
return xmlDefinition;
}
async function processTransfer(prompt, res) {
var xmlDefinition = generateXMLTransfer(prompt);
var serverResponse = await buildCPaaSResponse(xmlDefinition);
res.type('application/xml');
res.send(serverResponse);
}
function generateXMLTransfer(prompt) {
var responseXML = ix.response({content: [
ix.say({
language: enums.Language.EN,
text: prompt,
voice : enums.Voice.FEMALE
}),
ix.dial({
content : [
ix.number({
number: TRANSFER_NUMBER
})
]
})
]});
return responseXML;
}
async function sendResponseToCPaaS(prompt, release, res) {
var xmlDefinition = generateXMLSpeech(REQUEST_URL, prompt, release);
var serverResponse = await buildCPaaSResponse(xmlDefinition);
res.type('application/xml');
res.send(serverResponse);
}
function generateXMLSpeech(request_url, prompt, hangup ) {
var xml_content = [];
if (hangup == false) {
var gather = ix.gather({
action : request_url,
method : "POST",
input : "speech",
timeout : "20",
content : [
ix.say({
language: enums.Language.EN,
text: prompt,
voice : enums.Voice.FEMALE
})
]
});
xml_content.push(gather);
} else {
var say = ix.say({
language: enums.Language.EN,
text: prompt ,
voice : enums.Voice.FEMALE
});
var hangup = ix.hangup();
xml_content.push(say);
xml_content.push(hangup);
}
var xmlDefinition = ix.response({content: xml_content});
return xmlDefinition;
}
async function buildCPaaSResponse(xmlDefinition) {
var result = await ix.build(xmlDefinition).then(function(xml){
return xml;
}).catch(function(err){
console.log('The generated XML is not valid!', err);
});
return result;
}