-
Notifications
You must be signed in to change notification settings - Fork 3
/
oAuthHelpers.js
131 lines (109 loc) · 5.26 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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
const { AttachmentLayoutTypes, CardFactory } = require('botbuilder');
const { SimpleGraphClient } = require('./simple-graph-client');
const { SimpleAdaptiveCard } = require('./resources/simpleAdaptiveCard.json');
/**
* 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.Digest };
for (let cnt = 0; cnt < numberOfMessages; cnt++) {
const mail = messages[cnt];
const card = CardFactory.heroCard(
mail.subject,
mail.bodyPreview,
[{type: 'Image', alt: 'Outlook Logo', url: 'https://botframeworksamples.blob.core.windows.net/samples/OutlookLogo.jpg', height: '5px', width:'5px'}],
[],
{ 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.');
}
}
/**
* Displays emails for search parameter (Indexed fuzzy search)
* @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 searchMails(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);
//Hard-Coded search for orders
//ToDo: Use Search API instead of recent mails. (Next version)
const me = await client.getDocuments('orders');
await context.sendActivity(`You are ${ me.displayName }.`);
}
}
exports.OAuthHelpers = OAuthHelpers;