microsoft/teams.net
Publicmirrored fromhttps://github.com/microsoft/teams.netAvailable
core/src/Microsoft.Teams.Bot.Apps/Handlers/ConversationUpdateHandler.cs
483lines · 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 | /// <summary> |
| 175 | /// Registers a handler for channel shared events. |
| 176 | /// </summary> |
| 177 | /// <param name="app"></param> |
| 178 | /// <param name="handler"></param> |
| 179 | /// <returns></returns> |
| 180 | public static TeamsBotApplication OnChannelShared(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 181 | { |
| 182 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 183 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 184 | { |
| 185 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.ChannelShared]), |
| 186 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.ChannelShared, |
| 187 | Handler = async (ctx, cancellationToken) => |
| 188 | { |
| 189 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 190 | } |
| 191 | }); |
| 192 | |
| 193 | return app; |
| 194 | } |
| 195 | |
| 196 | /// <summary> |
| 197 | /// Registers a handler for channel unshared events. |
| 198 | /// </summary> |
| 199 | /// <param name="app"></param> |
| 200 | /// <param name="handler"></param> |
| 201 | /// <returns></returns> |
| 202 | public static TeamsBotApplication OnChannelUnshared(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 203 | { |
| 204 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 205 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 206 | { |
| 207 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.ChannelUnShared]), |
| 208 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.ChannelUnShared, |
| 209 | Handler = async (ctx, cancellationToken) => |
| 210 | { |
| 211 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 212 | } |
| 213 | }); |
| 214 | |
| 215 | return app; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /// <summary> |
| 220 | /// Registers a handler for channel member added events. |
| 221 | /// </summary> |
| 222 | /// <param name="app"></param> |
| 223 | /// <param name="handler"></param> |
| 224 | /// <returns></returns> |
| 225 | public static TeamsBotApplication OnChannelMemberAdded(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.ChannelMemberAdded]), |
| 231 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.ChannelMemberAdded, |
| 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 removed events. |
| 243 | /// </summary> |
| 244 | /// <param name="app"></param> |
| 245 | /// <param name="handler"></param> |
| 246 | /// <returns></returns> |
| 247 | public static TeamsBotApplication OnChannelMemberRemoved(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.ChannelMemberRemoved]), |
| 253 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.ChannelMemberRemoved, |
| 254 | Handler = async (ctx, cancellationToken) => |
| 255 | { |
| 256 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 257 | } |
| 258 | }); |
| 259 | |
| 260 | return app; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | /// <summary> |
| 265 | /// Registers a handler for channel restored events. |
| 266 | /// </summary> |
| 267 | /// <param name="app"></param> |
| 268 | /// <param name="handler"></param> |
| 269 | /// <returns></returns> |
| 270 | public static TeamsBotApplication OnChannelRestored(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 271 | { |
| 272 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 273 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 274 | { |
| 275 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.ChannelRestored]), |
| 276 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.ChannelRestored, |
| 277 | Handler = async (ctx, cancellationToken) => |
| 278 | { |
| 279 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 280 | } |
| 281 | }); |
| 282 | |
| 283 | return app; |
| 284 | } |
| 285 | */ |
| 286 | |
| 287 | // Team Event Handlers |
| 288 | |
| 289 | /// <summary> |
| 290 | /// Registers a handler for team member added events. |
| 291 | /// </summary> |
| 292 | /// <remarks> |
| 293 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 294 | /// </remarks> |
| 295 | /// <param name="app"></param> |
| 296 | /// <param name="handler"></param> |
| 297 | /// <returns></returns> |
| 298 | public static TeamsBotApplication OnTeamMemberAdded(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 299 | { |
| 300 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 301 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 302 | { |
| 303 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamMemberAdded]), |
| 304 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamMemberAdded, |
| 305 | Handler = async (ctx, cancellationToken) => |
| 306 | { |
| 307 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 308 | } |
| 309 | }); |
| 310 | |
| 311 | return app; |
| 312 | } |
| 313 | |
| 314 | /// <summary> |
| 315 | /// Registers a handler for team member removed events. |
| 316 | /// </summary> |
| 317 | /// <remarks> |
| 318 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 319 | /// </remarks> |
| 320 | /// <param name="app"></param> |
| 321 | /// <param name="handler"></param> |
| 322 | /// <returns></returns> |
| 323 | public static TeamsBotApplication OnTeamMemberRemoved(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 324 | { |
| 325 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 326 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 327 | { |
| 328 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamMemberRemoved]), |
| 329 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamMemberRemoved, |
| 330 | Handler = async (ctx, cancellationToken) => |
| 331 | { |
| 332 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 333 | } |
| 334 | }); |
| 335 | |
| 336 | return app; |
| 337 | } |
| 338 | |
| 339 | /// <summary> |
| 340 | /// Registers a handler for team archived events. |
| 341 | /// </summary> |
| 342 | /// <remarks> |
| 343 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 344 | /// </remarks> |
| 345 | /// <param name="app"></param> |
| 346 | /// <param name="handler"></param> |
| 347 | /// <returns></returns> |
| 348 | public static TeamsBotApplication OnTeamArchived(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 349 | { |
| 350 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 351 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 352 | { |
| 353 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamArchived]), |
| 354 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamArchived, |
| 355 | Handler = async (ctx, cancellationToken) => |
| 356 | { |
| 357 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 358 | } |
| 359 | }); |
| 360 | |
| 361 | return app; |
| 362 | } |
| 363 | |
| 364 | /// <summary> |
| 365 | /// Registers a handler for team deleted events. |
| 366 | /// </summary> |
| 367 | /// <remarks> |
| 368 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 369 | /// </remarks> |
| 370 | /// <param name="app"></param> |
| 371 | /// <param name="handler"></param> |
| 372 | /// <returns></returns> |
| 373 | public static TeamsBotApplication OnTeamDeleted(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 374 | { |
| 375 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 376 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 377 | { |
| 378 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamDeleted]), |
| 379 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamDeleted, |
| 380 | Handler = async (ctx, cancellationToken) => |
| 381 | { |
| 382 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 383 | } |
| 384 | }); |
| 385 | |
| 386 | return app; |
| 387 | } |
| 388 | |
| 389 | /// <summary> |
| 390 | /// Registers a handler for team renamed events. |
| 391 | /// </summary> |
| 392 | /// <remarks> |
| 393 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 394 | /// </remarks> |
| 395 | /// <param name="app"></param> |
| 396 | /// <param name="handler"></param> |
| 397 | /// <returns></returns> |
| 398 | public static TeamsBotApplication OnTeamRenamed(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 399 | { |
| 400 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 401 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 402 | { |
| 403 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamRenamed]), |
| 404 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamRenamed, |
| 405 | Handler = async (ctx, cancellationToken) => |
| 406 | { |
| 407 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 408 | } |
| 409 | }); |
| 410 | |
| 411 | return app; |
| 412 | } |
| 413 | |
| 414 | /// <summary> |
| 415 | /// Registers a handler for team unarchived events. |
| 416 | /// </summary> |
| 417 | /// <remarks> |
| 418 | /// Breaking change: previously only the first matching handler was invoked. All matching handlers are now invoked sequentially. |
| 419 | /// </remarks> |
| 420 | /// <param name="app"></param> |
| 421 | /// <param name="handler"></param> |
| 422 | /// <returns></returns> |
| 423 | public static TeamsBotApplication OnTeamUnarchived(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 424 | { |
| 425 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 426 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 427 | { |
| 428 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamUnarchived]), |
| 429 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamUnarchived, |
| 430 | Handler = async (ctx, cancellationToken) => |
| 431 | { |
| 432 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 433 | } |
| 434 | }); |
| 435 | |
| 436 | return app; |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | /// Registers a handler for team restored events. |
| 441 | /// </summary> |
| 442 | /// <param name="app"></param> |
| 443 | /// <param name="handler"></param> |
| 444 | /// <returns></returns> |
| 445 | public static TeamsBotApplication OnTeamRestored(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 446 | { |
| 447 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 448 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 449 | { |
| 450 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamRestored]), |
| 451 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamRestored, |
| 452 | Handler = async (ctx, cancellationToken) => |
| 453 | { |
| 454 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 455 | } |
| 456 | }); |
| 457 | |
| 458 | return app; |
| 459 | } |
| 460 | |
| 461 | /// <summary> |
| 462 | /// Registers a handler for team hard deleted events. |
| 463 | /// </summary> |
| 464 | /// <param name="app"></param> |
| 465 | /// <param name="handler"></param> |
| 466 | /// <returns></returns> |
| 467 | public static TeamsBotApplication OnTeamHardDeleted(this TeamsBotApplication app, ConversationUpdateHandler handler) |
| 468 | { |
| 469 | ArgumentNullException.ThrowIfNull(app, nameof(app)); |
| 470 | app.Router.Register(new Route<ConversationUpdateActivity> |
| 471 | { |
| 472 | Name = string.Join("/", [TeamsActivityType.ConversationUpdate, ConversationEventTypes.TeamHardDeleted]), |
| 473 | Selector = activity => activity.ChannelData?.EventType == ConversationEventTypes.TeamHardDeleted, |
| 474 | Handler = async (ctx, cancellationToken) => |
| 475 | { |
| 476 | await handler(ctx, cancellationToken).ConfigureAwait(false); |
| 477 | } |
| 478 | }); |
| 479 | |
| 480 | return app; |
| 481 | } |
| 482 | */ |
| 483 | } |
| 484 | |