Language

FileConsent Class

Definition

Provides routing for Microsoft Teams file consent card interactions.

public class FileConsent
type FileConsent = class
Public Class FileConsent
Inheritance
FileConsent

Remarks

Teams requires explicit user consent before a bot can upload a file into a conversation. The consent flow is:

  1. Register accept and decline handlers via OnAccept(FileConsentHandler, String[], UInt16) and OnDecline(FileConsentHandler, String[], UInt16).
  2. Send a file consent card attachment to prompt the user.
  3. If the user accepts, the registered accept handler is called with FileConsentValue containing upload details. Perform an HTTP PUT to UploadUrl to complete the upload.
  4. If the user declines, the registered decline handler is called.
The following example demonstrates the full consent-and-upload flow using route attributes.
[TeamsExtension]
public partial class MyAgent(AgentApplicationOptions options, IHttpClientFactory httpClientFactory) : AgentApplication(options)
{
    // Send a file consent card when the user requests a file upload.
    [MessageRoute]
    public Task OnMessageAsync(ITurnContext turnContext, ITurnState turnState, CancellationToken cancellationToken)
    {
        var consentCard = new
        {
            Description = "Here is the report you requested.",
            SizeInBytes = 42000,
            AcceptContext = new { filename = "report.txt" },
            DeclineContext = new { filename = "report.txt" }
        };

        var attachment = new Microsoft.Agents.Core.Models.Attachment
        {
            ContentType = "application/vnd.microsoft.teams.card.file.consent",
            Name = "report.txt",
            Content = consentCard
        };

        return turnContext.SendActivityAsync(MessageFactory.Attachment(attachment), cancellationToken);
    }

    [TeamsFileConsentAcceptRoute]
    public async Task OnFileConsentAcceptAsync(
        ITeamsTurnContext turnContext,
        ITurnState turnState,
        Microsoft.Teams.Apps.Files.FileConsentValue response,
        CancellationToken cancellationToken)
    {
        var filePath = Path.Combine("wwwroot", "report.txt");
        var fileInfo = new FileInfo(filePath);
        var client = httpClientFactory.CreateClient();

        using var fileStream = File.OpenRead(filePath);
        var fileContent = new StreamContent(fileStream);
        fileContent.Headers.ContentLength = fileInfo.Length;
        fileContent.Headers.ContentRange =
            new System.Net.Http.Headers.ContentRangeHeaderValue(0, fileInfo.Length - 1, fileInfo.Length);

        await client.PutAsync(response.UploadInfo.UploadUrl, fileContent, cancellationToken);

        await turnContext.SendActivityAsync(
            $"File **{response.UploadInfo.Name}** uploaded successfully.",
            cancellationToken: cancellationToken);
    }

    [TeamsFileConsentDeclineRoute]
    public Task OnFileConsentDeclineAsync(
        ITeamsTurnContext turnContext,
        ITurnState turnState,
        Microsoft.Teams.Apps.Files.FileConsentValue response,
        CancellationToken cancellationToken)
    {
        return turnContext.SendActivityAsync(
            "File upload was declined.",
            cancellationToken: cancellationToken);
    }
}

Methods

Name Description
OnAccept(FileConsentHandler, String[], UInt16)

Handles when a file consent card is accepted by the user.

OnDecline(FileConsentHandler, String[], UInt16)

Handles when a file consent card is declined by the user.

Applies to