-
Notifications
You must be signed in to change notification settings - Fork 13
/
victorops-twilio-multi-number.js
1055 lines (965 loc) · 30.1 KB
/
victorops-twilio-multi-number.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==========================================================================
// Copyright 2017 VictorOps, Inc.
// https://github.com/victorops/twilio-live-call-routing/blob/master/LICENSE
// ==========================================================================
const qs = require('qs');
const got = require('got');
const _ = require('lodash');
module.exports = {
assignTeam,
buildOnCallList,
call,
callOrMessage,
handler,
isHuman,
leaveAMessage,
main,
postToVictorOps,
teamsMenu
};
// Make changes to messages if you want to modify what is spoken during a call
// Message keys starting with 'vo' are the text that show up in VictorOps timeline alerts
function handler (context, event, callback) {
const messages = {
missingConfig: 'There is a missing configuration value. Please contact your administrator to fix the problem.',
greeting: 'Welcome to Victor Ops Lyve Call Routing.',
menu: 'Please press 1 to reach an on-call representative or press 2 to leave a message.',
zeroToRepeat: 'Press zero to repeat this menu.',
noResponse: 'We did not receive a response.',
invalidResponse: 'We did not receive a valid response.',
goodbye: 'Goodbye.',
noTeamsError: 'There was an error retrieving the list of teams for your organization.',
otherPartyDisconnect: 'The other party has disconnected.',
attemptTranscription: 'Twilio will attempt to transcribe your message and create an incident in Victor Ops.',
pressKeyToConnect: 'This is Victor Ops Lyve Call Routing. Press any number to connect.',
errorGettingPhoneNumbers: 'There was an error retrieving the on-call phone numbers. Please try again.',
nextOnCall: 'Trying next on-call representative.',
connected: 'You are now connected.',
noAnswer: 'We were unable to reach an on-call representative.',
voicemail: (team) => `Please leave a message for the ${team} team and hang up when you are finished.'`,
connecting: (team) => `We are connecting you to the representative on-call for the ${team} team - Please hold.`,
voTwilioMessageDirect: (team) => `Twilio: message left for the ${team} team`,
voTwilioMessageAfter: (team) => `Twilio: unable to reach on-call for the ${team} team`,
voTwilioTransciption: (transcription, log) => `Transcribed message from Twilio:\n${transcription}${log || ''}`,
voTwilioTransciptionFail: (log) => `Twilio was unable to transcribe message.${log || ''}`,
voCallAnswered: (user, caller, log) => `${user} answered a call from ${caller}.${log}`,
voCallCompleted: (user, caller, duration, log) => `${user} answered a call from ${caller} that lasted ${duration} seconds.${log}`,
noTeam: (team) => `Team ${team} does not exist. Please contact your administrator to fix the problem.`
};
const {VICTOROPS_API_KEY, VICTOROPS_API_ID} = context;
const {payloadString, To} = event;
const payload = _.isUndefined(payloadString)
? {}
: JSON.parse(payloadString);
let {ALERT_HOST, API_HOST, NUMBER_OF_MENUS, voice} = context;
context.ALERT_HOST = _.isUndefined(ALERT_HOST)
? 'alert.victorops.com'
: ALERT_HOST;
context.API_HOST = _.isUndefined(API_HOST)
? 'api.victorops.com'
: API_HOST;
context.messages = messages;
context.headers = {
'Content-Type': 'application/json',
'X-VO-Api-Key': VICTOROPS_API_KEY,
'X-VO-Api-Id': VICTOROPS_API_ID
};
switch (NUMBER_OF_MENUS) {
case '1':
break;
case '2':
break;
default:
context.NUMBER_OF_MENUS = '0';
break;
}
// Add 'voice' key in Twilio config to change how Twilio sounds [default = 'Polly.Salli', 'Polly.Matthew', 'Polly.Joanna']
payload.voice = (voice === 'alice' || voice === 'man')
? voice
: 'Polly.Salli';
let {callerId} = payload;
payload.callerId = _.isUndefined(callerId)
? To
: callerId;
let twiml = new Twilio.twiml.VoiceResponse();
if (requiredConfigsExist(context)) {
main(twiml, context, event, payload)
.then(result => callback(null, result))
.catch(err => console.log(err));
} else {
twiml.say(
{voice: payload.voice},
context.messages.missingConfig
);
callback(null, twiml);
}
}
// Checks that all required configuration values have been entered in Twilio configure
function requiredConfigsExist (context) {
const {VICTOROPS_API_ID, VICTOROPS_API_KEY, VICTOROPS_TWILIO_SERVICE_API_KEY} = context;
if (
_.isUndefined(VICTOROPS_API_ID) ||
_.isUndefined(VICTOROPS_API_KEY) ||
_.isUndefined(VICTOROPS_TWILIO_SERVICE_API_KEY)
) {
return false;
} else {
return true;
}
}
// Routes to the appropriate function based on the value of 'runFunction'
function main (twiml, context, event, payload) {
const {NUMBER_OF_MENUS} = context;
const {runFunction} = payload;
if (_.isUndefined(runFunction)) {
switch (NUMBER_OF_MENUS) {
case '1':
return teamsMenu(twiml, context, event, payload);
case '2':
return callOrMessage(twiml, context, payload);
default:
return teamsMenu(twiml, context, event, payload);
}
}
switch (runFunction) {
case 'teamsMenu':
return teamsMenu(twiml, context, event, payload);
case 'assignTeam':
return assignTeam(twiml, context, event, payload);
case 'buildOnCallList':
return buildOnCallList(twiml, context, payload);
case 'call':
return call(twiml, context, event, payload);
case 'isHuman':
return isHuman(twiml, context, event, payload);
case 'leaveAMessage':
return leaveAMessage(twiml, context, event, payload);
case 'postToVictorOps':
return postToVictorOps(event, context, payload);
default:
return new Promise((resolve, reject) => reject(new Error('No function was called.')));
}
}
// Wrapper that prevents logging while running local test
function log (string, content) {
if (process.env.NODE_ENV !== 'test') {
console.log(string, content);
}
}
// Menu to choose to reach someone on-call or leave a message
function callOrMessage (twiml, context, payload) {
log('callOrMessage', payload);
return new Promise((resolve, reject) => {
const {messages} = context;
const {callerId, voice} = payload;
twiml.gather(
{
input: 'dtmf',
timeout: 10,
action: generateCallbackURI(
context,
{
callerId,
fromCallorMessage: true,
runFunction: 'teamsMenu'
}
),
numDigits: 1
}
)
.say(
{voice},
`${messages.greeting} ${messages.menu} ${messages.zeroToRepeat}`
);
twiml.say(
{voice},
`${messages.noResponse} ${messages.goodbye}`
);
resolve(twiml);
});
}
// Helper function to generate the callback URI with the required data
function generateCallbackURI (context, json) {
const {DOMAIN_NAME} = context;
const payloadString = JSON.stringify(json);
return `https://${DOMAIN_NAME}/victorops-live-call-routing?${qs.stringify({payloadString})}`;
}
// Menu to select team to contact for on-call or leaving a message
function teamsMenu (twiml, context, event, payload) {
log('teamsMenu', event);
return new Promise((resolve, reject) => {
const {API_HOST, headers, messages, NUMBER_OF_MENUS} = context;
let {Digits} = event;
Digits = parseInt(Digits);
const {callerId, fromCallorMessage, voice} = payload;
let {goToVM} = payload;
// Repeats the call or message menu if caller pressed 0
if (Digits === 0) {
twiml.redirect(
generateCallbackURI(
context,
{callerId}
)
);
resolve(twiml);
// Repeats the call or message menu if caller did not enter a valid response
} else if (fromCallorMessage === true && Digits !== 1 && Digits !== 2) {
twiml.say(
{voice},
`${messages.invalidResponse}`
);
twiml.redirect(
generateCallbackURI(
context,
{callerId}
)
);
resolve(twiml);
} else {
got(
`https://${API_HOST}/api-public/v1/team`,
{headers}
)
.then(response => {
let teamsArray;
let teamLookupFail = false;
if (Digits === 2) {
goToVM = true;
}
// If Twilio configure has any keys starting with 'TEAM',
// these teams will be used instead of pulling a list of teams from VictorOps
if (_.isEmpty(buildManualTeamList(context))) {
teamsArray = JSON.parse(response.body)
.map(team => {
return {
name: team.name,
slug: team.slug
};
});
} else {
teamsArray = buildManualTeamList(context)
.map(team => {
const lookupResult = lookupTeamSlug(team.name, JSON.parse(response.body));
if (lookupResult.teamExists) {
return {
name: team.name,
slug: lookupResult.slug,
escPolicyName: team.escPolicyName
};
} else {
teamLookupFail = true;
twiml.say(
{voice},
`${messages.noTeam(team.name)} ${messages.goodbye}`
);
resolve(twiml);
}
});
}
if (teamLookupFail) {
return;
}
// An error message is read and the call ends if there are no teams available
if (teamsArray.length === 0) {
twiml.say(
{voice},
`${messages.noTeamsError} ${messages.goodbye}`
);
// Automatically moves on to next step if there is only one team
} else if (teamsArray.length === 1 || NUMBER_OF_MENUS === '0') {
teamsArray = [teamsArray[0]];
const autoTeam = true;
twiml.redirect(
generateCallbackURI(
context,
{
autoTeam,
callerId,
goToVM,
runFunction: 'assignTeam',
teamsArray
}
)
);
// Generates the menu of teams to prompt the caller to make a selection
} else {
let menuPrompt = 'Please press';
teamsArray.forEach((team, i, array) => {
menuPrompt += ` ${i + 1} for ${team.name}.`;
});
if (NUMBER_OF_MENUS === '1') {
menuPrompt = `${messages.greeting} ${menuPrompt}`;
}
twiml.gather(
{
input: 'dtmf',
timeout: 5,
action: generateCallbackURI(
context,
{
callerId,
goToVM,
runFunction: 'assignTeam',
teamsArray
}
),
numDigits: teamsArray.length.toString().length
}
)
.say(
{voice},
`${menuPrompt} ${messages.zeroToRepeat}`
);
// If no response is received from the caller, the call ends
twiml.say(
{voice},
`${messages.noResponse} ${messages.goodbye}`
);
}
resolve(twiml);
})
.catch(err => {
console.log(err);
twiml.say(
{voice},
`${messages.noTeamsError} ${messages.goodbye}`
);
resolve(twiml);
});
}
});
}
// Creates a list of teams for the teamsMenu if there are any keys that begin with 'TEAM' in Twilio configure
function buildManualTeamList (context) {
log('buildManualTeamsList', context);
const arrayOfTeams = [];
Object.keys(context).forEach((key) => {
if (key.substring(0, 5).toLowerCase() === 'team2') {
const name = context[key];
const keyId = key.substring(5);
let escPolicyName;
Object.keys(context).forEach((key) => {
if (key.substring(0, 8).toLowerCase() === 'esc_pol2' && key.substring(8) === keyId) {
escPolicyName = context[key];
}
});
arrayOfTeams.unshift(
{
name,
escPolicyName
}
);
}
});
return arrayOfTeams;
}
// Gets the team slug for a team if it exists
function lookupTeamSlug (teamName, teamList) {
for (let team of teamList) {
if (team.name === teamName) {
return {
teamExists: true,
slug: team.slug
};
}
}
return {
teamExists: false,
name: teamName
};
}
// Handles the caller's input and chooses the appropriate team
function assignTeam (twiml, context, event, payload) {
log('assignTeam', event);
return new Promise((resolve, reject) => {
const {messages} = context;
let {Digits} = event;
Digits = parseInt(Digits);
const {autoTeam, callerId, goToVM, voice} = payload;
// Repeats the teams menu if caller pressed 0
if (Digits === 0) {
twiml.redirect(
generateCallbackURI(
context,
{
callerId,
goToVM,
runFunction: 'teamsMenu'
}
)
);
// If caller enters an invalid selection, the call ends
} else if (isNaN(Digits) && autoTeam !== true) {
twiml.say(
{voice},
`${messages.invalidResponse} ${messages.goodbye}`
);
// Take the appropriate action based on call or message menu
} else {
let {teamsArray} = payload;
// Take the caller to voicemail
if (goToVM === true) {
if (teamsArray.length === 1) {
twiml.redirect(
generateCallbackURI(
context,
{
callerId,
goToVM,
runFunction: 'leaveAMessage',
teamsArray
}
)
);
} else if (Digits <= teamsArray.length) {
teamsArray = [teamsArray[Digits - 1]];
twiml.redirect(
generateCallbackURI(
context,
{
callerId,
goToVM,
runFunction: 'leaveAMessage',
teamsArray
}
)
);
// If the caller entered an invalid response, the call ends
} else {
twiml.say(
{voice},
`${messages.invalidResponse} ${messages.goodbye}`
);
}
// Proceed to attempt to build a list of people on-call
} else if (teamsArray.length === 1) {
twiml.redirect(
generateCallbackURI(
context,
{
callerId,
goToVM,
runFunction: 'buildOnCallList',
teamsArray
}
)
);
} else if (Digits <= teamsArray.length) {
teamsArray = [teamsArray[Digits - 1]];
twiml.redirect(
generateCallbackURI(
context,
{
callerId,
goToVM,
runFunction: 'buildOnCallList',
teamsArray
}
)
);
// If the caller entered an invalid response, the call ends
} else {
twiml.say(
{voice},
`${messages.invalidResponse} ${messages.goodbye}`
);
}
}
resolve(twiml);
});
}
// Generates a list of people on-call and their phone numbers
function buildOnCallList (twiml, context, payload) {
log('buildOnCallList', payload);
return new Promise((resolve, reject) => {
const {messages, NUMBER_OF_MENUS} = context;
const {callerId, teamsArray, voice} = payload;
// Creates a list of phone numbers based on the first 3 escalation policies
const escPolicyUrlArray = createEscPolicyUrls(context, teamsArray[0].slug);
const phoneNumberArray = escPolicyUrlArray.map(url => getPhoneNumbers(context, url, teamsArray[0].name, teamsArray[0].escPolicyName));
Promise.all(phoneNumberArray)
.then(phoneNumbers => {
phoneNumbers = phoneNumbers.filter(phoneNumber => phoneNumber !== false);
log('phoneNumbers', phoneNumbers);
let message = messages.connecting(teamsArray[0].name);
// Welcome message if caller has not heard any other menu
if (NUMBER_OF_MENUS === '0') {
message = `${messages.greeting} ${message}`;
}
// If there is no one on-call with a phone number, go to voicemail
if (phoneNumbers.length === 0) {
twiml.redirect(
generateCallbackURI(
context,
{
phoneNumbers,
runFunction: 'leaveAMessage',
teamsArray
}
)
);
// Move on to trying connect caller with people on-call
} else {
twiml.say(
{voice},
message
);
twiml.redirect(
generateCallbackURI(
context,
{
callerId,
firstCall: true,
phoneNumbers,
runFunction: 'call',
teamsArray
}
)
);
}
resolve(twiml);
})
.catch(err => {
console.log(err);
twiml.say(
{voice},
`${messages.errorGettingPhoneNumbers}`
);
resolve(twiml);
});
});
}
// Helper function that generates a list of URI's from which to request data from VictorOps with
function createEscPolicyUrls (context, teamSlug) {
log('createEscPolicyUrls', teamSlug);
const {API_HOST} = context;
const onCallUrl = `https://${API_HOST}/api-public/v2/team/${teamSlug}/oncall/schedule?step=`;
const arrayOfUrls = [];
for (let i = 0; i <= 2; i++) {
arrayOfUrls.push(`${onCallUrl}${i}`);
}
return arrayOfUrls;
}
// Generates a list of phone numbers
// Randomly picks on person if there is more than one person on-call for an escalation policy
function getPhoneNumbers (context, escPolicyUrl, teamName, escPolicyName) {
return new Promise((resolve, reject) => {
const {API_HOST, headers} = context;
got(
escPolicyUrl,
{headers}
)
.then(response => {
const body = JSON.parse(response.body);
const {schedules} = body;
const onCallArray = [];
let escPolicyAssigned;
let schedule;
// Check if an escalation policy has been specified in the Twilio UI
if (!(_.isUndefined(escPolicyName))) {
escPolicyAssigned = true;
} else {
escPolicyAssigned = false;
}
// Get the specified escalation policy or get the first one if none is specified
if (escPolicyAssigned) {
schedule = setSchedule(schedules, escPolicyName, teamName);
} else if (schedules.length > 0) {
schedule = schedules[0].schedule;
} else {
schedule = false;
}
if (schedule === false) {
return resolve(false);
}
schedule.forEach((rotation, i, array) => {
if (!(_.isUndefined(rotation.onCallUser))) {
if (!(_.isUndefined(rotation.overrideOnCallUser))) {
onCallArray.push(rotation.overrideOnCallUser.username);
} else {
onCallArray.push(rotation.onCallUser.username);
}
}
});
if (onCallArray.length === 0) {
return resolve(false);
}
const randomIndex = Math.floor(Math.random() * onCallArray.length);
got(
`https://${API_HOST}/api-public/v1/user/${onCallArray[randomIndex]}/contact-methods/phones`,
{headers}
)
.then(response => {
const body = JSON.parse(response.body);
if (body.contactMethods.length === 0) {
return resolve(false);
} else {
return resolve(
{
phone: body.contactMethods[0].value,
user: onCallArray[randomIndex]
}
);
}
})
.catch(err => {
console.log(err);
return reject(err);
});
})
.catch(err => {
console.log(err);
return reject(err);
});
});
}
// Helper function that returns the schedule object if a valid escalation policy is configured in the Twilio UI
function setSchedule (schedulesArray, escPolicyName) {
for (let schedule in schedulesArray) {
if (schedulesArray[schedule].policy.name === escPolicyName) {
return schedulesArray[schedule].schedule;
}
}
return false;
}
// Connects caller to people on-call and builds a log of calls made
function call (twiml, context, event, payload) {
log('call', event);
return new Promise((resolve, reject) => {
const {messages} = context;
const {DialCallStatus, From} = event;
const {callerId, firstCall, goToVM, phoneNumbers, teamsArray, voice} = payload;
let {detailedLog, realCallerId} = payload;
let phoneNumber;
// Caller was connected to on-call person and call completed
if (DialCallStatus === 'completed') {
twiml.say(
{voice},
`${messages.otherPartyDisconnect} ${messages.goodbye}`
);
} else {
if (firstCall !== true) {
twiml.say(
{voice},
`${messages.nextOnCall}`
);
} else {
realCallerId = From;
}
// Attempt to connect to last on-call person and go to voicemail if no answer
if (phoneNumbers.length === 1) {
phoneNumber = phoneNumbers[0];
detailedLog = `\n\n${From} calling ${phoneNumber.user}...${detailedLog || ''}`;
twiml.dial(
{
action: generateCallbackURI(
context,
{
callerId,
goToVM,
detailedLog,
phoneNumber,
phoneNumbers,
realCallerId,
runFunction: 'leaveAMessage',
teamsArray
}
),
callerId
}
)
.number(
{
url: generateCallbackURI(
context,
{
callerId,
detailedLog,
phoneNumber,
phoneNumbers,
runFunction: 'isHuman',
teamsArray
}
),
statusCallback: generateCallbackURI(
context,
{
callerId,
detailedLog,
goToVM,
phoneNumber,
phoneNumbers,
runFunction: 'postToVictorOps',
teamsArray
}
),
statusCallbackEvent: 'completed'
},
phoneNumber.phone
);
// Attempt to connect to first on-call person and attempt to connect to next on-call person if no answer
} else {
phoneNumber = phoneNumbers[0];
phoneNumbers.shift();
detailedLog = `\n\n${From} calling ${phoneNumber.user}...${detailedLog || ''}`;
twiml.dial(
{
action: generateCallbackURI(
context,
{
callerId,
detailedLog,
phoneNumber,
phoneNumbers,
realCallerId,
runFunction: 'call',
teamsArray
}
),
callerId
}
)
.number(
{
url: generateCallbackURI(
context,
{
callerId,
detailedLog,
phoneNumber,
phoneNumbers,
realCallerId,
runFunction: 'isHuman',
teamsArray
}
),
statusCallback: generateCallbackURI(
context,
{
callerId,
detailedLog,
phoneNumber,
phoneNumbers,
realCallerId,
runFunction: 'postToVictorOps',
teamsArray
}
),
statusCallbackEvent: 'completed'
},
phoneNumber.phone
);
}
}
resolve(twiml);
});
}
// Asks called party for an input when they pick up the phone to differentiate between human and voicemail
function isHuman (twiml, context, event, payload) {
log('isHuman', event);
return new Promise((resolve, reject) => {
const {messages} = context;
const {Digits} = event;
const {detailedLog, phoneNumber, phoneNumbers, realCallerId, teamsArray, voice} = payload;
if (_.isUndefined(Digits)) {
twiml.gather(
{
input: 'dtmf',
timeout: 8,
action: generateCallbackURI(
context,
{
detailedLog,
phoneNumber,
phoneNumbers,
realCallerId,
runFunction: 'isHuman',
teamsArray
}
),
numDigits: 1
}
)
.say(
{voice},
`${messages.pressKeyToConnect}`
);
twiml.say(
{voice},
`${messages.noResponse} ${messages.goodbye}`
);
twiml.hangup();
} else {
twiml.say(
{voice},
`${messages.connected}`
);
twiml.redirect(
generateCallbackURI(
context,
{
callAnsweredByHuman: true,
detailedLog,
phoneNumber,
phoneNumbers,
realCallerId,
runFunction: 'postToVictorOps',
teamsArray
}
)
);
}
resolve(twiml);
});
}
// Records caller's message and transcribes it
function leaveAMessage (twiml, context, event, payload) {
log('leaveAMessage', event);
return new Promise((resolve, reject) => {
const {messages} = context;
const {DialCallStatus} = event;
const {callerId, detailedLog, goToVM, teamsArray, sayGoodbye, voice} = payload;
// Caller was connected to on-call person and call completed
if (DialCallStatus === 'completed') {
twiml.say(
{voice},
`${messages.otherPartyDisconnect} ${messages.goodbye}`
);
// If caller does not hang up after leaving message,
// this message will play and then end the call
} else if (sayGoodbye === true) {
twiml.say(
{voice},
`${messages.attemptTranscription} ${messages.goodbye}`
);
let message = messages.voicemail(teamsArray[0].name);
if (goToVM !== true) {
message = `${messages.noAnswer} ${message}`;
}
twiml.say(
{voice},
message
);
twiml.record(
{
transcribe: true,
transcribeCallback: generateCallbackURI(
context,
{
callerId,
detailedLog,
goToVM,
runFunction: 'postToVictorOps',
teamsArray
}
),
timeout: 10,
action: generateCallbackURI(
context,
{
callerId,
detailedLog,
runFunction: 'leaveAMessage',
sayGoodbye: true,
teamsArray
}
)
}
);
// Play a message, record the caller's message, transcribe caller's message
} else {
let message = messages.voicemail(teamsArray[0].name);
if (goToVM !== true) {
message = `${messages.noAnswer} ${message}`;
}
twiml.say(
{voice},
message
);
twiml.record(
{
transcribe: true,
transcribeCallback: generateCallbackURI(
context,
{
callerId,
detailedLog,
goToVM,
runFunction: 'postToVictorOps',
teamsArray
}
),
timeout: 10,
action: generateCallbackURI(
context,
{
callerId,
detailedLog,
runFunction: 'leaveAMessage',
sayGoodbye: true,
teamsArray
}
)
}
);
}
resolve(twiml);
});
}
// Posts information to VictorOps that generates alerts that show up in the timeline
function postToVictorOps (event, context, payload) {
return new Promise((resolve, reject) => {
const {ALERT_HOST, messages, VICTOROPS_TWILIO_SERVICE_API_KEY} = context;
const {CallSid, CallStatus, CallDuration, TranscriptionStatus, TranscriptionText} = event;
const {callAnsweredByHuman, detailedLog, goToVM, phoneNumber, realCallerId, teamsArray} = payload;
const alert = {