forked from microsoft/BotBuilder-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Teams manifest, update README, add Graph client (microsoft#2149)
* Added documentation on how to set up in Teams * Added Graph API call * Logout now works even w/bot mention in channels * Add manifest, update readme * fix packages Co-authored-by: Bob German <[email protected]>
- Loading branch information
1 parent
66b04f2
commit 89ae930
Showing
10 changed files
with
242 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
samples/csharp_dotnetcore/46.teams-auth/SimpleGraphClient.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading.Tasks; | ||
using Microsoft.Graph; | ||
|
||
namespace Microsoft.BotBuilderSamples | ||
{ | ||
// This class is a wrapper for the Microsoft Graph API | ||
// See: https://developer.microsoft.com/en-us/graph | ||
public class SimpleGraphClient | ||
{ | ||
private readonly string _token; | ||
|
||
public SimpleGraphClient(string token) | ||
{ | ||
if (string.IsNullOrWhiteSpace(token)) | ||
{ | ||
throw new ArgumentNullException(nameof(token)); | ||
} | ||
|
||
_token = token; | ||
} | ||
|
||
// Sends an email on the users behalf using the Microsoft Graph API | ||
public async Task SendMailAsync(string toAddress, string subject, string content) | ||
{ | ||
if (string.IsNullOrWhiteSpace(toAddress)) | ||
{ | ||
throw new ArgumentNullException(nameof(toAddress)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(subject)) | ||
{ | ||
throw new ArgumentNullException(nameof(subject)); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(content)) | ||
{ | ||
throw new ArgumentNullException(nameof(content)); | ||
} | ||
|
||
var graphClient = GetAuthenticatedClient(); | ||
var recipients = new List<Recipient> | ||
{ | ||
new Recipient | ||
{ | ||
EmailAddress = new EmailAddress | ||
{ | ||
Address = toAddress, | ||
}, | ||
}, | ||
}; | ||
|
||
// Create the message. | ||
var email = new Message | ||
{ | ||
Body = new ItemBody | ||
{ | ||
Content = content, | ||
ContentType = BodyType.Text, | ||
}, | ||
Subject = subject, | ||
ToRecipients = recipients, | ||
}; | ||
|
||
// Send the message. | ||
await graphClient.Me.SendMail(email, true).Request().PostAsync(); | ||
} | ||
|
||
// Gets mail for the user using the Microsoft Graph API | ||
public async Task<Message[]> GetRecentMailAsync() | ||
{ | ||
var graphClient = GetAuthenticatedClient(); | ||
var messages = await graphClient.Me.MailFolders.Inbox.Messages.Request().GetAsync(); | ||
return messages.Take(5).ToArray(); | ||
} | ||
|
||
// Get information about the user. | ||
public async Task<User> GetMeAsync() | ||
{ | ||
var graphClient = GetAuthenticatedClient(); | ||
var me = await graphClient.Me.Request().GetAsync(); | ||
return me; | ||
} | ||
|
||
// gets information about the user's manager. | ||
public async Task<User> GetManagerAsync() | ||
{ | ||
var graphClient = GetAuthenticatedClient(); | ||
var manager = await graphClient.Me.Manager.Request().GetAsync() as User; | ||
return manager; | ||
} | ||
|
||
// // Gets the user's photo | ||
// public async Task<PhotoResponse> GetPhotoAsync() | ||
// { | ||
// HttpClient client = new HttpClient(); | ||
// client.DefaultRequestHeaders.Add("Authorization", "Bearer " + _token); | ||
// client.DefaultRequestHeaders.Add("Accept", "application/json"); | ||
|
||
// using (var response = await client.GetAsync("https://graph.microsoft.com/v1.0/me/photo/$value")) | ||
// { | ||
// if (!response.IsSuccessStatusCode) | ||
// { | ||
// throw new HttpRequestException($"Graph returned an invalid success code: {response.StatusCode}"); | ||
// } | ||
|
||
// var stream = await response.Content.ReadAsStreamAsync(); | ||
// var bytes = new byte[stream.Length]; | ||
// stream.Read(bytes, 0, (int)stream.Length); | ||
|
||
// var photoResponse = new PhotoResponse | ||
// { | ||
// Bytes = bytes, | ||
// ContentType = response.Content.Headers.ContentType?.ToString(), | ||
// }; | ||
|
||
// if (photoResponse != null) | ||
// { | ||
// photoResponse.Base64String = $"data:{photoResponse.ContentType};base64," + | ||
// Convert.ToBase64String(photoResponse.Bytes); | ||
// } | ||
|
||
// return photoResponse; | ||
// } | ||
// } | ||
|
||
// Get an Authenticated Microsoft Graph client using the token issued to the user. | ||
private GraphServiceClient GetAuthenticatedClient() | ||
{ | ||
var graphClient = new GraphServiceClient( | ||
new DelegateAuthenticationProvider( | ||
requestMessage => | ||
{ | ||
// Append the access token to the request. | ||
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", _token); | ||
|
||
// Get event times in the current time zone. | ||
requestMessage.Headers.Add("Prefer", "outlook.timezone=\"" + TimeZoneInfo.Local.Id + "\""); | ||
|
||
return Task.CompletedTask; | ||
})); | ||
return graphClient; | ||
} | ||
} | ||
} |
Binary file added
BIN
+1.2 KB
samples/csharp_dotnetcore/46.teams-auth/TeamsAppManifest/icon-color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+383 Bytes
samples/csharp_dotnetcore/46.teams-auth/TeamsAppManifest/icon-outline.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions
44
samples/csharp_dotnetcore/46.teams-auth/TeamsAppManifest/manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
"$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.5/MicrosoftTeams.schema.json", | ||
"manifestVersion": "1.5", | ||
"version": "1.0.0", | ||
"id": "<<YOUR-MICROSOFT-APP-ID>>", | ||
"packageName": "com.microsoft.teams.samples", | ||
"developer": { | ||
"name": "Microsoft", | ||
"websiteUrl": "https://example.azurewebsites.net", | ||
"privacyUrl": "https://example.azurewebsites.net/privacy", | ||
"termsOfUseUrl": "https://example.azurewebsites.net/termsofuse" | ||
}, | ||
"icons": { | ||
"color": "color.png", | ||
"outline": "outline.png" | ||
}, | ||
"name": { | ||
"short": "Task Module", | ||
"full": "Simple Task Module" | ||
}, | ||
"description": { | ||
"short": "Test Task Module Scenario", | ||
"full": "Simple Task Module Scenario Test" | ||
}, | ||
"accentColor": "#FFFFFF", | ||
"bots": [ | ||
{ | ||
"botId": "<<YOUR-MICROSOFT-APP-ID>>", | ||
"scopes": [ | ||
"personal" | ||
], | ||
"supportsFiles": false, | ||
"isNotificationOnly": false | ||
} | ||
], | ||
"permissions": [ | ||
"identity", | ||
"messageTeamMembers" | ||
], | ||
"validDomains": [ | ||
"token.botframework.com", | ||
"*.ngrok.io" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters