microsoft/teams.net
Publicmirrored from https://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Handlers/ConversationUpdateHandler.cs
482lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using Microsoft.Teams.Bot.Apps.Routing; |
| 5 | using Microsoft.Teams.Bot.Apps.Schema; |
| 6 | |
| 7 | namespace Microsoft.Teams.Bot.Apps.Handlers; |
| 8 | |
| 9 | /// <summary> |
| 10 | /// Delegate for handling conversation update activities. |
| 11 | /// </summary> |
| 12 | /// <param name="context"></param> |
| 13 | /// <param name="cancellationToken"></param> |
| 14 | /// <returns></returns> |
| 15 | public delegate Task ConversationUpdateHandler(Context<ConversationUpdateActivity> context, CancellationToken cancellationToken = default); |
| 16 | |
| 17 | /// <summary> |
| 18 | /// Extension methods for registering conversation update activity handlers. |
| 19 | /// </summary> |
| 20 | public static class ConversationUpdateExtensions |
| 21 | { |
| 22 | /// <summary> |
| 23 | /// Registers a handler for conversation update activities. |
| 24 | /// </summary> |
| 25 | /// <remarks> |
| 26 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 27 | /// </remarks> |
| 28 | /// <param name="app"></param> |
| 29 | /// <param name="handler"></param> |
| 30 | /// <returns></returns> |
| 31 | public static TeamsBotApplication OnConversationUpdate(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 32 | { |
| 33 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 34 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 35 | { |
| 36 | Name = TeamsActivityType.ConversationUpdate, |
| 37 | Selector = _ => true, |
| 38 | Handler = async (ctx, cancellationToken) => |
| 39 | { |
| 40 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 41 | } |
| 42 | }); |
| 43 | |
| 44 | return app; |
| 45 | } |
| 46 | |
| 47 | /// <summary> |
| 48 | /// Registers a handler for conversation update activities where members were added. |
| 49 | /// </summary> |
| 50 | /// <remarks> |
| 51 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 52 | /// </remarks> |
| 53 | /// <param name="app"></param> |
| 54 | /// <param name="handler"></param> |
| 55 | /// <returns></returns> |
| 56 | public static TeamsBotApplication OnMembersAdded(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 57 | { |
| 58 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 59 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 60 | { |
| 61 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, "membersAdded"]), |
| 62 | Selector = activity => activity.MembersAdded?.Count > 0, |
| 63 | Handler = async (ctx, cancellationToken) => |
| 64 | { |
| 65 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 66 | } |
| 67 | }); |
| 68 | |
| 69 | return app; |
| 70 | } |
| 71 | |
| 72 | /// <summary> |
| 73 | /// Registers a handler for conversation update activities where members were removed. |
| 74 | /// </summary> |
| 75 | /// <remarks> |
| 76 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 77 | /// </remarks> |
| 78 | /// <param name="app"></param> |
| 79 | /// <param name="handler"></param> |
| 80 | /// <returns></returns> |
| 81 | public static TeamsBotApplication OnMembersRemoved(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 82 | { |
| 83 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 84 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 85 | { |
| 86 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, "membersRemoved"]), |
| 87 | Selector = activity => activity.MembersRemoved?.Count > 0, |
| 88 | Handler = async (ctx, cancellationToken) => |
| 89 | { |
| 90 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 91 | } |
| 92 | }); |
| 93 | |
| 94 | return app; |
| 95 | } |
| 96 | |
| 97 | // Channel Event Handlers |
| 98 | |
| 99 | /// <summary> |
| 100 | /// Registers a handler for channel created events. |
| 101 | /// </summary> |
| 102 | /// <remarks> |
| 103 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 104 | /// </remarks> |
| 105 | /// <param name="app"></param> |
| 106 | /// <param name="handler"></param> |
| 107 | /// <returns></returns> |
| 108 | public static TeamsBotApplication OnChannelCreated(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 109 | { |
| 110 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 111 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 112 | { |
| 113 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.ChannelCreated]), |
| 114 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.ChannelCreated, |
| 115 | Handler = async (ctx, cancellationToken) => |
| 116 | { |
| 117 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 118 | } |
| 119 | }); |
| 120 | |
| 121 | return app; |
| 122 | } |
| 123 | |
| 124 | /// <summary> |
| 125 | /// Registers a handler for channel deleted events. |
| 126 | /// </summary> |
| 127 | /// <remarks> |
| 128 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 129 | /// </remarks> |
| 130 | /// <param name="app"></param> |
| 131 | /// <param name="handler"></param> |
| 132 | /// <returns></returns> |
| 133 | public static TeamsBotApplication OnChannelDeleted(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 134 | { |
| 135 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 136 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 137 | { |
| 138 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.ChannelDeleted]), |
| 139 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.ChannelDeleted, |
| 140 | Handler = async (ctx, cancellationToken) => |
| 141 | { |
| 142 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 143 | } |
| 144 | }); |
| 145 | |
| 146 | return app; |
| 147 | } |
| 148 | |
| 149 | /// <summary> |
| 150 | /// Registers a handler for channel renamed events. |
| 151 | /// </summary> |
| 152 | /// <remarks> |
| 153 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 154 | /// </remarks> |
| 155 | /// <param name="app"></param> |
| 156 | /// <param name="handler"></param> |
| 157 | /// <returns></returns> |
| 158 | public static TeamsBotApplication OnChannelRenamed(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 159 | { |
| 160 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 161 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 162 | { |
| 163 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.ChannelRenamed]), |
| 164 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.ChannelRenamed, |
| 165 | Handler = async (ctx, cancellationToken) => |
| 166 | { |
| 167 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 168 | } |
| 169 | }); |
| 170 | |
| 171 | return app; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | /// <summary> |
| 176 | /// Registers a handler for channel restored events. |
| 177 | /// </summary> |
| 178 | /// <param name="app"></param> |
| 179 | /// <param name="handler"></param> |
| 180 | /// <returns></returns> |
| 181 | public static TeamsBotApplication OnChannelRestored(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 182 | { |
| 183 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 184 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 185 | { |
| 186 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.ChannelRestored]), |
| 187 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.ChannelRestored, |
| 188 | Handler = async (ctx, cancellationToken) => |
| 189 | { |
| 190 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 191 | } |
| 192 | }); |
| 193 | |
| 194 | return app; |
| 195 | } |
| 196 | |
| 197 | /// <summary> |
| 198 | /// Registers a handler for channel shared events. |
| 199 | /// </summary> |
| 200 | /// <param name="app"></param> |
| 201 | /// <param name="handler"></param> |
| 202 | /// <returns></returns> |
| 203 | public static TeamsBotApplication OnChannelShared(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 204 | { |
| 205 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 206 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 207 | { |
| 208 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.ChannelShared]), |
| 209 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.ChannelShared, |
| 210 | Handler = async (ctx, cancellationToken) => |
| 211 | { |
| 212 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 213 | } |
| 214 | }); |
| 215 | |
| 216 | return app; |
| 217 | } |
| 218 | |
| 219 | /// <summary> |
| 220 | /// Registers a handler for channel unshared events. |
| 221 | /// </summary> |
| 222 | /// <param name="app"></param> |
| 223 | /// <param name="handler"></param> |
| 224 | /// <returns></returns> |
| 225 | public static TeamsBotApplication OnChannelUnshared(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 226 | { |
| 227 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 228 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 229 | { |
| 230 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.ChannelUnShared]), |
| 231 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.ChannelUnShared, |
| 232 | Handler = async (ctx, cancellationToken) => |
| 233 | { |
| 234 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 235 | } |
| 236 | }); |
| 237 | |
| 238 | return app; |
| 239 | } |
| 240 | |
| 241 | /// <summary> |
| 242 | /// Registers a handler for channel member added events. |
| 243 | /// </summary> |
| 244 | /// <param name="app"></param> |
| 245 | /// <param name="handler"></param> |
| 246 | /// <returns></returns> |
| 247 | public static TeamsBotApplication OnChannelMemberAdded(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 248 | { |
| 249 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 250 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 251 | { |
| 252 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.ChannelMemberAdded]), |
| 253 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.ChannelMemberAdded, |
| 254 | Handler = async (ctx, cancellationToken) => |
| 255 | { |
| 256 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 257 | } |
| 258 | }); |
| 259 | |
| 260 | return app; |
| 261 | } |
| 262 | |
| 263 | /// <summary> |
| 264 | /// Registers a handler for channel member removed events. |
| 265 | /// </summary> |
| 266 | /// <param name="app"></param> |
| 267 | /// <param name="handler"></param> |
| 268 | /// <returns></returns> |
| 269 | public static TeamsBotApplication OnChannelMemberRemoved(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 270 | { |
| 271 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 272 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 273 | { |
| 274 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.ChannelMemberRemoved]), |
| 275 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.ChannelMemberRemoved, |
| 276 | Handler = async (ctx, cancellationToken) => |
| 277 | { |
| 278 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 279 | } |
| 280 | }); |
| 281 | |
| 282 | return app; |
| 283 | } |
| 284 | */ |
| 285 | |
| 286 | // Team Event Handlers |
| 287 | |
| 288 | /// <summary> |
| 289 | /// Registers a handler for team member added events. |
| 290 | /// </summary> |
| 291 | /// <remarks> |
| 292 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 293 | /// </remarks> |
| 294 | /// <param name="app"></param> |
| 295 | /// <param name="handler"></param> |
| 296 | /// <returns></returns> |
| 297 | public static TeamsBotApplication OnTeamMemberAdded(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 298 | { |
| 299 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 300 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 301 | { |
| 302 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamMemberAdded]), |
| 303 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamMemberAdded, |
| 304 | Handler = async (ctx, cancellationToken) => |
| 305 | { |
| 306 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 307 | } |
| 308 | }); |
| 309 | |
| 310 | return app; |
| 311 | } |
| 312 | |
| 313 | /// <summary> |
| 314 | /// Registers a handler for team member removed events. |
| 315 | /// </summary> |
| 316 | /// <remarks> |
| 317 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 318 | /// </remarks> |
| 319 | /// <param name="app"></param> |
| 320 | /// <param name="handler"></param> |
| 321 | /// <returns></returns> |
| 322 | public static TeamsBotApplication OnTeamMemberRemoved(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 323 | { |
| 324 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 325 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 326 | { |
| 327 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamMemberRemoved]), |
| 328 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamMemberRemoved, |
| 329 | Handler = async (ctx, cancellationToken) => |
| 330 | { |
| 331 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 332 | } |
| 333 | }); |
| 334 | |
| 335 | return app; |
| 336 | } |
| 337 | |
| 338 | /// <summary> |
| 339 | /// Registers a handler for team archived events. |
| 340 | /// </summary> |
| 341 | /// <remarks> |
| 342 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 343 | /// </remarks> |
| 344 | /// <param name="app"></param> |
| 345 | /// <param name="handler"></param> |
| 346 | /// <returns></returns> |
| 347 | public static TeamsBotApplication OnTeamArchived(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 348 | { |
| 349 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 350 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 351 | { |
| 352 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamArchived]), |
| 353 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamArchived, |
| 354 | Handler = async (ctx, cancellationToken) => |
| 355 | { |
| 356 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 357 | } |
| 358 | }); |
| 359 | |
| 360 | return app; |
| 361 | } |
| 362 | |
| 363 | /// <summary> |
| 364 | /// Registers a handler for team deleted events. |
| 365 | /// </summary> |
| 366 | /// <remarks> |
| 367 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 368 | /// </remarks> |
| 369 | /// <param name="app"></param> |
| 370 | /// <param name="handler"></param> |
| 371 | /// <returns></returns> |
| 372 | public static TeamsBotApplication OnTeamDeleted(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 373 | { |
| 374 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 375 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 376 | { |
| 377 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamDeleted]), |
| 378 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamDeleted, |
| 379 | Handler = async (ctx, cancellationToken) => |
| 380 | { |
| 381 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 382 | } |
| 383 | }); |
| 384 | |
| 385 | return app; |
| 386 | } |
| 387 | |
| 388 | /// <summary> |
| 389 | /// Registers a handler for team renamed events. |
| 390 | /// </summary> |
| 391 | /// <remarks> |
| 392 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 393 | /// </remarks> |
| 394 | /// <param name="app"></param> |
| 395 | /// <param name="handler"></param> |
| 396 | /// <returns></returns> |
| 397 | public static TeamsBotApplication OnTeamRenamed(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 398 | { |
| 399 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 400 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 401 | { |
| 402 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamRenamed]), |
| 403 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamRenamed, |
| 404 | Handler = async (ctx, cancellationToken) => |
| 405 | { |
| 406 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 407 | } |
| 408 | }); |
| 409 | |
| 410 | return app; |
| 411 | } |
| 412 | |
| 413 | /// <summary> |
| 414 | /// Registers a handler for team unarchived events. |
| 415 | /// </summary> |
| 416 | /// <remarks> |
| 417 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 418 | /// </remarks> |
| 419 | /// <param name="app"></param> |
| 420 | /// <param name="handler"></param> |
| 421 | /// <returns></returns> |
| 422 | public static TeamsBotApplication OnTeamUnarchived(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 423 | { |
| 424 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 425 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 426 | { |
| 427 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamUnarchived]), |
| 428 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamUnarchived, |
| 429 | Handler = async (ctx, cancellationToken) => |
| 430 | { |
| 431 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 432 | } |
| 433 | }); |
| 434 | |
| 435 | return app; |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | /// Registers a handler for team restored events. |
| 440 | /// </summary> |
| 441 | /// <param name="app"></param> |
| 442 | /// <param name="handler"></param> |
| 443 | /// <returns></returns> |
| 444 | public static TeamsBotApplication OnTeamRestored(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 445 | { |
| 446 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 447 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 448 | { |
| 449 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamRestored]), |
| 450 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamRestored, |
| 451 | Handler = async (ctx, cancellationToken) => |
| 452 | { |
| 453 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 454 | } |
| 455 | }); |
| 456 | |
| 457 | return app; |
| 458 | } |
| 459 | |
| 460 | /// <summary> |
| 461 | /// Registers a handler for team hard deleted events. |
| 462 | /// </summary> |
| 463 | /// <param name="app"></param> |
| 464 | /// <param name="handler"></param> |
| 465 | /// <returns></returns> |
| 466 | public static TeamsBotApplication OnTeamHardDeleted(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 467 | { |
| 468 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 469 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 470 | { |
| 471 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamHardDeleted]), |
| 472 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamHardDeleted, |
| 473 | Handler = async (ctx, cancellationToken) => |
| 474 | { |
| 475 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 476 | } |
| 477 | }); |
| 478 | |
| 479 | return app; |
| 480 | } |
| 481 | */ |
| 482 | } |
| 483 | |