-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
209 lines (180 loc) · 7.06 KB
/
index.html
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
<html>
<head>
<title>webRTC Test</title>
<script src='https://sdk-cdn.mypurecloud.com/javascript/27.0.0/purecloud-platform-client-v2.min.js'></script>
<script src='https://sdk-cdn.mypurecloud.com/webrtc-sdk/v6/genesys-cloud-webrtc-sdk.bundle.min.js'></script>
</head>
<body>
<button id='register-button' type='button'>Register Me</button>
<br><br>
<input id="destNum" value="">
<button id='makecall-button' type='button'>MakeCall</button>
<br><br>
<input id="conversationid" style="width:300px">
<button id='disconnect-button' type='button'>Disconnect</button>
</body>
</html>
<script>
document.getElementById('makecall-button').addEventListener('click', makeCall);
document.getElementById('register-button').addEventListener('click', register);
document.getElementById('disconnect-button').addEventListener('click', disconnect);
// This needs to be a global since it is initialized by platformClientInit.js and utilized in app-controller.js
window.getCurrentUrlParams = () => {
let params = null;
const urlParts = window.location.href.split('#');
if (urlParts[1]) {
const urlParamsArr = urlParts[1].split('&');
if (urlParamsArr.length) {
params = {};
for (let i = 0; i < urlParamsArr.length; i++) {
const currParam = urlParamsArr[i].split('=');
const key = currParam[0];
const value = currParam[1];
params[key] = value;
}
}
}
return params;
}
// Genesys Cloud OAuth Info
// const clientId = 'ea9c71c9-79f5-4e4d-bb5f-0d291fc69a76';
// const redirectUri = 'https://gc-test.ga/index.html';
// let environment = 'apne2.pure.cloud';
const clientId = '';
const redirectUri = '';
let environment = 'apne2.pure.cloud';
// Set Genesys Cloud objects
const platformClient = require('platformClient');
const client = platformClient.ApiClient.instance;
const conversationsApi = new platformClient.ConversationsApi();
const notificationApi = new platformClient.NotificationsApi();
const userApi = new platformClient.UsersApi();
let usersMeId;
let topicId;
client.setEnvironment(environment);
client.setReturnExtendedResponses(true);
// Login & Get Users Me Info
document.addEventListener('DOMContentLoaded', function () {
client.loginImplicitGrant(clientId, redirectUri)
.then(() => {
console.log("login Succ");
return userApi.getUsersMe();
})
.then(function(getMeResult) {
console.log("GetUsersMe Succ");
console.log(getMeResult);
usersMeId = getMeResult.body.id;
});
});
// Check if there is auth info on the URL from a redirect
const urlParams = window.getCurrentUrlParams();
if (urlParams && urlParams.access_token) {
console.log(urlParams);
}
// Get Genesys WebRTC SDK
const webrtcSdk = new GenesysCloudWebrtcSdk({
environment: 'apne2.pure.cloud',
accessToken: urlParams.access_token,
});
// Optionally set up some SDK event listeners (not an exhaustive list)
webrtcSdk.on('sdkError', (event) => {
console.log("sdkError");
console.log(event);
});
webrtcSdk.on('pendingSession', (event) => {
console.log("pendingSession");
console.log(event);
});
webrtcSdk.on('sessionStarted', (event) => {
console.log("sessingStarted");
console.log(event);
});
webrtcSdk.initialize().then(() => {
// the web socket has connected and the SDK is ready to use
console.log('sdk.initialize()');
});
// MakeCall button
var websocketClient;
function makeCall() {
console.log('makeCall func')
conversationsApi.postConversationsCalls({
phoneNumber: destNum.value
})
}
// Register Button
function register() {
console.log('register func')
notificationApi.postNotificationsChannels()
.then((data) => {
console.log(`postNotificationsChannels success! data: ${JSON.stringify(data, null, 2)}`);
websocketClient = new WebSocket(data.body.connectUri);
websocketClient.onopen = function () {
console.log("WebSocket Open Succ. url(" + websocketClient.url + ")");
topicId = "v2.users." + usersMeId + ".conversations";
topic = [{ id: topicId }];
notificationApi.postNotificationsChannelSubscriptions(data.body.id, topic)
.then((data) => {
console.log(`postNotificationsChannelSubscriptions Succ. data: ${JSON.stringify(data, null, 2)}`);
})
.catch((err) => {
console.log('There was a failure calling postNotificationsChannelSubscriptions');
console.error(err);
});
websocketClient.onmessage = onMessage;
};
})
.catch((err) => {
console.log('There was a failure calling postNotificationsChannels');
console.error(err);
});
}
// Disconnect Button
function disconnect() {
console.log('disconnect func')
document.getElementById('conversationid')
conversationsApi.postConversationDisconnect(document.getElementById('conversationid').value)
.then((data) => {
console.log(`postConversationDisconnect success! data: ${JSON.stringify(data, null, 2)}`);
})
.catch((err) => {
console.log('There was a failure calling postConversationDisconnect');
console.error(err);
});
}
// WebSocket Func
function onMessage(message) {
console.log("onMessage func");
console.log(message)
try {
// Make sure there is data in the message
// (data should never be empty, but prevent parse error)
if (!message.data) {
console.warn('Message recieved, but there was no data!');
console.log(message);
return;
}
// Parse data into JSON object
var data = JSON.parse(message.data);
// Look for heartbeat
if (data.topicName.toLowerCase() == 'channel.metadata') {
console.log('THUMP thump...');
return;
}
// Conversation Event
if (data.topicName.toLowerCase() == topicId) {
let state = data.eventBody.participants[0].calls[0].state;
console.log(state);
if (state == "contacting") {
document.getElementById('conversationid').value = data.eventBody.id;
} else if (state == "terminated") {
document.getElementById('conversationid').value = "";
}
return;
}
// If we got here, the topic wasn't something we know about
console.warn('Unexpected notification topic: ' + data.topicName);
} catch(error) {
console.error(error);
}
}
</script>