microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Handlers/FileConsentHandler.cs
50lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Bot.Apps.Routing; |
| 5 | using Microsoft.Teams.Bot.Apps.Schema; |
| 6 | |
| 7 | namespace Microsoft.Teams.Bot.Apps.Handlers; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Delegate for handling file consent invoke activities. |
| 11 | /// </summary> |
| 12 | /// <param name="context">The context for the invoke activity, providing access to the activity data and bot application.</param> |
| 13 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 14 | /// <returns>A task that represents the asynchronous operation. The task result contains the invoke response.</returns> |
| 15 | public delegate Task<InvokeResponse<AdaptiveCardResponse>> FileConsentValueHandler(Context<InvokeActivity<FileConsentValue>> context, CancellationToken cancellationToken = default); |
| 16 | |
| 17 | /// <summary> |
| 18 | /// Extension methods for registering file consent invoke handlers. |
| 19 | /// </summary> |
| 20 | public static class FileConsentExtensions |
| 21 | { |
| 22 | |
| 23 | /// <summary> |
| 24 | /// Registers a handler for file consent invoke activities. |
| 25 | /// Cannot be combined with <see cref="InvokeExtensions.OnInvoke"/>. |
| 26 | /// </summary> |
| 27 | /// <remarks> |
| 28 | /// Breaking change: previously a catch-all invoke handler could be registered alongside specific invoke handlers. This combination now throws at registration time. |
| 29 | /// </remarks> |
| 30 | /// <param name="app">The Teams bot application.</param> |
| 31 | /// <param name="handler">The handler to register.</param> |
| 32 | /// <returns>The updated Teams bot application.</returns> |
| 33 | public static TeamsBotApplication OnFileConsent(this TeamsBotApplication app, FileConsentValueHandler handler) |
| 34 | { |
| 35 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 36 | app.Router.Register(new Route<InvokeActivity> |
| 37 | { |
| 38 | Name = string.Join("/", TeamsActivityType.Invoke, InvokeNames.FileConsent), |
| 39 | Selector = activity => activity.Name == InvokeNames.FileConsent, |
| 40 | HandlerWithReturn = async (ctx, cancellationToken) => |
| 41 | { |
| 42 | InvokeActivity<FileConsentValue> typedActivity = new(ctx.Activity); |
| 43 | Context<InvokeActivity<FileConsentValue>> typedContext = new(ctx.TeamsBotApplication, typedActivity); |
| 44 | return await handler(typedContext, cancellationToken).ConfigureAwait(false); |
| 45 | } |
| 46 | }); |
| 47 | |
| 48 | return app; |
| 49 | } |
| 50 | } |
| 51 | |