-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
191 lines (164 loc) · 5.18 KB
/
background.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
import { App } from 'https://cdn.jsdelivr.net/npm/@wazo/[email protected]/lib/esm/app.js';
import 'https://cdn.jsdelivr.net/npm/@wazo/[email protected]/dist/wazo-sdk.min.js'
let session;
let url;
let endpoints = [];
let answer_call_id;
const app = new App();
const getParkingList = async () => {
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': session.token
}
};
return fetch(`https://${url}/api/confd/1.1/parkinglots`, options).then(response => response.json());
};
const getParkingCallList = async (parkingLot) => {
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': session.token
}
};
return fetch(`https://${url}/api/calld/1.0/parkinglots/${parkingLot}`, options).then(response => response.json());
};
const getCallsList = async () => {
const options = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': session.token
}
};
return fetch(`https://${url}/api/calld/1.0/users/me/calls`, options).then(response => response.json());
};
const parkCall = async (parkingLot, callId, callbackChannel, parkTimeout) => {
const payload = {
call_id: callId,
parking_id: parkingLot,
callback_channel: callbackChannel,
timeout: parkTimeout,
prefered_slot: null
};
const options = {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'X-Auth-Token': session.token
},
body: JSON.stringify(payload)
};
return fetch(`https://${url}/api/calld/1.0/users/me/calls/${callId}/park`, options).then(response => response.json());
};
// Need to be fixed on EUC SDK
Wazo.Websocket.ws.on('onmessage', payload => {
const msg = JSON.parse(payload).data;
switch(msg.name) {
case "call_parked":
case "call_unparked":
case "parked_call_hungup":
case "parked_call_timed_out":
case "call_answered":
// FIXME: We need to improve the information in call object
answer_call_id = msg.data.call_id;
case "call_updated":
// FIXME: We need to improve the information in call object
answer_call_id = msg.data.call_id;
case "call_ended":
app.sendMessageToIframe(msg);
break;
}
});
// FIXME: We need to improve the information in call object
app.onCallAnswered = (call) => {
addParkingButtonOnCall(call);
};
// FIXME: We need to improve the information in call object
app.onCallMade = (call) => {
addParkingButtonOnCall(call);
};
// FIXME: We need to add method on our SDK to help people to create button or other widget to control a call
// FIXME: Remove hardcoded information and also find a way to know what is the callback channel we want to use.
const addParkingButtonOnCall = (call) => {
const div = document.createElement("div");
div.className = 'div-btn-parking';
const button = document.createElement("button");
button.textContent = 'P';
button.className = 'btn-parking';
button.addEventListener('click', async () => {
const call_id = answer_call_id;
const parkTimeout = 45;
const parkingLot = "1"; // FIXME: We need to find a way to know what is the parking lot we want to use.
const callback_channel = endpoints[1].name;
await parkCall(parkingLot, call_id, callback_channel, parkTimeout);
app.displayNotification("Parking action", "Call has been parked successfully!");
});
div.appendChild(button);
document.querySelector('div.videoIsMinimized').appendChild(div);
};
const addStyle = (() => {
const style = document.createElement('style');
document.head.append(style);
return (styleString) => style.textContent = styleString;
})();
app.onBackgroundMessage = async (msg) => {
switch(msg.name) {
case "getParkingList":
msg = await getParkingList();
app.sendMessageToIframe({name: "getParkingList", data: msg});
break;
case "getCallsList":
msg = await getCallsList();
app.sendMessageToIframe({name: "getCallsList", data: msg});
break;
case "getParkingCallList":
msg = await getParkingCallList(msg.value);
app.sendMessageToIframe({name: "getParkingCallList", data: msg});
break;
case "parkCall":
await parkCall(...msg.value);
break;
};
};
const populateEndpoints = (lines) => {
for (let i = 0; i < lines.length; i++) {
let value = lines[i];
if (value.endpointSip) {
endpoints.push({id: i+1, name: `PJSIP/${value.endpointSip.name}`});
};
};
};
(async () => {
await app.initialize();
const context = app.getContext();
session = context.user;
url = context.user.host;
app.displayBanner({
url: `${context.app.extra.baseUrl}/banner.html`,
height: "50px",
width: "100px",
hideCloseButton: false
});
populateEndpoints(session.profile.lines);
addStyle(`
.btn-parking {
border: none;
color: white;
text-decoration: none;
display: inline-block;
font-size: 25px;
margin-left: -465px;
width: 40px;
height: 40px;
background-color: #4CAF50;
border-radius: 50%;
text-align: center;
line-height: 40px;
cursor: pointer;
}
`);
console.log('parking background - background launched');
})();