Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,17 @@ public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancel
}
}

// Download any input files
IList<IInputFileDownloader>? fileDownloaders = Options.FileDownloaders;
if (fileDownloaders != null && fileDownloaders.Count > 0)
{
foreach (IInputFileDownloader downloader in fileDownloaders)
{
var files = await downloader.DownloadFilesAsync(turnContext, turnState, cancellationToken).ConfigureAwait(false);
turnState.Temp.InputFiles = [.. turnState.Temp.InputFiles, .. files];
}
}

// Call before turn handler
foreach (TurnEventHandler beforeTurnHandler in _beforeTurn)
{
Expand All @@ -830,17 +841,6 @@ public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancel
}
}

// Download any input files
IList<IInputFileDownloader>? fileDownloaders = Options.FileDownloaders;
if (fileDownloaders != null && fileDownloaders.Count > 0)
{
foreach (IInputFileDownloader downloader in fileDownloaders)
{
var files = await downloader.DownloadFilesAsync(turnContext, turnState, cancellationToken).ConfigureAwait(false);
turnState.Temp.InputFiles = [.. turnState.Temp.InputFiles, .. files];
}
}

// Execute first matching handler. The RouteList enumerator is ordered by Invoke & Rank, then by Rank & add order.
foreach (Route route in _routes.Enumerate())
{
Expand Down
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public IAccessTokenProvider GetConnection(string name)
/// <returns>Returns <c>true</c> if successful; otherwise <c>false</c>.</returns>
public bool TryGetConnection(string name, out IAccessTokenProvider connection)
{
if (!_connections.TryGetValue(name, out ConnectionDefinition definition))
if (string.IsNullOrEmpty(name) || !_connections.TryGetValue(name, out ConnectionDefinition definition))
{
connection = null;
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,162 +2,27 @@
// Licensed under the MIT License.

using Microsoft.Agents.Authentication;
using Microsoft.Agents.Builder;
using Microsoft.Agents.Builder.App;
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.Extensions.Teams.App
{
/// <summary>
/// Downloads attachments from Teams using the configure Token Provider (from IConnections).
/// </summary>
public class TeamsAttachmentDownloader : IInputFileDownloader
[Obsolete("Use Microsoft.Agents.Builder.App.TeamsAttachmentDownloader instead.")]
public class TeamsAttachmentDownloader(
Builder.App.TeamsAttachmentDownloaderOptions options,
IConnections connections,
IHttpClientFactory httpClientFactory) : Microsoft.Agents.Builder.App.TeamsAttachmentDownloader(connections, httpClientFactory, options)
{
private readonly TeamsAttachmentDownloaderOptions _options;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IAccessTokenProvider _accessTokenProvider;


/// <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(TeamsAttachmentDownloaderOptions options, IConnections connections, IHttpClientFactory httpClientFactory)
{
AssertionHelpers.ThrowIfNull(options, nameof(options));
AssertionHelpers.ThrowIfNull(connections, nameof(connections));
AssertionHelpers.ThrowIfNull(httpClientFactory, nameof(httpClientFactory));

_options = options;
if (string.IsNullOrEmpty(_options.TokenProviderName))
{
throw new ArgumentException("TeamsAttachmentDownloader.TokenProviderName is empty.");
}

_accessTokenProvider = connections.GetConnection(_options.TokenProviderName);
if (_accessTokenProvider == null)
{
throw new ArgumentException("TeamsAttachmentDownloader.TokenProviderName not found.");
}

_httpClientFactory = httpClientFactory;
}

/// <inheritdoc />
public async Task<IList<InputFile>> DownloadFilesAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken)
{
if (turnContext.Activity.ChannelId != Channels.Msteams)
{
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)
{
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;

using var httpClient = _httpClientFactory.CreateClient(nameof(TeamsAttachmentDownloader));

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 HttpRequestMessage request = new(HttpMethod.Get, downloadUrl);
request.Headers.Add("Authorization", $"Bearer {accessToken}");

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
[Obsolete("Use Microsoft.Agents.Builder.App.TeamsAttachmentDownloaderOptions instead.")]
public class TeamsAttachmentDownloaderOptions : Microsoft.Agents.Builder.App.TeamsAttachmentDownloaderOptions
{
public string TokenProviderName { get; set; }
public bool UseAnonymous { get; set; } = false;
public IList<string> Scopes { get; set; } = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

<ItemGroup>
<ProjectReference Include="..\..\libraries\Authentication\Authentication.Msal\Microsoft.Agents.Authentication.Msal.csproj" />
<ProjectReference Include="..\..\libraries\Extensions\Microsoft.Agents.Extensions.Teams\Microsoft.Agents.Extensions.Teams.csproj" />
<ProjectReference Include="..\..\libraries\Hosting\AspNetCore\Microsoft.Agents.Hosting.AspNetCore.csproj" />
</ItemGroup>

Expand Down
Loading
Loading