forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OAuthHelpers.cs
149 lines (130 loc) · 5.44 KB
/
OAuthHelpers.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
using Attachment = Microsoft.Bot.Schema.Attachment;
namespace Microsoft.BotBuilderSamples
{
// This class calls 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
public static class OAuthHelpers
{
// Enable the user to send an email via the bot.
public static async Task SendMailAsync(ITurnContext turnContext, TokenResponse tokenResponse, string recipient)
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
if (tokenResponse == null)
{
throw new ArgumentNullException(nameof(tokenResponse));
}
var client = new SimpleGraphClient(tokenResponse.Token);
var me = await client.GetMeAsync();
await client.SendMailAsync(
recipient,
"Message from a bot!",
$"Hi there! I had this message sent from a bot. - Your friend, {me.DisplayName}");
await turnContext.SendActivityAsync(
$"I sent a message to '{recipient}' from your account.");
}
// Displays information about the user in the bot.
public static async Task ListMeAsync(ITurnContext turnContext, TokenResponse tokenResponse)
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
if (tokenResponse == null)
{
throw new ArgumentNullException(nameof(tokenResponse));
}
// Pull in the data from the Microsoft Graph.
var client = new SimpleGraphClient(tokenResponse.Token);
var me = await client.GetMeAsync();
var manager = await client.GetManagerAsync();
var photoResponse = await client.GetPhotoAsync();
// Generate the reply activity.
var reply = turnContext.Activity.CreateReply();
var photoText = string.Empty;
if (photoResponse != null)
{
var replyAttachment = new Attachment(photoResponse.ContentType, photoResponse.Base64String);
reply.Attachments.Add(replyAttachment);
}
else
{
photoText = "Consider adding an image to your Outlook profile.";
}
reply.Text = $"You are {me.DisplayName} and you report to {manager.DisplayName}. {photoText}";
await turnContext.SendActivityAsync(reply);
}
// Gets recent mail the user has received within the last hour and displays up
// to 5 of the emails in the bot.
public static async Task ListRecentMailAsync(ITurnContext turnContext, TokenResponse tokenResponse)
{
if (turnContext == null)
{
throw new ArgumentNullException(nameof(turnContext));
}
if (tokenResponse == null)
{
throw new ArgumentNullException(nameof(tokenResponse));
}
var client = new SimpleGraphClient(tokenResponse.Token);
var messages = await client.GetRecentMailAsync();
var reply = turnContext.Activity.CreateReply();
if (messages.Any())
{
var count = messages.Length;
if (count > 5)
{
count = 5;
}
reply.Attachments = new List<Attachment>();
reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
for (var i = 0; i < count; i++)
{
var mail = messages[i];
var card = new HeroCard(
mail.Subject,
$"{mail.From.EmailAddress.Name} <{mail.From.EmailAddress.Address}>",
mail.BodyPreview,
new List<CardImage>()
{
new CardImage(
"https://botframeworksamples.blob.core.windows.net/samples/OutlookLogo.jpg",
"Outlook Logo"),
});
reply.Attachments.Add(card.ToAttachment());
}
}
else
{
reply.Text = "Unable to find any recent unread mail.";
}
await turnContext.SendActivityAsync(reply);
}
// Prompts the user to log in using the OAuth provider specified by the connection name.
public static OAuthPrompt Prompt(string connectionName)
{
return new OAuthPrompt(
"loginPrompt",
new OAuthPromptSettings
{
ConnectionName = connectionName,
Text = "Please login",
Title = "Login",
Timeout = 300000, // User has 5 minutes to login
});
}
}
}