microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Handlers/EventHandler.cs
69lines · 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 any event activity. |
| 11 | /// </summary> |
| 12 | /// <param name="context">The context for the event 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 representing the asynchronous operation.</returns> |
| 15 | public delegate Task EventActivityHandler(Context<EventActivity> context, CancellationToken cancellationToken = default); |
| 16 | |
| 17 | /// <summary> |
| 18 | /// Extension methods for registering generic event activity handlers. |
| 19 | /// </summary> |
| 20 | public static class EventExtensions |
| 21 | { |
| 22 | /// <summary> |
| 23 | /// Registers a handler for all event activities. |
| 24 | /// </summary> |
| 25 | /// <remarks> |
| 26 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 27 | /// </remarks> |
| 28 | /// <param name="app">The Teams bot application.</param> |
| 29 | /// <param name="handler">The handler to register.</param> |
| 30 | /// <returns>The updated Teams bot application.</returns> |
| 31 | public static TeamsBotApplication OnEvent(this TeamsBotApplication app, EventActivityHandler handler) |
| 32 | { |
| 33 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 34 | app.Router.Register(new Route<EventActivity> |
| 35 | { |
| 36 | Name = TeamsActivityType.Event, |
| 37 | Selector = _ => true, |
| 38 | Handler = async (ctx, cancellationToken) => |
| 39 | { |
| 40 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 41 | } |
| 42 | }); |
| 43 | |
| 44 | return app; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | /// <summary> |
| 49 | /// Registers a handler for read receipt event activities. |
| 50 | /// Fired by Teams when a user reads a message sent by the bot in a 1:1 chat. |
| 51 | /// No value payload — the event itself is the notification. |
| 52 | /// </summary> |
| 53 | public static TeamsBotApplication OnReadReceipt(this TeamsBotApplication app, EventActivityHandler handler) |
| 54 | { |
| 55 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 56 | app.Router.Register(new Route<EventActivity> |
| 57 | { |
| 58 | Name = string.Join("/", TeamsActivityType.Event, EventNames.ReadReceipt), |
| 59 | Selector = activity => activity.Name == EventNames.ReadReceipt, |
| 60 | Handler = async (ctx, cancellationToken) => |
| 61 | { |
| 62 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 63 | } |
| 64 | }); |
| 65 | |
| 66 | return app; |
| 67 | } |
| 68 | */ |
| 69 | } |
| 70 | |