// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using Microsoft.Teams.Apps.Routing; using Microsoft.Teams.Apps.Schema; namespace Microsoft.Teams.Apps.Handlers; /// /// Delegate for handling adaptive card action invoke activities. /// /// The context for the invoke activity, providing access to the activity data and bot application. /// A cancellation token that can be used to cancel the operation. /// A task that represents the asynchronous operation. The task result contains the invoke response. public delegate Task AdaptiveCardActionHandler(Context> context, CancellationToken cancellationToken = default); /// /// Extension methods for registering adaptive card action invoke handlers. /// public static class AdaptiveCardExtensions { /// /// Registers a handler for adaptive card action invoke activities. /// Cannot be combined with . /// /// /// Breaking change: previously a catch-all invoke handler could be registered alongside specific invoke handlers. This combination now throws at registration time. /// /// The Teams bot application. /// The handler to register. /// The updated Teams bot application. public static TeamsBotApplication OnAdaptiveCardAction(this TeamsBotApplication app, AdaptiveCardActionHandler handler) { ArgumentNullException.ThrowIfNull(app, nameof(app)); app.Router.Register(new Route { Name = string.Join("/", TeamsActivityTypes.Invoke, InvokeNames.AdaptiveCardAction), Selector = activity => activity.Name == InvokeNames.AdaptiveCardAction, HandlerWithReturn = async (ctx, cancellationToken) => { InvokeActivity typedActivity = new(ctx.Activity); var typedContext = ctx.CreateDerivedContext(typedActivity); return await handler(typedContext, cancellationToken).ConfigureAwait(false); } }); return app; } }