-
Notifications
You must be signed in to change notification settings - Fork 65
Move TeamsAttachmentDownloader to Core #492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tracyboehrer
wants to merge
14
commits into
main
Choose a base branch
from
users/tracyboehrer/teamsdownloader-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7fc43ac
TeamsAttachmentDownload move to Core
fda1f29
Merge branch 'main' into users/tracyboehrer/teamsdownloader-core
16356df
Finalize TeamsAttachmentDownloader move to Core
03e44e1
Corrected test samples use of TeamsAttachmentDownloader
f40b2bc
Run FileDownloader prior to OnBeforeTurn handlers
c7f784c
Merge branch 'main' into users/tracyboehrer/teamsdownloader-core
tracyboehrer 7929d57
Merge branch 'main' into users/tracyboehrer/teamsdownloader-core
MattB-msft d849b99
Teams attachment downloader now accepts M365Copilot channels
aaa061c
Minor improvments in TeamsAttachmentDownloader
e5e76d7
Merge branch 'main' into users/tracyboehrer/teamsdownloader-core
tracyboehrer cf4ed6f
Using IConnection.GetTokenProvider in TeamsAttachmentDownloader to ge…
27aff7a
Simplified HandlingAttachments a tad
1dea236
Corrected HandlingAttachments Teams manifest
a6cb841
Merge branch 'main' into users/tracyboehrer/teamsdownloader-core
tracyboehrer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
163 changes: 163 additions & 0 deletions
163
src/libraries/Builder/Microsoft.Agents.Builder/App/TeamsAttachmentDownloader.cs
This file contains hidden or 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,163 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Agents.Authentication; | ||
| using Microsoft.Agents.Builder.State; | ||
| using Microsoft.Agents.Core; | ||
| using Microsoft.Agents.Core.Models; | ||
| using Microsoft.Agents.Core.Serialization; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Net.Http; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Microsoft.Agents.Builder.App | ||
| { | ||
| /// <summary> | ||
| /// Downloads attachments from Teams using the configured Token Provider (from IConnections). | ||
| /// </summary> | ||
| public class TeamsAttachmentDownloader : IInputFileDownloader | ||
| { | ||
| private readonly TeamsAttachmentDownloaderOptions _options; | ||
| private readonly IHttpClientFactory _httpClientFactory; | ||
| private readonly IConnections _connections; | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Creates the TeamsAttachmentDownloader | ||
| /// </summary> | ||
| /// <param name="options">The options</param> | ||
| /// <param name="connections"></param> | ||
| /// <param name="httpClientFactory"></param> | ||
| /// <exception cref="System.ArgumentException"></exception> | ||
| public TeamsAttachmentDownloader(IConnections connections, IHttpClientFactory httpClientFactory, TeamsAttachmentDownloaderOptions options = null) | ||
| { | ||
| AssertionHelpers.ThrowIfNull(connections, nameof(connections)); | ||
| AssertionHelpers.ThrowIfNull(httpClientFactory, nameof(httpClientFactory)); | ||
|
|
||
| _options = options ?? new(); | ||
| _connections = connections; | ||
| _httpClientFactory = httpClientFactory; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public async Task<IList<InputFile>> DownloadFilesAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken) | ||
| { | ||
| if (turnContext.Activity.ChannelId != Channels.Msteams && turnContext.Activity.ChannelId != Channels.M365Copilot) | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| // Filter out HTML attachments | ||
| IEnumerable<Attachment>? attachments = turnContext.Activity.Attachments?.Where((a) => !a.ContentType.StartsWith("text/html")); | ||
| if (attachments == null || !attachments.Any()) | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| string accessToken = ""; | ||
|
|
||
| // If authentication is enabled, get access token | ||
| if (!_options.UseAnonymous) | ||
| { | ||
| IAccessTokenProvider accessTokenProvider = null; | ||
| if (string.IsNullOrEmpty(_options.TokenProviderName)) | ||
| { | ||
| accessTokenProvider = _connections.GetTokenProvider(turnContext.Identity, turnContext.Activity); | ||
| } | ||
| else | ||
| { | ||
| if (!_connections.TryGetConnection(_options.TokenProviderName, out accessTokenProvider)) | ||
| { | ||
| accessTokenProvider = _connections.GetTokenProvider(turnContext.Identity, turnContext.Activity); | ||
| } | ||
| } | ||
|
|
||
| accessToken = await accessTokenProvider.GetAccessTokenAsync(AgentClaims.GetTokenAudience(turnContext.Identity), _options.Scopes).ConfigureAwait(false); | ||
| } | ||
|
|
||
| List<InputFile> files = []; | ||
|
|
||
| foreach (Attachment attachment in attachments) | ||
| { | ||
| InputFile? file = await DownloadFileAsync(attachment, accessToken); | ||
| if (file != null) | ||
| { | ||
| files.Add(file); | ||
| } | ||
| } | ||
|
|
||
| return files; | ||
| } | ||
|
|
||
|
|
||
| private async Task<InputFile?> DownloadFileAsync(Attachment attachment, string accessToken) | ||
| { | ||
| string? name = attachment.Name; | ||
|
|
||
| if (attachment.ContentUrl != null && (attachment.ContentUrl.StartsWith("https://") || attachment.ContentUrl.StartsWith("http://localhost"))) | ||
| { | ||
| // Get downloadable content link | ||
| string downloadUrl; | ||
| var contentProperties = ProtocolJsonSerializer.ToJsonElements(attachment.Content); | ||
| if (contentProperties == null || !contentProperties.TryGetValue("downloadUrl", out System.Text.Json.JsonElement value)) | ||
| { | ||
| downloadUrl = attachment.ContentUrl; | ||
| } | ||
| else | ||
| { | ||
| downloadUrl = value.ToString(); | ||
| } | ||
|
|
||
| using var httpClient = _httpClientFactory.CreateClient(nameof(TeamsAttachmentDownloader)); | ||
|
|
||
| using HttpRequestMessage request = new(HttpMethod.Get, downloadUrl); | ||
| request.Headers.Add("Authorization", $"Bearer {accessToken}"); | ||
|
|
||
| using HttpResponseMessage response = await httpClient.SendAsync(request).ConfigureAwait(false); | ||
|
|
||
| // Failed to download file | ||
| if (!response.IsSuccessStatusCode) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| // Convert to a buffer | ||
| byte[] content = await response.Content.ReadAsByteArrayAsync(); | ||
|
|
||
| // Fixup content type | ||
| string contentType = response.Content.Headers.ContentType.MediaType; | ||
| if (contentType.StartsWith("image/")) | ||
| { | ||
| contentType = "image/png"; | ||
| } | ||
|
|
||
| return new InputFile(new BinaryData(content), contentType) | ||
| { | ||
| ContentUrl = attachment.ContentUrl, | ||
| Filename = name | ||
| }; | ||
| } | ||
| else | ||
| { | ||
| return new InputFile(new BinaryData(attachment.Content), attachment.ContentType) | ||
| { | ||
| ContentUrl = attachment.ContentUrl, | ||
| Filename = name | ||
| }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// The TeamsAttachmentDownloader options | ||
| /// </summary> | ||
| public class TeamsAttachmentDownloaderOptions | ||
| { | ||
| public string TokenProviderName { get; set; } | ||
| public bool UseAnonymous { get; set; } = false; | ||
| public IList<string> Scopes { get; set; } = null; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.