-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.gs
247 lines (215 loc) · 8.09 KB
/
Code.gs
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
var BASE_DOMAIN = "meet.jit.si";
var MORE_NUMBERS_LINK = "https://" + BASE_DOMAIN + "/static/dialInInfo.html?room=";
var PHONE_NUMBERS_LIST_LINK = "https://api.jitsi.net/phoneNumberList?calendar=true";
var CONF_MAPPER_LINK = "https://api.jitsi.net/conferenceMapper?conference=";
var MUC_HOST = "@conference." + BASE_DOMAIN;
var CONFERANCE_DISPLAY_NAME = "\"<put here your conferance display name>\""
/*
* Authentication info
*/
var APP_ID = "<your app id>";
var APP_TOKEN = "<your app token>";
/**
* Creates a conference, then builds and returns a ConferenceData object
* with the corresponding conference information. This method is called
* when a user selects a conference solution defined by the add-on that
* uses this function as its 'onCreateFunction' in the add-on manifest.
*
* @param {Object} arg The default argument passed to a 'onCreateFunction';
* it carries information about the Google Calendar event.
* @return {ConferenceData}
*/
function createConference(arg) {
const eventData = arg.eventData;
const calendarId = eventData.calendarId;
const eventId = eventData.eventId;
// Retrieve the Calendar event information using the Calendar
// Advanced service.
var calendarEvent;
try {
calendarEvent = Calendar.Events.get(calendarId, eventId);
} catch (err) {
// The calendar event does not exist just yet; just proceed with the
// given event ID and allow the event details to sync later.
console.log(err);
calendarEvent = {
id: eventId,
};
}
// Create a conference on the third-party service and return the
// conference data or errors in a custom JSON object.
var conferenceInfo = create3rdPartyConference(calendarEvent);
// Build and return a ConferenceData object, either with conference or
// error information.
var dataBuilder = ConferenceDataService.newConferenceDataBuilder();
if (!conferenceInfo.error) {
// No error, so build the ConferenceData object from the
// returned conference info.
var conferenceTypeParameter = ConferenceDataService.newConferenceParameter()
.setKey('conferenceSolutionType')
.setValue('jitsi');
dataBuilder.setConferenceId(conferenceInfo.id)
.addConferenceParameter(conferenceTypeParameter)
conferenceInfo.numbers.forEach(function(number) {
var phoneEntryPoint = ConferenceDataService.newEntryPoint()
.setEntryPointType(ConferenceDataService.EntryPointType.PHONE)
.setUri('tel:' + number)
.setPin(conferenceInfo.phonePin);
dataBuilder.addEntryPoint(phoneEntryPoint);
});
var moreEntryPoint = ConferenceDataService.newEntryPoint()
.setEntryPointType(ConferenceDataService.EntryPointType.MORE)
.setUri(conferenceInfo.moreLink);
dataBuilder.addEntryPoint(moreEntryPoint);
if (conferenceInfo.videoUri) {
var videoEntryPoint = ConferenceDataService.newEntryPoint()
.setEntryPointType(ConferenceDataService.EntryPointType.VIDEO)
.setUri(conferenceInfo.videoUri);
dataBuilder.addEntryPoint(videoEntryPoint);
}
} else {
// Other error type;
var error = ConferenceDataService.newConferenceError()
.setConferenceErrorType(
ConferenceDataService.ConferenceErrorType.TEMPORARY);
dataBuilder.setError(error);
}
// Don't forget to build the ConferenceData object.
return dataBuilder.build();
}
function base64Encode(text, isJson)
{
const data = isJson ? JSON.stringify(text) : text;
return Utilities.base64EncodeWebSafe(data).replace(/=+$/, '');
};
function createJwt(privateKey, payload) {
// Sign token using HMAC with SHA-256 algorithm
const header = {
alg: 'HS256',
typ: 'JWT'
};
const toSign = base64Encode(header, true) + "." + base64Encode(payload, true);
const signatureBytes = Utilities.computeHmacSha256Signature(toSign, privateKey, Utilities.Charset.US_ASCII);
const signature = base64Encode(signatureBytes, false);
return toSign + "." + signature;
};
/**
* Contact the third-party conferencing system to create a conference there,
* using the provided calendar event information. Collects and returns the
* conference data returned by the third-party system in a custom JSON object
* with the following fields:
*
* data.adminEmail - the conference administrator's email
* data.conferenceLegalNotice - the conference legal notice text
* data.error - Only present if there was an error during
* conference creation. Equal to 'AUTH' if the add-on user needs to
* authorize on the third-party system.
* data.id - the conference ID
* data.phoneNumber - the conference phone entry point phone number
* data.phonePin - the conference phone entry point PIN
* data.videoPasscode - the conference video entry point passcode
* data.videoUri - the conference video entry point URI
*
* The above fields are specific to this example; which conference information
* you add-on needs is dependent on the third-party conferencing system
* requirements.
*
* @param {Object} calendarEvent A Calendar Event resource object returned by
* the Google Calendar API.
* @return {Object}
*/
function create3rdPartyConference(calendarEvent) {
var data = {};
var jsonobj = {};
var roomName = Utilities.getUuid().toString();
const now = Date.now();
const expires = new Date(now);
expires.setMonth(expires.getMonth() + 12);
var claims = {
'aud' : APP_ID,
'iss' : APP_ID,
'sub' : BASE_DOMAIN,
'exp' : Math.floor(expires.getTime() / 1000),
'context' : {
"user": {
"avatar":"",
"name":"",
"email":"",
"id":""},
"group":""
},
'room' : roomName
}
var jwt = createJwt(APP_TOKEN, claims);
data.id = BASE_DOMAIN +"/" + roomName;
data.videoUri = "https://" + BASE_DOMAIN +"/" + roomName + "?jwt=" + jwt.toString() + "#config.callDisplayName=" + encodeURI(CONFERANCE_DISPLAY_NAME);
data.moreLink = MORE_NUMBERS_LINK + roomName;
var responseMapper = UrlFetchApp.fetch(CONF_MAPPER_LINK + roomName + MUC_HOST);
var jsonobjMapper = JSON.parse(responseMapper.getContentText());
data.phonePin = jsonobjMapper.id;
data.numbers = [];
var keys = [];
for (var key in jsonobj.numbers) {
if (jsonobj.numbers.hasOwnProperty(key)) {
data.numbers.push.apply(data.numbers, jsonobj.numbers[key]);
}
}
return data;
}
/*
* Returns true if the string 's' contains one of the
* template strings.
*/
function hasTemplate(s, categories) {
for (var template in categories){
if (s.indexOf(template) >= 0){
return true;
}
}
}
/**
* Generates random int within the range [min, max]
* @param min the minimum value for the generated number
* @param max the maximum value for the generated number
* @returns random int number
*/
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* Get random element from array or string.
* @param {Array|string} arr source
* @returns array element or string character
*/
function randomElement(arr) {
return arr[randomInt(0, arr.length -1)];
}
var PATTERNS = [
"_ADJECTIVE__PLURALNOUN__VERB__ADVERB_"
];
/**
* Generates new room name.
* @param customDictionary a dictionary containing keys pluralNouns, verbs,
* adverbs and adjectives, values are array of strings.
*/
function generateRoomWithoutSeparator(customDictionary) {
// Note that if more than one pattern is available, the choice of
// 'name' won't have a uniform distribution amongst all patterns (names
// from patterns with fewer options will have higher probability of
// being chosen that names from patterns with more options).
var name = randomElement(PATTERNS);
var word;
var categories = {
"_PLURALNOUN_": customDictionary.pluralNouns,
"_VERB_": customDictionary.verbs,
"_ADVERB_": customDictionary.adverbs,
"_ADJECTIVE_": customDictionary.adjectives
};
while (hasTemplate(name, categories)) {
for (var template in categories) {
word = randomElement(categories[template]);
name = name.replace(template, word);
}
}
return name;
}