-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxm_livecall.txt
409 lines (270 loc) · 16.8 KB
/
xm_livecall.txt
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
var got = require('got');
const request = require('request');
const util = require('util');
exports.handler = function(context, event, callback) {
// Get Settings
var settings = JSON.parse(decodeURI(event.setting));
console.log("EVENT DIGITS: " + event.Digits);
console.log("EVENT: " + JSON.stringify(event));
let twiml = new Twilio.twiml.VoiceResponse();
// Group selection is correct move on to recording
if (( parseInt(event.Digits) <= parseInt(settings.NumberofGroups) && parseInt(event.Digits) !== 0) ) {
if (event.groupIndex === undefined) {
// Adds a Recipient element to the Event Object which will be passed to xMatters to target the appropriate group.
// the element in xMatters_Groups is one less than the digit recieved from user input.
var index = (parseInt(event.Digits)) - 1;
event.recipient = settings.xMatters_Groups[index];
} else {
var targets = JSON.parse(decodeURI(event.targets));
var groupIndex = parseInt(event.groupIndex);
event.recipient = targets[groupIndex].targetName;
// Remove the group we are working on now so we can insert membership in its place
targets.splice(groupIndex, 1);
}
//=================================================================
//Get Whos-On-call
var currenttime = new Date();
console.log("Getting Group On Call " + event.recipient)
return got(settings.xmatters + '/api/xm/1/on-call?groups=' + encodeURIComponent(event.recipient) + "&membersPerShift=100" + "&at=" + currenttime.toISOString(), {
headers: {
'accept': 'application/json',
'Authorization': 'Basic ' + Buffer.from(settings.xm_user + ':' + settings.xm_pass).toString('base64')
},
json: true
})
.then(function(response) {
console.log("Get Group " + JSON.stringify(response.statusCode) + JSON.stringify(response.body));
response = response.body;
// If groupIndex is undefined this is the first time looking for groups
if (event.groupIndex === undefined) {
var oncallTarget = [];
}
// There is a group index we need to look up the group and get oncall
else {
var oncallTarget = targets;
}
console.log("oncallTarget " + JSON.stringify(oncallTarget));
var oncall_array = [];
// Check if there are any members in the group. If there are not record no primary on call and continue.
if (response['data'][0]['members'] === undefined) {
console.log("No Members");
// Group with no member, remove from oncallTargets
oncallTarget.splice(groupIndex, 1);
oncall_array.push(oncallTarget);
return oncall_array;
}
// Get each member thats on call
var targetArray = [];
for (var m in response['data'][0]['members']['data']) {
console.log("Fetch the Group Members: " + response['data'][0]['members']['data'][m]['member']['targetName'] + " " + response['data'][0]['members']['data'][m]['member']['status'] + " " + response['data'][0]['members']['data'][m]['member']['recipientType']);
// Check if ACTIVE
if (response['data'][0]['members']['data'][m]['member']['status'] === "ACTIVE") {
// Check if recipientType = PERSON
if (response['data'][0]['members']['data'][m]['member']['recipientType'] === "PERSON") {
//Check for replacement
if (response['data'][0]['members']['data'][m]['absences'] !== undefined) {
for (var a in response['data'][0]['members']['data'][m]['absences']['data']) {
if (response['data'][0]['members']['data'][m]['absences']['data'][a]['absenceType'] !== "VACANCY") {
console.log("HAS Absence, GET REPLACEMENT");
// Compare current time in UTC to response['data'][0]['members']['data'][0]['absences']['data'][a]['start'] and response['data'][0]['members']['data'][0]['absences']['data'][a]['end']
if (currenttime >= new Date(response['data'][0]['members']['data'][m]['absences']['data'][a]['start']) && currenttime <= new Date(response['data'][0]['members']['data'][m]['absences']['data'][a]['end'])) {
var newTarget = {};
newTarget.type = response['data'][0]['members']['data'][m]['member']['recipientType'];
newTarget.position = response['data'][0]['members']['data'][m]['position'];
newTarget.fullName = response['data'][0]['members']['data'][m]['absences']['data'][a]['replacement']['firstName'] + " " + response['data'][0]['members']['data'][m]['absences']['data'][a]['replacement']['lastName'];
newTarget.targetName = response['data'][0]['members']['data'][m]['absences']['data'][a]['replacement']['targetName'];
newTarget.voice = "";
if (event.groupIndex === undefined) {
oncallTarget.push(newTarget);
} else {
oncallTarget.splice(groupIndex, 0, newTarget);
groupIndex = groupIndex + 1;
}
}
} // Close VACANCY check
}
} else {
console.log("No Absence, set member to oncall list");
var newTarget = {};
newTarget.type = response['data'][0]['members']['data'][m]['member']['recipientType'];
newTarget.position = response['data'][0]['members']['data'][m]['position'];
newTarget.fullName = response['data'][0]['members']['data'][m]['member']['firstName'] + " " + response['data'][0]['members']['data'][m]['member']['lastName'];
newTarget.targetName = response['data'][0]['members']['data'][m]['member']['targetName'];
newTarget.voice = "";
if (event.groupIndex === undefined) {
oncallTarget.push(newTarget);
} else {
oncallTarget.splice(groupIndex, 0, newTarget);
groupIndex = groupIndex + 1;
}
} // Close else and Absence check
} // Close is PERSON
else if (response['data'][0]['members']['data'][m]['member']['recipientType'] === "DEVICE") {
if (response['data'][0]['members']['data'][m]['member']['deviceType'] === "VOICE") {
var newTarget = {};
newTarget.type = response['data'][0]['members']['data'][m]['member']['recipientType'];
newTarget.position = response['data'][0]['members']['data'][m]['position'];
newTarget.fullName = response['data'][0]['members']['data'][m]['member']['owner']['firstName'] + " " + response['data'][0]['members']['data'][m]['member']['owner']['lastName'];
newTarget.targetName = response['data'][0]['members']['data'][m]['member']['owner']['targetName'];
newTarget.voice = response['data'][0]['members']['data'][m]['member']['phoneNumber'];
if (event.groupIndex === undefined) {
oncallTarget.push(newTarget);
} else {
oncallTarget.splice(groupIndex, 0, newTarget);
groupIndex = groupIndex + 1;
}
}
} else if (response['data'][0]['members']['data'][m]['member']['recipientType'] === "GROUP") {
console.log("Nested groups");
var newTarget = {};
newTarget.type = response['data'][0]['members']['data'][m]['member']['recipientType'];
newTarget.position = response['data'][0]['members']['data'][m]['position'];
newTarget.fullName = "";
newTarget.targetName = response['data'][0]['members']['data'][m]['member']['targetName'];
newTarget.voice = "";
if (event.groupIndex === undefined) {
oncallTarget.push(newTarget);
} else {
oncallTarget.splice(groupIndex, 0, newTarget);
groupIndex = groupIndex + 1;
}
}
} // Close ACTIVE
} // Close Loop members
//======================================================================================
return oncallTarget;
}).then(function(response) {
var targets = response;
console.log("Updated targets " + JSON.stringify(targets));
for (var gi in targets) {
if (targets[gi].type === "GROUP") {
console.log("Group Found at " + gi + " " + targets[gi].targetName);
twiml.redirect(settings.xm_livecall + '?setting=' + encodeURI(JSON.stringify(settings)) + "&targets=" + encodeURI(JSON.stringify(targets)) + '&Message_Phrase=' + encodeURI(event.Message_Phrase) + "&groupIndex=" + gi + "&Digits=" + event.Digits);
callback(null, twiml);
return "group";
}
}
console.log("No groups found, start getting devices.");
return targets;
}).then(function(response) {
console.log("after group check : " + JSON.stringify(response));
if (response !== "group") {
const promises = [];
var targets = response;
// Get user devices from xMatters and add to oncall list
for (var p in targets) {
console.log("Getting Voice Device for : " + JSON.stringify(targets[p].targetName));
promises.push(getVoice(p, targets));
}
Promise.all(promises).then(function(results) {
console.log("FINAL RESULT " + JSON.stringify(results));
results = results[0];
// Clean up target members and remove anyone who does not have a voice device
if (results !== undefined) {
// Loop backwords to prevent re-indexing issues with splice
for (var blank = results.length - 1; blank >= 0; blank--) {
if (results[blank].voice === '') {
results.splice(blank, 1);
}
}
}
// No primary on call members
if (results === undefined || results.length < 1) {
console.log("No primary oncall members");
twiml.say({
voice: 'alice'
}, "Sorry, there is no one with a voice device on call right now. Try again later or target a different group.");
twiml.redirect(settings.xm_action + '?setting=' + encodeURI(JSON.stringify(settings)));
callback(null, twiml);
}
// Call the Primary on call
else if (results.length > 0 && results[0].type === "PERSON") {
console.log("Has Primary oncall members");
var index = (parseInt(event.Digits)) - 1;
recipientGroup = settings.Speak_Groups[index]
// Tell user who we are about to call
twiml.say({
voice: 'alice'
}, "Calling " + recipientGroup + ", primary on call " + results[0].fullName + "..., good luck with your problem!");
// Make the call to primary on call
const dial = twiml.dial({
'callerId': settings.callerID,
'timeout': 12,
'action': settings.xm_escalate + '?setting=' + encodeURI(JSON.stringify(settings)) + '&targets=' + encodeURI(JSON.stringify(results)) + "&escalation=0"
});
dial.number(results[0].voice);
callback(null, twiml);
}
}).catch(function(e) {
console.log('error', e);
twiml.say({
voice: 'alice'
}, "Oops, Something went wrong. Please try again NOW.");
twiml.redirect(settings.xm_action + '?setting=' + encodeURI(JSON.stringify(settings)));
callback(null, twiml);
});
} // Close Group check
function getVoice(p, targets) {
const promise = new Promise((resolve, reject) => {
return get_xm_devices(p, targets)
.then(value => resolve(value))
.catch(error => reject(error));
});
return promise;
}
//==========
// Det target users Voice Device
function get_xm_devices(p, targets) {
return got(settings.xmatters + "/api/xm/1/people/" + targets[p].targetName + "/devices", {
headers: {
'accept': 'application/json',
'Authorization': 'Basic ' + Buffer.from(settings.xm_user + ':' + settings.xm_pass).toString('base64')
},
json: true
})
.then(function(response) {
response = response.body;
console.log("Get Device Response " + JSON.stringify(response));
for (var t in response.data) {
if (response.data[t].deviceType === "VOICE") {
targets[p].voice = response.data[t].phoneNumber;
}
}
console.log("Targets with Voice Devices added: " + JSON.stringify(targets));
return targets;
}).catch(function(error) {
console.log("error " + error);
twiml.say({
voice: 'alice'
}, "Oops, Somthing went wrong. Please try again LAST.");
twiml.redirect(settings.xm_action + '?setting=' + encodeURI(JSON.stringify(settings)));
callback(null, twiml);
});
} // Close function
//=============
})
.catch(function(error) {
console.log("error " + error);
twiml.say({
voice: 'alice'
}, "Oops, Somthing went wrong. Please try again.");
twiml.redirect(settings.xm_action + '?setting=' + encodeURI(JSON.stringify(settings)));
callback(null, twiml);
});
}
// If group selection incorrect repeat the step
else {
if (event.groupIndex === undefined){
twiml.say({ voice: 'alice', loop: 1},'Invalid Digit. Listen carefully.');
const gather = twiml.gather({
input: 'dtmf',
numDigits: 1,
action: settings.xm_livecall + '?setting=' + encodeURI(JSON.stringify(settings)) + '&Message_Phrase=' + encodeURI(event.Message_Phrase) + '&what=' + event.what + '&Digits=' + event.Digits
});
gather.say({ voice: 'alice', loop: 1}, event.Message_Phrase + settings.Group_Speak_Text);
twiml.pause({ length: 1 });
twiml.redirect(settings.xm_livecall + '?setting=' + encodeURI(JSON.stringify(settings)) + '&Message_Phrase=' + encodeURI(event.Message_Phrase) + '&what=' + event.what + '&Digits=' + event.Digits);
callback(null, twiml);
}
}
}; // close handler