microsoft/teams.net

Public

mirrored from https://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
kavin/conversation-fix

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

core/src/Microsoft.Teams.Bot.Apps/Routing/Route.cs

109lines · modecode

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