This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
app.js
527 lines (479 loc) · 18.3 KB
/
app.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*-----------------------------------------------------------------------------------------
Veronica Bot - makes use of Microsoft Graph in order to store the users's request in a list
Author: Giuliano De Luca (MVP Office Development) - Twitter @giuleon
Date: February 20, 2018
-----------------------------------------------------------------------------------------*/
var restify = require('restify');
var builder = require('botbuilder');
var botbuilder_azure = require("botbuilder-azure");
var request = require("request");
var Q = require('q');
// Setup Restify Server
var server = restify.createServer();
server.use(restify.plugins.bodyParser({
mapParams: true
})); // To be able to get the authorization code (req.params.code)
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});
// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword,
});
// Config
var config = {
'clientId': process.env.AAD_CLIENT_ID, // The client Id retrieved from the Azure AD App
'clientSecret': process.env.AAD_CLIENT_SECRET, // The client secret retrieved from the Azure AD App
'tenant': process.env.TENANT, // The tenant Id or domain name (e.g mydomain.onmicrosoft.com)
'tokenEndpoint': process.env.tokenEndpoint, // This URL will be used for the Azure AD Application to send the authorization code.
'resource': process.env.RESOURCE, // The resource endpoint we want to give access to (in this case, SharePoint Online)
'listId': process.env.List_Id, // The list Id where the Bot will save the user's submission
}
// Graph
var graph = {};
// Listen for messages from users
server.post('/api/messages', connector.listen());
/*----------------------------------------------------------------------------------------
* Bot Storage: This is a great spot to register the private state storage for your bot.
* We provide adapters for Azure Table, CosmosDb, SQL Azure, or you can implement your own!
* For samples and documentation, see: https://github.com/Microsoft/BotBuilder-Azure
* ---------------------------------------------------------------------------------------- */
var tableName = 'botdata';
var azureTableClient = new botbuilder_azure.AzureTableClient(tableName, process.env['AzureWebJobsStorage']);
var tableStorage = new botbuilder_azure.AzureBotStorage({ gzipData: false }, azureTableClient);
// Create your bot with a function to receive messages from the user
var bot = new builder.UniversalBot(connector);
bot.set('storage', tableStorage);
bot.dialog('/', [
function (session) {
if (session.privateConversationData["welcome"]) {
session.send("Hi I'm your SharePoint Bot to assist you to request a new SharePoint site or Teams, what do you want to request?");
}
session.privateConversationData["welcome"] = 'true';
session.beginDialog('makeYourChoice');
},
function (session, results) {
session.privateConversationData["SiteType"] = results.response;
session.beginDialog('askForTitle');
},
function (session, results) {
session.privateConversationData["Title"] = results.response;
session.beginDialog('askForReason');
},
function (session, results) {
session.privateConversationData["Description"] = results.response;
session.beginDialog('askForOwner');
},
function (session, results) {
session.privateConversationData["Owner"] = results.response;
session.beginDialog('askForAlias');
},
function (session, results) {
session.privateConversationData["Alias"] = results.response;
session.beginDialog('askForConfirmation');
},
function (session, results) {
if (results.response === "confirmed") {
// Get an access token for the app.
auth.getAccessToken().then(function (token) {
// create a new list item
var params = {
"fields": {
"Title": session.privateConversationData['Title'],
"Status": "Requested",
"Owner": session.privateConversationData['Owner'],
"Description": session.privateConversationData['Description'],
"SiteType": session.privateConversationData['SiteType'],
"Alias": session.privateConversationData['Alias']
}
};
graph.postListItem(token, params)
.then(function (result) {
console.log(result);
session.send("Request submitted successfully");
session.beginDialog('askForAnotherRequest');
}, function (error) {
console.error('>>> Error creating a list item: ' + error);
session.beginDialog('askForAnotherRequest');
});
}, function (error) {
console.error('>>> Error getting access token: ' + error);
session.beginDialog('askForAnotherRequest');
});
} else {
session.beginDialog('askForAnotherRequest');
}
},
function (session, results) {
if (results.response === 'yes') {
session.beginDialog('/');
} else {
session.endDialog();
}
},
]).triggerAction({ matches: /^(show|list|restart)/i });
// Add dialog to return list of choices available
bot.dialog('makeYourChoice', [
function (session) {
var msg = new builder.Message(session)
.speak('what do you want to request?')
.text('what do you want to request?');
msg.attachmentLayout(builder.AttachmentLayout.carousel)
msg.attachments([
new builder.ThumbnailCard(session)
.title("SharePoint Site")
.subtitle("A SharePoint team site connects you and your team to the content, information, and apps you rely on every day.")
.text("Create a SharePoint Onlineteam site to provide a location where you and your team can work on projects and share information.")
.images([builder.CardImage.create(session, 'https://pbs.twimg.com/profile_images/920356547015749632/2in54ehS_400x400.jpg')])
.buttons([
builder.CardAction.imBack(session, "TeamSite", "Confirm")
]),
new builder.ThumbnailCard(session)
.title("SharePoint Site")
.subtitle("SharePoint communication sites are a great way to share information with others in a visually compelling format.")
.text("With a communication site, typically only a small set of members contribute content that is consumed by a much larger audience.")
.images([builder.CardImage.create(session, 'https://pbs.twimg.com/profile_images/920356547015749632/2in54ehS_400x400.jpg')])
.buttons([
builder.CardAction.imBack(session, "CommunicationSite", "Confirm")
]),
new builder.ThumbnailCard(session)
.title("Microsoft Teams")
.subtitle("Microsoft Teams is the hub for teamwork in Office 365 that integrates all the people, content, and tools your team needs to be more engaged and effective.")
.text("Work closer with your team by using chat, meeting, conference call, documents.")
.images([builder.CardImage.create(session, 'https://pbs.twimg.com/profile_images/873316258980036608/QXYh4F8U_400x400.jpg')])
.buttons([
builder.CardAction.imBack(session, "Teams", "Confirm")
])
]);
builder.Prompts.text(session, msg);
},
function name(session, results) {
session.endDialogWithResult({ response: results.response });
}
]);
// This dialog prompts the user for a title.
bot.dialog('askForTitle', [
function (session, args) {
var question = 'What is the title of your ' + session.privateConversationData["SiteType"] + '?';
var msg = new builder.Message(session)
.speak(question)
.text(question);
builder.Prompts.text(session, msg);
},
function (session, results) {
session.endDialogWithResult({ response: results.response });
}
]);
// This dialog prompts the reason why
bot.dialog('askForReason', [
function (session, args) {
var question = 'Describe the reason of your request:';
var msg = new builder.Message(session)
.speak(question)
.text(question);
builder.Prompts.text(session, msg);
},
function (session, results) {
session.endDialogWithResult({ response: results.response });
}
]);
// This dialog prompts the Owner
bot.dialog('askForOwner', [
function (session, args) {
var question = '';
var msg = '';
if (args && args.reprompt) {
question = 'The user doesn\'t exists, please insert a valid email';
msg = new builder.Message(session)
.speak(question)
.text(question);
builder.Prompts.text(session, msg);
} else {
question = 'Please insert the email of the owner:';
msg = new builder.Message(session)
.speak(question)
.text(question);
builder.Prompts.text(session, msg);
}
},
function (session, results) {
var userEmail = results.response;
// Get an access token for the app.
auth.getAccessToken().then(function (token) {
graph.getUserByEmail(token, userEmail)
.then(function (result) {
console.log(result);
session.endDialogWithResult({ response: results.response });
}, function (error) {
console.error('>>> Error getting the user: ' + error);
// Repeat the dialog
session.replaceDialog('askForOwner', { reprompt: true });
});
});
}
]);
// This dialog prompts the user for a phone number.
// It will re-prompt the user if the input does not match a pattern for phone number.
bot.dialog('askForAlias', [
function (session, args) {
var question = '';
var msg = '';
if (args && args.reprompt) {
question = 'The alias already exists please choose another one.';
msg = new builder.Message(session)
.speak(question)
.text(question);
builder.Prompts.text(session, msg);
} else {
question = 'Please insert an alias for your ' + session.privateConversationData["SiteType"] + ' without blank spaces or special characters.';
msg = new builder.Message(session)
.speak(question)
.text(question);
builder.Prompts.text(session, msg);
}
},
function (session, results) {
var alias = results.response;
// Get an access token for the app.
auth.getAccessToken().then(function (token) {
graph.getUnifiedGroupByAlias(token, alias)
.then(function (result) {
console.log(result);
if (result.length === 0) {
session.endDialogWithResult({ response: alias });
} else {
// Repeat the dialog
session.replaceDialog('askForAlias', { reprompt: true });
}
}, function (error) {
console.error('>>> Error getting the group: ' + error);
session.endDialogWithResult({ response: alias });
});
});
}
]);
// Add dialog to request a confirmation
bot.dialog('askForConfirmation', [
function (session) {
var msg = new builder.Message(session)
.speak(
"Here the summary of your request. " +
"Title: " + session.privateConversationData['Title'] + " " +
"Owner: " + session.privateConversationData['Owner'] + " " +
"Description: " + session.privateConversationData['Description'] + " " +
"SiteType: " + session.privateConversationData['SiteType'] + " " +
"Alias: " + session.privateConversationData['Alias']);
msg.attachmentLayout(builder.AttachmentLayout.list)
msg.attachments([
new builder.ThumbnailCard(session)
.title("Summary")
.subtitle("Here the summary of your request")
.text(
"Title: " + session.privateConversationData['Title'] + "\n\n" +
"Owner: " + session.privateConversationData['Owner'] + "\n\n" +
"Description: " + session.privateConversationData['Description'] + "\n\n" +
"SiteType: " + session.privateConversationData['SiteType'] + "\n\n" +
"Alias: " + session.privateConversationData['Alias'])
// .images([builder.CardImage.create(session, '/images/sp.png')])
.buttons([
builder.CardAction.imBack(session, "canceled", "Cancel"),
builder.CardAction.imBack(session, "confirmed", "Confirm"),
])
]);
builder.Prompts.text(session, msg);
},
function name(session, results) {
session.endDialogWithResult({ response: results.response });
}
]);
// Add dialog to request a confirmation
bot.dialog('askForAnotherRequest', [
function (session) {
var msg = new builder.Message(session)
.speak("Do you want to submit another request?");
msg.attachmentLayout(builder.AttachmentLayout.list)
msg.attachments([
new builder.ThumbnailCard(session)
.title("Request")
.subtitle("Do you want to submit another request?")
.text("Please confirm or not.")
// .images([builder.CardImage.create(session, '/images/sp.png')])
.buttons([
builder.CardAction.imBack(session, "no", "No"),
builder.CardAction.imBack(session, "yes", "Yes"),
])
]);
builder.Prompts.text(session, msg);
},
function name(session, results) {
session.endDialogWithResult({ response: results.response });
}
]);
/*----------------------------------------------------------------------------------------
* GRAPH API
------------------------------------------------------------------------------------------ */
/**
* Get all users
* @param {*} token to append in the header in order to make the request
*/
graph.getUsers = function (token) {
var deferred = Q.defer();
// Make a request to get all users in the tenant. Use $select to only get
// necessary values to make the app more performant.
request.get('https://graph.microsoft.com/v1.0/users?$select=id,displayName', {
auth: {
bearer: token
}
}, function (err, response, body) {
var parsedBody = JSON.parse(body);
if (err) {
deferred.reject(err);
} else if (parsedBody.error) {
deferred.reject(parsedBody.error.message);
} else {
// The value of the body will be an array of all users.
deferred.resolve(parsedBody.value);
}
});
return deferred.promise;
};
/**
* Get user by email
* @param {*} token to append in the header in order to make the request
* @param {*} email the email of the user
*/
graph.getUserByEmail = function (token, email) {
var deferred = Q.defer();
// Make a request to get all users in the tenant. Use $select to only get
// necessary values to make the app more performant.
request.get('https://graph.microsoft.com/v1.0/users/' + email, {
auth: {
bearer: token
}
}, function (err, response, body) {
var parsedBody = JSON.parse(body);
if (err) {
deferred.reject(err);
} else if (parsedBody.error) {
deferred.reject(parsedBody.error.message);
} else {
deferred.resolve(parsedBody.mail);
}
});
return deferred.promise;
};
/**
* Get unified group
*/
graph.getUnifiedGroupByAlias = function (token, mailNickname) {
var deferred = Q.defer();
// Make a request to get all users in the tenant. Use $select to only get
// necessary values to make the app more performant.
request.get('https://graph.microsoft.com/v1.0/groups?$filter=groupTypes/any(c:c+eq+\'Unified\') and mailNickname eq \'' + mailNickname + '\'', {
auth: {
bearer: token
}
}, function (err, response, body) {
var parsedBody = JSON.parse(body);
if (err) {
deferred.reject(err);
} else if (parsedBody.error) {
deferred.reject(parsedBody.error.message);
} else {
deferred.resolve(parsedBody.value);
}
});
return deferred.promise;
};
/**
* Create Group in the SP list
* @param {*} token to append in the header in order to make the request
* @param {*} params the json in order to create a new o365 group
*/
graph.createGroup = (token, params) => {
var deferred = Q.defer();
var endpointUrl = "https://graph.microsoft.com/v1.0/groups";
request.post({
url: endpointUrl,
headers: {
"Authorization": "Bearer " + token,
"Content-Type": "application/json"
},
body: JSON.stringify(params)
}, function (err, response, body) {
var parsedBody = JSON.parse(body);
if (err) {
deferred.reject(err);
} else if (parsedBody.error) {
deferred.reject(parsedBody.error.message);
} else {
// The value of the body will be an array of all users.
deferred.resolve(parsedBody.id);
}
});
return deferred.promise;
}
/**
*
* @param {*} token to append in the header in order to make the request
* @param {*} params the json in order to create a new list item
*/
graph.postListItem = (token, params) => {
var deferred = Q.defer();
var listId = config.listId;
var endpointUrl = "https://graph.microsoft.com/v1.0/sites/root/lists/" + listId + "/items";
request.post({
url: endpointUrl,
headers: {
"Authorization": "Bearer " + token,
"Content-Type": "application/json"
},
body: JSON.stringify(params)
}, function (err, response, body) {
var parsedBody = JSON.parse(body);
if (err) {
deferred.reject(err);
} else if (parsedBody.error) {
deferred.reject(parsedBody.error.message);
} else {
// The value of the body will be an array of all users.
deferred.resolve(parsedBody.value);
}
});
return deferred.promise;
}
/*----------------------------------------------------------------------------------------
* Authentication
------------------------------------------------------------------------------------------ */
// The auth module object.
var auth = {};
/**
* Get access token
*/
auth.getAccessToken = function () {
var deferred = Q.defer();
// These are the parameters necessary for the OAuth 2.0 Client Credentials Grant Flow.
// For more information, see Service to Service Calls Using Client Credentials (https://msdn.microsoft.com/library/azure/dn645543.aspx).
var requestParams = {
grant_type: 'client_credentials',
client_id: config.clientId,
client_secret: config.clientSecret,
resource: config.resource
};
/**
* post: Make a request to the token issuing endpoint
*/
request.post({ url: config.tokenEndpoint, form: requestParams }, function (err, response, body) {
var parsedBody = JSON.parse(body);
if (err) {
deferred.reject(err);
} else if (parsedBody.error) {
deferred.reject(parsedBody.error_description);
} else {
// If successful, return the access token.
deferred.resolve(parsedBody.access_token);
}
});
return deferred.promise;
};