-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMS-Macro.js
330 lines (275 loc) · 9.8 KB
/
SMS-Macro.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
import xapi from 'xapi';
// Add your own PMR information here
const PMR = '##########';
// Specify the default duration in hours
const DEFAULT_DURATION = '1';
// Customise your SMS message
const MESSAGE = 'Please join my meeting at: ';
// Add your own imiconnect Webhook URL
const IMI_URL = 'https://hooks-us.imiconnect.io/events/#######';
// Add your imiconnect JWT
const IMI_JWT = '######';
// Choose if the user can enter an alternative PMR
const ALLOW_ALTERNATIVE_PMR = true;
///////////////////////////////////
// Do not change anything below
///////////////////////////////////
// This is the guest link generator URL
const GUEST_URL = 'https://wxsd.wbx.ninja/wxsd-guest-demo/create_url';
// Temporary values for alternative values
let tempPMR = '';
let tempNumber = '';
let callState = '';
// Save the serial number for logging
let serialNumber = '';
xapi.Status.SystemUnit.Hardware.Module.SerialNumber
.get()
.then(value => {serialNumber = value;});
// Enable the HTTP client if it isn't already
xapi.Config.HttpClient.Mode.get().then(value => {
console.log('HTTP Client is : ' + value);
if(value == 'Off'){
console.log('Enabling HTTP Client');
xapi.Config.HttpClient.Mode.set('On');
}
});
// Add the Button to the touch panel
xapi.command('UserInterface Extensions Panel Save', {
PanelId: 'sms_invite'
}, `<Extensions>
<Version>1.8</Version>
<Panel>
<Order>1</Order>
<Type>Statusbar</Type>
<Icon>Input</Icon>
<Color>#A866FF</Color>
<Name>SMS Invite</Name>
<ActivityType>Custom</ActivityType>
</Panel>
</Extensions>`);
// This function requests the guest link
function getGuestLink(number, pmr){
console.log('Number: ' + number + ' Link: ' + pmr);
let data = {
expire_hours: DEFAULT_DURATION
, sip_target: pmr
, serial_number: serialNumber
};
console.log(data);
xapi.command('HttpClient Post', {
Header: ["Content-Type: application/json"],
Url: GUEST_URL,
ResultBody: 'plaintext'
},
JSON.stringify(data))
.then((result) => {
//console.log(result.Body);
var body = JSON.parse(result.Body)
//console.log(body.urls.Guest[0]);
sendInvite(number, body.urls.Guest[0]);
})
.catch((err) => {
console.log("Failed: " + JSON.stringify(err));
console.log(err);
// Should close panel and notifiy errors
xapi.Command.UserInterface.Message.Alert.Display
({ Duration: 3
, Text: 'Could not generate meeting link'
, Title: 'Failure'});
});
}
// This function prepares the invite message and sends it to the
// imiconnect SMS service to the target mobile number
function sendInvite(number, link){
console.log('Sending Invite');
console.log('Number received: ' + number);
console.log('Link received: ' + link);
console.log('Current state of PMR: ' + PMR);
let invite = MESSAGE + link;
console.log(invite);
// Prepare the content to be sent to IMIConnect
var messageContent = {
number: number
, message: invite
};
xapi.command('HttpClient Post', {
Header: ["Content-Type: application/json",
"Authorization: Bearer "+ IMI_JWT],
Url: IMI_URL,
},
JSON.stringify(messageContent))
.then((result) => {
console.log("success: " + result.StatusCode)
xapi.command("UserInterface Message Prompt Display", {
Title: "Invite sent successfully"
, Text: 'Would you like to automatically join: ' + ((tempPMR != '') ? tempPMR : PMR)
, FeedbackId: 'join_meeting'
, 'Option.1': 'Yes, join meeting'
, 'Option.2':'No, I will join later'
}).catch((error) => { console.error(error); });
})
.catch((err) => {
console.log("failed: " + err.message)
console.log("success: " + result.StatusCode)
xapi.Command.UserInterface.Message.Alert.Display
({ Duration: 3
, Text: 'Invite was not sent'
, Title: 'Failure'});
});
}
// Check if we are in a call
async function checkAnswerState(){
try{
callState = await xapi.Status.Call.AnswerState.get();
} catch(e){
callState = "NoCall";
}
// If we are in an answered state then we have an active call
// We can get the current SIP URI of the call and use that
// For the guest link generation
if(callState == 'Answered'){
let callback = await xapi.Status.Call.CallbackNumber.get();
let answerURI = callback.split(":");
console.log('Currently in a call with ' + answerURI[1]);
tempPMR = answerURI[1];
xapi.command("UserInterface Message Prompt Display", {
Title: "SMS Invite"
, Text: 'Please enter the number you wish to invite to: ' +tempPMR
, FeedbackId: 'create_invite'
, 'Option.1':'Tap to enter number'
}).catch((error) => { console.error(error); });
}else{
console.log('Not in a call');
let cmdObject = {
Title: "SMS Invite"
, Text: 'Please enter the number you wish to invite'
, FeedbackId: 'create_invite'
, 'Option.1':'Tap to enter number'
}
let uiMessage = "UserInterface Message Prompt Display";
if(ALLOW_ALTERNATIVE_PMR){
cmdObject['Option.2'] = 'Tap to change invite from: '+PMR;
xapi.command(uiMessage, cmdObject).catch((error) => { console.error(error); });
}else {
xapi.command(uiMessage, cmdObject).catch((error) => { console.error(error); });
}
}
}
// Listen for the SMS_Invite panel and display initial prompt
xapi.event.on('UserInterface Extensions Panel Clicked', (event) => {
if(event.PanelId == 'sms_invite'){
console.log('SMS_Invite Selected')
tempNumber = '';
tempPMR = '';
// Creating the default panel
checkAnswerState();
}
});
// Handle all the SMS Invite preparation screens
xapi.event.on('UserInterface Message TextInput Response', (event) => {
switch(event.FeedbackId){
case 'enter_number':
tempNumber = event.Text;
console.log('Number Entered: ' + tempNumber)
if(ALLOW_ALTERNATIVE_PMR){
xapi.command("UserInterface Message Prompt Display", {
Title: "SMS Invite"
, Text: 'Please enter the number you wish to invite'
, FeedbackId: 'create_invite'
, 'Option.1': 'Tap to change number: ' +tempNumber
, 'Option.2': 'Tap to change invite from: '+ ((tempPMR != '') ? tempPMR : PMR)
, 'Option.3': 'Send Invite'
}).catch((error) => { console.error(error); });
}else{
xapi.command("UserInterface Message Prompt Display", {
Title: "SMS Invite"
, Text: 'Please enter the number you wish to invite'
, FeedbackId: 'create_invite_no_alt'
, 'Option.1': 'Tap to change number: ' +tempNumber
, 'Option.2': 'Send Invite'
}).catch((error) => { console.error(error); });
}
break;
case 'enter_pmr':
tempPMR = event.Text;
console.log('PMR Entered: ' + tempPMR)
console.log('Temp Number: ' +tempNumber);
let uiMessage = "UserInterface Message Prompt Display";
let cmdObject = {
Title: "SMS Invite"
, Text: 'Please enter the number you wish to invite'
, FeedbackId: 'create_invite'
, 'Option.1': 'Tap to enter number'
, 'Option.2':'Tap to change invite from: '+ tempPMR
};
if(tempNumber === ''){
xapi.command(uiMessage, cmdObject).catch((error) => { console.error(error); });
} else {
cmdObject['Option.3'] = 'Send Invite';
xapi.command(uiMessage, cmdObject).catch((error) => { console.error(error); });
}
break;
}
});
// Handle all the Text Inputs
xapi.event.on('UserInterface Message Prompt Response', (event) => {
console.log('FeedbackId: ' + event.FeedbackId + ' Option: '+ event.OptionId);
switch(event.FeedbackId){
case 'create_invite':
switch(event.OptionId){
case '1': // This choice handles a new number input
xapi.command('UserInterface Message TextInput Display', {
FeedbackId: 'enter_number',
Text: 'Please enter the mobile number to invite',
InputType: 'Numeric',
Placeholder: ' ',
Duration: 0,
}).catch((error) => { console.error(error); });
break;
case '2': // This choice handles a new PMR input
xapi.command('UserInterface Message TextInput Display', {
FeedbackId: 'enter_pmr',
Text: 'Enter alternative PMR',
InputType: 'SingleLine',
Placeholder: PMR,
Duration: 0,
}).catch((error) => { console.error(error); });
break;
case '3': // This choice sends the meeting invitation
console.log('PMR Before doing anything: ' + PMR);
getGuestLink(tempNumber, ((tempPMR != '') ? tempPMR : PMR));
break;
}
break;
case 'join_meeting':
switch(event.OptionId){
case '1':
console.log('PMR:' + PMR);
console.log('Dialling the PMR: ' + ((tempPMR != '') ? tempPMR : PMR));
xapi.command('Dial',{
Number: ((tempPMR != '') ? tempPMR : PMR)
})
break;
case '2':
console.log('Not dialling PMR');
break;
}
break;
case 'create_invite_no_alt':
switch(event.OptionId){
case '1':
xapi.command('UserInterface Message TextInput Display', {
FeedbackId: 'enter_number',
Text: 'Please enter the mobile number to invite',
InputType: 'Numeric',
Placeholder: ' ',
Duration: 0,
}).catch((error) => { console.error(error); });
break;
case '2':
console.log('PMR Before doing anything: ' + PMR);
getGuestLink(tempNumber, ((tempPMR != '') ? tempPMR : PMR));
break;
}
}
});