-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.html
217 lines (180 loc) · 6.24 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
210
211
212
213
214
215
216
217
<!-- CC-BY-SA (c) Joan Balague -->
<html>
<head>
<meta charset="UTF-8">
<title>My Server</title>
<style>
body {
font-family: sans-serif;
font-weight: 300;
font-size: 1em;
}
h3 {
font-weight: bold;
}
div {
margin-top: 1.5em;
}
input,
textarea {
/* To make sure that all text fields have the same font settings
By default, textareas have a monospace font */
font: 1em sans-serif;
font-weight: 300;
/* To give the same size to all text fields */
width: 100%;
border: 0px;
margin-top: 0.5em;
}
textarea {
vertical-align: top;
}
button {
width: 100%;
padding: 10px;
border-radius: 0px;
background-color: orange;
font-size: 1em;
}
/* Class selectors */
.frame {
/* Just to center the form on the page */
margin: 0 auto;
width: 600px;
padding: 1em;
background-color:lightgoldenrodyellow;
}
.resetButton {
padding-top: 1em;
display: none;
}
</style>
</head>
<body>
<div class="frame">
<h3>Add one or more persons to a room by email address</h3>
<div>
First we need a valid Cisco Spark API token. To get yours please go to <a target="_blank" href="https://developer.ciscospark.com">developer.ciscospark.com</a>, log in with your credentials and click on your avatar.
<input type="text" id="token" placeholder="Paste your API token here">
</div>
<div>
Then we will fetch the room ID for the room name you specify.
<input type="text" id="roomName" placeholder="Enter room name here" onfocus="clearRoomId()" onchange="getRoomId()">
<input type="text" id="roomId" placeholder="Room ID will be shown here" readonly>
</div>
<div>
Finally enter the list of members to add to the room.
<textarea id="members" rows="3" placeholder="Comma separated email list"></textarea>
</div>
<div>
<button id="buttonAddMembers" disabled onclick="addMembersToRoom()">Add members to the room</button>
<textarea id="log" rows="3" hidden readonly></textarea>
</div>
<div class="resetButton" id="buttonResetDiv">
<button id="buttonReset" onclick="clearAll()">Reset</button>
</div>
</div>
</body>
<script>
document.getElementById("token").focus();
function clearRoomId() {
document.getElementById("roomId").value = "";
}
function clearAll() {
document.getElementById("token").value = "";
document.getElementById("token").focus();
document.getElementById("roomName").value = "";
document.getElementById("roomId").value = "";
document.getElementById("members").value = "";
document.getElementById("buttonAddMembers").disabled = true;
document.getElementById("log").value = "";
document.getElementById("log").rows = "2";
document.getElementById("log").hidden = true;
document.getElementById("buttonResetDiv").style.display = "none";
}
function getRoomId() {
const apiToken = document.getElementById("token").value;
const roomName = document.getElementById("roomName").value;
if (apiToken !== "" && roomName !== "") {
const headers = new Headers();
headers.append("Content-Type", "application/json; charset=utf-8");
headers.append("Authorization", "Bearer " + apiToken);
const options = {
method: "GET",
headers: headers,
mode: "cors",
body: {
type: "group"
}
};
fetch("https://api.ciscospark.com/v1/rooms", options)
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("network response was not ok: " + response.status);
}
})
.then(roomList => {
const rooms = roomList.items.filter(room => room.title === roomName);
if (rooms.length < 1) {
throw new Error("no room named '" + roomName + "' was found");
}
if (rooms.length > 1) {
throw new Error("more than one room named '" + roomName + "' were found");
}
document.getElementById("roomId").value = rooms[0].id;
document.getElementById("members").focus();
document.getElementById("buttonAddMembers").disabled = false;
})
.catch(error => {
alert("Something went wrong retrieving the room ID: " + error);
});
}
}
function addMembersToRoom() {
const apiToken = document.getElementById("token").value;
const roomName = document.getElementById("roomName").value;
const roomId = document.getElementById("roomId").value;
const members = document.getElementById("members").value;
if (roomId !== "" && members !== "") {
const headers = new Headers();
headers.append("Content-Type", "application/json; charset=utf-8");
headers.append("Authorization", "Bearer " + apiToken);
document.getElementById("log").rows = members.split(",").length + 1;
document.getElementById("log").hidden = false;
for (const member of members.split(",")) {
const options = {
method: "POST",
headers: headers,
body: JSON.stringify({
roomId: roomId,
personEmail: member.trim()
})
};
fetch("https://api.ciscospark.com/v1/memberships", options)
.then(response => {
if (response.ok) {
return response.json();
} else {
throw new Error("network response was not OK (" + response.status + ")");
}
})
.then(membership => {
let log = document.getElementById("log").value;
document.getElementById("log").value = log + "✅ " + membership.personDisplayName + "(" + membership.personEmail +
")\n"
})
.catch(error => {
let log = document.getElementById("log").value;
document.getElementById("log").value = log + "🆘 " + "(" + member.trim() + ") " + error + "\n"
});
}
document.getElementById("buttonResetDiv").style.display = "block";
document.getElementById("buttonReset").focus();
} else {
document.getElementById("members").focus();
}
}
</script>
</html>