-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
479 lines (413 loc) · 14.2 KB
/
index.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
'use strict'
const { dialogflow, Suggestions } = require('actions-on-google');
const express = require('express');
const bodyParser = require('body-parser');
const CircuitClient = require('./circuitClient');
// Client ID for IMPLICIT app on Circuit. Same ID needs to be defined
// in Account Linking of your project at console.actions.google.com
const CLIENT_ID = 'fd5b604003b94aef84811bb06d03b313';
// Circuit session timeout
const SESSION_TIMEOUT = 5 * 60 * 1000; // 5min session timeout
const sessions = {}; // Active sessions
const app = dialogflow({clientId: CLIENT_ID, scope: 'ALL'});
// Create express app to for handling the /_ah/start request posted
// by AppEngine
const expressApp = express();
expressApp.get('/_ah/start', (req, res) => {
console.log('handle _ah/start');
res.sendStatus(200);
});
// Add dialogFlow as middleware
expressApp.use(bodyParser.json(), app);
// Start server
expressApp.listen(process.env.PORT || 8080, () => console.log(`Server started`));
/**
* Default Welcome Intent
*/
app.intent('Default Welcome Intent', conv => {
// Create a session for this user at the beginning so user
// is logged on to Circuit by the time needed
if (!sessions[conv.user.id]) {
createSession(conv.user);
}
conv.ask(`What can I do for you?`);
conv.ask(new Suggestions('Send a message', 'Make a call'));
});
/**
* message.send
*/
app.intent('send.message', async (conv, {target, message}) => {
const circuit = await getCircuit(conv);
if (!circuit) {
return;
}
target = target || conv.contexts.input['sendmessage_data'].parameters.target;
message = message || conv.contexts.input['sendmessage_data'].parameters.message;
let users = await circuit.searchUsers(target);
let convs = await circuit.searchConversationsByName(target);
if (!users.length && !convs.length) {
conv.ask(`I cannot find any user or conversation called ${target}. What's the name?`);
return;
}
if (users.length + convs.length === 1) {
// One result found. Ask user for confirmation.
const { convId } = users.length && await circuit.getDirectConversationWithUser(users[0].userId, true);
const name = users.length && users[0].displayName || convs[0].topic;
conv.ask(`<speak>Ready to send <break time='0.5s'/>${message}<break time='0.5s'/> to ${name}?</speak>`, new Suggestions('Yes', `No, don't send it`));
conv.contexts.set('sendmessage_send', 5, {
convId: convId || convs[0].convId
});
return;
}
// Multiple matches. Show suggestions of the first few matches.
users = users.slice(0, Math.min(7, users.length));
//convs = convs.slice(0, Math.min(7, convs.length));
const suggestions = users.map(u => u.displayName);
conv.contexts.set('sendmessage_getconv', 5);
conv.ask(`More than one user found with name ${target}. What's the full name?`, new Suggestions(suggestions));
conv.ask(new Suggestions(suggestions));
});
/**
* send.message - collect.target
*/
app.intent('send.message - collect.target', async conv => {
const circuit = await getCircuit(conv);
if (!circuit) {
return;
}
let users = await circuit.searchUsers(conv.parameters.target);
let convs = await circuit.searchConversationsByName(conv.parameters.target);
if (!users.length && !convs.length) {
conv.ask(`I cannot find any user or conversation called ${target}. What's the name?`);
return;
}
const { convId } = users.length && await circuit.getDirectConversationWithUser(users[0].userId, true);
const { message } = conv.contexts.input['sendmessage_data'].parameters;
const name = users.length && users[0].displayName || convs[0].topic;
conv.ask(`<speak>Ready to send <break time='0.5s'/>${message}<break time='0.5s'/> to ${name}?</speak>`, new Suggestions('Yes', `No, don't send it`));
conv.contexts.set('sendmessage_send', 5, {
convId: convId || convs[0].convId
});
});
/**
* send.message - yes
*/
app.intent('send.message - yes', async conv => {
const circuit = await getCircuit(conv);
if (!circuit) {
return;
}
const { message } = conv.contexts.input['sendmessage_data'].parameters;
const { convId } = conv.contexts.input['sendmessage_send'].parameters;
await circuit.addTextItem(convId, message);
conv.contexts.delete('sendmessage_data');
conv.ask('Message sent. Is there anything else I can do for you?');
conv.ask(new Suggestions('No, that\'s all', 'Yes'));
conv.contexts.set('anything_else', 2);
});
/**
* send.message - no
*/
app.intent('send.message - no', async conv => {
conv.contexts.delete('sendmessage_data');
conv.ask('Message not sent. Is there anything else I can do for you?');
conv.ask(new Suggestions('No, that\'s all', 'Yes'));
conv.contexts.set('anything_else', 2);
});
/**
* call.user
*/
app.intent('call.user', async (conv, {target}) => {
const circuit = await getCircuit(conv);
if (!circuit) {
return;
}
let users = await circuit.searchUsers(target);
if (!users.length) {
conv.contexts.set('calluser_getuser', 5);
conv.ask(`I cannot find any user called ${target}. What's the name?`);
return;
}
if (users.length === 1) {
// One result found. Ask user for confirmation.
const name = users.length && users[0].displayName;
conv.ask(`<speak>Ready to call ${name}?</speak>`, new Suggestions('Yes', `No`));
conv.contexts.set('calluser_data', 5, {
email: users[0].emailAddress,
name: name
});
return;
}
// Multiple matches. Show suggestions of the first few matches.
users = users.slice(0, Math.min(7, users.length));
const suggestions = users.map(u => u.displayName);
conv.contexts.set('calluser_getuser', 5);
conv.ask(`More than one user found with name ${target}. What's the full name?`, new Suggestions(suggestions));
conv.ask(new Suggestions(suggestions));
});
/**
* call.user - collect target
*/
app.intent('call.user - collect target', async (conv, {target}) => {
const circuit = await getCircuit(conv);
if (!circuit) {
return;
}
let users = await circuit.searchUsers(target);
if (!users.length) {
conv.ask(`I cannot find any user called ${target}.`);
return;
}
if (users.length === 1) {
// One result found. Ask user for confirmation.
const name = users.length && users[0].displayName;
conv.ask(`<speak>Ready to call ${name}?</speak>`, new Suggestions('Yes', `No`));
conv.contexts.set('calluser_data', 5, {
email: users[0].emailAddress,
name: name
});
return;
}
// Multiple matches. Show suggestions of the first few matches.
users = users.slice(0, Math.min(7, users.length));
const suggestions = users.map(u => u.displayName);
conv.contexts.set('calluser_getuser', 5);
conv.ask(`More than one user or conversation found with name ${target}. What's the full name?`, new Suggestions(suggestions));
conv.ask(new Suggestions(suggestions));
});
/**
* call.user - yes
*/
app.intent('call.user - yes', async conv => {
const circuit = await getCircuit(conv);
if (!circuit) {
return;
}
const device = await findWebClient(circuit);
const { email, name } = conv.contexts.input['calluser_data'].parameters;
try {
await circuit.sendClickToCallRequest(email, null, device && device.clientId, true);
conv.ask(`Ok, calling ${name} on your browser.`);
} catch (err) {
conv.ask(`Looks like you are not logged in to Circuit on your browser on the desktop. Login and try again.`);
}
conv.contexts.delete('calluser_data');
conv.close();
});
app.intent('call.user - no', async conv => {
conv.contexts.delete('calluser_data');
conv.ask('Is there anything else I can do for you?');
conv.ask(new Suggestions('No, that\'s all', 'Yes'));
conv.contexts.set('anything_else', 2);
});
/**
* Common intents
*/
app.intent('anything.else - yes', async conv => {
conv.followup('Welcome');
});
app.intent('anything.else - no', async conv => {
conv.ask('Good Bye');
conv.close();
});
/**
* Exception handler
*/
app.catch((conv, e) => {
console.error(e);
conv.close('Oops. Something went wrong.');
});
/**
* Get the circuit instance from the session. Create a new session if needed
*/
async function getCircuit(conv) {
try {
const session = sessions[conv.user.id] || (await createSession(conv.user));
return session.circuit;
} catch (err) {
conv.ask('No circuit session found. Start over please.');
conv.close();
}
}
/**
* Traverses through the presence types to set to available or dnd
*/
async function traversePresence(conv) {
const circuit = await getCircuit(conv);
if (!circuit) {
return;
}
const { presenceType } = conv.contexts.input['setpresence_data'].parameters;
const presence = presenceType.toLowerCase();
const { untilTime } = conv.contexts.input['setpresence_data'].parameters;
const { duration } = conv.contexts.input['setpresence_data'].parameters;
if (presence === 'available') {
await circuit.setPresenceAvailable();
conv.ask(`Your online presence is set to ${presence}. Anything Else?`);
conv.contexts.set('anything_else', 5);
} else if (presence === 'dnd' || presence === 'do not disturb') {
if (untilTime || duration) {
await circuit.setPresenceDnd(untilTime, duration);
conv.ask(`Your online presence is set to ${presence}. Anything Else?`);
conv.contexts.set('anything_else', 5);
} else {
conv.ask(`How long would you like to be set to DND?`);
conv.contexts.set('setdnd_time', 5);
}
} else {
conv.ask(`I didn't catch the presence. What would you like to be set to?`);
conv.contexts.set('setPresence_getPresence', 5);
}
}
/**
* Set a logged on user's presence
*/
app.intent('set.presence', async conv => {
const circuit = await getCircuit(conv);
if (!circuit) {
return;
}
await traversePresence(conv);
});
/**
* Collects the presenceType of an online user and sets the presence
*/
app.intent('set.presence - collect presenceType', async conv => {
const circuit = await getCircuit(conv);
if (!circuit) {
return;
}
await traversePresence(conv);
});
/**
* Set a logged on user to dnd
*/
app.intent('set.presence - dnd', async conv => {
const circuit = await getCircuit(conv);
if (!circuit) {
return;
}
const { untilTime } = conv.contexts.input['setpresence_data'].parameters;
const { duration } = conv.contexts.input['setpresence_data'].parameters;
await circuit.setPresenceDnd(untilTime, duration);
conv.ask(`Your online presence is set to Do Not Disturb. Anything else?`);
conv.contexts.set('anything_else', 5);
});
/**
* Retrieves the presence of an online user
*/
app.intent('get.presence', async conv => {
const circuit = await getCircuit(conv);
if (!circuit) {
return;
}
const userPresence = await circuit.getUserPresence();
conv.ask(`Your online presence is set to ${userPresence}. Anything else?`);
conv.contexts.set('anything_else', 5);
});
/**
* Retrieves the remaining time left in dnd
*/
app.intent('get.dndTime', async conv => {
const circuit = await getCircuit(conv);
if (!circuit) {
return;
}
const userPresence = await circuit.getUserPresence();
if (userPresence === Circuit.Enums.PresenceState.DND) {
const timeLeft = await circuit.getDndTime();
const mLeft = Math.floor((timeLeft - Date.now()) / 60000);//sets the time left in minutes
if (mLeft > 60) {
conv.ask(`You are set to "Do Not Disturb" for another ${Math.floor(mLeft / 60)} hour(s) and ${Math.floor(((mLeft / 60)- Math.floor(mLeft / 60)) * 60)} minute(s). Anything Else?`);
conv.contexts.set('anything_else', 5);
} else {
conv.ask(`You are set to "Do Not Disturb" for another ${mLeft} minute(s). Would there be anything else?`);
conv.contexts.set('anything_else', 5);
}
} else {
conv.ask('It seems that you are not set to Do Not Disturb. Would you like to do anything else?');
conv.contexts.set('anything_else', 5);
}
});
/**
* Set a logged on user's status message
*/
app.intent('set.statusmessage', async conv => {
const circuit = await getCircuit(conv);
if (!circuit) {
return;
}
const { statusMessage } = conv.contexts.input['setstatusmessage_data'].parameters;
await circuit.client.setStatusMessage(statusMessage)
conv.ask(`Your status message is now set to '${statusMessage}'. May I do anything else for you today?`);
conv.contexts.set('anything_else', 5);
});
/**
* Retrieve a logged on user's status message
*/
app.intent('get.statusmessage', async conv => {
const circuit = await getCircuit(conv);
if (!circuit) {
return;
}
const statusMessage = await circuit.client.getStatusMessage();
if (statusMessage) {
conv.ask(`Your status message is '${statusMessage}'. May I do anything else for you today?`);
conv.contexts.set('anything_else', 5);
} else {
conv.ask(`It appears your status message is blank. May I do anything else for you today?`);
conv.contexts.set('anything_else', 5);
conv.ask(new Suggestions('Set status Message', 'Yes, please', 'No, thank you'));
}
});
/**
* Create Circuit session
*/
function createSession(user) {
const circuit = new CircuitClient({client_id: CLIENT_ID});
return circuit.logon(user.access.token)
.then(() => {
const session = {
circuit: circuit,
timer: setTimeout(clearSession.bind(null, user.id), SESSION_TIMEOUT)
}
sessions[user.id] = session;
return session;
})
.catch(err => console.error(`Unable to logon to Circuit`, err));
}
/**
* Find web client of logged on user
*/
function findWebClient(circuit) {
return circuit.getDevices().then(devices => {
return devices.find(device => {
return (device.clientId !== circuit.user.clientId) &&
((device.clientInfo.deviceType === 'WEB') ||
(device.clientInfo.deviceType === 'APPLICATION' && device.clientInfo.deviceSubtype === 'DESKTOP_APP'));
});
});
}
/**
* clearSession
*/
function clearSession(sessionId) {
let session = sessions[sessionId];
if (!session) {
return Promise.resolve();
}
console.log(`Clearing session ${sessionId}`);
clearTimeout(session.timer);
session.timer = null;
sessions[sessionId] = null;
return session.circuit.logout();
}
/**
* destroy
*/
function destroy() {;
let promises = [];
Object.keys(sessions).forEach(key => promises.push(clearSession(key)));
sessions = [];
return Promise.all(promises);
}