-
Notifications
You must be signed in to change notification settings - Fork 2
/
xm_settings.txt
228 lines (159 loc) · 8.57 KB
/
xm_settings.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
var got = require('got');
const request = require('request');
const util = require('util');
exports.handler = function(context, event, callback) {
console.log("EVENT: " + JSON.stringify(event));
//***************************************
// CONFIGURATION
//***************************************
//
settings = {};
// Twilio Function Paths
setting.twilio_path = 'https://some-path-1234.twil.io';
// xMatters Base URL
// Do not include trailing slash
setting.xmatters = 'https://acme.xmatters.com';
// xMatters Webservice User name and password
setting.xm_user = 'twiliouser';
setting.xm_pass = 'mycomplexpassword';
// This is a verified phone number to use as the outgoing caller ID when initiating live calls to the primary on call of a group.
// Instructions on how to Verified a phone number in Twilio.
// https://support.twilio.com/hc/en-us/articles/223180048-Adding-a-Verified-Phone-Number-or-Caller-ID-with-Twilio
// If you do not set a number Verified number here, the caller id will be the caller ID of the pone initiating this integration.
setting.callerID = '15555555555';
// Twilio SID and Secret API key for authenticating
// into the Twilio API for transcription
setting.twilio_sid = 'LONG-SID-VALUE-HERE';
setting.twilio_sec = 'LONG-SECRET-HERE';
// This is the Inbound Integration url for "On-Call Alert"
setting.url_alert = 'https://acme.xmatters.com/api/integration/1/functions/UUID/triggers?apiKey=UUID-KEY-HERE';
// This is the Inbound Integration url for "On-Call Conference Bridge"
setting.url_conf = 'https://acme.xmatters.com/api/integration/1/functions/UUID/triggers?apiKey=UUID-KEY-HERE';
// List of xMatters users that are allowed to initiate this integration
// The caller ID initiating this event must be a voice device listed on an xMatters user profile
Authorized_Users = ['userid1', 'userid2', 'userid3'];
// Set the number of groups you would like to be able to target from this integration.
// The number set below must match the number of groups you have in the xMatters_Groups and Speak_Groups arrays below.
// If you set this value to 0 you must add groups / users as default recipients in the xMatters form layout.
setting.NumberofGroups = 3;
// Set the xMatters Group Names.
// Group names must match the name of your xMatters groups exactly.
// You can add additional Groups by adding parameters to the array below.
// This array must have the same number of elements as the Speak_Group array below.
setting.xMatters_Groups = ["Cloud DevOps", "CAB", "Database"];
// Set the Twillio Group Name.
// You can add additional punctuation, spaces, dashes, and upper case letters to group names to help the text to speach engine pronounce it better.
// This array must have the same number of elements as the xMatters_Groups array above.
setting.Speak_Groups = ["Cloud Dev Ops.", "C.A.B.", "Database"];
// Bitley Access Token
setting.bitly_token = 'KEY-HERE';
//***************************************
// DO NOT MAKE CHANGES BELOW THIS COMMENT
//***************************************
// Sets required variables. Do not change this.
setting.xm_settings = setting.twilio_path + '/xm_settings';
setting.xm_action = setting.twilio_path + '/xm_action';
setting.xm_group = setting.twilio_path + '/xm_group';
setting.xm_record = setting.twilio_path + '/xm_record';
setting.xm_livecall = setting.twilio_path + '/xm_livecall';
setting.xm_escalate = setting.twilio_path + '/xm_escalate';
setting.xm_confirmRec = setting.twilio_path + '/xm_confirmRec';
setting.xm_shorten = setting.twilio_path + '/xm_shorten';
setting.xm_message = setting.twilio_path + '/xm_message';
event.what = '';
event.who = '';
setting.url;
setting.Group_Speak_Text = '';
setting.digit = '';
setting.Message_Phrase = '';
setting.Alert_Phrase = 'What group would you like to Alert? ';
setting.Conference_Phrase = 'What group would you like to invite to the conference? ';
setting.Livecall_Phrase = 'What group would you like to speak live with? ';
setting.Wait_Phrase = 'You are a natural! Im sending this over to x matters.';
// Set up the template variables.
// You can customize this but be very careful.
for (i = 0; i < setting.NumberofGroups; i++) {
digit = i + 1;
setting.Group_Speak_Text += "Press " + digit + " for " + setting.Speak_Groups[i] + ". ";
}
if (setting.NumberofGroups === 0) {
setting.Alert_Phrase = 'Sure, I will send an Alert to the groups defined in xMatters. ';
setting.Conference_Phrase = 'Sure, I will start a conference bridge with the groups defined in xMatters. ';
setting.Livecall_Phrase = 'Sure, I will connect you to speak with the primary on call. ';
setting.Group_Speak_Text = '';
}
// Get user devices from xMatters and compare with CallerID
const promises = [];
for (var user in Authorized_Users) {
console.log("Checking if User is Authorized: " + Authorized_Users[user]);
promises.push(get_auth(Authorized_Users[user]));
}
Promise.all(promises).then(function(results) {
console.log('Final Authentication Results: ', results);
// If we find a user who is authorized proceed
if (results.indexOf('Yes') > -1) {
// there's a yes
let twiml = new Twilio.twiml.VoiceResponse();
twiml.redirect({
method: 'POST'
}, setting.xm_action + '?setting=' + encodeURI(JSON.stringify(setting)));
callback(null, twiml);
}
// We did not find an athorized user exit
else {
let twiml = new Twilio.twiml.VoiceResponse();
twiml.say({
voice: 'alice'
}, "Sorry, you are not authorized for this. Please speak with your x matters admin");
callback(null, twiml);
}
}).catch(function(e) {
console.log('Promises error', e);
});
function get_auth(user) {
const promise = new Promise((resolve, reject) => {
return get_xm_devices(user)
.then(value => resolve(value))
.catch(error => reject(error));
});
return promise;
}
function get_xm_devices(user) {
console.log('Getting devices for "' + (setting.xmatters + '/api/xm/1/people/' + user + '/devices') + '"');
return got(setting.xmatters + '/api/xm/1/people/' + user + '/devices', {
headers: {
'accept': 'application/json',
'Authorization': 'Basic ' + Buffer.from(setting.xm_user + ':' + setting.xm_pass).toString('base64')
},
json: true
})
.then(function(response) {
resdata = response.body.data;
console.log("get_xm_devices Response " + JSON.stringify(resdata));
for (var device in resdata) {
// User has voice device check if the caller Id on initiating phone exists.
if (resdata[device].deviceType === "VOICE") {
console.log("User VOICE Device: '" + JSON.stringify(resdata[device].phoneNumber) + "' ?= '" + JSON.stringify(event.From) + "': " + (resdata[device].phoneNumber === event.From));
// Authorized. User Voice device matching caller ID found.
if (resdata[device].phoneNumber === event.From) {
return 'Yes';
}
}
}
// To disable user authentication change the next line to: "return 'Yes';"
return 'No';
})
.catch(function(error) {
console.log("get_xm_devices error " + error);
// We'll just skip the 404, if the user isn't found, then
// he wouldn't be auth'd anyway
if (error.statusCode != 404)
callback(error);
// Boo, there was an error.
let twiml = new Twilio.twiml.VoiceResponse();
twiml.say({
voice: 'alice'
}, 'Oops, something went wrong. The event has not been sent. You will need to send this event directly from x matters.');
});
}
};