microsoft/teams.net

Public

mirrored fromhttps://github.com/microsoft/teams.netAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
samples/migration-bot

Branches

Tags

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

Clone

HTTPS

Download ZIP

Libraries/Microsoft.Teams.Apps/Activities/Activity.cs

234lines · modecode

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4using Microsoft.Teams.Api.Activities;
5using Microsoft.Teams.Apps.Routing;
6
7namespace Microsoft.Teams.Apps.Activities;
8
9[AttributeUsage(AttributeTargets.Method, Inherited = true)]
10public class ActivityAttribute(string? name = null, Type? type = null) : Attribute
11{
12 public readonly ActivityType? Name = name is not null ? new(name) : null;
13 public readonly Type Type = type ?? typeof(Activity);
14
15 public virtual bool Select(IActivity activity) => Name is null || Name.Equals(activity.Type);
16 public virtual object Coerce(IContext<IActivity> context) => context.ToActivityType<Activity>();
17}
18
19public static partial class AppActivityExtensions
20{
21 public static App OnActivity(this App app, Func<IContext<IActivity>, Task> handler)
22 {
23 app.Router.Register(async (context) =>
24 {
25 await handler(context);
26 return null;
27 });
28
29 return app;
30 }
31
32 public static App OnActivity(this App app, Func<IContext<IActivity>, CancellationToken, Task> handler)
33 {
34 app.Router.Register(async (context) =>
35 {
36 await handler(context, context.CancellationToken);
37 return null;
38 });
39
40 return app;
41 }
42
43 public static App OnActivity(this App app, Func<IContext<IActivity>, Task<object?>> handler)
44 {
45 app.Router.Register(handler);
46 return app;
47 }
48
49 public static App OnActivity(this App app, Func<IContext<IActivity>, CancellationToken, Task<object?>> handler)
50 {
51 app.Router.Register((context) => handler(context, context.CancellationToken));
52 return app;
53 }
54
55 public static App OnActivity(this App app, ActivityType type, Func<IContext<IActivity>, Task> handler)
56 {
57 app.Router.Register(new Route()
58 {
59 Name = type,
60 Type = app.Status is null ? RouteType.System : RouteType.User,
61 Handler = async (context) =>
62 {
63 await handler(context);
64 return null;
65 },
66 Selector = (activity) => activity.Type.Equals(type),
67 });
68
69 return app;
70 }
71
72 public static App OnActivity(this App app, ActivityType type, Func<IContext<IActivity>, CancellationToken, Task> handler)
73 {
74 app.Router.Register(new Route()
75 {
76 Name = type,
77 Type = app.Status is null ? RouteType.System : RouteType.User,
78 Handler = async (context) =>
79 {
80 await handler(context, context.CancellationToken);
81 return null;
82 },
83 Selector = (activity) => activity.Type.Equals(type),
84 });
85
86 return app;
87 }
88
89 public static App OnActivity(this App app, ActivityType type, Func<IContext<IActivity>, Task<object?>> handler)
90 {
91 app.Router.Register(new Route()
92 {
93 Name = type,
94 Type = app.Status is null ? RouteType.System : RouteType.User,
95 Handler = handler,
96 Selector = (activity) => activity.Type.Equals(type),
97 });
98
99 return app;
100 }
101
102 public static App OnActivity(this App app, ActivityType type, Func<IContext<IActivity>, CancellationToken, Task<object?>> handler)
103 {
104 app.Router.Register(new Route()
105 {
106 Name = type,
107 Type = app.Status is null ? RouteType.System : RouteType.User,
108 Handler = (context) => handler(context, context.CancellationToken),
109 Selector = (activity) => activity.Type.Equals(type),
110 });
111
112 return app;
113 }
114
115 public static App OnActivity<TActivity>(this App app, Func<IContext<TActivity>, Task> handler) where TActivity : IActivity
116 {
117 app.Router.Register(new Route()
118 {
119 Name = "activity",
120 Type = app.Status is null ? RouteType.System : RouteType.User,
121 Handler = async (context) =>
122 {
123 await handler(context.ToActivityType<TActivity>());
124 return null;
125 },
126 Selector = (activity) => activity.GetType() == typeof(TActivity),
127 });
128
129 return app;
130 }
131
132 public static App OnActivity<TActivity>(this App app, Func<IContext<TActivity>, CancellationToken, Task> handler) where TActivity : IActivity
133 {
134 app.Router.Register(new Route()
135 {
136 Name = "activity",
137 Type = app.Status is null ? RouteType.System : RouteType.User,
138 Handler = async (context) =>
139 {
140 await handler(context.ToActivityType<TActivity>(), context.CancellationToken);
141 return null;
142 },
143 Selector = (activity) => activity.GetType() == typeof(TActivity),
144 });
145
146 return app;
147 }
148
149 public static App OnActivity<TActivity>(this App app, Func<IContext<TActivity>, Task<object?>> handler) where TActivity : IActivity
150 {
151 app.Router.Register(new Route()
152 {
153 Name = "activity",
154 Type = app.Status is null ? RouteType.System : RouteType.User,
155 Handler = (context) => handler(context.ToActivityType<TActivity>()),
156 Selector = (activity) => activity.GetType() == typeof(TActivity),
157 });
158
159 return app;
160 }
161
162 public static App OnActivity<TActivity>(this App app, Func<IContext<TActivity>, CancellationToken, Task<object?>> handler) where TActivity : IActivity
163 {
164 app.Router.Register(new Route()
165 {
166 Name = "activity",
167 Type = app.Status is null ? RouteType.System : RouteType.User,
168 Handler = (context) => handler(context.ToActivityType<TActivity>(), context.CancellationToken),
169 Selector = (activity) => activity.GetType() == typeof(TActivity),
170 });
171
172 return app;
173 }
174
175 public static App OnActivity(this App app, Func<IActivity, bool> select, Func<IContext<IActivity>, Task> handler)
176 {
177 app.Router.Register(new Route()
178 {
179 Name = "activity",
180 Type = app.Status is null ? RouteType.System : RouteType.User,
181 Selector = select,
182 Handler = async (context) =>
183 {
184 await handler(context);
185 return null;
186 }
187 });
188
189 return app;
190 }
191
192 public static App OnActivity(this App app, Func<IActivity, bool> select, Func<IContext<IActivity>, CancellationToken, Task> handler)
193 {
194 app.Router.Register(new Route()
195 {
196 Name = "activity",
197 Type = app.Status is null ? RouteType.System : RouteType.User,
198 Selector = select,
199 Handler = async (context) =>
200 {
201 await handler(context, context.CancellationToken);
202 return null;
203 }
204 });
205
206 return app;
207 }
208
209 public static App OnActivity(this App app, Func<IActivity, bool> select, Func<IContext<IActivity>, Task<object?>> handler)
210 {
211 app.Router.Register(new Route()
212 {
213 Name = "activity",
214 Type = app.Status is null ? RouteType.System : RouteType.User,
215 Selector = select,
216 Handler = handler
217 });
218
219 return app;
220 }
221
222 public static App OnActivity(this App app, Func<IActivity, bool> select, Func<IContext<IActivity>, CancellationToken, Task<object?>> handler)
223 {
224 app.Router.Register(new Route()
225 {
226 Name = "activity",
227 Type = app.Status is null ? RouteType.System : RouteType.User,
228 Selector = select,
229 Handler = (context) => handler(context, context.CancellationToken)
230 });
231
232 return app;
233 }
234}