microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Handlers/MeetingHandler.cs
153lines · 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 meeting start event activities. |
| 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 MeetingStartHandler(Context<EventActivity<MeetingStartValue>> context, CancellationToken cancellationToken = default); |
| 16 | |
| 17 | /// <summary> |
| 18 | /// Delegate for handling meeting end event activities. |
| 19 | /// </summary> |
| 20 | /// <param name="context">The context for the event activity, providing access to the activity data and bot application.</param> |
| 21 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 22 | /// <returns>A task representing the asynchronous operation.</returns> |
| 23 | public delegate Task MeetingEndHandler(Context<EventActivity<MeetingEndValue>> context, CancellationToken cancellationToken = default); |
| 24 | |
| 25 | /// <summary> |
| 26 | /// Delegate for handling meeting participant join event activities. |
| 27 | /// </summary> |
| 28 | /// <param name="context">The context for the event activity, providing access to the activity data and bot application.</param> |
| 29 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 30 | /// <returns>A task representing the asynchronous operation.</returns> |
| 31 | public delegate Task MeetingParticipantJoinHandler(Context<EventActivity<MeetingParticipantJoinValue>> context, CancellationToken cancellationToken = default); |
| 32 | |
| 33 | /// <summary> |
| 34 | /// Delegate for handling meeting participant leave event activities. |
| 35 | /// </summary> |
| 36 | /// <param name="context">The context for the event activity, providing access to the activity data and bot application.</param> |
| 37 | /// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param> |
| 38 | /// <returns>A task representing the asynchronous operation.</returns> |
| 39 | public delegate Task MeetingParticipantLeaveHandler(Context<EventActivity<MeetingParticipantLeaveValue>> context, CancellationToken cancellationToken = default); |
| 40 | |
| 41 | /// <summary> |
| 42 | /// Extension methods for registering meeting event activity handlers. |
| 43 | /// </summary> |
| 44 | public static class MeetingExtensions |
| 45 | { |
| 46 | /// <summary> |
| 47 | /// Registers a handler for meeting start event activities. |
| 48 | /// </summary> |
| 49 | /// <remarks> |
| 50 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 51 | /// </remarks> |
| 52 | /// <param name="app">The Teams bot application.</param> |
| 53 | /// <param name="handler">The handler to register.</param> |
| 54 | /// <returns>The updated Teams bot application.</returns> |
| 55 | public static TeamsBotApplication OnMeetingStart(this TeamsBotApplication app, MeetingStartHandler handler) |
| 56 | { |
| 57 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 58 | app.Router.Register(new Route<EventActivity> |
| 59 | { |
| 60 | Name = string.Join("/", TeamsActivityType.Event, EventNames.MeetingStart), |
| 61 | Selector = activity => activity.Name == EventNames.MeetingStart, |
| 62 | Handler = async (ctx, cancellationToken) => |
| 63 | { |
| 64 | EventActivity<MeetingStartValue> typedActivity = new(ctx.Activity); |
| 65 | Context<EventActivity<MeetingStartValue>> typedContext = new(ctx.TeamsBotApplication, typedActivity); |
| 66 | await handler(typedContext, cancellationToken).ConfigureAwait(false); |
| 67 | } |
| 68 | }); |
| 69 | |
| 70 | return app; |
| 71 | } |
| 72 | |
| 73 | /// <summary> |
| 74 | /// Registers a handler for meeting end event activities. |
| 75 | /// </summary> |
| 76 | /// <remarks> |
| 77 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 78 | /// </remarks> |
| 79 | /// <param name="app">The Teams bot application.</param> |
| 80 | /// <param name="handler">The handler to register.</param> |
| 81 | /// <returns>The updated Teams bot application.</returns> |
| 82 | public static TeamsBotApplication OnMeetingEnd(this TeamsBotApplication app, MeetingEndHandler handler) |
| 83 | { |
| 84 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 85 | app.Router.Register(new Route<EventActivity> |
| 86 | { |
| 87 | Name = string.Join("/", TeamsActivityType.Event, EventNames.MeetingEnd), |
| 88 | Selector = activity => activity.Name == EventNames.MeetingEnd, |
| 89 | Handler = async (ctx, cancellationToken) => |
| 90 | { |
| 91 | EventActivity<MeetingEndValue> typedActivity = new(ctx.Activity); |
| 92 | Context<EventActivity<MeetingEndValue>> typedContext = new(ctx.TeamsBotApplication, typedActivity); |
| 93 | await handler(typedContext, cancellationToken).ConfigureAwait(false); |
| 94 | } |
| 95 | }); |
| 96 | |
| 97 | return app; |
| 98 | } |
| 99 | |
| 100 | /// <summary> |
| 101 | /// Registers a handler for meeting participant join event activities. |
| 102 | /// </summary> |
| 103 | /// <remarks> |
| 104 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 105 | /// </remarks> |
| 106 | /// <param name="app">The Teams bot application.</param> |
| 107 | /// <param name="handler">The handler to register.</param> |
| 108 | /// <returns>The updated Teams bot application.</returns> |
| 109 | public static TeamsBotApplication OnMeetingParticipantJoin(this TeamsBotApplication app, MeetingParticipantJoinHandler handler) |
| 110 | { |
| 111 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 112 | app.Router.Register(new Route<EventActivity> |
| 113 | { |
| 114 | Name = string.Join("/", TeamsActivityType.Event, EventNames.MeetingParticipantJoin), |
| 115 | Selector = activity => activity.Name == EventNames.MeetingParticipantJoin, |
| 116 | Handler = async (ctx, cancellationToken) => |
| 117 | { |
| 118 | EventActivity<MeetingParticipantJoinValue> typedActivity = new(ctx.Activity); |
| 119 | Context<EventActivity<MeetingParticipantJoinValue>> typedContext = new(ctx.TeamsBotApplication, typedActivity); |
| 120 | await handler(typedContext, cancellationToken).ConfigureAwait(false); |
| 121 | } |
| 122 | }); |
| 123 | |
| 124 | return app; |
| 125 | } |
| 126 | |
| 127 | /// <summary> |
| 128 | /// Registers a handler for meeting participant leave event activities. |
| 129 | /// </summary> |
| 130 | /// <remarks> |
| 131 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 132 | /// </remarks> |
| 133 | /// <param name="app">The Teams bot application.</param> |
| 134 | /// <param name="handler">The handler to register.</param> |
| 135 | /// <returns>The updated Teams bot application.</returns> |
| 136 | public static TeamsBotApplication OnMeetingParticipantLeave(this TeamsBotApplication app, MeetingParticipantLeaveHandler handler) |
| 137 | { |
| 138 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 139 | app.Router.Register(new Route<EventActivity> |
| 140 | { |
| 141 | Name = string.Join("/", TeamsActivityType.Event, EventNames.MeetingParticipantLeave), |
| 142 | Selector = activity => activity.Name == EventNames.MeetingParticipantLeave, |
| 143 | Handler = async (ctx, cancellationToken) => |
| 144 | { |
| 145 | EventActivity<MeetingParticipantLeaveValue> typedActivity = new(ctx.Activity); |
| 146 | Context<EventActivity<MeetingParticipantLeaveValue>> typedContext = new(ctx.TeamsBotApplication, typedActivity); |
| 147 | await handler(typedContext, cancellationToken).ConfigureAwait(false); |
| 148 | } |
| 149 | }); |
| 150 | |
| 151 | return app; |
| 152 | } |
| 153 | } |
| 154 | |