microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Handlers/InstallUpdateHandler.cs
96lines · 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 installation update activities. |
| 11 | /// </summary> |
| 12 | /// <param name="context"></param> |
| 13 | /// <param name="cancellationToken"></param> |
| 14 | /// <returns></returns> |
| 15 | public delegate Task InstallUpdateHandler(Context<InstallUpdateActivity> context, CancellationToken cancellationToken = default); |
| 16 | |
| 17 | /// <summary> |
| 18 | /// Extension methods for registering installation update activity handlers. |
| 19 | /// </summary> |
| 20 | public static class InstallUpdateExtensions |
| 21 | { |
| 22 | /// <summary> |
| 23 | /// Registers a handler for installation update 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"></param> |
| 29 | /// <param name="handler"></param> |
| 30 | /// <returns></returns> |
| 31 | public static TeamsBotApplication OnInstallUpdate(this TeamsBotApplication app, InstallUpdateHandler handler) |
| 32 | { |
| 33 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 34 | app.Router.Register(new Route<InstallUpdateActivity> |
| 35 | { |
| 36 | Name = TeamsActivityType.InstallationUpdate, |
| 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 | /// <summary> |
| 48 | /// Registers a handler for installation add activities. |
| 49 | /// </summary> |
| 50 | /// <remarks> |
| 51 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 52 | /// </remarks> |
| 53 | /// <param name="app"></param> |
| 54 | /// <param name="handler"></param> |
| 55 | /// <returns></returns> |
| 56 | public static TeamsBotApplication OnInstall(this TeamsBotApplication app, InstallUpdateHandler handler) |
| 57 | { |
| 58 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 59 | app.Router.Register(new Route<InstallUpdateActivity> |
| 60 | { |
| 61 | Name = string.Join("/", [TeamsActivityType.InstallationUpdate, InstallUpdateActions.Add]), |
| 62 | Selector = activity => activity.Action == InstallUpdateActions.Add, |
| 63 | Handler = async (ctx, cancellationToken) => |
| 64 | { |
| 65 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 66 | } |
| 67 | }); |
| 68 | |
| 69 | return app; |
| 70 | } |
| 71 | |
| 72 | /// <summary> |
| 73 | /// Registers a handler for installation remove activities. |
| 74 | /// </summary> |
| 75 | /// <remarks> |
| 76 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 77 | /// </remarks> |
| 78 | /// <param name="app"></param> |
| 79 | /// <param name="handler"></param> |
| 80 | /// <returns></returns> |
| 81 | public static TeamsBotApplication OnUnInstall(this TeamsBotApplication app, InstallUpdateHandler handler) |
| 82 | { |
| 83 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 84 | app.Router.Register(new Route<InstallUpdateActivity> |
| 85 | { |
| 86 | Name = string.Join("/", [TeamsActivityType.InstallationUpdate, InstallUpdateActions.Remove]), |
| 87 | Selector = activity => activity.Action == InstallUpdateActions.Remove, |
| 88 | Handler = async (ctx, cancellationToken) => |
| 89 | { |
| 90 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 91 | } |
| 92 | }); |
| 93 | |
| 94 | return app; |
| 95 | } |
| 96 | } |
| 97 | |