microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Handlers/MessageExtension/MessageExtensionHandler.cs
307lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using System.Text.Json; |
| 5 | using Microsoft.Teams.Bot.Apps.Routing; |
| 6 | using Microsoft.Teams.Bot.Apps.Schema; |
| 7 | |
| 8 | namespace Microsoft.Teams.Bot.Apps.Handlers.MessageExtension; |
| 9 | |
| 10 | /// <summary> |
| 11 | /// Delegate for handling message extension query invoke activities. |
| 12 | /// </summary> |
| 13 | public delegate Task<InvokeResponse<MessageExtensionResponse>> MessageExtensionQueryHandler(Context<InvokeActivity<MessageExtensionQuery>> context, CancellationToken cancellationToken = default); |
| 14 | |
| 15 | /// <summary> |
| 16 | /// Delegate for handling message extension submit action invoke activities. |
| 17 | /// Can return either a TaskModuleResponse or MessageExtensionResponse. |
| 18 | /// </summary> |
| 19 | public delegate Task<InvokeResponse<MessageExtensionActionResponse>> MessageExtensionSubmitActionHandler(Context<InvokeActivity<MessageExtensionAction>> context, CancellationToken cancellationToken = default); |
| 20 | |
| 21 | /// <summary> |
| 22 | /// Delegate for handling message extension fetch task invoke activities. |
| 23 | /// Can return either a TaskModuleResponse or MessageExtensionResponse. |
| 24 | /// </summary> |
| 25 | public delegate Task<InvokeResponse<MessageExtensionActionResponse>> MessageExtensionFetchTaskHandler(Context<InvokeActivity<MessageExtensionAction>> context, CancellationToken cancellationToken = default); |
| 26 | |
| 27 | /// <summary> |
| 28 | /// Delegate for handling message extension query link invoke activities. |
| 29 | /// </summary> |
| 30 | public delegate Task<InvokeResponse<MessageExtensionResponse>> MessageExtensionQueryLinkHandler(Context<InvokeActivity<MessageExtensionQueryLink>> context, CancellationToken cancellationToken = default); |
| 31 | |
| 32 | /// <summary> |
| 33 | /// Delegate for handling message extension anonymous query link invoke activities. |
| 34 | /// </summary> |
| 35 | public delegate Task<InvokeResponse<MessageExtensionResponse>> MessageExtensionAnonQueryLinkHandler(Context<InvokeActivity<MessageExtensionQueryLink>> context, CancellationToken cancellationToken = default); |
| 36 | |
| 37 | /// <summary> |
| 38 | /// Delegate for handling message extension select item invoke activities. |
| 39 | /// </summary> |
| 40 | public delegate Task<InvokeResponse<MessageExtensionResponse>> MessageExtensionSelectItemHandler(Context<InvokeActivity<JsonElement>> context, CancellationToken cancellationToken = default); |
| 41 | |
| 42 | /// <summary> |
| 43 | /// Delegate for handling message extension query setting URL invoke activities. |
| 44 | /// </summary> |
| 45 | public delegate Task<InvokeResponse<MessageExtensionResponse>> MessageExtensionQuerySettingUrlHandler(Context<InvokeActivity<MessageExtensionQuery>> context, CancellationToken cancellationToken = default); |
| 46 | |
| 47 | /* |
| 48 | /// <summary> |
| 49 | /// Delegate for handling message extension card button clicked invoke activities. |
| 50 | /// </summary> |
| 51 | public delegate Task<InvokeResponse> MessageExtensionCardButtonClickedHandler(Context<InvokeActivity<JsonElement>> context, CancellationToken cancellationToken = default); |
| 52 | |
| 53 | /// <summary> |
| 54 | /// Delegate for handling message extension setting invoke activities. |
| 55 | /// </summary> |
| 56 | public delegate Task<InvokeResponse> MessageExtensionSettingHandler(Context<InvokeActivity<Query>> context, CancellationToken cancellationToken = default); |
| 57 | */ |
| 58 | |
| 59 | /// <summary> |
| 60 | /// Extension methods for registering message extension invoke handlers. |
| 61 | /// </summary> |
| 62 | public static class MessageExtensionExtensions |
| 63 | { |
| 64 | //TODO : add msg ext prefix to handlers ? very confusing right now as we have both onFetchTask and onTaskFetch. |
| 65 | //onSubmitAction is confusing as it is similar to adaptive cards |
| 66 | |
| 67 | /// <summary> |
| 68 | /// Registers a handler for message extension query invoke activities. |
| 69 | /// Cannot be combined with <see cref="InvokeExtensions.OnInvoke"/>. |
| 70 | /// </summary> |
| 71 | /// <remarks> |
| 72 | /// Breaking change: previously a catch-all invoke handler could be registered alongside specific invoke handlers. This combination now throws at registration time. |
| 73 | /// </remarks> |
| 74 | /// <param name="app">The Teams bot application.</param> |
| 75 | /// <param name="handler">The handler to register.</param> |
| 76 | /// <returns>The updated Teams bot application.</returns> |
| 77 | public static TeamsBotApplication OnQuery(this TeamsBotApplication app, MessageExtensionQueryHandler handler) |
| 78 | { |
| 79 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 80 | app.Router.Register(new Route<InvokeActivity> |
| 81 | { |
| 82 | Name = string.Join("/", TeamsActivityType.Invoke, InvokeNames.MessageExtensionQuery), |
| 83 | Selector = activity => activity.Name == InvokeNames.MessageExtensionQuery, |
| 84 | HandlerWithReturn = async (ctx, cancellationToken) => |
| 85 | { |
| 86 | InvokeActivity<MessageExtensionQuery> typedActivity = new(ctx.Activity); |
| 87 | Context<InvokeActivity<MessageExtensionQuery>> typedContext = new(ctx.TeamsBotApplication, typedActivity); |
| 88 | return await handler(typedContext, cancellationToken).ConfigureAwait(false); |
| 89 | } |
| 90 | }); |
| 91 | |
| 92 | return app; |
| 93 | } |
| 94 | |
| 95 | /// <summary> |
| 96 | /// Registers a handler for message extension submit action invoke activities. |
| 97 | /// Cannot be combined with <see cref="InvokeExtensions.OnInvoke"/>. |
| 98 | /// </summary> |
| 99 | /// <remarks> |
| 100 | /// Breaking change: previously a catch-all invoke handler could be registered alongside specific invoke handlers. This combination now throws at registration time. |
| 101 | /// </remarks> |
| 102 | /// <param name="app">The Teams bot application.</param> |
| 103 | /// <param name="handler">The handler to register.</param> |
| 104 | /// <returns>The updated Teams bot application.</returns> |
| 105 | public static TeamsBotApplication OnSubmitAction(this TeamsBotApplication app, MessageExtensionSubmitActionHandler handler) |
| 106 | { |
| 107 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 108 | app.Router.Register(new Route<InvokeActivity> |
| 109 | { |
| 110 | Name = string.Join("/", TeamsActivityType.Invoke, InvokeNames.MessageExtensionSubmitAction), |
| 111 | Selector = activity => activity.Name == InvokeNames.MessageExtensionSubmitAction, |
| 112 | HandlerWithReturn = async (ctx, cancellationToken) => |
| 113 | { |
| 114 | InvokeActivity<MessageExtensionAction> typedActivity = new(ctx.Activity); |
| 115 | Context<InvokeActivity<MessageExtensionAction>> typedContext = new(ctx.TeamsBotApplication, typedActivity); |
| 116 | return await handler(typedContext, cancellationToken).ConfigureAwait(false); |
| 117 | } |
| 118 | }); |
| 119 | |
| 120 | return app; |
| 121 | } |
| 122 | |
| 123 | /// <summary> |
| 124 | /// Registers a handler for message extension query link invoke activities. |
| 125 | /// Cannot be combined with <see cref="InvokeExtensions.OnInvoke"/>. |
| 126 | /// </summary> |
| 127 | /// <remarks> |
| 128 | /// Breaking change: previously a catch-all invoke handler could be registered alongside specific invoke handlers. This combination now throws at registration time. |
| 129 | /// </remarks> |
| 130 | /// <param name="app">The Teams bot application.</param> |
| 131 | /// <param name="handler">The handler to register.</param> |
| 132 | /// <returns>The updated Teams bot application.</returns> |
| 133 | public static TeamsBotApplication OnQueryLink(this TeamsBotApplication app, MessageExtensionQueryLinkHandler handler) |
| 134 | { |
| 135 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 136 | app.Router.Register(new Route<InvokeActivity> |
| 137 | { |
| 138 | Name = string.Join("/", TeamsActivityType.Invoke, InvokeNames.MessageExtensionQueryLink), |
| 139 | Selector = activity => activity.Name == InvokeNames.MessageExtensionQueryLink, |
| 140 | HandlerWithReturn = async (ctx, cancellationToken) => |
| 141 | { |
| 142 | InvokeActivity<MessageExtensionQueryLink> typedActivity = new(ctx.Activity); |
| 143 | Context<InvokeActivity<MessageExtensionQueryLink>> typedContext = new(ctx.TeamsBotApplication, typedActivity); |
| 144 | return await handler(typedContext, cancellationToken).ConfigureAwait(false); |
| 145 | } |
| 146 | }); |
| 147 | |
| 148 | return app; |
| 149 | } |
| 150 | |
| 151 | /// <summary> |
| 152 | /// Registers a handler for message extension anonymous query link invoke activities. |
| 153 | /// Cannot be combined with <see cref="InvokeExtensions.OnInvoke"/>. |
| 154 | /// </summary> |
| 155 | /// <remarks> |
| 156 | /// Breaking change: previously a catch-all invoke handler could be registered alongside specific invoke handlers. This combination now throws at registration time. |
| 157 | /// </remarks> |
| 158 | /// <param name="app">The Teams bot application.</param> |
| 159 | /// <param name="handler">The handler to register.</param> |
| 160 | /// <returns>The updated Teams bot application.</returns> |
| 161 | public static TeamsBotApplication OnAnonQueryLink(this TeamsBotApplication app, MessageExtensionAnonQueryLinkHandler handler) |
| 162 | { |
| 163 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 164 | app.Router.Register(new Route<InvokeActivity> |
| 165 | { |
| 166 | Name = string.Join("/", TeamsActivityType.Invoke, InvokeNames.MessageExtensionAnonQueryLink), |
| 167 | Selector = activity => activity.Name == InvokeNames.MessageExtensionAnonQueryLink, |
| 168 | HandlerWithReturn = async (ctx, cancellationToken) => |
| 169 | { |
| 170 | InvokeActivity<MessageExtensionQueryLink> typedActivity = new(ctx.Activity); |
| 171 | Context<InvokeActivity<MessageExtensionQueryLink>> typedContext = new(ctx.TeamsBotApplication, typedActivity); |
| 172 | return await handler(typedContext, cancellationToken).ConfigureAwait(false); |
| 173 | } |
| 174 | }); |
| 175 | |
| 176 | return app; |
| 177 | } |
| 178 | |
| 179 | /// <summary> |
| 180 | /// Registers a handler for message extension fetch task invoke activities. |
| 181 | /// Cannot be combined with <see cref="InvokeExtensions.OnInvoke"/>. |
| 182 | /// </summary> |
| 183 | /// <remarks> |
| 184 | /// Breaking change: previously a catch-all invoke handler could be registered alongside specific invoke handlers. This combination now throws at registration time. |
| 185 | /// </remarks> |
| 186 | /// <param name="app">The Teams bot application.</param> |
| 187 | /// <param name="handler">The handler to register.</param> |
| 188 | /// <returns>The updated Teams bot application.</returns> |
| 189 | public static TeamsBotApplication OnFetchTask(this TeamsBotApplication app, MessageExtensionFetchTaskHandler handler) |
| 190 | { |
| 191 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 192 | app.Router.Register(new Route<InvokeActivity> |
| 193 | { |
| 194 | Name = string.Join("/", TeamsActivityType.Invoke, InvokeNames.MessageExtensionFetchTask), |
| 195 | Selector = activity => activity.Name == InvokeNames.MessageExtensionFetchTask, |
| 196 | HandlerWithReturn = async (ctx, cancellationToken) => |
| 197 | { |
| 198 | InvokeActivity<MessageExtensionAction> typedActivity = new(ctx.Activity); |
| 199 | Context<InvokeActivity<MessageExtensionAction>> typedContext = new(ctx.TeamsBotApplication, typedActivity); |
| 200 | return await handler(typedContext, cancellationToken).ConfigureAwait(false); |
| 201 | } |
| 202 | }); |
| 203 | |
| 204 | return app; |
| 205 | } |
| 206 | |
| 207 | /// <summary> |
| 208 | /// Registers a handler for message extension select item invoke activities. |
| 209 | /// Cannot be combined with <see cref="InvokeExtensions.OnInvoke"/>. |
| 210 | /// </summary> |
| 211 | /// <remarks> |
| 212 | /// Breaking change: previously a catch-all invoke handler could be registered alongside specific invoke handlers. This combination now throws at registration time. |
| 213 | /// </remarks> |
| 214 | /// <param name="app">The Teams bot application.</param> |
| 215 | /// <param name="handler">The handler to register.</param> |
| 216 | /// <returns>The updated Teams bot application.</returns> |
| 217 | public static TeamsBotApplication OnSelectItem(this TeamsBotApplication app, MessageExtensionSelectItemHandler handler) |
| 218 | { |
| 219 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 220 | app.Router.Register(new Route<InvokeActivity> |
| 221 | { |
| 222 | Name = string.Join("/", TeamsActivityType.Invoke, InvokeNames.MessageExtensionSelectItem), |
| 223 | Selector = activity => activity.Name == InvokeNames.MessageExtensionSelectItem, |
| 224 | HandlerWithReturn = async (ctx, cancellationToken) => |
| 225 | { |
| 226 | InvokeActivity<JsonElement> typedActivity = new(ctx.Activity); |
| 227 | Context<InvokeActivity<JsonElement>> typedContext = new(ctx.TeamsBotApplication, typedActivity); |
| 228 | return await handler(typedContext, cancellationToken).ConfigureAwait(false); |
| 229 | } |
| 230 | }); |
| 231 | |
| 232 | return app; |
| 233 | } |
| 234 | |
| 235 | /// <summary> |
| 236 | /// Registers a handler for message extension query setting URL invoke activities. |
| 237 | /// Cannot be combined with <see cref="InvokeExtensions.OnInvoke"/>. |
| 238 | /// </summary> |
| 239 | /// <remarks> |
| 240 | /// Breaking change: previously a catch-all invoke handler could be registered alongside specific invoke handlers. This combination now throws at registration time. |
| 241 | /// </remarks> |
| 242 | /// <param name="app">The Teams bot application.</param> |
| 243 | /// <param name="handler">The handler to register.</param> |
| 244 | /// <returns>The updated Teams bot application.</returns> |
| 245 | public static TeamsBotApplication OnQuerySettingUrl(this TeamsBotApplication app, MessageExtensionQuerySettingUrlHandler handler) |
| 246 | { |
| 247 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 248 | app.Router.Register(new Route<InvokeActivity> |
| 249 | { |
| 250 | Name = string.Join("/", TeamsActivityType.Invoke, InvokeNames.MessageExtensionQuerySettingUrl), |
| 251 | Selector = activity => activity.Name == InvokeNames.MessageExtensionQuerySettingUrl, |
| 252 | HandlerWithReturn = async (ctx, cancellationToken) => |
| 253 | { |
| 254 | InvokeActivity<MessageExtensionQuery> typedActivity = new(ctx.Activity); |
| 255 | Context<InvokeActivity<MessageExtensionQuery>> typedContext = new(ctx.TeamsBotApplication, typedActivity); |
| 256 | return await handler(typedContext, cancellationToken).ConfigureAwait(false); |
| 257 | } |
| 258 | }); |
| 259 | |
| 260 | return app; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | /* |
| 265 | /// <summary> |
| 266 | /// Registers a handler for message extension card button clicked invoke activities. |
| 267 | /// </summary> |
| 268 | public static TeamsBotApplication OnCardButtonClicked(this TeamsBotApplication app, MessageExtensionCardButtonClickedHandler handler) |
| 269 | { |
| 270 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 271 | app.Router.Register(new Route<InvokeActivity> |
| 272 | { |
| 273 | Name = string.Join("/", TeamsActivityType.Invoke, InvokeNames.MessageExtensionCardButtonClicked), |
| 274 | Selector = activity => activity.Name == InvokeNames.MessageExtensionCardButtonClicked, |
| 275 | HandlerWithReturn = async (ctx, cancellationToken) => |
| 276 | { |
| 277 | var typedActivity = new InvokeActivity<JsonElement>(ctx.Activity); |
| 278 | Context<InvokeActivity<JsonElement>> typedContext = new(ctx.TeamsBotApplication, typedActivity); |
| 279 | return await handler(typedContext, cancellationToken).ConfigureAwait(false); |
| 280 | } |
| 281 | }); |
| 282 | |
| 283 | return app; |
| 284 | } |
| 285 | |
| 286 | /// <summary> |
| 287 | /// Registers a handler for message extension setting invoke activities. |
| 288 | /// </summary> |
| 289 | public static TeamsBotApplication OnSetting(this TeamsBotApplication app, MessageExtensionSettingHandler handler) |
| 290 | { |
| 291 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 292 | app.Router.Register(new Route<InvokeActivity> |
| 293 | { |
| 294 | Name = string.Join("/", TeamsActivityType.Invoke, InvokeNames.MessageExtensionSetting), |
| 295 | Selector = activity => activity.Name == InvokeNames.MessageExtensionSetting, |
| 296 | HandlerWithReturn = async (ctx, cancellationToken) => |
| 297 | { |
| 298 | var typedActivity = new InvokeActivity<Query>(ctx.Activity); |
| 299 | Context<InvokeActivity<Query>> typedContext = new(ctx.TeamsBotApplication, typedActivity); |
| 300 | return await handler(typedContext, cancellationToken).ConfigureAwait(false); |
| 301 | } |
| 302 | }); |
| 303 | |
| 304 | return app; |
| 305 | } |
| 306 | */ |
| 307 | } |
| 308 | |