microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Routing/Route.cs
110lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Bot.Apps.Handlers; |
| 5 | using Microsoft.Teams.Bot.Apps.Schema; |
| 6 | |
| 7 | namespace Microsoft.Teams.Bot.Apps.Routing; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Base class for routes, providing non-generic access to route functionality |
| 11 | /// </summary> |
| 12 | public abstract class RouteBase |
| 13 | { |
| 14 | /// <summary> |
| 15 | /// Gets or sets the name of the route |
| 16 | /// </summary> |
| 17 | public abstract string Name { get; set; } |
| 18 | |
| 19 | /// <summary> |
| 20 | /// Determines if the route matches the given activity |
| 21 | /// </summary> |
| 22 | /// <param name="activity"></param> |
| 23 | /// <returns></returns> |
| 24 | public abstract bool Matches(TeamsActivity activity); |
| 25 | |
| 26 | /// <summary> |
| 27 | /// Invokes the route handler if the activity matches the expected type |
| 28 | /// </summary> |
| 29 | /// <param name="ctx"></param> |
| 30 | /// <param name="cancellationToken"></param> |
| 31 | /// <returns></returns> |
| 32 | public abstract Task InvokeRoute(Context<TeamsActivity> ctx, CancellationToken cancellationToken = default); |
| 33 | |
| 34 | /// <summary> |
| 35 | /// Invokes the route handler if the activity matches the expected type and returns a response |
| 36 | /// </summary> |
| 37 | /// <param name="ctx"></param> |
| 38 | /// <param name="cancellationToken"></param> |
| 39 | /// <returns></returns> |
| 40 | public abstract Task<InvokeResponse> InvokeRouteWithReturn(Context<TeamsActivity> ctx, CancellationToken cancellationToken = default); |
| 41 | } |
| 42 | |
| 43 | /// <summary> |
| 44 | /// Represents a route for handling Teams activities |
| 45 | /// </summary> |
| 46 | public class Route<TActivity> : RouteBase where TActivity : TeamsActivity |
| 47 | { |
| 48 | private string _name = string.Empty; |
| 49 | |
| 50 | /// <summary> |
| 51 | /// Gets or sets the name of the route |
| 52 | /// </summary> |
| 53 | public override string Name |
| 54 | { |
| 55 | get => _name; |
| 56 | set => _name = value; |
| 57 | } |
| 58 | |
| 59 | /// <summary> |
| 60 | /// Predicate function to determine if this route should handle the activity |
| 61 | /// </summary> |
| 62 | public Func<TActivity, bool> Selector { get; set; } = _ => true; |
| 63 | |
| 64 | /// <summary> |
| 65 | /// Handler function to process the activity |
| 66 | /// </summary> |
| 67 | public Func<Context<TActivity>, CancellationToken, Task>? Handler { get; set; } |
| 68 | |
| 69 | /// <summary> |
| 70 | /// Handler function to process the activity and return a response |
| 71 | /// </summary> |
| 72 | public Func<Context<TActivity>, CancellationToken, Task<InvokeResponse>>? HandlerWithReturn { get; set; } |
| 73 | |
| 74 | /// <summary> |
| 75 | /// Determines if the route matches the given activity |
| 76 | /// </summary> |
| 77 | /// <param name="activity"></param> |
| 78 | /// <returns></returns> |
| 79 | public override bool Matches(TeamsActivity activity) |
| 80 | { |
| 81 | ArgumentNullException.ThrowIfNull(activity); |
| 82 | return activity is TActivity && Selector((TActivity)activity); |
| 83 | } |
| 84 | |
| 85 | /// <summary> |
| 86 | /// Invokes the route handler if the activity matches the expected type |
| 87 | /// </summary> |
| 88 | /// <param name="ctx"></param> |
| 89 | /// <param name="cancellationToken"></param> |
| 90 | /// <returns></returns> |
| 91 | public override async Task InvokeRoute(Context<TeamsActivity> ctx, CancellationToken cancellationToken = default) |
| 92 | { |
| 93 | ArgumentNullException.ThrowIfNull(ctx); |
| 94 | Context<TActivity> typedContext = new(ctx.TeamsBotApplication, (TActivity)ctx.Activity); |
| 95 | await Handler!(typedContext, cancellationToken).ConfigureAwait(false); |
| 96 | } |
| 97 | |
| 98 | /// <summary> |
| 99 | /// Invokes the route handler if the activity matches the expected type and returns a response |
| 100 | /// </summary> |
| 101 | /// <param name="ctx"></param> |
| 102 | /// <param name="cancellationToken"></param> |
| 103 | /// <returns></returns> |
| 104 | public override async Task<InvokeResponse> InvokeRouteWithReturn(Context<TeamsActivity> ctx, CancellationToken cancellationToken = default) |
| 105 | { |
| 106 | ArgumentNullException.ThrowIfNull(ctx); |
| 107 | Context<TActivity> typedContext = new(ctx.TeamsBotApplication, (TActivity)ctx.Activity); |
| 108 | return await HandlerWithReturn!(typedContext, cancellationToken).ConfigureAwait(false); |
| 109 | } |
| 110 | } |
| 111 | |