// 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 any event activity.
///
/// The context for the event activity, providing access to the activity data and bot application.
/// A cancellation token that can be used to cancel the operation.
/// A task representing the asynchronous operation.
public delegate Task EventActivityHandler(Context context, CancellationToken cancellationToken = default);
///
/// Extension methods for registering generic event activity handlers.
///
public static class EventExtensions
{
///
/// Registers a handler for all event activities.
///
///
/// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially.
///
/// The Teams bot application.
/// The handler to register.
/// The updated Teams bot application.
public static TeamsBotApplication OnEvent(this TeamsBotApplication app, EventActivityHandler handler)
{
ArgumentNullException.ThrowIfNull(app, nameof(app));
app.Router.Register(new Route
{
Name = TeamsActivityTypes.Event,
Selector = _ => true,
Handler = async (ctx, cancellationToken) =>
{
await handler(ctx, cancellationToken).ConfigureAwait(false);
}
});
return app;
}
/*
///
/// Registers a handler for read receipt event activities.
/// Fired by Teams when a user reads a message sent by the bot in a 1:1 chat.
/// No value payload — the event itself is the notification.
///
public static TeamsBotApplication OnReadReceipt(this TeamsBotApplication app, EventActivityHandler handler)
{
ArgumentNullException.ThrowIfNull(app, nameof(app));
app.Router.Register(new Route
{
Name = string.Join("/", TeamsActivityTypes.Event, EventNames.ReadReceipt),
Selector = activity => activity.Name == EventNames.ReadReceipt,
Handler = async (ctx, cancellationToken) =>
{
await handler(ctx, cancellationToken).ConfigureAwait(false);
}
});
return app;
}
*/
}