microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
Libraries/Microsoft.Teams.Apps/Activities/Activity.cs
127lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Api.Activities; |
| 5 | using Microsoft.Teams.Apps.Routing; |
| 6 | |
| 7 | namespace Microsoft.Teams.Apps.Activities; |
| 8 | |
| 9 | [AttributeUsage(AttributeTargets.Method, Inherited = true)] |
| 10 | public 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 | |
| 19 | public 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>, Task<object?>> handler) |
| 33 | { |
| 34 | app.Router.Register(handler); |
| 35 | return app; |
| 36 | } |
| 37 | |
| 38 | public static App OnActivity(this App app, ActivityType type, Func<IContext<IActivity>, Task> handler) |
| 39 | { |
| 40 | app.Router.Register(new Route() |
| 41 | { |
| 42 | Name = type, |
| 43 | Type = app.Status is null ? RouteType.System : RouteType.User, |
| 44 | Handler = async (context) => |
| 45 | { |
| 46 | await handler(context); |
| 47 | return null; |
| 48 | }, |
| 49 | Selector = (activity) => activity.Type.Equals(type), |
| 50 | }); |
| 51 | |
| 52 | return app; |
| 53 | } |
| 54 | |
| 55 | public static App OnActivity(this App app, ActivityType type, Func<IContext<IActivity>, Task<object?>> handler) |
| 56 | { |
| 57 | app.Router.Register(new Route() |
| 58 | { |
| 59 | Name = type, |
| 60 | Type = app.Status is null ? RouteType.System : RouteType.User, |
| 61 | Handler = handler, |
| 62 | Selector = (activity) => activity.Type.Equals(type), |
| 63 | }); |
| 64 | |
| 65 | return app; |
| 66 | } |
| 67 | |
| 68 | public static App OnActivity<TActivity>(this App app, Func<IContext<TActivity>, Task> handler) where TActivity : IActivity |
| 69 | { |
| 70 | app.Router.Register(new Route() |
| 71 | { |
| 72 | Name = "activity", |
| 73 | Type = app.Status is null ? RouteType.System : RouteType.User, |
| 74 | Handler = async (context) => |
| 75 | { |
| 76 | await handler(context.ToActivityType<TActivity>()); |
| 77 | return null; |
| 78 | }, |
| 79 | Selector = (activity) => activity.GetType() == typeof(TActivity), |
| 80 | }); |
| 81 | |
| 82 | return app; |
| 83 | } |
| 84 | |
| 85 | public static App OnActivity<TActivity>(this App app, Func<IContext<TActivity>, Task<object?>> handler) where TActivity : IActivity |
| 86 | { |
| 87 | app.Router.Register(new Route() |
| 88 | { |
| 89 | Name = "activity", |
| 90 | Type = app.Status is null ? RouteType.System : RouteType.User, |
| 91 | Handler = (context) => handler(context.ToActivityType<TActivity>()), |
| 92 | Selector = (activity) => activity.GetType() == typeof(TActivity), |
| 93 | }); |
| 94 | |
| 95 | return app; |
| 96 | } |
| 97 | |
| 98 | public static App OnActivity(this App app, Func<IActivity, bool> select, Func<IContext<IActivity>, Task> handler) |
| 99 | { |
| 100 | app.Router.Register(new Route() |
| 101 | { |
| 102 | Name = "activity", |
| 103 | Type = app.Status is null ? RouteType.System : RouteType.User, |
| 104 | Selector = select, |
| 105 | Handler = async (context) => |
| 106 | { |
| 107 | await handler(context); |
| 108 | return null; |
| 109 | } |
| 110 | }); |
| 111 | |
| 112 | return app; |
| 113 | } |
| 114 | |
| 115 | public static App OnActivity(this App app, Func<IActivity, bool> select, Func<IContext<IActivity>, Task<object?>> handler) |
| 116 | { |
| 117 | app.Router.Register(new Route() |
| 118 | { |
| 119 | Name = "activity", |
| 120 | Type = app.Status is null ? RouteType.System : RouteType.User, |
| 121 | Selector = select, |
| 122 | Handler = handler |
| 123 | }); |
| 124 | |
| 125 | return app; |
| 126 | } |
| 127 | } |