-
Notifications
You must be signed in to change notification settings - Fork 0
/
RingCentralQueue.php
360 lines (264 loc) · 10.6 KB
/
RingCentralQueue.php
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
<html lang=en-AU>
<head>
<title>Call Queues</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<meta name="author" content="Robert Kosmac">
<script src="assets/js/jquery.min.js"></script>
<script type="text/javascript">
/** If checkbox unchecked, reload the page every 30 seconds */
window.setTimeout( function() {
if(!document.getElementById('ReloadEnable').checked){
window.location.reload();
}
}, 60000);
</script>
<style>
html{
width: calc(100% - 2px);
height: fit-content;
font-family: "Gill Sans", sans-serif;
}
table{
width: 100%;
font-size: 14pt;
background-color: rgba(150,150,150,0.8);
border: 1px solid rgba(150,150,150,0.8);
border-radius: 10px;
padding: 0.2em;
}
table thead{
font-weight: bold;
background-color: rgba(150,150,150,0.8);
}
table tbody{
font-weight: 100;
border-radius: 5px;
}
table th,table td{
padding: 1em 2em;
text-align: center;
vertical-align: middle;
}
td.non_status{
background-color: rgba(200,230,175,0.8); /** light grass green as the non-function colour */
}
td.non_statusgrey{
background-color: rgba(170,170,170,0.8); /** just a grey */
}
td.count_low{
background-color: rgba(145,190,230,0.8); /** blue, because it's soothing colour */
}
td.count_medium{
background-color: rgba(230,170,115,0.8); /** light warm colour, because it's concerning */
}
td.count_high{
background-color: rgba(230,60,60,0.8); /** Flashing bright warning */
animation: blinker 5s linear infinite;
}
@keyframes blinker {
50% {
background-color: rgba(230,60,60,0.3);
}
}
</style>
</head>
<body>
<?php
/**************************
*
* RING CENTRAL Processing
*
* ************************/
include 'processing/RingCentral_Setup.php';
$RingCnt_Config = read_RingCentralConfig();
$ringcentral_clientId = $RingCnt_Config['OAuth']['OAuth_ClientID']; // RingCentral client ID
$ringcentral_clientSecret = $RingCnt_Config['OAuth']['OAuth_ClientSecret']; // RingCentral client secret
$ringcentral_jwt = $RingCnt_Config['OAuth']['OAuth_jwt']; // Pre-generated JWT from RingCentral
/** Authenticate with RingCentral, will get an updated Token for next actions */
function getRingAccessToken($clientId, $clientSecret, $jwt) {
$url = 'https://platform.ringcentral.com/restapi/oauth/token';
$headers = [
'Authorization: Basic ' . base64_encode($clientId . ':' . $clientSecret),
'Content-Type: application/x-www-form-urlencoded'
];
$data = [
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'assertion' => $jwt
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
die('Error fetching access token: ' . curl_error($ch));
}
curl_close($ch);
$result = json_decode($response, true);
return $result['access_token'];
}
/** Get the queue data */
function getRingCallQueueDetails($accessToken) {
$url = 'https://platform.ringcentral.com/restapi/v1.0/account/~/call-queues';
$headers = [
'Authorization: Bearer ' . $accessToken
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
die('Error fetching call queue data: ' . curl_error($ch));
}
curl_close($ch);
$result = json_decode($response, true);
return $result['records'];
}
function getRingCallQueueCalls($accessToken, $queueId) {
$url = "https://platform.ringcentral.com/restapi/v1.0/account/~/extension/$queueId/active-calls"; // Example endpoint
$headers = [
'Authorization: Bearer ' . $accessToken
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
die('Error fetching call queue active calls: ' . curl_error($ch));
}
curl_close($ch);
$result = json_decode($response, true);
return $result['records'];
}
function getRingQueueAnalytics($accessToken, $queueId) {
$url = "https://analytics.ringcentral.com/platform/v1/queues/$queueId/summary"; // Example endpoint, check the actual API documentation
$headers = [
'Authorization: Bearer ' . $accessToken
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
die('Error fetching queue analytics: ' . curl_error($ch));
}
curl_close($ch);
$result = json_decode($response, true);
return $result;
}
function getRingTotalCallsToday($accessToken, $queueId) {
$url = 'https://platform.ringcentral.com/restapi/v1.0/account/~/extension/' . $queueId . '/call-log';
$headers = [
'Authorization: Bearer ' . $accessToken,
'Content-Type: application/json'
];
// Get today's date in ISO 8601 format
$today = date('Y-m-d') . 'T00:00:00.000Z';
$queryParams = http_build_query([
'dateFrom' => $today,
'dateTo' => date('Y-m-d\TH:i:s.000\Z'), // Current time
'type' => 'Voice'
]);
$ch = curl_init("$url?$queryParams");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_errno($ch)) {
die('Error fetching call log data: ' . curl_error($ch));
}
curl_close($ch);
$result = json_decode($response, true);
return count($result['records']);
}
/** Output the active call count for each queue */
function displayRingCallQueueDetails($queues, $accessToken) {
$display_waitlimit_seconds = 120; // 2 minute wait time
$callqueue_highlimit = 20; // 20 calls in queue
$output_html = "<table>
<thead>
<tr>
<th></th>
<th>Waiting</th>
<th>Active</th>
<th>Avg Wait</th>
<th>Total</th>
</tr>
</thead>
<tbody>"; //start table
/** Call Queue Count */
if (count($queues) === 0) {
$output_html.= "<tr><td colspan=\"5\" class=\"non_statusgrey\">No active queues</td></tr>";
} else {
foreach ($queues as $queue) {
$queueId = $queue['id'];
$queueName = $queue['name'];
$queueStatus = $queue['status'];
// Fetch call details for the queue
$calls = getRingCallQueueCalls($accessToken, $queueId);
$activeCallCount = count($calls);
$connectedCallCount = 0;
foreach ($calls as $call) {
if ($call['status'] === 'Connected') {
$connectedCallCount++;
}
}
// Fetch analytics data for the queue
$analytics = getRingQueueAnalytics($accessToken, $queue['id']);
$averageWaitTime = $analytics['averageWaitTime'] ?? 'N/A';
$totalCallsToday = getRingTotalCallsToday($accessToken, $queueId);
$active_call_class = "non_statusgrey";
$average_wait_class = "non_statusgrey";
if($activeCallCount <= ($display_waitlimit_seconds * 0.25)){
$active_call_class = "count_low";
}else if($activeCallCount > ($display_waitlimit_seconds * 0.25) && $activeCallCount <= ($display_waitlimit_seconds * 0.75)){
$active_call_class = "count_medium";
}else if($activeCallCount > ($display_waitlimit_seconds * 0.75)){
$active_call_class = "count_high";
}
if($averageWaitTime <= ($display_waitlimit_seconds * 0.25)){
$average_wait_class = "count_low";
}else if($averageWaitTime > ($display_waitlimit_seconds * 0.25) && $averageWaitTime <= ($display_waitlimit_seconds * 0.75)){
$average_wait_class = "count_medium";
}else if($averageWaitTime > ($display_waitlimit_seconds * 0.75)){
$average_wait_class = "count_high";
}
$output_html .= "<tr><td class=\"non_statusgrey\">" . $queueName ."[" . $queueStatus . "]</td>";
$output_html .= "<td class=\"".$active_call_class."\">" . $activeCallCount . "</td>";
$output_html .= "<td class=\"non_status\">" . $connectedCallCount . "</td>";
$output_html .= "<td class=\"".$average_wait_class."\">" . $averageWaitTime . " seconds</td>";
$output_html .= "<td class=\"non_status\">" . $totalCallsToday . "</td>";
}
}
$output_html .= "</tbody>
</table>
<br /><br />";
print $output_html;
print "Last refresh: ".date("h:i:sa, j/n/Y");
}
/** Call above scripts */
$ring_accessToken = getRingAccessToken($ringcentral_clientId, $ringcentral_clientSecret, $ringcentral_jwt);
$ringcentral_queues = getRingCallQueueDetails($ring_accessToken);
displayRingCallQueueDetails($ringcentral_queues, $ring_accessToken);
/**************************
*
* ZOHO ONE Processing
*
*
*
* Setup:
* 1. Head to https://api-console.zoho.com.au/ and start setup of new connection
* 2. Choose type "Self Client"
* 3. Copy-Paste the Secret and Client ID into the below variables
* 4. Set the SCOPE of "Desk.tickets.ALL", and input the scope description (not sure if this matters)
* 5. Select the Portal and an authorised Desk platform
* 6. When you select Generate, it will generate the REFRESH token - default is 3 minutes.
*
* ************************/
?>
<br />
<input type="checkbox" id="ReloadEnable" name="ReloadEnable" value="Enable Reload">
<label for="ReloadEnable"> Pause 60 second reload?</label><br>
</body>
</html>