forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoAuthHelpers.js
100 lines (89 loc) · 4.17 KB
/
oAuthHelpers.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { AttachmentLayoutTypes, CardFactory } = require('botbuilder');
const { SimpleGraphClient } = require('./simple-graph-client');
/**
* These methods call the Microsoft Graph API. The following OAuth scopes are used:
* 'OpenId' 'email' 'Mail.Send.Shared' 'Mail.Read' 'profile' 'User.Read' 'User.ReadBasic.All'
* for more information about scopes see:
* https://developer.microsoft.com/en-us/graph/docs/concepts/permissions_reference
*/
class OAuthHelpers {
/**
* Enable the user to send an email via the bot.
* @param {TurnContext} context A TurnContext instance containing all the data needed for processing this conversation turn.
* @param {TokenResponse} tokenResponse A response that includes a user token.
* @param {string} emailAddress The email address of the recipient.
*/
static async sendMail(context, tokenResponse, emailAddress) {
if (!context) {
throw new Error('OAuthHelpers.sendMail(): `context` cannot be undefined.');
}
if (!tokenResponse) {
throw new Error('OAuthHelpers.sendMail(): `tokenResponse` cannot be undefined.');
}
const client = new SimpleGraphClient(tokenResponse.token);
const me = await client.getMe();
await client.sendMail(
emailAddress,
'Message from a bot!',
`Hi there! I had this message sent from a bot. - Your friend, ${ me.displayName }`
);
await context.sendActivity(`I sent a message to ${ emailAddress } from your account.`);
}
/**
* Displays information about the user in the bot.
* @param {TurnContext} context A TurnContext instance containing all the data needed for processing this conversation turn.
* @param {TokenResponse} tokenResponse A response that includes a user token.
*/
static async listMe(context, tokenResponse) {
if (!context) {
throw new Error('OAuthHelpers.listMe(): `context` cannot be undefined.');
}
if (!tokenResponse) {
throw new Error('OAuthHelpers.listMe(): `tokenResponse` cannot be undefined.');
}
// Pull in the data from Microsoft Graph.
const client = new SimpleGraphClient(tokenResponse.token);
const me = await client.getMe();
await context.sendActivity(`You are ${ me.displayName }.`);
}
/**
* Lists the user's collected email.
* @param {TurnContext} context A TurnContext instance containing all the data needed for processing this conversation turn.
* @param {TokenResponse} tokenResponse A response that includes a user token.
*/
static async listRecentMail(context, tokenResponse) {
if (!context) {
throw new Error('OAuthHelpers.listRecentMail(): `context` cannot be undefined.');
}
if (!tokenResponse) {
throw new Error('OAuthHelpers.listRecentMail(): `tokenResponse` cannot be undefined.');
}
var client = new SimpleGraphClient(tokenResponse.token);
var response = await client.getRecentMail();
var messages = response.value;
if (Array.isArray(messages)) {
let numberOfMessages = messages.length;
if (messages.length > 5) {
numberOfMessages = 5;
}
const reply = { attachments: [], attachmentLayout: AttachmentLayoutTypes.Carousel };
for (let cnt = 0; cnt < numberOfMessages; cnt++) {
const mail = messages[cnt];
const card = CardFactory.heroCard(
mail.subject,
mail.bodyPreview,
[{ alt: 'Outlook Logo', url: 'https://botframeworksamples.blob.core.windows.net/samples/OutlookLogo.jpg' }],
[],
{ subtitle: `${ mail.from.emailAddress.name } <${ mail.from.emailAddress.address }>` }
);
reply.attachments.push(card);
}
await context.sendActivity(reply);
} else {
await context.sendActivity('Unable to find any recent unread mail.');
}
}
}
exports.OAuthHelpers = OAuthHelpers;